authentik.root.tests.test_db_wrapper
1"""Tests for authentik.root.db.base.DatabaseWrapper.get_connection_params.""" 2 3from unittest import TestCase, mock 4 5from django_prometheus.db.backends.postgresql.base import ( 6 DatabaseWrapper as BaseDatabaseWrapper, 7) 8 9from authentik.lib.config import DIRECT_DB_ALIAS 10from authentik.root.db.base import DatabaseWrapper 11 12 13def _call_get_connection_params(alias: str, refresh_table: dict): 14 """Construct a minimal DatabaseWrapper and call get_connection_params. 15 16 ``refresh_table`` maps ``"postgresql.direct.host"`` etc. to the value that 17 ``CONFIG.refresh`` should return for that key. Unmapped keys return ``None``. 18 ``super().get_connection_params()`` is mocked to return a stable baseline 19 dict so the test focuses on what our override does to it. 20 """ 21 wrapper = DatabaseWrapper.__new__(DatabaseWrapper) 22 wrapper.alias = alias 23 24 baseline = { 25 "host": "BASELINE_HOST", 26 "port": 0, 27 "user": "BASELINE_USER", 28 "password": "BASELINE_PW", 29 "dbname": "authentik", 30 "sslmode": "BASELINE_SSLMODE", 31 } 32 33 with mock.patch.object( 34 BaseDatabaseWrapper, "get_connection_params", return_value=dict(baseline) 35 ): 36 with mock.patch("authentik.root.db.base.CONFIG") as mock_config: 37 mock_config.refresh.side_effect = refresh_table.get 38 params = wrapper.get_connection_params() 39 calls = [c.args[0] for c in mock_config.refresh.call_args_list] 40 return params, calls 41 42 43class TestDatabaseWrapperGetConnectionParams(TestCase): 44 """Verify per-alias config prefix routing in get_connection_params.""" 45 46 def test_default_alias_reads_top_level_postgresql_prefix(self): 47 """The ``default`` alias pulls host/port/user/password from 48 ``postgresql.*`` and ignores any ``postgresql.direct.*`` keys.""" 49 refresh_table = { 50 "postgresql.host": "127.0.0.1", 51 "postgresql.port": 5432, 52 "postgresql.user": "ak", 53 "postgresql.password": "secret", 54 # Direct keys must not influence the default alias. 55 "postgresql.direct.host": "10.0.0.1", 56 "postgresql.direct.sslmode": "verify-ca", 57 } 58 params, calls = _call_get_connection_params("default", refresh_table) 59 self.assertEqual(params["host"], "127.0.0.1") 60 self.assertEqual(params["port"], 5432) 61 self.assertEqual(params["user"], "ak") 62 self.assertEqual(params["password"], "secret") 63 # OPTIONS untouched by our override 64 self.assertEqual(params["sslmode"], "BASELINE_SSLMODE") 65 self.assertIn("postgresql.host", calls) 66 self.assertNotIn("postgresql.direct.host", calls) 67 68 def test_direct_alias_reads_direct_prefix_then_falls_back(self): 69 """``direct`` alias pulls host from ``postgresql.direct.*`` and falls 70 back to ``postgresql.*`` for unset keys. Regression guard: without the 71 alias-specific prefix, the HOST was overwritten with the default 72 endpoint's host while ``OPTIONS`` (sslmode=verify-ca) stayed — producing 73 ``connection to server at "<default-host>"... SSL was required``. 74 """ 75 refresh_table = { 76 "postgresql.direct.host": "10.114.0.29", 77 "postgresql.direct.user": "direct_user", 78 "postgresql.host": "127.0.0.1", 79 "postgresql.port": 5432, 80 "postgresql.user": "default_user", 81 "postgresql.password": "default_pw", 82 } 83 params, calls = _call_get_connection_params(DIRECT_DB_ALIAS, refresh_table) 84 self.assertEqual(params["host"], "10.114.0.29") 85 self.assertEqual(params["user"], "direct_user") 86 self.assertEqual(params["port"], 5432) 87 self.assertEqual(params["password"], "default_pw") 88 self.assertIn("postgresql.direct.host", calls) 89 self.assertIn("postgresql.direct.port", calls) 90 self.assertIn("postgresql.port", calls) 91 92 def test_direct_alias_does_not_get_clobbered_with_default_host(self): 93 """Regression for the original failure: direct alias ending up with 94 HOST=default_host because the override didn't know about the prefix.""" 95 refresh_table = { 96 "postgresql.direct.host": "10.114.0.29", 97 "postgresql.host": "127.0.0.1", 98 } 99 params, _ = _call_get_connection_params(DIRECT_DB_ALIAS, refresh_table) 100 self.assertNotEqual( 101 params["host"], 102 "127.0.0.1", 103 "direct alias must not be clobbered with the default postgresql.host", 104 ) 105 self.assertEqual(params["host"], "10.114.0.29") 106 107 def test_replica_alias_reads_replica_prefix(self): 108 """The ``replica_*`` aliases should continue to read from 109 ``postgresql.read_replicas.<name>.*`` (the existing behavior pre-fix 110 must not regress).""" 111 refresh_table = { 112 "postgresql.read_replicas.eu1.host": "replica-eu1.example", 113 "postgresql.read_replicas.eu1.port": 5433, 114 "postgresql.host": "127.0.0.1", 115 "postgresql.port": 5432, 116 "postgresql.user": "ak", 117 "postgresql.password": "secret", 118 } 119 params, calls = _call_get_connection_params("replica_eu1", refresh_table) 120 self.assertEqual(params["host"], "replica-eu1.example") 121 self.assertEqual(params["port"], 5433) 122 # Replica didn't override user/password — should fall back to postgresql.* 123 self.assertEqual(params["user"], "ak") 124 self.assertEqual(params["password"], "secret") 125 self.assertIn("postgresql.read_replicas.eu1.host", calls) 126 self.assertIn("postgresql.user", calls) # the fallback happened
44class TestDatabaseWrapperGetConnectionParams(TestCase): 45 """Verify per-alias config prefix routing in get_connection_params.""" 46 47 def test_default_alias_reads_top_level_postgresql_prefix(self): 48 """The ``default`` alias pulls host/port/user/password from 49 ``postgresql.*`` and ignores any ``postgresql.direct.*`` keys.""" 50 refresh_table = { 51 "postgresql.host": "127.0.0.1", 52 "postgresql.port": 5432, 53 "postgresql.user": "ak", 54 "postgresql.password": "secret", 55 # Direct keys must not influence the default alias. 56 "postgresql.direct.host": "10.0.0.1", 57 "postgresql.direct.sslmode": "verify-ca", 58 } 59 params, calls = _call_get_connection_params("default", refresh_table) 60 self.assertEqual(params["host"], "127.0.0.1") 61 self.assertEqual(params["port"], 5432) 62 self.assertEqual(params["user"], "ak") 63 self.assertEqual(params["password"], "secret") 64 # OPTIONS untouched by our override 65 self.assertEqual(params["sslmode"], "BASELINE_SSLMODE") 66 self.assertIn("postgresql.host", calls) 67 self.assertNotIn("postgresql.direct.host", calls) 68 69 def test_direct_alias_reads_direct_prefix_then_falls_back(self): 70 """``direct`` alias pulls host from ``postgresql.direct.*`` and falls 71 back to ``postgresql.*`` for unset keys. Regression guard: without the 72 alias-specific prefix, the HOST was overwritten with the default 73 endpoint's host while ``OPTIONS`` (sslmode=verify-ca) stayed — producing 74 ``connection to server at "<default-host>"... SSL was required``. 75 """ 76 refresh_table = { 77 "postgresql.direct.host": "10.114.0.29", 78 "postgresql.direct.user": "direct_user", 79 "postgresql.host": "127.0.0.1", 80 "postgresql.port": 5432, 81 "postgresql.user": "default_user", 82 "postgresql.password": "default_pw", 83 } 84 params, calls = _call_get_connection_params(DIRECT_DB_ALIAS, refresh_table) 85 self.assertEqual(params["host"], "10.114.0.29") 86 self.assertEqual(params["user"], "direct_user") 87 self.assertEqual(params["port"], 5432) 88 self.assertEqual(params["password"], "default_pw") 89 self.assertIn("postgresql.direct.host", calls) 90 self.assertIn("postgresql.direct.port", calls) 91 self.assertIn("postgresql.port", calls) 92 93 def test_direct_alias_does_not_get_clobbered_with_default_host(self): 94 """Regression for the original failure: direct alias ending up with 95 HOST=default_host because the override didn't know about the prefix.""" 96 refresh_table = { 97 "postgresql.direct.host": "10.114.0.29", 98 "postgresql.host": "127.0.0.1", 99 } 100 params, _ = _call_get_connection_params(DIRECT_DB_ALIAS, refresh_table) 101 self.assertNotEqual( 102 params["host"], 103 "127.0.0.1", 104 "direct alias must not be clobbered with the default postgresql.host", 105 ) 106 self.assertEqual(params["host"], "10.114.0.29") 107 108 def test_replica_alias_reads_replica_prefix(self): 109 """The ``replica_*`` aliases should continue to read from 110 ``postgresql.read_replicas.<name>.*`` (the existing behavior pre-fix 111 must not regress).""" 112 refresh_table = { 113 "postgresql.read_replicas.eu1.host": "replica-eu1.example", 114 "postgresql.read_replicas.eu1.port": 5433, 115 "postgresql.host": "127.0.0.1", 116 "postgresql.port": 5432, 117 "postgresql.user": "ak", 118 "postgresql.password": "secret", 119 } 120 params, calls = _call_get_connection_params("replica_eu1", refresh_table) 121 self.assertEqual(params["host"], "replica-eu1.example") 122 self.assertEqual(params["port"], 5433) 123 # Replica didn't override user/password — should fall back to postgresql.* 124 self.assertEqual(params["user"], "ak") 125 self.assertEqual(params["password"], "secret") 126 self.assertIn("postgresql.read_replicas.eu1.host", calls) 127 self.assertIn("postgresql.user", calls) # the fallback happened
Verify per-alias config prefix routing in get_connection_params.
47 def test_default_alias_reads_top_level_postgresql_prefix(self): 48 """The ``default`` alias pulls host/port/user/password from 49 ``postgresql.*`` and ignores any ``postgresql.direct.*`` keys.""" 50 refresh_table = { 51 "postgresql.host": "127.0.0.1", 52 "postgresql.port": 5432, 53 "postgresql.user": "ak", 54 "postgresql.password": "secret", 55 # Direct keys must not influence the default alias. 56 "postgresql.direct.host": "10.0.0.1", 57 "postgresql.direct.sslmode": "verify-ca", 58 } 59 params, calls = _call_get_connection_params("default", refresh_table) 60 self.assertEqual(params["host"], "127.0.0.1") 61 self.assertEqual(params["port"], 5432) 62 self.assertEqual(params["user"], "ak") 63 self.assertEqual(params["password"], "secret") 64 # OPTIONS untouched by our override 65 self.assertEqual(params["sslmode"], "BASELINE_SSLMODE") 66 self.assertIn("postgresql.host", calls) 67 self.assertNotIn("postgresql.direct.host", calls)
The default alias pulls host/port/user/password from
postgresql.* and ignores any postgresql.direct.* keys.
69 def test_direct_alias_reads_direct_prefix_then_falls_back(self): 70 """``direct`` alias pulls host from ``postgresql.direct.*`` and falls 71 back to ``postgresql.*`` for unset keys. Regression guard: without the 72 alias-specific prefix, the HOST was overwritten with the default 73 endpoint's host while ``OPTIONS`` (sslmode=verify-ca) stayed — producing 74 ``connection to server at "<default-host>"... SSL was required``. 75 """ 76 refresh_table = { 77 "postgresql.direct.host": "10.114.0.29", 78 "postgresql.direct.user": "direct_user", 79 "postgresql.host": "127.0.0.1", 80 "postgresql.port": 5432, 81 "postgresql.user": "default_user", 82 "postgresql.password": "default_pw", 83 } 84 params, calls = _call_get_connection_params(DIRECT_DB_ALIAS, refresh_table) 85 self.assertEqual(params["host"], "10.114.0.29") 86 self.assertEqual(params["user"], "direct_user") 87 self.assertEqual(params["port"], 5432) 88 self.assertEqual(params["password"], "default_pw") 89 self.assertIn("postgresql.direct.host", calls) 90 self.assertIn("postgresql.direct.port", calls) 91 self.assertIn("postgresql.port", calls)
direct alias pulls host from postgresql.direct.* and falls
back to postgresql.* for unset keys. Regression guard: without the
alias-specific prefix, the HOST was overwritten with the default
endpoint's host while OPTIONS (sslmode=verify-ca) stayed — producing
connection to server at "<default-host>"... SSL was required.
93 def test_direct_alias_does_not_get_clobbered_with_default_host(self): 94 """Regression for the original failure: direct alias ending up with 95 HOST=default_host because the override didn't know about the prefix.""" 96 refresh_table = { 97 "postgresql.direct.host": "10.114.0.29", 98 "postgresql.host": "127.0.0.1", 99 } 100 params, _ = _call_get_connection_params(DIRECT_DB_ALIAS, refresh_table) 101 self.assertNotEqual( 102 params["host"], 103 "127.0.0.1", 104 "direct alias must not be clobbered with the default postgresql.host", 105 ) 106 self.assertEqual(params["host"], "10.114.0.29")
Regression for the original failure: direct alias ending up with HOST=default_host because the override didn't know about the prefix.
108 def test_replica_alias_reads_replica_prefix(self): 109 """The ``replica_*`` aliases should continue to read from 110 ``postgresql.read_replicas.<name>.*`` (the existing behavior pre-fix 111 must not regress).""" 112 refresh_table = { 113 "postgresql.read_replicas.eu1.host": "replica-eu1.example", 114 "postgresql.read_replicas.eu1.port": 5433, 115 "postgresql.host": "127.0.0.1", 116 "postgresql.port": 5432, 117 "postgresql.user": "ak", 118 "postgresql.password": "secret", 119 } 120 params, calls = _call_get_connection_params("replica_eu1", refresh_table) 121 self.assertEqual(params["host"], "replica-eu1.example") 122 self.assertEqual(params["port"], 5433) 123 # Replica didn't override user/password — should fall back to postgresql.* 124 self.assertEqual(params["user"], "ak") 125 self.assertEqual(params["password"], "secret") 126 self.assertIn("postgresql.read_replicas.eu1.host", calls) 127 self.assertIn("postgresql.user", calls) # the fallback happened
The replica_* aliases should continue to read from
postgresql.read_replicas.<name>.* (the existing behavior pre-fix
must not regress).