Skip to content

Commit

Permalink
fixup! Adding secrets to data_interfaces module
Browse files Browse the repository at this point in the history
  • Loading branch information
juditnovak committed Aug 28, 2023
1 parent e4f3544 commit 577ee22
Showing 1 changed file with 14 additions and 19 deletions.
33 changes: 14 additions & 19 deletions lib/charms/data_platform_libs/v0/data_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,11 @@ def _on_topic_requested(self, event: TopicRequestedEvent):
LIBID = "6c3e6b6680d64e9c89e611d1a15f65be"

# Increment this major API version when introducing breaking changes
LIBAPI = 0
LIBAPI = 1

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

PYDEPS = ["ops>=2.0.0"]

Expand Down Expand Up @@ -632,7 +632,7 @@ def update_relation_secret(self, relation_id, content):
full_content.update(content)
secret.set_content(full_content)

def set_fields(self, relation_id: int, fields: Dict[str, str]) -> None:
def set_relation_fields(self, relation_id: int, fields: Dict[str, str]) -> None:
"""Get the value of a field not caring whether it's a secret or not."""
relation = self.get_relation(self.relation_name, relation_id)
relation_secret_fields = relation.data.get(
Expand Down Expand Up @@ -669,7 +669,7 @@ def set_credentials(self, relation_id: int, username: str, password: str) -> Non
username: user that was created.
password: password of the created user.
"""
self.set_fields(relation_id, {"username": username, "password": password})
self.set_relation_fields(relation_id, {"username": username, "password": password})

def set_tls(self, relation_id: int, tls: str) -> None:
"""Set whether TLS is enabled.
Expand All @@ -678,7 +678,7 @@ def set_tls(self, relation_id: int, tls: str) -> None:
relation_id: the identifier for a particular relation.
tls: whether tls is enabled (True or False).
"""
self.set_fields(relation_id, {"tls": tls})
self.set_relation_fields(relation_id, {"tls": tls})

def set_tls_ca(self, relation_id: int, tls_ca: str) -> None:
"""Set the TLS CA in the application relation databag.
Expand All @@ -687,7 +687,7 @@ def set_tls_ca(self, relation_id: int, tls_ca: str) -> None:
relation_id: the identifier for a particular relation.
tls_ca: TLS certification authority.
"""
self.set_fields(relation_id, {"tls-ca": tls_ca})
self.set_relation_fields(relation_id, {"tls-ca": tls_ca})


class DataRequires(DataRelation):
Expand Down Expand Up @@ -727,7 +727,7 @@ def _diff(self, event: RelationChangedEvent) -> Diff:
"""
return diff(event, self.local_unit)

def get_fields(
def get_relation_fields(
self, relation_id: int, fields: List[str], relation_name: Optional[str] = None
) -> Dict[str, str]:
"""Get the value of a field not caring whether it's a secret or not."""
Expand Down Expand Up @@ -763,7 +763,7 @@ def _is_resource_created_for_relation(self, relation: Relation) -> bool:
if not relation.app:
return False

data = self.get_fields(relation.id, ["username", "password"])
data = self.get_relation_fields(relation.id, ["username", "password"])
return bool(data.get("username")) and bool(data.get("password"))

def is_resource_created(self, relation_id: Optional[int] = None) -> bool:
Expand Down Expand Up @@ -1199,19 +1199,14 @@ def is_postgresql_plugin_enabled(self, plugin: str, relation_index: int = 0) ->

host = host.split(":")[0]

if secret_id := relation_data.get("secret"):
secret = SecretCache(self.charm, secret_id).get_secret()

if not secret:
raise SecretError("Secret %s couldn't be retrieved", secret_id)

content = secret.get_content()
if relation_data.get("secret"):
content = self.get_relation_secret_data(relation_index)
user = content.get("username")
password = content.get("password")

else:
user = relation_data.get("username")
password = relation_data.get("password")

connection_string = (
f"host='{host}' dbname='{self.database}' user='{user}' password='{password}'"
)
Expand Down Expand Up @@ -1258,7 +1253,7 @@ def _on_relation_changed_event(self, event: RelationChangedEvent) -> None:

new_secret_content = None
if self.secrets_enabled and "secret" in diff.added:
new_secret_content = self.get_fields(event.relation.id, ["username", "password"])
new_secret_content = self.get_relation_fields(event.relation.id, ["username", "password"])

if ("username" in diff.added and "password" in diff.added) or new_secret_content:
# Emit the default event (the one without an alias).
Expand Down Expand Up @@ -1511,7 +1506,7 @@ def _on_relation_changed_event(self, event: RelationChangedEvent) -> None:

new_secret_content = None
if self.secrets_enabled and "secret" in diff.added:
new_secret_content = self.get_fields(event.relation.id, ["username", "password"])
new_secret_content = self.get_relation_fields(event.relation.id, ["username", "password"])

if ("username" in diff.added and "password" in diff.added) or new_secret_content:
# Emit the default event (the one without an alias).
Expand Down Expand Up @@ -1680,7 +1675,7 @@ def _on_relation_changed_event(self, event: RelationChangedEvent) -> None:

new_secret_content = None
if self.secrets_enabled and "secret" in diff.added:
new_secret_content = self.get_fields(event.relation.id, ["username", "password"])
new_secret_content = self.get_relation_fields(event.relation.id, ["username", "password"])

# Check if the index is created
# (the OpenSearch charm shares the credentials).
Expand Down

0 comments on commit 577ee22

Please sign in to comment.