diff --git a/src/relations/backend_database.py b/src/relations/backend_database.py index c8d9f2ccc..e141697fe 100644 --- a/src/relations/backend_database.py +++ b/src/relations/backend_database.py @@ -125,12 +125,12 @@ def _on_database_created(self, event: DatabaseCreatedEvent) -> None: return plaintext_password = pgb.generate_password() + hashed_password = pgb.get_hashed_password(self.auth_user, plaintext_password) # create authentication user on postgres database, so we can authenticate other users # later on - self.postgres.create_user(self.auth_user, plaintext_password, admin=True) + self.postgres.create_user(self.auth_user, hashed_password, admin=True) self.initialise_auth_function([self.database.database, PG]) - hashed_password = pgb.get_hashed_password(self.auth_user, plaintext_password) self.charm.render_auth_file(f'"{self.auth_user}" "{hashed_password}"') cfg = self.charm.read_pgb_config() diff --git a/tests/unit/relations/test_backend_database.py b/tests/unit/relations/test_backend_database.py index 5dcd9cba7..832f0a79a 100644 --- a/tests/unit/relations/test_backend_database.py +++ b/tests/unit/relations/test_backend_database.py @@ -75,11 +75,11 @@ def test_on_database_created( mock_event = MagicMock() mock_event.username = "mock_user" self.backend._on_database_created(mock_event) + hash_pw = get_hashed_password(self.backend.auth_user, pw) - postgres.create_user.assert_called_with(self.backend.auth_user, pw, admin=True) + postgres.create_user.assert_called_with(self.backend.auth_user, hash_pw, admin=True) _init_auth.assert_has_calls([call([self.backend.database.database, "postgres"])]) - hash_pw = get_hashed_password(self.backend.auth_user, pw) _render.assert_any_call( f"{PGB_DIR}/userlist.txt", f'"{self.backend.auth_user}" "{hash_pw}"', perms=0o700 )