Skip to content

Commit fe8bd8b

Browse files
committed
Change charm database user
1 parent 01b82ef commit fe8bd8b

File tree

6 files changed

+43
-48
lines changed

6 files changed

+43
-48
lines changed

actions.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33

44
get-primary:
55
description: Get the unit with is the primary/leader in the replication.
6-
get-postgres-password:
7-
description: Get the initial postgres user password for the database.
6+
get-operator-password:
7+
description: Get the operator user password used by charm.
8+
It is internal charm user, SHOULD NOT be used by applications.

src/charm.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from requests import ConnectionError
3131
from tenacity import RetryError
3232

33-
from constants import PEER
33+
from constants import PEER, USER
3434
from patroni import NotReadyError, Patroni
3535
from relations.db import DbProvides
3636
from relations.postgresql_provider import PostgreSQLProvider
@@ -59,7 +59,7 @@ def __init__(self, *args):
5959
self.framework.observe(self.on.postgresql_pebble_ready, self._on_postgresql_pebble_ready)
6060
self.framework.observe(self.on.upgrade_charm, self._on_upgrade_charm)
6161
self.framework.observe(
62-
self.on.get_postgres_password_action, self._on_get_postgres_password
62+
self.on.get_operator_password_action, self._on_get_operator_password
6363
)
6464
self.framework.observe(self.on.get_primary_action, self._on_get_primary)
6565
self.framework.observe(self.on.update_status, self._on_update_status)
@@ -74,8 +74,8 @@ def postgresql(self) -> PostgreSQL:
7474
"""Returns an instance of the object used to interact with the database."""
7575
return PostgreSQL(
7676
host=self.primary_endpoint,
77-
user="postgres",
78-
password=self._get_postgres_password(),
77+
user=USER,
78+
password=self._get_operator_password(),
7979
database="postgres",
8080
)
8181

@@ -250,11 +250,11 @@ def _get_hostname_from_unit(self, member: str) -> str:
250250
def _on_leader_elected(self, event: LeaderElectedEvent) -> None:
251251
"""Handle the leader-elected event."""
252252
data = self._peers.data[self.app]
253-
postgres_password = data.get("postgres-password", None)
253+
operator_password = data.get("operator-password", None)
254254
replication_password = data.get("replication-password", None)
255255

256-
if postgres_password is None:
257-
self._peers.data[self.app]["postgres-password"] = new_password()
256+
if operator_password is None:
257+
self._peers.data[self.app]["operator-password"] = new_password()
258258

259259
if replication_password is None:
260260
self._peers.data[self.app]["replication-password"] = new_password()
@@ -387,9 +387,9 @@ def _create_resources(self) -> None:
387387
self.unit.status = BlockedStatus(f"failed to create services {e}")
388388
return
389389

390-
def _on_get_postgres_password(self, event: ActionEvent) -> None:
391-
"""Returns the password for the postgres user as an action response."""
392-
event.set_results({"postgres-password": self._get_postgres_password()})
390+
def _on_get_operator_password(self, event: ActionEvent) -> None:
391+
"""Returns the password for the operator user as an action response."""
392+
event.set_results({"operator-password": self._get_operator_password()})
393393

394394
def _on_get_primary(self, event: ActionEvent) -> None:
395395
"""Get primary instance."""
@@ -501,8 +501,8 @@ def _postgresql_layer(self) -> Layer:
501501
"PATRONI_SCOPE": f"patroni-{self._name}",
502502
"PATRONI_REPLICATION_USERNAME": "replication",
503503
"PATRONI_REPLICATION_PASSWORD": self._replication_password,
504-
"PATRONI_SUPERUSER_USERNAME": "postgres",
505-
"PATRONI_SUPERUSER_PASSWORD": self._get_postgres_password(),
504+
"PATRONI_SUPERUSER_USERNAME": USER,
505+
"PATRONI_SUPERUSER_PASSWORD": self._get_operator_password(),
506506
},
507507
}
508508
},
@@ -519,10 +519,10 @@ def _peers(self) -> Relation:
519519
"""
520520
return self.model.get_relation(PEER)
521521

522-
def _get_postgres_password(self) -> str:
523-
"""Get postgres user password."""
522+
def _get_operator_password(self) -> str:
523+
"""Get operator user password."""
524524
data = self._peers.data[self.app]
525-
return data.get("postgres-password", None)
525+
return data.get("operator-password", None)
526526

527527
@property
528528
def _replication_password(self) -> str:

src/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55

66
DATABASE_PORT = "5432"
77
PEER = "database-peers"
8+
USER = "operator"

tests/integration/helpers.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ async def check_database_users_existence(
3030
"""
3131
unit = ops_test.model.applications[DATABASE_APP_NAME].units[0]
3232
unit_address = await get_unit_address(ops_test, unit.name)
33-
password = await get_postgres_password(ops_test)
33+
password = await get_operator_password(ops_test)
3434

3535
# Retrieve all users in the database.
3636
output = await execute_query_on_unit(
@@ -61,7 +61,7 @@ async def check_database_creation(ops_test: OpsTest, database: str) -> None:
6161
ops_test: The ops test framework
6262
database: Name of the database that should have been created
6363
"""
64-
password = await get_postgres_password(ops_test)
64+
password = await get_operator_password(ops_test)
6565

6666
for unit in ops_test.model.applications[DATABASE_APP_NAME].units:
6767
unit_address = await get_unit_address(ops_test, unit.name)
@@ -196,12 +196,12 @@ def get_application_units(ops_test: OpsTest, application_name: str) -> List[str]
196196
]
197197

198198

199-
async def get_postgres_password(ops_test: OpsTest):
200-
"""Retrieve the postgres user password using the action."""
199+
async def get_operator_password(ops_test: OpsTest):
200+
"""Retrieve the operator user password using the action."""
201201
unit = ops_test.model.units.get(f"{DATABASE_APP_NAME}/0")
202-
action = await unit.run_action("get-postgres-password")
202+
action = await unit.run_action("get-operator-password")
203203
result = await action.wait()
204-
return result.results["postgres-password"]
204+
return result.results["operator-password"]
205205

206206

207207
async def get_primary(ops_test: OpsTest, unit_id=0) -> str:

tests/integration/test_charm.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
convert_records_to_dict,
1919
get_application_units,
2020
get_cluster_members,
21+
get_operator_password,
2122
get_unit_address,
2223
scale_application,
2324
)
@@ -75,7 +76,7 @@ async def test_database_is_up(ops_test: OpsTest, unit_id: int):
7576

7677
@pytest.mark.parametrize("unit_id", UNIT_IDS)
7778
async def test_settings_are_correct(ops_test: OpsTest, unit_id: int):
78-
password = await get_postgres_password(ops_test)
79+
password = await get_operator_password(ops_test)
7980

8081
# Connect to PostgreSQL.
8182
host = await get_unit_address(ops_test, f"{APP_NAME}/{unit_id}")
@@ -163,7 +164,7 @@ async def test_scale_down_and_up(ops_test: OpsTest):
163164
async def test_persist_data_through_graceful_restart(ops_test: OpsTest):
164165
"""Test data persists through a graceful restart."""
165166
primary = await get_primary(ops_test)
166-
password = await get_postgres_password(ops_test)
167+
password = await get_operator_password(ops_test)
167168
address = await get_unit_address(ops_test, primary)
168169

169170
# Write data to primary IP.
@@ -191,7 +192,7 @@ async def test_persist_data_through_graceful_restart(ops_test: OpsTest):
191192
async def test_persist_data_through_failure(ops_test: OpsTest):
192193
"""Test data persists through a failure."""
193194
primary = await get_primary(ops_test)
194-
password = await get_postgres_password(ops_test)
195+
password = await get_operator_password(ops_test)
195196
address = await get_unit_address(ops_test, primary)
196197

197198
# Write data to primary IP.
@@ -291,14 +292,6 @@ async def get_primary(ops_test: OpsTest, unit_id=0) -> str:
291292
return action.results["primary"]
292293

293294

294-
async def get_postgres_password(ops_test: OpsTest):
295-
"""Retrieve the postgres user password using the action."""
296-
unit = ops_test.model.units.get(f"{APP_NAME}/0")
297-
action = await unit.run_action("get-postgres-password")
298-
result = await action.wait()
299-
return result.results["postgres-password"]
300-
301-
302295
def db_connect(host: str, password: str):
303296
"""Returns psycopg2 connection object linked to postgres db in the given host.
304297
@@ -307,7 +300,7 @@ def db_connect(host: str, password: str):
307300
password: postgres password
308301
309302
Returns:
310-
psycopg2 connection object linked to postgres db, under "postgres" user.
303+
psycopg2 connection object linked to postgres db, under "operator" user.
311304
"""
312305
return psycopg2.connect(
313306
f"dbname='postgres' user='postgres' host='{host}' password='{password}' connect_timeout=10"

tests/unit/test_charm.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def test_on_leader_elected(self, _, __, _render_postgresql_conf_file, ___):
5252

5353
# Check that a new password was generated on leader election.
5454
self.harness.set_leader()
55-
superuser_password = self.charm._peers.data[self.charm.app].get("postgres-password", None)
55+
superuser_password = self.charm._peers.data[self.charm.app].get("operator-password", None)
5656
self.assertIsNotNone(superuser_password)
5757

5858
replication_password = self.charm._peers.data[self.charm.app].get(
@@ -65,7 +65,7 @@ def test_on_leader_elected(self, _, __, _render_postgresql_conf_file, ___):
6565
self.harness.set_leader(False)
6666
self.harness.set_leader()
6767
self.assertEqual(
68-
self.charm._peers.data[self.charm.app].get("postgres-password", None),
68+
self.charm._peers.data[self.charm.app].get("operator-password", None),
6969
superuser_password,
7070
)
7171
self.assertEqual(
@@ -104,13 +104,13 @@ def test_on_postgresql_pebble_ready(
104104
self.assertEqual(container.get_service(self._postgresql_service).is_running(), True)
105105
_render_patroni_yml_file.assert_called_once()
106106

107-
@patch("charm.PostgresqlOperatorCharm._get_postgres_password")
108-
def test_on_get_postgres_password(self, _get_postgres_password):
107+
@patch("charm.PostgresqlOperatorCharm._get_operator_password")
108+
def test_on_get_operator_password(self, _get_operator_password):
109109
mock_event = Mock()
110-
_get_postgres_password.return_value = "test-password"
111-
self.charm._on_get_postgres_password(mock_event)
112-
_get_postgres_password.assert_called_once()
113-
mock_event.set_results.assert_called_once_with({"postgres-password": "test-password"})
110+
_get_operator_password.return_value = "test-password"
111+
self.charm._on_get_operator_password(mock_event)
112+
_get_operator_password.assert_called_once()
113+
mock_event.set_results.assert_called_once_with({"operator-password": "test-password"})
114114

115115
@patch_network_get(private_address="1.1.1.1")
116116
@patch("charm.Patroni.get_primary")
@@ -278,8 +278,8 @@ def test_postgresql_layer(self, _, __, ___, ____):
278278
"PATRONI_SCOPE": f"patroni-{self.charm._name}",
279279
"PATRONI_REPLICATION_USERNAME": "replication",
280280
"PATRONI_REPLICATION_PASSWORD": self.charm._replication_password,
281-
"PATRONI_SUPERUSER_USERNAME": "postgres",
282-
"PATRONI_SUPERUSER_PASSWORD": self.charm._get_postgres_password(),
281+
"PATRONI_SUPERUSER_USERNAME": "operator",
282+
"PATRONI_SUPERUSER_PASSWORD": self.charm._get_operator_password(),
283283
},
284284
}
285285
},
@@ -290,13 +290,13 @@ def test_postgresql_layer(self, _, __, ___, ____):
290290
@patch("charm.Patroni.render_postgresql_conf_file")
291291
@patch("charm.PostgresqlOperatorCharm._patch_pod_labels")
292292
@patch("charm.PostgresqlOperatorCharm._create_resources")
293-
def test_get_postgres_password(self, _, __, ___, ____):
293+
def test_get_operator_password(self, _, __, ___, ____):
294294
# Test for a None password.
295295
self.harness.add_relation(self._peer_relation, self.charm.app.name)
296-
self.assertIsNone(self.charm._get_postgres_password())
296+
self.assertIsNone(self.charm._get_operator_password())
297297

298298
# Then test for a non empty password after leader election and peer data set.
299299
self.harness.set_leader()
300-
password = self.charm._get_postgres_password()
300+
password = self.charm._get_operator_password()
301301
self.assertIsNotNone(password)
302302
self.assertNotEqual(password, "")

0 commit comments

Comments
 (0)