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

feat: secret owners do not auto-peek, and can use refresh #1067

Merged
7 changes: 1 addition & 6 deletions ops/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2445,12 +2445,7 @@ def secret_get(self, *,
secret = self._ensure_secret_id_or_label(id, label)

# Check that caller has permission to get this secret
if secret.owner_name in [self.app_name, self.unit_name]:
# Owner or peer is calling, get latest revision
peek = True
if refresh:
raise ValueError('Secret owner cannot use refresh=True')
else:
if secret.owner_name not in [self.app_name, self.unit_name]:
# Observer is calling: does secret have a grant on relation between
# this charm (the observer) and the secret owner's app?
owner_app = secret.owner_name.split('/')[0]
Expand Down
51 changes: 51 additions & 0 deletions test/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4712,6 +4712,57 @@ def test_add_model_secret_by_unit_instance(self):
self.assertEqual(secret.id, secret_id)
self.assertEqual(secret.get_content(), {'password': 'hunter4'})

def test_get_secret_as_owner(self):
harness = ops.testing.Harness(ops.CharmBase, meta='name: webapp')
self.addCleanup(harness.cleanup)
harness.begin()
# App secret.
secret_id = harness.charm.app.add_secret({'password': 'hunter5'}).id
secret = harness.model.get_secret(id=secret_id)
self.assertEqual(secret.id, secret_id)
self.assertEqual(secret.get_content(), {'password': 'hunter5'})
# Unit secret.
secret_id = harness.charm.unit.add_secret({'password': 'hunter6'}).id
secret = harness.model.get_secret(id=secret_id)
self.assertEqual(secret.id, secret_id)
self.assertEqual(secret.get_content(), {'password': 'hunter6'})

def test_get_secret_and_refresh(self):
harness = ops.testing.Harness(ops.CharmBase, meta='name: webapp')
self.addCleanup(harness.cleanup)
harness.begin()
secret = harness.charm.app.add_secret({'password': 'hunter6'})
secret.set_content({"password": "hunter7"})
retrieved_secret = harness.model.get_secret(id=secret.id)
self.assertEqual(retrieved_secret.id, secret.id)
self.assertEqual(retrieved_secret.get_content(), {'password': 'hunter6'})
self.assertEqual(retrieved_secret.peek_content(), {'password': 'hunter7'})
self.assertEqual(retrieved_secret.get_content(refresh=True), {'password': 'hunter7'})
self.assertEqual(retrieved_secret.get_content(), {'password': 'hunter7'})

def test_get_secret_removed(self):
harness = ops.testing.Harness(ops.CharmBase, meta='name: webapp')
self.addCleanup(harness.cleanup)
harness.begin()
secret = harness.charm.app.add_secret({'password': 'hunter8'})
secret.set_content({"password": "hunter9"})
secret.remove_revision(secret.get_info().revision)
with self.assertRaises(ops.SecretNotFoundError):
harness.model.get_secret(id=secret.id)

def test_get_secret_by_label(self):
harness = ops.testing.Harness(ops.CharmBase, meta='name: webapp')
self.addCleanup(harness.cleanup)
harness.begin()
secret_id = harness.charm.app.add_secret({'password': 'hunter9'}, label="my-pass").id
secret = harness.model.get_secret(label="my-pass")
self.assertEqual(secret.label, "my-pass")
self.assertEqual(secret.get_content(), {'password': 'hunter9'})
secret = harness.model.get_secret(id=secret_id, label="other-name")
self.assertEqual(secret.get_content(), {'password': 'hunter9'})
secret = harness.model.get_secret(label="other-name")
self.assertEqual(secret.get_content(), {'password': 'hunter9'})

def test_add_model_secret_invalid_content(self):
harness = ops.testing.Harness(ops.CharmBase, meta='name: webapp')
self.addCleanup(harness.cleanup)
Expand Down
Loading