From 8e3fe04d7287cd92fc0cec31f818d52641588ec3 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Sat, 29 Aug 2026 20:57:07 +0200 Subject: [PATCH] fix(mcp): read a provider's bearer where provisioning writes it Discovery resolved applications[role].credentials, the address from before f713ba8350 moved every credential under secrets.credentials. The run's own inventory shows which one is real: twelve applications carry secrets.credentials, none carries the top-level key. So every provider declaring credential.source: credentials - fifteen of the twenty - handed back an empty mapping, and reconciliation aborted on a secret that had been provisioned all along: mcp_servers: 1 authorized MCP server(s) cannot be rendered. web-app-prometheus: credential_missing: web-app-prometheus declares owner 'mcp-web-app-prometheus' but no secret resolved The five providers on token_store read through users and were never affected, which is why only some clients broke. Provider and client were reading the same secret at two addresses: web-app-prometheus/vars/main.yml:32 renders its bearer from secrets.credentials.mcp_bearer. Without the strict assert this would have surfaced as a 401 at runtime rather than a failed deploy. The lookup was correct when 6881ac5132 wrote it - the rename landed on a branch it was not an ancestor of, and arrived here through the main merges of 08-15. Its own message notes that no regex over lookup('config') could see an access path like this one; a .get("credentials") split across two lines in a Python plugin is equally invisible to it. The address now comes from credential_key's constants, which is where the module already claims a credential is addressed by exactly one string. resolve_credential was always handed a fabricated mapping directly, so the function was covered and its caller's address was not. The new tests close that seam, one of them end to end over the shape provisioning produces. Co-Authored-By: Claude Opus 5 (1M context) --- plugins/lookup/mcp_servers.py | 28 ++++++++++-- .../python/plugins/lookup/test_mcp_servers.py | 44 +++++++++++++++++++ 2 files changed, 68 insertions(+), 4 deletions(-) diff --git a/plugins/lookup/mcp_servers.py b/plugins/lookup/mcp_servers.py index 99ede74114..091a7610e9 100644 --- a/plugins/lookup/mcp_servers.py +++ b/plugins/lookup/mcp_servers.py @@ -45,6 +45,7 @@ from ansible.errors import AnsibleError from ansible.plugins.loader import lookup_loader from ansible.plugins.lookup import LookupBase +from utils.manager.credential_key import CREDENTIALS_KEY, SECRETS_KEY from utils.roles.applications.mcp import DEFAULT_MCP_TRANSPORT if TYPE_CHECKING: @@ -80,6 +81,26 @@ def endpoint_url(endpoint: Mapping[str, Any]) -> str: return f"http://{endpoint.get('service_key')}:{endpoint.get('port')}{endpoint.get('path')}" +def role_credentials_of( + applications: Mapping[str, Any], role: str +) -> Mapping[str, Any]: + """Return a role's own secrets from the merged applications payload. + + Args: + applications: the merged applications mapping. + role: the provider's application id. + + They live at ``secrets.credentials``, which is where the roles' own vars + and every other consumer address them. Reading the top-level + ``credentials`` instead returns nothing for every provider whose + ``credential.source`` is ``credentials``, and the deploy then aborts on a + secret that was provisioned all along. + """ + return ((applications.get(role) or {}).get(SECRETS_KEY) or {}).get( + CREDENTIALS_KEY + ) or {} + + def resolve_credential( server: Mapping[str, Any], users: Mapping[str, Any], @@ -90,7 +111,8 @@ def resolve_credential( Args: server: a discovered ``direction=server`` entry. users: the merged users mapping, carrying each principal's tokens. - role_credentials: the provider role's own ``credentials`` mapping. + role_credentials: the provider role's own ``secrets.credentials`` + mapping, which is where every other consumer addresses them. Returns an empty token when the declaration is incomplete or the principal holds nothing; the caller turns that into ``credential_missing``. @@ -333,9 +355,7 @@ class LookupModule(LookupBase): credentials: dict[str, tuple[str, str]] = {} for server in servers: server_id = str(server.get("id") or "") - role_credentials = (applications.get(server_id) or {}).get( - "credentials" - ) or {} + role_credentials = role_credentials_of(applications, server_id) credentials[server_id] = resolve_credential(server, users, role_credentials) discovery = build_mcp_discovery(servers, consumer_id, consumer, credentials) diff --git a/tests/unit/python/plugins/lookup/test_mcp_servers.py b/tests/unit/python/plugins/lookup/test_mcp_servers.py index 9f75a321bc..93377772fb 100644 --- a/tests/unit/python/plugins/lookup/test_mcp_servers.py +++ b/tests/unit/python/plugins/lookup/test_mcp_servers.py @@ -15,6 +15,7 @@ plugin_module = importlib.import_module("plugins.lookup.mcp_servers") build_mcp_discovery = plugin_module.build_mcp_discovery resolve_credential = plugin_module.resolve_credential resolve_consumer_id = plugin_module.resolve_consumer_id +role_credentials_of = plugin_module.role_credentials_of CONSUMER = "web-app-openwebui" @@ -275,6 +276,49 @@ class TestResolveCredential(unittest.TestCase): resolve_credential(server, {}, {"mcp_token": "v"}), ("v", "mcp-web-app-x") ) + def test_the_role_secret_is_read_where_the_inventory_writes_it(self): + """The function was tested; the address its caller used was not. + + Provisioning writes `.secrets.credentials.`, which is also + how the roles' own vars address it. Discovery read the top-level + `credentials` and therefore found nothing for all fifteen providers + whose source is `credentials`, aborting the deploy on a secret that + had been generated and vaulted all along. + """ + applications = { + "web-app-prometheus": { + "secrets": {"credentials": {"mcp_bearer": "resolved"}} + } + } + self.assertEqual( + {"mcp_bearer": "resolved"}, + role_credentials_of(applications, "web-app-prometheus"), + ) + + def test_a_top_level_credentials_block_is_not_the_address(self): + applications = {"web-app-x": {"credentials": {"mcp_bearer": "wrong-place"}}} + self.assertEqual({}, role_credentials_of(applications, "web-app-x")) + + def test_an_absent_role_yields_an_empty_mapping(self): + self.assertEqual({}, role_credentials_of({}, "web-app-missing")) + + def test_the_extraction_feeds_resolve_credential(self): + """End to end: the payload shape provisioning produces must resolve.""" + applications = { + "web-app-prometheus": { + "secrets": {"credentials": {"mcp_bearer": "resolved"}} + } + } + server = self._server( + owner="mcp-web-app-prometheus", source="credentials", key="mcp_bearer" + ) + self.assertEqual( + ("resolved", "mcp-web-app-prometheus"), + resolve_credential( + server, {}, role_credentials_of(applications, "web-app-prometheus") + ), + ) + def test_administrator_owner_resolves_to_nothing(self): server = self._server( owner="administrator", source="token_store", key="web-app-baserow"