Skip to content
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

fix: update secret label when getting with both id and label #172

Merged
merged 1 commit into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions scenario/mocking.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,9 @@ def secret_get(
peek: bool = False,
) -> Dict[str, str]:
secret = self._get_secret(id, label)
# If both the id and label are provided, then update the label.
if id is not None and label is not None:
secret._set_label(label)
juju_version = self._context.juju_version
if not (juju_version == "3.1.7" or juju_version >= "3.3.1"):
# In this medieval Juju chapter,
Expand All @@ -424,6 +427,9 @@ def secret_info_get(
label: Optional[str] = None,
) -> SecretInfo:
secret = self._get_secret(id, label)
# If both the id and label are provided, then update the label.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we be doing this in _get_secret?
we-re side-effecting anyway, so...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wondered about that. _get_secret is used by three other methods - for grant/revoke it's not relevant, but the code would skipped anyway because the label will always be None. For set, it is relevant, but it's already done because it's updating potentially all the metadata.

So it's either updated twice in set or there's the code duplication. Maybe it's just cleaner in _get_secret so that's the better choice.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mmm right. I'm 50-50 on this one, I don't mind either way ;)

if id is not None and label is not None:
secret._set_label(label)

# only "manage"=write access level can read secret info
self._check_can_manage_secret(secret)
Expand Down
5 changes: 5 additions & 0 deletions scenario/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,10 @@ def __post_init__(self):
# bypass frozen dataclass
object.__setattr__(self, "latest_content", self.tracked_content)

def _set_label(self, label):
# bypass frozen dataclass
object.__setattr__(self, "label", label)

def _track_latest_revision(self):
"""Set the current revision to the tracked revision."""
# bypass frozen dataclass
Expand All @@ -340,6 +344,7 @@ def _update_metadata(
object.__setattr__(self, "_latest_revision", self._latest_revision + 1)
# TODO: if this is done twice in the same hook, then Juju ignores the
# first call, it doesn't continue to update like this does.
# Fix when https://github.com/canonical/operator/issues/1288 is resolved.
if content:
object.__setattr__(self, "latest_content", content)
if label:
Expand Down
18 changes: 18 additions & 0 deletions tests/test_e2e/test_secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,24 @@ def _on_secret_remove(self, event):
)


def test_set_label_on_get():
class SecretCharm(CharmBase):
def __init__(self, framework):
super().__init__(framework)
self.framework.observe(self.on.start, self._on_start)

def _on_start(self, _):
id = self.unit.add_secret({"foo": "bar"}).id
secret = self.model.get_secret(id=id, label="label1")
assert secret.label == "label1"
secret = self.model.get_secret(id=id, label="label2")
assert secret.label == "label2"

ctx = Context(SecretCharm, meta={"name": "foo"})
state = ctx.run(ctx.on.start(), State())
assert state.get_secret(label="label2").tracked_content == {"foo": "bar"}


def test_no_additional_positional_arguments():
with pytest.raises(TypeError):
Secret({}, {}, None)
Expand Down
Loading