From b7b0b9165f78b713219e4a90e1415d5a19ee658d Mon Sep 17 00:00:00 2001 From: Elouan Martinet Date: Tue, 28 Nov 2023 17:06:46 +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 | 3 ++- tests/test_connection_options.py | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/databases/backends/postgres.py b/databases/backends/postgres.py index e30c12d7..85972c3d 100644 --- a/databases/backends/postgres.py +++ b/databases/backends/postgres.py @@ -55,7 +55,8 @@ 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 = ssl.lower() + kwargs["ssl"] = {"true": True, "false": False}.get(ssl, ssl) kwargs.update(self._options) diff --git a/tests/test_connection_options.py b/tests/test_connection_options.py index 9e4435ad..81ce2ac7 100644 --- a/tests/test_connection_options.py +++ b/tests/test_connection_options.py @@ -46,12 +46,24 @@ 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() assert kwargs == {"ssl": True} +def test_postgres_explicit_ssl_verify_full(): + backend = PostgresBackend("postgres://localhost/database", ssl="verify-full") + kwargs = backend._get_connection_kwargs() + assert kwargs == {"ssl": "verify-full"} + + def test_postgres_no_extra_options(): backend = PostgresBackend("postgres://localhost/database") kwargs = backend._get_connection_kwargs()