From b4af1d4b89061929d29030d767a73c74fc62395a Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Thu, 3 Sep 2026 09:18:07 +0200 Subject: [PATCH] fix(keycloak): register redirect URIs only for the apps a play deploys lookup('redirect_uris') walked every app in the domains map whose services.sso.enabled resolves truthy, which every role spells as "'web-app-keycloak' in group_names". A round deploying 21 apps therefore registered 69 redirect URIs and web origins on the shared client, 65 of them onion, and an app that is not running has no reason to be a redirect target. group_names is now required and scopes the result, matching KEYCLOAK_SAML_APPS which already selects on it. Co-Authored-By: Claude Opus 5 (1M context) --- plugins/lookup/redirect_uris.py | 18 +++++++-- .../plugins/lookup/test_redirect_uris.py | 38 +++++++++++++++++++ 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/plugins/lookup/redirect_uris.py b/plugins/lookup/redirect_uris.py index 17553b5ed2..ea32ad684f 100644 --- a/plugins/lookup/redirect_uris.py +++ b/plugins/lookup/redirect_uris.py @@ -57,8 +57,9 @@ def build_redirect_uris( wildcard: str = DEFAULT_WILDCARD, features: Iterable[str] = DEFAULT_FEATURES, dedup: bool = True, + deployed: Iterable[str] | None = None, ) -> list[str]: - """Registered redirect URIs for every SSO consumer, one per domain. + """Registered redirect URIs for every deployed SSO consumer, one per domain. Args: domains: app_id -> domain, list of domains, or nested mapping of those. @@ -69,14 +70,19 @@ def build_redirect_uris( wildcard: suffix appended to every URI. features: config paths ORed together to decide whether an app gets a URI. dedup: drop repeated URIs, preserving first-seen order. + deployed: application ids this play deploys (``group_names``); an app + outside it gets no URI. None registers every app in ``domains``. """ if not isinstance(domains, dict): raise AnsibleError( "redirect_uris: 'domains' must be a dict mapping app_id -> domain or list of domains" ) + deployed_ids = None if deployed is None else set(deployed) uris: list[str] = [] for app_id, domain_value in domains.items(): + if deployed_ids is not None and app_id not in deployed_ids: + continue try: has_feature = any( bool(get(applications, app_id, f, False)) for f in features @@ -113,9 +119,10 @@ class LookupModule(LookupBase): {{ lookup('redirect_uris', wildcard='/cb') }} Returns the redirect URIs Keycloak must whitelist: one per domain of every - application whose config enables SSO. Each URI's scheme is resolved for the - CONSUMER -- its own ``server.tls.enabled`` over the global ``TLS_ENABLED``, - and always plaintext for an .onion -- never from Keycloak's own TLS state. + application this play deploys (``group_names``) whose config enables SSO. + Each URI's scheme is resolved for the CONSUMER -- its own + ``server.tls.enabled`` over the global ``TLS_ENABLED``, and always + plaintext for an .onion -- never from Keycloak's own TLS state. - parameters: wildcard: suffix appended to every URI (default '/*') @@ -135,6 +142,8 @@ class LookupModule(LookupBase): variables = variables or getattr(self._templar, "available_variables", {}) or {} if "TLS_ENABLED" not in variables: raise AnsibleError("redirect_uris: TLS_ENABLED is not defined.") + if "group_names" not in variables: + raise AnsibleError("redirect_uris: group_names is not defined.") loader = getattr(self, "_loader", None) templar = getattr(self, "_templar", None) @@ -154,5 +163,6 @@ class LookupModule(LookupBase): wildcard=kwargs.get("wildcard", DEFAULT_WILDCARD), features=features, dedup=bool(kwargs.get("dedup", True)), + deployed=variables["group_names"], ) ] diff --git a/tests/unit/python/plugins/lookup/test_redirect_uris.py b/tests/unit/python/plugins/lookup/test_redirect_uris.py index b280ecc7d1..9a4772ea9a 100644 --- a/tests/unit/python/plugins/lookup/test_redirect_uris.py +++ b/tests/unit/python/plugins/lookup/test_redirect_uris.py @@ -1,6 +1,7 @@ from __future__ import annotations import unittest +from unittest import mock from ansible.errors import AnsibleError @@ -83,6 +84,18 @@ class TestBuildRedirectUris(unittest.TestCase): ], ) + def test_only_deployed_consumers_are_registered(self): + domains = {"deployed": "a.example.org", "parked": "b.example.org"} + result = build_redirect_uris( + domains, {"deployed": SSO, "parked": SSO}, True, deployed=["deployed"] + ) + self.assertEqual(result, ["https://a.example.org/*"]) + + def test_no_deployment_scope_registers_every_consumer(self): + domains = {"one": "a.example.org", "two": "b.example.org"} + result = build_redirect_uris(domains, {"one": SSO, "two": SSO}, True) + self.assertEqual(result, ["https://a.example.org/*", "https://b.example.org/*"]) + def test_invalid_domains_type_raises(self): with self.assertRaises(AnsibleError): build_redirect_uris(["not-a-dict"], {}, True) # type: ignore[arg-type] @@ -105,6 +118,31 @@ class TestRedirectUrisLookup(unittest.TestCase): with self.assertRaises(AnsibleError): self.lookup.run([], variables={}) + def test_missing_group_names_raises(self): + with self.assertRaises(AnsibleError): + self.lookup.run([], variables={"TLS_ENABLED": True}) + + def test_group_names_scope_the_registered_consumers(self): + class _Stub: + def __init__(self, payload): + self.payload = payload + + def run(self, terms, variables=None, **kwargs): + return [self.payload] + + payloads = { + "domains": _Stub({"deployed": "a.example.org", "parked": "b.example.org"}), + "applications": _Stub({"deployed": SSO, "parked": SSO}), + } + with mock.patch( + "plugins.lookup.redirect_uris.lookup_loader.get", + side_effect=lambda name, **kwargs: payloads[name], + ): + result = self.lookup.run( + [], variables={"TLS_ENABLED": True, "group_names": ["deployed"]} + ) + self.assertEqual(result, [["https://a.example.org/*"]]) + if __name__ == "__main__": unittest.main()