Skip to content

Commit 4c06bcd

Browse files
committed
Set postgresql ALPN on SSL contexts created by asyncpg
PostgreSQL 17+ rejects direct SSL connections without the `postgresql` ALPN protocol, so `direct_tls=True` only worked through TLS-terminating proxies. Set it on the contexts asyncpg creates, like libpq does, and document that user-provided contexts need it for direct TLS. Fixes MagicStack#1371.
1 parent 6be5915 commit 4c06bcd

3 files changed

Lines changed: 26 additions & 0 deletions

File tree

‎asyncpg/connect_utils.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535

3636
_SSL_REQUEST_CODE = 80877103
3737

38+
# Required by PostgreSQL 17+ for direct TLS connections.
39+
_ALPN_PROTOCOLS = ['postgresql']
40+
3841

3942
class SSLMode(enum.IntEnum):
4043
disable = 0
@@ -811,8 +814,11 @@ def _parse_connect_dsn_and_args(*, dsn, host, port, user,
811814
ssl_max_protocol_version
812815
)
813816

817+
ssl.set_alpn_protocols(_ALPN_PROTOCOLS)
818+
814819
elif ssl is True:
815820
ssl = ssl_module.create_default_context()
821+
ssl.set_alpn_protocols(_ALPN_PROTOCOLS)
816822
sslmode = SSLMode.verify_full
817823
elif isinstance(ssl, ssl_module.SSLContext):
818824
sslmode = SSLMode.require

‎asyncpg/connection.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2337,6 +2337,10 @@ async def connect(dsn=None, *,
23372337
Pass ``True`` to skip PostgreSQL STARTTLS mode and perform a direct
23382338
SSL connection. Requires ``ssl='require'``, ``'verify-ca'``,
23392339
``'verify-full'``, ``True``, or an explicit ``SSLContext``.
2340+
PostgreSQL 17+ requires the ``postgresql`` ALPN protocol for direct
2341+
SSL connections: asyncpg sets it on the contexts it creates, but an
2342+
explicit ``SSLContext`` must set it with
2343+
``ctx.set_alpn_protocols(['postgresql'])``.
23402344
23412345
:param dict server_settings:
23422346
An optional dict of server runtime parameters. Refer to
@@ -2456,6 +2460,8 @@ async def connect(dsn=None, *,
24562460
``ssl=True``, or an explicit ``SSLContext``. Other values
24572461
(``'disable'``, ``'allow'``, and ``'prefer'``) will raise a
24582462
``ClientConfigurationError``.
2463+
SSL contexts created by asyncpg now set the ``postgresql`` ALPN
2464+
protocol, which PostgreSQL 17+ requires for direct SSL connections.
24592465
24602466
.. _SSLContext: https://docs.python.org/3/library/ssl.html#ssl.SSLContext
24612467
.. _create_default_context:

‎tests/test_connect.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2331,6 +2331,20 @@ async def test_direct_tls_connection(self):
23312331
self.assertEqual(ctx.verify_mode, ssl.CERT_NONE)
23322332
self.assertFalse(ctx.check_hostname)
23332333

2334+
async def test_direct_tls_native(self):
2335+
if self.cluster.get_pg_version() < (17, 0):
2336+
self.skipTest('native direct TLS requires PostgreSQL 17+')
2337+
2338+
for mode in ('require', 'verify-ca', 'verify-full', True):
2339+
with self.subTest(mode=mode):
2340+
with unittest.mock.patch.dict(os.environ, {
2341+
'SSL_CERT_FILE': SSL_CA_CERT_FILE,
2342+
}):
2343+
await self._test_works(
2344+
dsn='postgresql://ssl_user@localhost/postgres'
2345+
'?sslrootcert=' + SSL_CA_CERT_FILE,
2346+
ssl=mode, direct_tls=True, expected_ssl=True)
2347+
23342348
async def test_direct_tls_configuration_sources(self):
23352349
base_dsn = 'postgresql://ssl_user@localhost/postgres'
23362350

0 commit comments

Comments
 (0)