Skip to content

Commit 4d39a05

Browse files
committed
Fix SSLContext deprecation warnings
`SSLContext(PROTOCOL_SSL...)` should not be used anymore. Also, silence the one test where we deliberately test TLS v1.1
1 parent 2f4fe53 commit 4d39a05

File tree

1 file changed

+40
-31
lines changed

1 file changed

+40
-31
lines changed

tests/test_connect.py

+40-31
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import unittest
2121
import unittest.mock
2222
import urllib.parse
23+
import warnings
2324
import weakref
2425

2526
import asyncpg
@@ -1144,7 +1145,7 @@ def check():
11441145

11451146
@unittest.skipIf(os.environ.get('PGHOST'), 'unmanaged cluster')
11461147
async def test_connection_ssl_to_no_ssl_server(self):
1147-
ssl_context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
1148+
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
11481149
ssl_context.load_verify_locations(SSL_CA_CERT_FILE)
11491150

11501151
with self.assertRaisesRegex(ConnectionError, 'rejected SSL'):
@@ -1268,7 +1269,7 @@ def _add_hba_entry(self):
12681269
auth_method='trust')
12691270

12701271
async def test_ssl_connection_custom_context(self):
1271-
ssl_context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
1272+
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
12721273
ssl_context.load_verify_locations(SSL_CA_CERT_FILE)
12731274

12741275
con = await self.connect(
@@ -1360,7 +1361,7 @@ async def test_ssl_connection_default_context(self):
13601361
self.loop.set_exception_handler(old_handler)
13611362

13621363
async def test_ssl_connection_pool(self):
1363-
ssl_context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
1364+
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
13641365
ssl_context.load_verify_locations(SSL_CA_CERT_FILE)
13651366

13661367
pool = await self.create_pool(
@@ -1385,7 +1386,7 @@ async def worker():
13851386
await pool.close()
13861387

13871388
async def test_executemany_uvloop_ssl_issue_700(self):
1388-
ssl_context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
1389+
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
13891390
ssl_context.load_verify_locations(SSL_CA_CERT_FILE)
13901391

13911392
con = await self.connect(
@@ -1417,38 +1418,46 @@ async def test_tls_version(self):
14171418

14181419
# XXX: uvloop artifact
14191420
old_handler = self.loop.get_exception_handler()
1420-
try:
1421-
self.loop.set_exception_handler(lambda *args: None)
1422-
with self.assertRaisesRegex(ssl.SSLError, 'protocol version'):
1423-
await self.connect(
1424-
dsn='postgresql://ssl_user@localhost/postgres'
1425-
'?sslmode=require&ssl_min_protocol_version=TLSv1.3'
1426-
)
1427-
with self.assertRaises(ssl.SSLError):
1428-
await self.connect(
1429-
dsn='postgresql://ssl_user@localhost/postgres'
1430-
'?sslmode=require'
1431-
'&ssl_min_protocol_version=TLSv1.1'
1432-
'&ssl_max_protocol_version=TLSv1.1'
1433-
)
1434-
with self.assertRaisesRegex(ssl.SSLError, 'no protocols'):
1435-
await self.connect(
1421+
1422+
with warnings.catch_warnings():
1423+
warnings.filterwarnings(
1424+
"ignore",
1425+
message="ssl.TLSVersion.TLSv1_1 is deprecated",
1426+
category=DeprecationWarning
1427+
)
1428+
try:
1429+
self.loop.set_exception_handler(lambda *args: None)
1430+
with self.assertRaisesRegex(ssl.SSLError, 'protocol version'):
1431+
await self.connect(
1432+
dsn='postgresql://ssl_user@localhost/postgres'
1433+
'?sslmode=require&ssl_min_protocol_version=TLSv1.3'
1434+
)
1435+
with self.assertRaises(ssl.SSLError):
1436+
await self.connect(
1437+
dsn='postgresql://ssl_user@localhost/postgres'
1438+
'?sslmode=require'
1439+
'&ssl_min_protocol_version=TLSv1.1'
1440+
'&ssl_max_protocol_version=TLSv1.1'
1441+
)
1442+
with self.assertRaisesRegex(ssl.SSLError, 'no protocols'):
1443+
await self.connect(
1444+
dsn='postgresql://ssl_user@localhost/postgres'
1445+
'?sslmode=require'
1446+
'&ssl_min_protocol_version=TLSv1.2'
1447+
'&ssl_max_protocol_version=TLSv1.1'
1448+
)
1449+
con = await self.connect(
14361450
dsn='postgresql://ssl_user@localhost/postgres'
14371451
'?sslmode=require'
14381452
'&ssl_min_protocol_version=TLSv1.2'
1439-
'&ssl_max_protocol_version=TLSv1.1'
1453+
'&ssl_max_protocol_version=TLSv1.2'
14401454
)
1441-
con = await self.connect(
1442-
dsn='postgresql://ssl_user@localhost/postgres?sslmode=require'
1443-
'&ssl_min_protocol_version=TLSv1.2'
1444-
'&ssl_max_protocol_version=TLSv1.2'
1445-
)
1446-
try:
1447-
self.assertEqual(await con.fetchval('SELECT 42'), 42)
1455+
try:
1456+
self.assertEqual(await con.fetchval('SELECT 42'), 42)
1457+
finally:
1458+
await con.close()
14481459
finally:
1449-
await con.close()
1450-
finally:
1451-
self.loop.set_exception_handler(old_handler)
1460+
self.loop.set_exception_handler(old_handler)
14521461

14531462

14541463
@unittest.skipIf(os.environ.get('PGHOST'), 'unmanaged cluster')

0 commit comments

Comments
 (0)