|
26 | 26 | "DSN_FILE_ENV_VAR", |
27 | 27 | "ConfigError", |
28 | 28 | "async_url", |
| 29 | + "asyncpg_connect_args", |
29 | 30 | "load_document", |
30 | 31 | "load_plans", |
31 | 32 | "load_python_hooks", |
@@ -130,19 +131,74 @@ def _dsn_from_file() -> str | None: |
130 | 131 | raise ConfigError(msg) from exc |
131 | 132 |
|
132 | 133 |
|
| 134 | +# libpq's TLS parameters. asyncpg reads every one of them the way libpq does, |
| 135 | +# but only from a DSN it parses itself: SQLAlchemy hands the parameters of its |
| 136 | +# URL to asyncpg.connect() as keywords, and asyncpg has no keyword by these |
| 137 | +# names -- a DSN copied from psql with ?sslmode=require failed with a TypeError. |
| 138 | +_LIBPQ_TLS_PARAMETERS = frozenset( |
| 139 | + { |
| 140 | + "sslmode", |
| 141 | + "sslcert", |
| 142 | + "sslkey", |
| 143 | + "sslrootcert", |
| 144 | + "sslcrl", |
| 145 | + "sslpassword", |
| 146 | + "sslnegotiation", |
| 147 | + "ssl_min_protocol_version", |
| 148 | + "ssl_max_protocol_version", |
| 149 | + } |
| 150 | +) |
| 151 | + |
| 152 | + |
133 | 153 | def async_url(dsn: str) -> str: |
134 | 154 | """The DSN with an async driver named, since that is what the CLI drives. |
135 | 155 |
|
136 | 156 | ``postgresql://…`` means psycopg2 to SQLAlchemy, which cannot be driven |
137 | 157 | asynchronously; a DSN that already names its driver is left exactly as it |
138 | 158 | is, so ``postgresql+psycopg://`` keeps working for whoever installed it. |
| 159 | + libpq's TLS parameters leave the URL on the way, for |
| 160 | + :func:`asyncpg_connect_args` to hand to asyncpg. |
139 | 161 | """ |
140 | | - scheme, separator, rest = dsn.partition("://") |
141 | | - if not separator or "+" in scheme: |
| 162 | + rewritten = _asyncpg_rewrite(dsn) |
| 163 | + if rewritten is None: |
142 | 164 | return dsn |
143 | | - if scheme in {"postgresql", "postgres"}: |
144 | | - return f"postgresql+asyncpg://{rest}" |
145 | | - return dsn |
| 165 | + rest, _ = rewritten |
| 166 | + return f"postgresql+asyncpg://{rest}" |
| 167 | + |
| 168 | + |
| 169 | +def asyncpg_connect_args(dsn: str) -> dict[str, Any]: |
| 170 | + """The ``connect_args`` that carry a DSN's libpq TLS parameters to asyncpg. |
| 171 | +
|
| 172 | + They travel as a DSN of their own, ``postgresql://?sslmode=…``, which |
| 173 | + asyncpg parses the way libpq would -- ``sslrootcert`` and the rest |
| 174 | + included -- while host, user and database still come from the URL. |
| 175 | + Empty when the DSN names its own driver or carries no TLS parameters. |
| 176 | + """ |
| 177 | + rewritten = _asyncpg_rewrite(dsn) |
| 178 | + if rewritten is None or not rewritten[1]: |
| 179 | + return {} |
| 180 | + return {"dsn": "postgresql://?" + "&".join(rewritten[1])} |
| 181 | + |
| 182 | + |
| 183 | +def _asyncpg_rewrite(dsn: str) -> tuple[str, list[str]] | None: |
| 184 | + """Split a DSN the CLI will hand to asyncpg: the rest, and its TLS query parameters. |
| 185 | +
|
| 186 | + None for a DSN that is not rewritten -- one naming its driver, or not a |
| 187 | + ``postgresql://`` URL at all. Parameters are split as written, never |
| 188 | + decoded and encoded again, so everything that stays reads exactly as it did. |
| 189 | + """ |
| 190 | + scheme, separator, rest = dsn.partition("://") |
| 191 | + if not separator or "+" in scheme or scheme not in {"postgresql", "postgres"}: |
| 192 | + return None |
| 193 | + base, question, query = rest.partition("?") |
| 194 | + if not question: |
| 195 | + return rest, [] |
| 196 | + kept: list[str] = [] |
| 197 | + tls: list[str] = [] |
| 198 | + for parameter in query.split("&"): |
| 199 | + name = parameter.partition("=")[0] |
| 200 | + (tls if name in _LIBPQ_TLS_PARAMETERS else kept).append(parameter) |
| 201 | + return (f"{base}?{'&'.join(kept)}" if kept else base), tls |
146 | 202 |
|
147 | 203 |
|
148 | 204 | def select_configs(document: PartitionsDocument, tables: tuple[str, ...]) -> tuple[TablePartitionConfig, ...]: |
|
0 commit comments