Skip to content

Commit 8e9d9ee

Browse files
committed
Suppress oversee users in standby clusters
1 parent dbd46b5 commit 8e9d9ee

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

actions.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,5 @@ set-tls-private-key:
6262
private-key:
6363
type: string
6464
description: The content of private key for communications with clients. Content will be auto-generated if this option is not specified.
65+
reenable-oversee-users:
66+
description: Reenable purging of managed credentials after a standby cluster is promoted.

src/relations/async_replication.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ def __init__(self, charm):
108108
self.framework.observe(
109109
self.charm.on.promote_to_primary_action, self._on_promote_to_primary
110110
)
111+
self.framework.observe(
112+
self.charm.on.reenable_oversee_users_action, self._on_reenable_oversee_users
113+
)
111114

112115
self.framework.observe(self.charm.on.secret_changed, self._on_secret_changed)
113116

@@ -193,6 +196,7 @@ def _configure_standby_cluster(self, event: RelationChangedEvent) -> bool:
193196
filename = f"{POSTGRESQL_DATA_PATH}-{str(datetime.now()).replace(' ', '-').replace(':', '-')}.tar.gz"
194197
subprocess.check_call(f"tar -zcf {filename} {POSTGRESQL_DATA_PATH}".split())
195198
logger.warning("Please review the backup file %s and handle its removal", filename)
199+
self.charm.app_peer_data["suppress-oversee-users"] = "true"
196200
return True
197201

198202
def get_all_primary_cluster_endpoints(self) -> List[str]:
@@ -599,6 +603,18 @@ def _on_promote_to_primary(self, event: ActionEvent) -> None:
599603
# Set the status.
600604
self.charm.unit.status = MaintenanceStatus("Creating replication...")
601605

606+
def _on_reenable_oversee_users(self, event: ActionEvent) -> None:
607+
"""Re-enable oversee users after cluster was promoted."""
608+
if not self.charm.unit.is_leader():
609+
event.fail("Unit is not leader")
610+
return
611+
612+
if "suppress-oversee-users" not in self.charm.app_peer_data:
613+
event.fail("Oversee users is not suppressed")
614+
return
615+
616+
del self.charm.app_peer_data["suppress-oversee-users"]
617+
602618
def _on_secret_changed(self, event: SecretChangedEvent) -> None:
603619
"""Update the internal secret when the relation secret changes."""
604620
relation = self._relation

src/relations/postgresql_provider.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ def oversee_users(self) -> None:
137137
if not self.charm.unit.is_leader():
138138
return
139139

140+
if "suppress-oversee-users" in self.charm.app_peer_data:
141+
logger.debug("Oversee users is suppressed by peer data")
142+
return
143+
140144
# Retrieve database users.
141145
try:
142146
database_users = {

tests/unit/test_async_replication.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright 2023 Canonical Ltd.
2+
# See LICENSE file for licensing details.
3+
from unittest.mock import Mock
4+
5+
import pytest
6+
from ops.testing import Harness
7+
8+
from charm import PostgresqlOperatorCharm
9+
10+
11+
@pytest.fixture(autouse=True)
12+
def harness():
13+
"""Set up the test."""
14+
harness = Harness(PostgresqlOperatorCharm)
15+
harness.begin()
16+
upgrade_relation_id = harness.add_relation("upgrade", "postgresql")
17+
peer_relation_id = harness.add_relation("database-peers", "postgresql")
18+
for rel_id in (upgrade_relation_id, peer_relation_id):
19+
harness.add_relation_unit(rel_id, "postgresql/1")
20+
with harness.hooks_disabled():
21+
harness.update_relation_data(upgrade_relation_id, "postgresql/1", {"state": "idle"})
22+
yield harness
23+
harness.cleanup()
24+
25+
26+
def test_on_reenable_oversee_users(harness):
27+
# Fail if unit is not leader
28+
event = Mock()
29+
30+
harness.charm.async_replication._on_reenable_oversee_users(event)
31+
32+
event.fail.assert_called_once_with("Unit is not leader")
33+
event.fail.reset_mock()
34+
35+
# Fail if peer data is not set
36+
with harness.hooks_disabled():
37+
harness.set_leader()
38+
39+
harness.charm.async_replication._on_reenable_oversee_users(event)
40+
41+
event.fail.assert_called_once_with("Oversee users is not suppressed")
42+
event.fail.reset_mock()
43+
44+
with harness.hooks_disabled():
45+
harness.charm._peers.data[harness.charm.app].update({"suppress-oversee-users": "true"})
46+
47+
harness.charm.async_replication._on_reenable_oversee_users(event)
48+
assert harness.charm._peers.data[harness.charm.app] == {}

0 commit comments

Comments
 (0)