-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DPE-2342] Charm Relation (i.e. 'external') Secrets #91
Open
juditnovak
wants to merge
10
commits into
main
Choose a base branch
from
DPE-2342_relation_secrets
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
62f42b0
Tox changes: adding pytest-mock to integration tests
juditnovak b6a9eb0
Adding secrets to data_interfaces module
juditnovak 4f7857e
Unittests
juditnovak a6ad997
Integration tests
juditnovak 510d00b
Changes on the Kafka tests
juditnovak 57caab5
Openserch tests
juditnovak 595a225
Multi-secret mechansim (mostly) in place
juditnovak 99ca0dc
Unittests on multi-secrets
juditnovak 18f3d6c
Integration tests updated with multi-secrets
juditnovak 96d890d
No braking change
juditnovak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -300,7 +300,7 @@ def _on_topic_requested(self, event: TopicRequestedEvent): | |
from enum import Enum | ||
from typing import Dict, List, Optional, Union | ||
|
||
from ops import Handle, JujuVersion, Secret, SecretInfo | ||
from ops import JujuVersion, Secret, SecretInfo | ||
from ops.charm import ( | ||
CharmBase, | ||
CharmEvents, | ||
|
@@ -905,19 +905,61 @@ def extra_user_roles(self) -> Optional[str]: | |
|
||
|
||
class AuthenticationEvent(RelationEvent): | ||
"""Base class for authentication fields for events.""" | ||
"""Base class for authentication fields for events. | ||
|
||
def __init__( | ||
self, handle: Handle, relation: Relation, app: Optional[Application], unit: Optional[Unit] | ||
): | ||
super().__init__(handle, relation, app, unit) | ||
The amount of logic added here is not ideal -- but this was the only way to preserve | ||
the interface when moving to Juju Secrets | ||
""" | ||
|
||
@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) -> JujuVersion: | ||
"""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 | ||
|
@@ -926,6 +968,11 @@ def password(self) -> Optional[str]: | |
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 | ||
|
@@ -934,6 +981,11 @@ def tls(self) -> Optional[str]: | |
if not self.relation.app: | ||
return None | ||
|
||
if self.secrets_enabled: | ||
secret = self._get_secret("tls") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit (use walrus): if secret := self._get_secret("tls"):
return secret.get("tls") |
||
if secret: | ||
return secret.get("tls") | ||
|
||
return self.relation.data[self.relation.app].get("tls") | ||
|
||
@property | ||
|
@@ -942,6 +994,11 @@ def tls_ca(self) -> Optional[str]: | |
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") | ||
|
||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively, I can add this init() function:
...and then copy this into 6 --currently very pretty, one-liner-- descendant classes:
...but this is so ugly, that it doesn't even feel right -- just for two internal helper variables, that aren't supposed to be used outside of their encapsulated scope.... That I'd rather go with the technically less safe, but in another sense still more elegant solution. As instructions on the property functions clearly indicate: the helper variabes are meant to be encapsulated inside, noone should refer to them otherwise.
Anyway, if strong feelings on the other side, I can go for the
__init__()
solution.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
less ugly?