From a3dcf9b8690eadbf738943f338e70a71ac800fea Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Sat, 29 Aug 2026 20:58:42 +0200 Subject: [PATCH] fix(inventory): drop the credentials the inventory actually holds _drop_app_credentials walked applications[app].credentials, which f713ba8350 moved to secrets.credentials. Nothing lives at the old address any more - the run's inventory has twelve applications under secrets.credentials and none above it - so the isinstance check never matched and the function returned 0 every time. The rotation ran, reported success and rotated no application secret at all. It stayed hidden because the sibling half worked: _drop_user_passwords reads document["users"], which did not move, so the total was never zero and the call looked healthy. That matters more than a dead branch usually would. Rotating between two deploy passes is what turns the second pass into a test of the update path, where a role has to push a new secret into the application and the database it already created. With the drop silently skipped, every such run has been re-deploying the values the first pass wrote. The fixture pinned the pre-rename shape, so the test confirmed the bug rather than catching it. Migrated to the real layout, with the address in one helper instead of four assertions; against the old code it now fails 1 != 4. Co-Authored-By: Claude Opus 5 (1M context) --- .../inventory/provision/reset.py | 6 ++- .../inventory/provision/test_reset.py | 48 +++++++++++-------- 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/cli/administration/inventory/provision/reset.py b/cli/administration/inventory/provision/reset.py index f137b01bea..4f3374d775 100644 --- a/cli/administration/inventory/provision/reset.py +++ b/cli/administration/inventory/provision/reset.py @@ -15,6 +15,7 @@ from typing import TYPE_CHECKING from ruamel.yaml.comments import CommentedMap from cli.administration.inventory.credentials.vault import is_ruamel_vault +from utils.manager.credential_key import CREDENTIALS_KEY, SECRETS_KEY from .credentials_generator import generate_credentials_for_roles from .ruamel_io import dump_document, load_document @@ -66,7 +67,10 @@ def _drop_app_credentials(document: CommentedMap, exclude: set[str]) -> int: for application_id, application in applications.items(): if application_id in exclude or not isinstance(application, CommentedMap): continue - credentials = application.get("credentials") + secrets = application.get(SECRETS_KEY) + if not isinstance(secrets, CommentedMap): + continue + credentials = secrets.get(CREDENTIALS_KEY) if isinstance(credentials, CommentedMap): dropped += _drop_generated(credentials) return dropped diff --git a/tests/unit/python/cli/administration/inventory/provision/test_reset.py b/tests/unit/python/cli/administration/inventory/provision/test_reset.py index 65b49d0886..91d038ee8f 100644 --- a/tests/unit/python/cli/administration/inventory/provision/test_reset.py +++ b/tests/unit/python/cli/administration/inventory/provision/test_reset.py @@ -23,22 +23,24 @@ ansible_become_password: !vault | applications: web-app-a: - credentials: - api_key: !vault | - $ANSIBLE_VAULT;1.1;AES256 - 6170695f6b6579 - smtp_password: '' - recaptcha: - secret: !vault | + secrets: + credentials: + api_key: !vault | $ANSIBLE_VAULT;1.1;AES256 - 7365637265740a + 6170695f6b6579 + smtp_password: '' + recaptcha: + secret: !vault | + $ANSIBLE_VAULT;1.1;AES256 + 7365637265740a features: matomo: true web-app-keycloak: - credentials: - database_password: !vault | - $ANSIBLE_VAULT;1.1;AES256 - 6462706173730a + secrets: + credentials: + database_password: !vault | + $ANSIBLE_VAULT;1.1;AES256 + 6462706173730a users: administrator: @@ -100,23 +102,28 @@ class TestResetCredentials(unittest.TestCase): def _document(self): return load_document(self.host_vars_file) + def _credentials(self, application_id): + """Return one application's credentials from the rotated document. + + Args: + application_id: the application to read. + """ + application = self._document()["applications"][application_id] + return application["secrets"]["credentials"] + def test_every_generated_credential_is_dropped(self): self._reset() - credentials = self._document()["applications"]["web-app-a"]["credentials"] + credentials = self._credentials("web-app-a") self.assertNotIn("api_key", credentials) self.assertNotIn("secret", credentials["recaptcha"]) def test_an_operator_supplied_plain_value_survives(self): self._reset() - credentials = self._document()["applications"]["web-app-a"]["credentials"] - self.assertEqual(credentials["smtp_password"], "") + self.assertEqual(self._credentials("web-app-a")["smtp_password"], "") def test_an_excluded_application_is_untouched(self): self._reset(exclude={"administrator", "web-app-keycloak"}) - credentials = self._document()["applications"]["web-app-keycloak"][ - "credentials" - ] - self.assertIn("database_password", credentials) + self.assertIn("database_password", self._credentials("web-app-keycloak")) def test_an_excluded_user_keeps_its_password(self): self._reset() @@ -151,8 +158,7 @@ class TestResetCredentials(unittest.TestCase): def test_only_users_leaves_the_credentials_alone(self): _, credentials, users = self._reset(schema=False) - applications = self._document()["applications"] - self.assertIn("api_key", applications["web-app-a"]["credentials"]) + self.assertIn("api_key", self._credentials("web-app-a")) credentials.assert_not_called() users.assert_called_once()