File: test_enable_ssl.py

package info (click to toggle)
pg-auto-failover 2.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,368 kB
  • sloc: ansic: 58,369; python: 5,515; sql: 3,177; makefile: 629; sh: 35
file content (316 lines) | stat: -rw-r--r-- 8,209 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import tests.pgautofailover_utils as pgautofailover
import tests.ssl_cert_utils as cert
import subprocess
import os
import time

cluster = None
monitor = None
node1 = None
node2 = None


def setup_module():
    global cluster
    cluster = pgautofailover.Cluster()


def teardown_module():
    cluster.destroy()

    # remove client side setup for certificates too
    client_top_directory = os.path.join(os.getenv("HOME"), ".postgresql")

    p = subprocess.Popen(
        [
            "sudo",
            "-E",
            "-u",
            os.getenv("USER"),
            "env",
            "PATH=" + os.getenv("PATH"),
            "rm",
            "-rf",
            client_top_directory,
        ]
    )
    assert p.wait() == 0

    # also remove certificates we created for the servers
    p = subprocess.run(
        [
            "sudo",
            "-E",
            "-u",
            os.getenv("USER"),
            "env",
            "PATH=" + os.getenv("PATH"),
            "rm",
            "-rf",
            "/tmp/certs",
        ]
    )
    assert p.returncode == 0


def test_000_create_monitor():
    global monitor
    monitor = cluster.create_monitor("/tmp/enable/monitor")
    monitor.run()
    monitor.wait_until_pg_is_running()

    monitor.check_ssl("off", "prefer")


def test_001_init_primary():
    global node1
    node1 = cluster.create_datanode("/tmp/enable/node1")
    node1.create()
    node1.run()
    assert node1.wait_until_state(target_state="single")

    node1.wait_until_pg_is_running()
    node1.check_ssl("off", "prefer", primary=True)


def test_002_create_t1():
    node1.run_sql_query("CREATE TABLE t1(a int)")
    node1.run_sql_query("INSERT INTO t1 VALUES (1), (2)")


def test_003_init_secondary():
    global node2
    node2 = cluster.create_datanode("/tmp/enable/node2")
    node2.create()
    node2.run()

    assert node2.wait_until_state(target_state="secondary")
    assert node1.wait_until_state(target_state="primary")

    node2.check_ssl("off", "prefer")


def test_004_maintenance():
    print()
    print("Enabling maintenance on node2")
    node2.enable_maintenance()
    assert node2.wait_until_state(target_state="maintenance")


def test_005_enable_ssl_monitor():
    monitor.enable_ssl(sslSelfSigned=True, sslMode="require")
    monitor.sleep(2)  # we signaled, wait some time

    monitor.check_ssl("on", "require")


def test_006_enable_ssl_primary():
    # we stop pg_autoctl to make it easier for the test to be reliable
    # without too much delay/sleep hacking; when doing the `pg_autoctl
    # enable ssl` online we need to make sure the signal made it to the
    # running process and then was acted upon
    node1.stop_pg_autoctl()
    node1.enable_ssl(sslSelfSigned=True, sslMode="require")
    node1.run()

    node1.wait_until_pg_is_running()
    node1.check_ssl("on", "require", primary=True)


def test_007_enable_ssl_secondary():
    node2.stop_pg_autoctl()
    node2.enable_ssl(sslSelfSigned=True, sslMode="require")
    node2.run()

    node2.wait_until_pg_is_running()
    node2.check_ssl("on", "require")


def test_008_disable_maintenance():
    print("Disabling maintenance on node2")
    node2.disable_maintenance()
    assert node2.wait_until_pg_is_running()
    assert node2.wait_until_state(target_state="secondary")
    assert node1.wait_until_state(target_state="primary")


# upgrade to verify full
def test_009_enable_maintenance():
    print()
    print("Enabling maintenance on node2")
    node2.enable_maintenance()

    assert node2.wait_until_state(target_state="maintenance")


def test_010_enable_ssl_verify_ca_monitor():
    client_top_directory = os.path.join(os.getenv("HOME"), ".postgresql")

    print()
    print("Creating cluster root certificate")
    cluster.create_root_cert(
        client_top_directory, basename="root", CN="/CN=root.pgautofailover.ca"
    )

    p = subprocess.run(
        [
            "ls",
            "-ld",
            client_top_directory,
            cluster.cert.crt,
            cluster.cert.csr,
            cluster.cert.key,
        ],
        text=True,
        capture_output=True,
    )
    print("%s" % p.stdout)

    # now create and sign the CLIENT certificate
    print("Creating cluster client certificate")
    clientCert = cert.SSLCert(
        client_top_directory, basename="postgresql", CN="/CN=autoctl_node"
    )
    clientCert.create_signed_certificate(cluster.cert)

    p = subprocess.run(
        [
            "ls",
            "-ld",
            client_top_directory,
            clientCert.crt,
            clientCert.csr,
            clientCert.key,
        ],
        text=True,
        capture_output=True,
    )
    print("%s" % p.stdout)

    # the root user also needs the certificates, tests are connecting with it
    root_top_directory = "/root/.postgresql"
    p = subprocess.run(
        ["sudo", "install", "-d", "-m", "740", root_top_directory]
    )
    assert p.returncode == 0

    p = subprocess.run(
        [
            "sudo",
            "cp",
            clientCert.crt,
            clientCert.csr,
            clientCert.key,
            root_top_directory,
        ]
    )
    assert p.returncode == 0

    p = subprocess.run(
        ["ls", "-l", "/root/.postgresql"], text=True, capture_output=True
    )
    print("%s" % p.stdout)

    # now create and sign the SERVER certificate for the monitor
    print("Creating monitor server certificate")
    monitorCert = cert.SSLCert(
        "/tmp/certs/monitor", "server", "/CN=monitor.pgautofailover.ca"
    )
    monitorCert.create_signed_certificate(cluster.cert)

    p = subprocess.run(
        [
            "ls",
            "-ld",
            client_top_directory,
            cluster.cert.crt,
            cluster.cert.csr,
            cluster.cert.key,
            clientCert.crt,
            clientCert.csr,
            clientCert.key,
            monitorCert.crt,
            monitorCert.csr,
            monitorCert.key,
        ],
        text=True,
        capture_output=True,
    )
    print("%s" % p.stdout)

    monitor.enable_ssl(
        sslCAFile=cluster.cert.crt,
        sslServerKey=monitorCert.key,
        sslServerCert=monitorCert.crt,
        sslMode="verify-ca",
    )

    monitor.sleep(2)  # we signaled, wait some time

    monitor.check_ssl("on", "verify-ca")


def test_011_enable_ssl_verify_ca_primary():
    node1Cert = cert.SSLCert(
        "/tmp/certs/node1", "server", "/CN=node1.pgautofailover.ca"
    )
    node1Cert.create_signed_certificate(cluster.cert)

    node1.stop_pg_autoctl()
    node1.enable_ssl(
        sslCAFile=cluster.cert.crt,
        sslServerKey=node1Cert.key,
        sslServerCert=node1Cert.crt,
        sslMode="verify-ca",
    )
    node1.run()
    node1.wait_until_pg_is_running()
    node1.check_ssl("on", "verify-ca", primary=True)


def test_012_enable_ssl_verify_ca_secondary():
    node2Cert = cert.SSLCert(
        "/tmp/certs/node2", "server", "/CN=node2.pgautofailover.ca"
    )
    node2Cert.create_signed_certificate(cluster.cert)

    node2.stop_pg_autoctl()
    node2.enable_ssl(
        sslCAFile=cluster.cert.crt,
        sslServerKey=node2Cert.key,
        sslServerCert=node2Cert.crt,
        sslMode="verify-ca",
    )
    node2.run()
    node2.wait_until_pg_is_running()
    node2.check_ssl("on", "verify-ca")


def test_013_disable_maintenance():
    print("Disabling maintenance on node2")
    node2.disable_maintenance()
    assert node2.wait_until_pg_is_running()
    assert node2.wait_until_state(target_state="secondary")
    assert node1.wait_until_state(target_state="primary")


def test_014_enable_ssl_require_primary():
    node1Cert = cert.SSLCert(
        "/tmp/certs/node1", "server", "/CN=node1.pgautofailover.ca"
    )
    node1Cert.create_signed_certificate(cluster.cert)

    node1.enable_ssl(
        sslServerKey=node1Cert.key,
        sslServerCert=node1Cert.crt,
        sslMode="require",
    )

    node1.pg_autoctl.sighup()
    time.sleep(6)

    # to avoid flackyness here, we allow a second run/timeout of waiting
    if not node1.wait_until_pg_is_running():
        assert node1.wait_until_pg_is_running()

    node1.check_ssl("on", "require", primary=True)