direct_tls doesn't work with PostgreSQL 17+ because ALPN isn't set
還沒有人認領這個 Issue。
評估
研究方向
首先定位 asyncpg 為 direct_tls 建立的 SSL 內容,以及處理直接 SSL 協商的連線路徑。使用 issue 中的 PostgreSQL 18 Docker 命令重現,然後驗證 direct_tls 連線可與預設內容正常運作,並為 PostgreSQL ALPN 協定新增或更新涵蓋率。
由索引模型根據 Issue 內容生成。
描述
- asyncpg version: 0.31.0 (also checked master)
- PostgreSQL version: 17.11 and 18.0
- Do you use a PostgreSQL SaaS? If so, which? Can you reproduce
the issue with a local PostgreSQL install?: No, reproduced with a local PostgreSQL in Docker - Python version: 3.12
- Platform: Linux
- Do you use pgbouncer?: No
- Did you install asyncpg with pip?: Yes (via uv)
- If you built asyncpg locally, which version of Cython did you use?: N/A
- Can the issue be reproduced under both asyncio and
uvloop?: Only tried asyncio
When connecting with direct_tls=True, the connection gets dropped right away:
await asyncpg.connect(dsn, ssl="require", direct_tls=True)
# ConnectionDoesNotExistError: connection was closed in the middle of operation
The server log says why:
LOG: received direct SSL connection request without ALPN protocol negotiation extension
PostgreSQL 17 requires the postgresql ALPN protocol for direct SSL connections, and libpq always sends it. asyncpg doesn't set ALPN on the context it builds, so as far as I can tell direct_tls (and sslnegotiation=direct in the DSN) can't work against a stock PG 17+ server unless you pass your own context:
ctx = ssl.create_default_context()
ctx.set_alpn_protocols(["postgresql"])
await asyncpg.connect(dsn, ssl=ctx, direct_tls=True) # works
Repro with Docker:
docker run --rm -e POSTGRES_PASSWORD=postgres -p 5432:5432 postgres:18 \
-c ssl=on \
-c ssl_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem \
-c ssl_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
I'll send a PR that sets postgresql ALPN on the contexts asyncpg creates.
- 主要語言
- Python
- 星號
- 8.1k
- 分支
- 469
- 平均合併
- 4 天 1 小時
- 30 天內合併 PR
- 14
貢獻指南
這個儲存庫沒有索引到貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
MagicStack/asyncpg 的其他 Issue
-
難度 3/5 1-2 天 新手友好度 72/100
MagicStack/asyncpg#1342 ·
-
難度 3/5 1-2 天 新手友好度 56/100
MagicStack/asyncpg#1340 · 1 則留言 ·
-
難度 2/5 1-3 小時 新手友好度 28/100
MagicStack/asyncpg#1337 ·
-
難度 4/5 3-5 天 新手友好度 42/100
MagicStack/asyncpg#1330 · 1 則留言 ·
-
難度 5/5 一週以上 新手友好度 38/100
MagicStack/asyncpg#1322 ·
查看 MagicStack/asyncpg 的全部 Issue
相似的 Issue
-
bug
難度 2/5 1-3 小時 新手友好度 75/100
stephrobert/dsoxlab#238 ·
-
難度 2/5 1-3 小時 新手友好度 75/100
-
難度 2/5 1-3 小時 新手友好度 75/100
sublimehq/package_control#1780 ·
-
難度 2/5 1-3 小時 新手友好度 65/100
-
難度 2/5 1-3 小時 新手友好度 70/100
nwg-piotr/nwg-displays#145 ·