Skip to content

Commit

Permalink
No braking change
Browse files Browse the repository at this point in the history
  • Loading branch information
juditnovak committed Sep 3, 2023
1 parent 18f3d6c commit 8ba180c
Showing 1 changed file with 104 additions and 1 deletion.
105 changes: 104 additions & 1 deletion lib/charms/data_platform_libs/v0/data_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,110 @@ def extra_user_roles(self) -> Optional[str]:


class AuthenticationEvent(RelationEvent):
"""Base class for authentication fields for events."""
"""Base class for authentication fields for events.
The amount of logic added here is not ideal -- but this was the only way to preserve
the interface when moving to Juju Secrets
"""

def __init__(
self,
handle: Handle,
relation: Relation,
app: Optional[Application] = None,
unit: Optional[Unit] = None,
):
super().__init__(handle, relation, app, unit)

@property
def _secrets(self) -> dict:
"""Caching secrets to avoid fetching them each time a field is referrd
DON'T USE the encapsulated helper variable outside of this function
"""
if not hasattr(self, "_cached_secrets"):
self._cached_secrets = {}
return self._cached_secrets

@property
def _jujuversion(self) -> str:
"""Caching jujuversion to avoid a Juju call on each field evaluation
DON'T USE the encapsulated helper variable outside of this function
"""
if not hasattr(self, "_cached_jujuversion"):
self._cached_jujuversion = None
if not self._cached_jujuversion:
self._cached_jujuversion = JujuVersion.from_environ()
return self._cached_jujuversion

def _get_secret(self, label) -> Optional[Dict[str, str]]:
"""Retrieveing secrets."""
if not self.app:
return
if not self._secrets.get(label):
self._secrets[label] = None
if secret_uri := self.relation.data[self.app].get(f"secret-{label}"):
secret = self.framework.model.get_secret(id=secret_uri)
self._secrets[label] = secret.get_content()
return self._secrets[label]

@property
def secrets_enabled(self):
"""Is this Juju version allowing for Secrets usage?"""
return self._jujuversion.has_secrets

@property
def username(self) -> Optional[str]:
"""Returns the created username."""
if not self.relation.app:
return None

if self.secrets_enabled:
secret = self._get_secret("user")
if secret:
return secret.get("username")

return self.relation.data[self.relation.app].get("username")

@property
def password(self) -> Optional[str]:
"""Returns the password for the created user."""
if not self.relation.app:
return None

if self.secrets_enabled:
secret = self._get_secret("user")
if secret:
return secret.get("password")

return self.relation.data[self.relation.app].get("password")

@property
def tls(self) -> Optional[str]:
"""Returns whether TLS is configured."""
if not self.relation.app:
return None

if self.secrets_enabled:
secret = self._get_secret("tls")
if secret:
return secret.get("tls")

return self.relation.data[self.relation.app].get("tls")

@property
def tls_ca(self) -> Optional[str]:
"""Returns TLS CA."""
if not self.relation.app:
return None

if self.secrets_enabled:
secret = self._get_secret("tls")
if secret:
return secret.get("tls-ca")

return self.relation.data[self.relation.app].get("tls-ca")

def __init__(
self, handle: Handle, relation: Relation, app: Optional[Application], unit: Optional[Unit]
Expand Down

0 comments on commit 8ba180c

Please sign in to comment.