Skip to content

Commit

Permalink
Secret tweaks (#298)
Browse files Browse the repository at this point in the history
  • Loading branch information
dragomirp authored Jun 10, 2024
1 parent 90bd19a commit 137a950
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
14 changes: 9 additions & 5 deletions lib/charms/data_platform_libs/v0/data_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ def _on_topic_requested(self, event: TopicRequestedEvent):

# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 35
LIBPATCH = 37

PYDEPS = ["ops>=2.0.0"]

Expand Down Expand Up @@ -642,22 +642,26 @@ def _move_to_new_label_if_needed(self):
return

# Create a new secret with the new label
old_meta = self._secret_meta
content = self._secret_meta.get_content()
self._secret_uri = None

# I wish we could just check if we are the owners of the secret...
try:
self._secret_meta = self.add_secret(content, label=self.label)
except ModelError as err:
if "this unit is not the leader" not in str(err):
raise
old_meta.remove_all_revisions()
self.current_label = None

def set_content(self, content: Dict[str, str]) -> None:
"""Setting cached secret content."""
if not self.meta:
return

# DPE-4182: do not create new revision if the content stay the same
if content == self.get_content():
return

if content:
self._move_to_new_label_if_needed()
self.meta.set_content(content)
Expand Down Expand Up @@ -1586,7 +1590,7 @@ def _register_secret_to_relation(
"""
label = self._generate_secret_label(relation_name, relation_id, group)

# Fetchin the Secret's meta information ensuring that it's locally getting registered with
# Fetching the Secret's meta information ensuring that it's locally getting registered with
CachedSecret(self._model, self.component, label, secret_id).meta

def _register_secrets_to_relation(self, relation: Relation, params_name_list: List[str]):
Expand Down Expand Up @@ -2309,7 +2313,7 @@ def _secrets(self) -> dict:
return self._cached_secrets

def _get_secret(self, group) -> Optional[Dict[str, str]]:
"""Retrieveing secrets."""
"""Retrieving secrets."""
if not self.app:
return
if not self._secrets.get(group):
Expand Down
10 changes: 3 additions & 7 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,11 +580,10 @@ def get_secret(self, scope: Scopes, key: str) -> Optional[str]:
peers = self.model.get_relation(PEER_RELATION_NAME)
if not peers:
return None

secret_key = self._translate_field_to_secret_key(key)
# Old translation in databag is to be taken
if key != secret_key and (
result := self.peer_relation_data(scope).fetch_my_relation_field(peers.id, key)
):
if result := self.peer_relation_data(scope).fetch_my_relation_field(peers.id, key):
return result

return self.peer_relation_data(scope).get_secret(peers.id, secret_key)
Expand All @@ -600,10 +599,7 @@ def set_secret(self, scope: Scopes, key: str, value: Optional[str]) -> Optional[
peers = self.model.get_relation(PEER_RELATION_NAME)
secret_key = self._translate_field_to_secret_key(key)
# Old translation in databag is to be deleted
if key != secret_key and self.peer_relation_data(scope).fetch_my_relation_field(
peers.id, key
):
self.peer_relation_data(scope).delete_relation_data(peers.id, [key])
self.peer_relation_data(scope).delete_relation_data(peers.id, [key])
self.peer_relation_data(scope).set_secret(peers.id, secret_key, value)

def remove_secret(self, scope: Scopes, key: str) -> None:
Expand Down
1 change: 0 additions & 1 deletion src/relations/peers.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ def __init__(self, charm: CharmBase):
self.framework.observe(charm.on[PEER_RELATION_NAME].relation_joined, self._on_joined)
self.framework.observe(charm.on[PEER_RELATION_NAME].relation_changed, self._on_changed)
self.framework.observe(charm.on.secret_changed, self._on_changed)
self.framework.observe(charm.on.secret_remove, self._on_changed)
self.framework.observe(charm.on[PEER_RELATION_NAME].relation_departed, self._on_departed)
self.framework.observe(charm.on.leader_elected, self._on_leader_elected)

Expand Down

0 comments on commit 137a950

Please sign in to comment.