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"