Skip to content

Commit ec24388

Browse files
authored
General secrets resetting fix (#822)
1 parent 1cc4d7e commit ec24388

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

lib/charms/postgresql_k8s/v0/postgresql_tls.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545

4646
# Increment this PATCH version before using `charmcraft publish-lib` or reset
4747
# to 0 if you are raising the major API version.
48-
LIBPATCH = 12
48+
LIBPATCH = 13
4949

5050
logger = logging.getLogger(__name__)
5151
SCOPE = "unit"
@@ -135,16 +135,10 @@ def _on_certificate_available(self, event: CertificateAvailableEvent) -> None:
135135
event.defer()
136136
return
137137

138-
chain = self.charm.get_secret(SCOPE, "chain")
139138
new_chain = "\n".join(event.chain) if event.chain is not None else None
140-
if chain != new_chain:
141-
self.charm.set_secret(SCOPE, "chain", new_chain)
142-
cert = self.charm.get_secret(SCOPE, "cert")
143-
if cert != event.certificate:
144-
self.charm.set_secret(SCOPE, "cert", event.certificate)
145-
ca = self.charm.get_secret(SCOPE, "ca")
146-
if ca != event.ca:
147-
self.charm.set_secret(SCOPE, "ca", event.ca)
139+
self.charm.set_secret(SCOPE, "chain", new_chain)
140+
self.charm.set_secret(SCOPE, "cert", event.certificate)
141+
self.charm.set_secret(SCOPE, "ca", event.ca)
148142

149143
try:
150144
if not self.charm.push_tls_files_to_workload():

src/charm.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,13 @@ def app_units(self) -> set[Unit]:
286286

287287
return {self.unit, *self._peers.units}
288288

289+
def scoped_peer_data(self, scope: Scopes) -> dict | None:
290+
"""Returns peer data based on scope."""
291+
if scope == APP_SCOPE:
292+
return self.app_peer_data
293+
elif scope == UNIT_SCOPE:
294+
return self.unit_peer_data
295+
289296
@property
290297
def app_peer_data(self) -> dict:
291298
"""Application peer relation data object."""
@@ -361,7 +368,7 @@ def set_secret(self, scope: Scopes, key: str, value: str | None) -> str | None:
361368

362369
secret_key = self._translate_field_to_secret_key(key)
363370
# Old translation in databag is to be deleted
364-
self.peer_relation_data(scope).delete_relation_data(peers.id, [key])
371+
self.scoped_peer_data(scope).pop(key, None)
365372
self.peer_relation_data(scope).set_secret(peers.id, secret_key, value)
366373

367374
def remove_secret(self, scope: Scopes, key: str) -> None:
@@ -373,10 +380,8 @@ def remove_secret(self, scope: Scopes, key: str) -> None:
373380
return None
374381

375382
secret_key = self._translate_field_to_secret_key(key)
376-
if scope == APP_SCOPE:
377-
self.peer_relation_app.delete_relation_data(peers.id, [secret_key])
378-
else:
379-
self.peer_relation_unit.delete_relation_data(peers.id, [secret_key])
383+
384+
self.peer_relation_data(scope).delete_relation_data(peers.id, [secret_key])
380385

381386
@property
382387
def is_cluster_initialised(self) -> bool:

0 commit comments

Comments
 (0)