forked from geldata/gel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_backend_connect.py
1042 lines (894 loc) · 36 KB
/
test_backend_connect.py
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# This source file is part of the EdgeDB open source project.
#
# Copyright 2021-present MagicStack Inc. and the EdgeDB authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import warnings
from typing import Optional, Unpack
import asyncio
import contextlib
import ipaddress
import os
import pathlib
import pickle
import platform
import shutil
import socket
import ssl
import stat
import sys
import tempfile
import unittest
import unittest.mock
import urllib.parse
import click
from click.testing import CliRunner
from edb.pgsql import params as pg_params
from edb.server import args as edb_args
from edb.server import bootstrap
from edb.server import pgcluster
from edb.server import pgconnparams
from edb.server.pgconnparams import SSLMode
from edb.server import pgcon
from edb.server.pgcon import errors
from edb.testbase import server as tb
CERTS = os.path.join(os.path.dirname(__file__), 'certs')
SSL_CA_CERT_FILE = os.path.join(CERTS, 'ca.cert.pem')
SSL_CA_CRL_FILE = os.path.join(CERTS, 'ca.crl.pem')
SSL_CERT_FILE = os.path.join(CERTS, 'server.cert.pem')
SSL_KEY_FILE = os.path.join(CERTS, 'server.key.pem')
CLIENT_CA_CERT_FILE = os.path.join(CERTS, 'client_ca.cert.pem')
CLIENT_SSL_CERT_FILE = os.path.join(CERTS, 'client.cert.pem')
CLIENT_SSL_KEY_FILE = os.path.join(CERTS, 'client.key.pem')
CLIENT_SSL_PROTECTED_KEY_FILE = os.path.join(CERTS, 'client.key.protected.pem')
@contextlib.contextmanager
def mock_dot_postgresql(*, ca=True, crl=False, client=False, protected=False):
with tempfile.TemporaryDirectory() as temp_dir:
home = pathlib.Path(temp_dir)
pg_home = home / '.postgresql'
pg_home.mkdir()
if ca:
shutil.copyfile(SSL_CA_CERT_FILE, pg_home / 'root.crt')
if crl:
shutil.copyfile(SSL_CA_CRL_FILE, pg_home / 'root.crl')
if client:
shutil.copyfile(CLIENT_SSL_CERT_FILE, pg_home / 'postgresql.crt')
if protected:
shutil.copyfile(
CLIENT_SSL_PROTECTED_KEY_FILE, pg_home / 'postgresql.key'
)
else:
shutil.copyfile(
CLIENT_SSL_KEY_FILE, pg_home / 'postgresql.key'
)
with unittest.mock.patch(
'pathlib.Path.home', unittest.mock.Mock(return_value=home)
):
yield
def _get_initdb_options(initdb_options=None):
if not initdb_options:
initdb_options = {}
else:
initdb_options = dict(initdb_options)
# Make the default superuser name stable.
if 'username' not in initdb_options:
initdb_options['username'] = 'postgres'
return initdb_options
@click.command()
@edb_args.server_options
def get_default_args(version, **kwargs):
pickle.dump(kwargs, sys.stdout.buffer)
class TempCluster(pgcluster.Cluster):
def __init__(self, *,
data_dir_suffix=None, data_dir_prefix=None,
data_dir_parent=None):
self._data_dir = tempfile.mkdtemp(suffix=data_dir_suffix,
prefix=data_dir_prefix,
dir=data_dir_parent)
super().__init__(self._data_dir)
class ClusterTestCase(tb.TestCase):
cluster: Optional[TempCluster]
loop: asyncio.AbstractEventLoop
@classmethod
def get_server_settings(cls):
return {
'log_connections': 'on',
# JITting messes up timing tests, and
# is not essential for testing.
'jit': 'off',
'listen_addresses': '127.0.0.1',
}
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.loop.run_until_complete(cls.init_temp_cluster())
@classmethod
async def init_temp_cluster(cls):
cluster = cls.cluster = TempCluster()
await cluster.lookup_postgres()
cluster.update_connection_params(
user='postgres',
)
await cluster.init(**_get_initdb_options({}))
await cluster.trust_local_connections()
port = tb.find_available_port()
await cluster.start(
port=port,
server_settings=cls.get_server_settings(),
wait=120,
)
result = CliRunner().invoke(get_default_args, [])
arg_input = pickle.loads(result.stdout_bytes)
arg_input["data_dir"] = pathlib.Path(cluster.get_data_dir())
arg_input["multitenant_config_file"] = ""
arg_input["tls_cert_mode"] = "generate_self_signed"
arg_input["jose_key_mode"] = "generate"
cls.dbname = cluster.get_db_name('main') # XXX
args = edb_args.parse_args(**arg_input)
await bootstrap.ensure_bootstrapped(cluster, args)
@classmethod
def tearDownClass(cls):
try:
cluster, cls.cluster = cls.cluster, None
if cluster is not None:
try:
cls.loop.run_until_complete(cluster.stop())
finally:
cluster.destroy()
finally:
super().tearDownClass()
@classmethod
async def connect(cls, **kwargs: Unpack[pgconnparams.CreateParamsKwargs]):
import inspect
assert cls.cluster is not None
source_description = ("ClusterTestCase: "
f"{inspect.currentframe().f_back.f_code.co_name}") # type: ignore
kwargs['database'] = cls.dbname
return await cls.cluster.connect(
source_description=source_description,
**kwargs
)
def setUp(self):
super().setUp()
self.con = self.loop.run_until_complete(self.connect())
def tearDown(self):
try:
if self.con:
self.con.terminate()
self.con = None
finally:
super().tearDown()
async def assertConnected(self, con):
self.assertEqual(await con.sql_fetch_val(b"SELECT 'OK'"), b'OK')
class TestAuthentication(ClusterTestCase):
def setUp(self):
super().setUp()
methods = [
('trust', None),
('reject', None),
('scram-sha-256', 'correctpassword'),
('md5', 'correctpassword'),
('password', 'correctpassword'),
]
self.cluster.reset_hba()
create_script = []
for method, password in methods:
username = method.replace('-', '_')
# if this is a SCRAM password, we need to set the encryption method
# to "scram-sha-256" in order to properly hash the password
if method == 'scram-sha-256':
create_script.append(
"SET password_encryption = 'scram-sha-256';"
)
create_script.append(
'CREATE ROLE {}_user WITH LOGIN{};'.format(
username,
' PASSWORD {!r}'.format(password) if password else ''
)
)
create_script.append(
f'GRANT postgres TO {username}_user;'
)
# to be courteous to the MD5 test, revert back to MD5 after the
# scram-sha-256 password is set
if method == 'scram-sha-256':
create_script.append(
"SET password_encryption = 'md5';"
)
self.cluster.add_hba_entry(
type='local',
database=self.dbname, user='{}_user'.format(username),
auth_method=method)
self.cluster.add_hba_entry(
type='host', address=ipaddress.ip_network('127.0.0.0/24'),
database=self.dbname, user='{}_user'.format(username),
auth_method=method)
self.cluster.add_hba_entry(
type='host', address=ipaddress.ip_network('::1/128'),
database=self.dbname, user='{}_user'.format(username),
auth_method=method)
# Put hba changes into effect
self.loop.run_until_complete(self.cluster.reload())
create_script = '\n'.join(create_script).encode()
self.loop.run_until_complete(self.con.sql_execute(create_script))
def tearDown(self):
# Reset cluster's pg_hba.conf since we've meddled with it
self.loop.run_until_complete(self.cluster.trust_local_connections())
methods = [
'trust',
'reject',
'scram-sha-256',
'md5',
'password',
]
drop_script = []
for method in methods:
username = method.replace('-', '_')
drop_script.append('DROP ROLE {}_user;'.format(username))
drop_script = '\n'.join(drop_script).encode()
self.loop.run_until_complete(self.con.sql_execute(drop_script))
super().tearDown()
async def test_auth_bad_user(self):
with self.assertRaises(errors.BackendError) as cm:
await self.connect(user='__nonexistent__')
self.assertTrue(
cm.exception.code_is(
errors.ERROR_INVALID_AUTHORIZATION_SPECIFICATION
)
)
async def test_auth_trust(self):
conn = await self.connect(user='trust_user')
conn.terminate()
async def test_auth_reject(self):
with self.assertRaisesRegex(
errors.BackendError,
'pg_hba.conf rejects connection'
):
await self.connect(user='reject_user')
async def test_auth_password_cleartext(self):
conn = await self.connect(
user='password_user',
password='correctpassword')
conn.terminate()
async def test_auth_password_md5(self):
conn = await self.connect(
user='md5_user', password='correctpassword')
conn.terminate()
with self.assertRaisesRegex(
errors.BackendError,
'password authentication failed for user "md5_user"'
):
await self.connect(
user='md5_user', password='wrongpassword')
async def test_auth_password_scram_sha_256(self):
conn = await self.connect(
user='scram_sha_256_user', password='correctpassword')
conn.terminate()
with self.assertRaisesRegex(
errors.BackendError,
'password authentication failed for user "scram_sha_256_user"'
):
await self.connect(
user='scram_sha_256_user', password='wrongpassword')
# various SASL prep tests
# first ensure that password are being hashed for SCRAM-SHA-256
await self.con.sql_execute(
b"SET password_encryption = 'scram-sha-256';",
)
alter_password = "ALTER ROLE scram_sha_256_user PASSWORD E{!r};"
passwords = [
'nonascii\u1680space', # C.1.2
'common\u1806nothing', # B.1
'ab\ufb01c', # normalization
'ab\u007fc', # C.2.1
'ab\u206ac', # C.2.2, C.6
'ab\ue000c', # C.3, C.5
'ab\ufdd0c', # C.4
'ab\u2ff0c', # C.7
'ab\u2000c', # C.8
'ab\ue0001', # C.9
]
# ensure the passwords that go through SASLprep work
for password in passwords:
# update the password
await self.con.sql_execute(
alter_password.format(password).encode(),
)
# test to see that passwords are properly SASL prepped
conn = await self.connect(
user='scram_sha_256_user', password=password)
conn.terminate()
alter_password = \
b"ALTER ROLE scram_sha_256_user PASSWORD 'correctpassword';"
await self.con.sql_execute(alter_password)
await self.con.sql_execute(b"SET password_encryption = 'md5';")
async def test_auth_unsupported(self):
pass
class TestConnectParams(tb.TestCase):
@contextlib.contextmanager
def environ(self, **kwargs):
old_vals = {}
for key in kwargs:
if key in os.environ:
old_vals[key] = os.environ[key]
for key, val in kwargs.items():
if val is None:
if key in os.environ:
del os.environ[key]
else:
os.environ[key] = val
try:
yield
finally:
for key in kwargs:
if key in os.environ:
del os.environ[key]
for key, val in old_vals.items():
os.environ[key] = val
def run_testcase(self, testcase):
env = testcase.get('env', {})
test_env = {'PGHOST': None, 'PGPORT': None,
'PGUSER': None, 'PGPASSWORD': None,
'PGDATABASE': None, 'PGSSLMODE': None}
test_env.update(env)
dsn = testcase.get('dsn', 'postgres://')
expected = testcase.get('result')
expected_error = testcase.get('error')
if expected is None and expected_error is None:
raise RuntimeError(
'invalid test case: either "result" or "error" key '
'has to be specified')
if expected is not None and expected_error is not None:
raise RuntimeError(
'invalid test case: either "result" or "error" key '
'has to be specified, got both')
with contextlib.ExitStack() as es:
es.enter_context(self.subTest(dsn=dsn, env=env))
es.enter_context(self.environ(**test_env))
if expected_error:
es.enter_context(self.assertRaisesRegex(*expected_error))
conn_params = pgconnparams.ConnectionParams(dsn=dsn)
conn_params = conn_params.resolve()
to_dict = conn_params.__dict__
host = to_dict.pop('host', None).split(',')
port = map(int, to_dict.pop('port', None).split(','))
to_dict.pop('sslmode', None)
result = (list(zip(host, port)), to_dict)
if expected is not None:
self.assertEqual(
expected,
result,
'Testcase: {}'.format(testcase.get('name', testcase))
)
def test_test_connect_params_environ(self):
self.assertNotIn('AAAAAAAAAA123', os.environ)
self.assertNotIn('AAAAAAAAAA456', os.environ)
self.assertNotIn('AAAAAAAAAA789', os.environ)
try:
os.environ['AAAAAAAAAA456'] = '123'
os.environ['AAAAAAAAAA789'] = '123'
with self.environ(AAAAAAAAAA123='1',
AAAAAAAAAA456='2',
AAAAAAAAAA789=None):
self.assertEqual(os.environ['AAAAAAAAAA123'], '1')
self.assertEqual(os.environ['AAAAAAAAAA456'], '2')
self.assertNotIn('AAAAAAAAAA789', os.environ)
self.assertNotIn('AAAAAAAAAA123', os.environ)
self.assertEqual(os.environ['AAAAAAAAAA456'], '123')
self.assertEqual(os.environ['AAAAAAAAAA789'], '123')
finally:
for key in {'AAAAAAAAAA123', 'AAAAAAAAAA456', 'AAAAAAAAAA789'}:
if key in os.environ:
del os.environ[key]
def test_test_connect_params_run_testcase(self):
with self.environ(PGPORT='777'):
self.run_testcase({
'env': {
'PGUSER': '__test__'
},
'dsn': 'postgres://abc',
'result': (
[('abc', 5432)],
{'user': '__test__', 'database': '__test__'}
)
})
def test_connect_pgpass_badness_mode(self):
# Verify that .pgpass permissions are checked
with tempfile.NamedTemporaryFile('w+t') as passfile:
os.chmod(passfile.name,
stat.S_IWUSR | stat.S_IRUSR | stat.S_IWGRP | stat.S_IRGRP)
with self.assertWarnsRegex(
UserWarning,
'Password file .* has group or world access'):
self.run_testcase({
'dsn': 'postgres://user@abc/db?passfile={}'.format(
passfile.name
),
'result': (
[('abc', 5432)],
{
'user': 'user',
'database': 'db',
}
)
})
def test_connect_pgpass_badness_non_file(self):
# Verify warnings when .pgpass is not a file
with tempfile.TemporaryDirectory() as passfile:
with self.assertWarnsRegex(
UserWarning,
'Password file .* is not a plain file'):
self.run_testcase({
'dsn': 'postgres://user@abc/db?passfile={}'.format(
passfile
),
'result': (
[('abc', 5432)],
{
'user': 'user',
'database': 'db',
}
)
})
def test_connect_pgpass_nonexistent(self):
# nonexistent passfile is OK
with self.assertWarnsRegex(
UserWarning,
'Password file .* does not exist',
):
self.run_testcase({
'dsn': 'postgres://user@abc/db?passfile=totally+nonexistent',
'result': (
[('abc', 5432)],
{
'user': 'user',
'database': 'db',
}
)
})
def test_connect_pgpass_inaccessible_file(self):
with tempfile.NamedTemporaryFile('w+t') as passfile:
os.chmod(passfile.name, stat.S_IWUSR)
with self.assertWarnsRegex(
UserWarning,
'Password file .* is not accessible'):
# inaccessible passfile is OK
self.run_testcase({
'dsn': 'postgres://user@abc/db?passfile={}'.format(
passfile.name
),
'result': (
[('abc', 5432)],
{
'user': 'user',
'database': 'db',
}
)
})
def test_connect_pgpass_inaccessible_directory(self):
with tempfile.TemporaryDirectory() as passdir:
with tempfile.NamedTemporaryFile('w+t', dir=passdir) as passfile:
os.chmod(passdir, stat.S_IWUSR)
try:
with self.assertWarnsRegex(
UserWarning,
'Password file .* is not accessible'):
# inaccessible passfile is OK
self.run_testcase({
'dsn': 'postgres://user@abc/db?passfile={}'.format(
passfile.name
),
'result': (
[('abc', 5432)],
{
'user': 'user',
'database': 'db',
}
)
})
finally:
os.chmod(passdir, stat.S_IRWXU)
async def test_connection_connect_timeout(self):
server = socket.socket()
gc = []
try:
server.bind(('localhost', 0))
if platform.system() != "Darwin":
# The backlog on macOS is different from Linux
server.listen(0)
host, port = server.getsockname()
conn_spec = pgconnparams.ConnectionParams(
hosts=[(host, port)],
user='foo',
connect_timeout=2,
)
async def placeholder():
async with asyncio.timeout(2):
_, w = await asyncio.open_connection(host, port)
gc.append(w)
# Fill up the TCP server backlog so that our future pgcon.connect
# could time out reliably
i = 0
while i < 8:
i += 1
try:
async with asyncio.TaskGroup() as tg:
for _ in range(4):
tg.create_task(placeholder())
except* TimeoutError:
i = 10
if i < 10:
self.fail("Couldn't fill TCP server backlog within 32 tries")
with self.assertRaises(errors.BackendConnectionError):
async with asyncio.timeout(4): # failsafe
await pgcon.pg_connect(
conn_spec,
source_description="test_connection_connect_timeout",
backend_params=pg_params.get_default_runtime_params(),
)
finally:
server.close()
if gc:
for writer in gc:
writer.close()
await asyncio.wait(
[asyncio.create_task(w.wait_closed()) for w in gc]
)
class TestConnection(ClusterTestCase):
async def test_connection_isinstance(self):
self.assertTrue(isinstance(self.con, pgcon.PGConnection))
self.assertTrue(isinstance(self.con, object))
self.assertFalse(isinstance(self.con, list))
async def test_connection_use_after_close(self):
def check():
return self.assertRaisesRegex(RuntimeError,
'not connected')
self.con.terminate()
with check():
await self.con.sql_fetch_val(b'SELECT 1')
with check():
await self.con.sql_fetch_col(b'SELECT 1')
with check():
await self.con.sql_fetch(b'SELECT 1')
with check():
await self.con.sql_execute(b'SELECT 1')
with check():
await self.con.parse_execute(query=None, bind_data=None)
with check():
await self.con.dump(None, None, None)
with check():
await self.con.restore(None, b'', {})
async def test_connection_ssl_to_no_ssl_server(self):
with self.assertRaisesRegex(ConnectionError, 'rejected SSL'):
await self.connect(
host='localhost',
sslmode=SSLMode.require)
async def test_connection_sslmode_no_ssl_server(self):
async def verify_works(sslmode):
con = None
try:
con = await self.connect(
sslmode=SSLMode.parse(sslmode),
user='postgres',
database='postgres',
host='localhost')
await self.assertConnected(con)
self.assertFalse(con.is_ssl)
finally:
if con:
con.terminate()
async def verify_fails(sslmode):
con = None
try:
with self.assertRaises(ConnectionError):
con = await self.connect(
sslmode=SSLMode.parse(sslmode),
user='postgres',
database='postgres',
host='localhost')
await self.assertConnected(con)
finally:
if con:
con.terminate()
await verify_works('disable')
await verify_works('allow')
await verify_works('prefer')
await verify_fails('require')
with mock_dot_postgresql():
await verify_fails('require')
await verify_fails('verify-ca')
await verify_fails('verify-full')
class BaseTestSSLConnection(ClusterTestCase):
@classmethod
def get_server_settings(cls):
conf = super().get_server_settings()
conf.update({
'ssl': 'on',
'ssl_cert_file': SSL_CERT_FILE,
'ssl_key_file': SSL_KEY_FILE,
'ssl_ca_file': CLIENT_CA_CERT_FILE,
'ssl_min_protocol_version': 'TLSv1.2',
'ssl_max_protocol_version': 'TLSv1.2',
})
return conf
def setUp(self):
super().setUp()
self.cluster.reset_hba()
create_script = []
create_script.append('CREATE ROLE ssl_user WITH LOGIN;')
create_script.append('GRANT postgres TO ssl_user;')
self._add_hba_entry()
# Put hba changes into effect
self.loop.run_until_complete(self.cluster.reload())
create_script = '\n'.join(create_script).encode()
self.loop.run_until_complete(
self.con.sql_execute(create_script)
)
def tearDown(self):
# Reset cluster's pg_hba.conf since we've meddled with it
self.loop.run_until_complete(self.cluster.trust_local_connections())
drop_script = []
drop_script.append('DROP ROLE ssl_user;')
drop_script = '\n'.join(drop_script).encode()
self.loop.run_until_complete(self.con.sql_execute(drop_script))
super().tearDown()
def _add_hba_entry(self):
raise NotImplementedError()
@unittest.skipIf(os.environ.get('PGHOST'), 'unmanaged cluster')
class TestSSLConnection(BaseTestSSLConnection):
def _add_hba_entry(self):
self.cluster.add_hba_entry(
type='hostssl', address="all",
database=self.dbname, user='ssl_user',
auth_method='trust')
async def test_ssl_connection_custom_context(self):
con = await self.connect(
host='localhost',
user='ssl_user',
sslmode=SSLMode.require,
sslrootcert=SSL_CA_CERT_FILE)
try:
await self.assertConnected(con)
self.assertTrue(con.is_ssl)
finally:
con.terminate()
async def test_ssl_connection_sslmode(self):
async def verify_works(sslmode, *, host='localhost'):
con = None
try:
con = await self.connect(
sslmode=SSLMode.parse(sslmode),
host=host,
user='ssl_user')
await self.assertConnected(con)
self.assertTrue(con.is_ssl)
finally:
if con:
con.terminate()
async def verify_fails(sslmode, *, host='localhost', exn_type):
# XXX: uvloop artifact
old_handler = self.loop.get_exception_handler()
con = None
try:
self.loop.set_exception_handler(lambda *args: None)
with self.assertRaises(exn_type, msg=f"{sslmode} {host}"):
con = await self.connect(
sslmode=SSLMode.parse(sslmode),
host=host,
user='ssl_user')
await self.assertConnected(con)
finally:
if con:
con.terminate()
self.loop.set_exception_handler(old_handler)
invalid_auth_err = errors.BackendError
await verify_fails('disable', exn_type=invalid_auth_err)
await verify_works('allow')
await verify_works('prefer')
await verify_works('require')
await verify_fails('verify-ca', exn_type=ValueError)
await verify_fails('verify-full', exn_type=ValueError)
with mock_dot_postgresql():
await verify_works('require')
await verify_works('verify-ca')
await verify_works('verify-ca', host='127.0.0.1')
await verify_works('verify-full')
await verify_fails('verify-full', host='127.0.0.1',
exn_type=ssl.CertificateError)
with mock_dot_postgresql(crl=True):
await verify_fails('disable', exn_type=invalid_auth_err)
await verify_works('allow')
await verify_works('prefer')
await verify_fails('require',
exn_type=ssl.SSLError)
await verify_fails('verify-ca',
exn_type=ssl.SSLError)
await verify_fails('verify-ca', host='127.0.0.1',
exn_type=ssl.SSLError)
await verify_fails('verify-full',
exn_type=ssl.SSLError)
async def test_ssl_connection_default_context(self):
# XXX: uvloop artifact
old_handler = self.loop.get_exception_handler()
try:
self.loop.set_exception_handler(lambda *args: None)
with self.assertRaisesRegex(ssl.SSLError, 'verify failed'):
await self.connect(
host='localhost',
user='ssl_user',
sslmode=SSLMode.verify_full,
# This won't validate
sslrootcert=CLIENT_CA_CERT_FILE
)
finally:
self.loop.set_exception_handler(old_handler)
async def test_tls_version_bad(self):
with self.assertRaisesRegex(ssl.SSLError, 'protocol version'):
await self.connect(
dsn=f'postgresql://ssl_user@localhost/{self.dbname}'
'?sslmode=require&ssl_min_protocol_version=TLSv1.3'
)
with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning)
with self.assertRaises(ssl.SSLError):
await self.connect(
dsn=f'postgresql://ssl_user@localhost/{self.dbname}'
'?sslmode=require'
'&ssl_min_protocol_version=TLSv1.1'
'&ssl_max_protocol_version=TLSv1.1'
)
with self.assertRaisesRegex(ssl.SSLError, 'no protocols'):
await self.connect(
dsn=f'postgresql://ssl_user@localhost/{self.dbname}'
'?sslmode=require'
'&ssl_min_protocol_version=TLSv1.2'
'&ssl_max_protocol_version=TLSv1.1'
)
async def test_tls_version_ok(self):
con = await self.connect(
dsn=f'postgresql://ssl_user@localhost/{self.dbname}'
'?sslmode=require'
)
try:
await self.assertConnected(con)
finally:
con.terminate()
class TestClientSSLConnection(BaseTestSSLConnection):
def _add_hba_entry(self):
self.cluster.add_hba_entry(
type='hostssl', address=ipaddress.ip_network('127.0.0.0/24'),
database=self.dbname, user='ssl_user',
auth_method='cert')
self.cluster.add_hba_entry(
type='hostssl', address=ipaddress.ip_network('::1/128'),
database=self.dbname, user='ssl_user',
auth_method='cert')
async def test_ssl_connection_client_auth_fails_with_wrong_setup(self):
with self.assertRaisesRegex(
errors.BackendError,
"requires a valid client certificate",
):
await self.connect(
host='localhost',
user='ssl_user',
sslmode=SSLMode.require,
sslrootcert=SSL_CA_CERT_FILE,
)
async def _test_works(self, **conn_args):
con = await self.connect(**conn_args)
try:
await self.assertConnected(con)
finally:
con.terminate()
async def test_ssl_connection_client_auth_custom_context(self):
for key_file in (CLIENT_SSL_KEY_FILE, CLIENT_SSL_PROTECTED_KEY_FILE):
await self._test_works(
host='localhost',
user='ssl_user',
sslmode=SSLMode.require,
sslcert=CLIENT_SSL_CERT_FILE,
sslrootcert=SSL_CA_CERT_FILE,
sslpassword='secret1234',
sslkey=key_file
)
async def test_ssl_connection_client_auth_dsn(self):
params = {
'sslrootcert': SSL_CA_CERT_FILE,
'sslcert': CLIENT_SSL_CERT_FILE,
'sslkey': CLIENT_SSL_KEY_FILE,
'sslmode': 'verify-full',
}
params_str = urllib.parse.urlencode(params)
dsn = 'postgres://ssl_user@localhost/postgres?' + params_str
await self._test_works(dsn=dsn)
params['sslkey'] = CLIENT_SSL_PROTECTED_KEY_FILE
params['sslpassword'] = 'secret1234'
params_str = urllib.parse.urlencode(params)
dsn = 'postgres://ssl_user@localhost/postgres?' + params_str
await self._test_works(dsn=dsn)
async def test_ssl_connection_client_auth_env(self):
env = {
'PGSSLROOTCERT': SSL_CA_CERT_FILE,
'PGSSLCERT': CLIENT_SSL_CERT_FILE,
'PGSSLKEY': CLIENT_SSL_KEY_FILE,
}
dsn = 'postgres://ssl_user@localhost/postgres?sslmode=verify-full'
with unittest.mock.patch.dict('os.environ', env):
await self._test_works(dsn=dsn)
env['PGSSLKEY'] = CLIENT_SSL_PROTECTED_KEY_FILE
with unittest.mock.patch.dict('os.environ', env):
await self._test_works(dsn=dsn + '&sslpassword=secret1234')
async def test_ssl_connection_client_auth_dot_postgresql(self):
dsn = 'postgres://ssl_user@localhost/postgres?sslmode=verify-full'
with mock_dot_postgresql(client=True):
await self._test_works(dsn=dsn)
with mock_dot_postgresql(client=True, protected=True):
await self._test_works(dsn=dsn + '&sslpassword=secret1234')
class TestNoSSLConnection(BaseTestSSLConnection):
def _add_hba_entry(self):
self.cluster.add_hba_entry(
type='hostnossl', address=ipaddress.ip_network('127.0.0.0/24'),
database=self.dbname, user='ssl_user',
auth_method='trust')
self.cluster.add_hba_entry(
type='hostnossl', address=ipaddress.ip_network('::1/128'),
database=self.dbname, user='ssl_user',
auth_method='trust')
async def test_nossl_connection_sslmode(self):
async def verify_works(sslmode, *, host='localhost'):
con = None
try:
con = await self.connect(
sslmode=SSLMode.parse(sslmode),