diff --git a/services/utils/__init__.py b/services/utils/__init__.py index de21ffd8..fa593bba 100644 --- a/services/utils/__init__.py +++ b/services/utils/__init__.py @@ -13,6 +13,7 @@ from packaging.version import Version, parse from importlib import metadata + USE_SEPARATE_READER_POOL = os.environ.get("USE_SEPARATE_READER_POOL", "0") in ["True", "true", "1"] version = metadata.version("metadata_service") @@ -272,17 +273,6 @@ def connection_string_url(self, type=None): elif type == DBType.READER: base_url = f'postgresql://{quote(self._user)}:{quote(self._password)}@{self._read_replica_host}:{self._port}/{self._database_name}' - if (self._ssl_mode in ['allow', 'prefer', 'require', 'verify-ca', 'verify-full']): - ssl_query = f'sslmode={self._ssl_mode}' - if self._ssl_cert_path is not None: - ssl_query = f'{ssl_query}&sslcert={self._ssl_cert_path}' - if self._ssl_key_path is not None: - ssl_query = f'{ssl_query}&sslkey={self._ssl_key_path}' - if self._ssl_root_cert_path is not None: - ssl_query = f'{ssl_query}&sslrootcert={self._ssl_root_cert_path}' - else: - ssl_query = f'sslmode=disable' - return f'{base_url}?{ssl_query}' def get_dsn(self, type=None): @@ -307,12 +297,13 @@ def get_dsn(self, type=None): 'sslkey': sslkey, 'sslrootcert': sslrootcert } - + if type == DBType.READER: # We assume that everything except the hostname remains the same for a reader. # At the moment this is a fair assumption for Postgres read replicas. kwargs.update({"host":self._read_replica_host}) + return psycopg2.extensions.make_dsn(**{k: v for k, v in kwargs.items() if v is not None}) else: return self._dsn diff --git a/services/utils/tests/unit_tests/utils_test.py b/services/utils/tests/unit_tests/utils_test.py index 781872c2..26ae16ef 100644 --- a/services/utils/tests/unit_tests/utils_test.py +++ b/services/utils/tests/unit_tests/utils_test.py @@ -130,6 +130,7 @@ def test_db_conf_env_dsn(): # valid DSN in env should set correctly. assert DBConfiguration().get_dsn() == 'dbname=testgres user=test_user host=test_host port=1234 password=test_pwd sslmode=verify-full sslrootcert=/test' + def test_db_conf_pool_size(): with set_env(): db_conf = DBConfiguration(pool_min=2, pool_max=4)