From 9e1aa484f667e956dd8c201d60b8a6d3a443aed2 Mon Sep 17 00:00:00 2001 From: Elouan Martinet Date: Tue, 28 Nov 2023 16:39:56 +0100 Subject: [PATCH] enabler(backends): allow ssl string parameters in PostgreSQL URL (#575) The underlying library asyncpg accepts string values in the ssl parameter. The old code only accepted the values true and false, which are converted to boolean. --- databases/backends/postgres.py | 6 +++++- tests/test_connection_options.py | 6 ++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/databases/backends/postgres.py b/databases/backends/postgres.py index e30c12d7..57cb7841 100644 --- a/databases/backends/postgres.py +++ b/databases/backends/postgres.py @@ -55,7 +55,11 @@ def _get_connection_kwargs(self) -> dict: if max_size is not None: kwargs["max_size"] = int(max_size) if ssl is not None: - kwargs["ssl"] = {"true": True, "false": False}[ssl.lower()] + ssl_lower = ssl.lower() + if ssl_lower == "true": + kwargs["ssl"] = True + elif ssl_lower == "false": + kwargs["ssl"] = False kwargs.update(self._options) diff --git a/tests/test_connection_options.py b/tests/test_connection_options.py index 9e4435ad..87a917f0 100644 --- a/tests/test_connection_options.py +++ b/tests/test_connection_options.py @@ -46,6 +46,12 @@ def test_postgres_ssl(): assert kwargs == {"ssl": True} +def test_postgres_ssl_verify_full(): + backend = PostgresBackend("postgres://localhost/database?ssl=verify-full") + kwargs = backend._get_connection_kwargs() + assert kwargs == {"ssl": "verify-full"} + + def test_postgres_explicit_ssl(): backend = PostgresBackend("postgres://localhost/database", ssl=True) kwargs = backend._get_connection_kwargs()