Files
computer-playbook/plugins/lookup/redirect_uris.py
T
kevinveenbirkenbachandClaude Opus 5 4b10d6b937 fix(keycloak): resolve each consumer's redirect scheme, not Keycloak's
KEYCLOAK_REDIRECT_URIS derived the scheme from Keycloak's OWN TLS state and
stamped that single scalar onto every SSO consumer. With svc-net-tor
deployed Keycloak is onion-primary, so resolve_enabled returns False and
lookup('tls', ...) yields "http" -- which then went onto clearnet apps too.
Keycloak registered http://matrix.infinito.example/* while Synapse sends
https://.../_synapse/client/oidc/callback, and its redirect-URI match is
scheme-exact: 90 invalid_redirect_uri events, an error page instead of a
login form, and a 60s selector timeout that looked like a Playwright bug.
jitsi hit the same wall on https://meet.infinito.example/oauth2/callback.

Resolve per consumer instead, via resolve_enabled: an .onion is always
plaintext, a clearnet app follows its own server.tls.enabled, and only
then the global TLS_ENABLED. That override existed and was silently
discarded -- passing a pre-computed scheme string threw it away, which is
the same "what I am" instead of "what the consumer is" mistake one layer
up. resolve_enabled handles the onion case itself, so the filter's own
is_onion_domain branch is gone rather than duplicated.

Move it from a filter to a lookup while at it: the thing needs domains,
applications and TLS_ENABLED anyway, so resolving them inside collapses
the call site to lookup('redirect_uris') and drops
KEYCLOAK_REDIRECT_FEATURES, which listed the same path twice. A lookup
under plugins/lookup/ is also plainly importable, so the test loses the
sys.modules stubbing apparatus a role-local filter needed -- 60 lines net
smaller.

KEYCLOAK_WEB_ORIGINS is regex-derived from these URIs and inherits the
fix. Verified against the real applications view: 69 URIs, all https with
TLS_ENABLED true and all http with it false -- the same 69 the failing run
registered as plaintext.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-10 10:02:59 +02:00

159 lines
5.2 KiB
Python

from __future__ import annotations
from typing import TYPE_CHECKING, Any
from ansible.errors import AnsibleError
from ansible.plugins.loader import lookup_loader
from ansible.plugins.lookup import LookupBase
from utils.get_url import get_url
from utils.roles.applications.config import (
AppConfigKeyError,
ConfigEntryNotSetError,
get,
)
from utils.tls_common import resolve_enabled
if TYPE_CHECKING:
from collections.abc import Iterable, Sequence
DEFAULT_FEATURES = ("services.sso.enabled",)
DEFAULT_WILDCARD = "/*"
def _stable_dedup(items: Sequence[str]) -> list[str]:
seen = set()
out: list[str] = []
for x in items:
if x not in seen:
seen.add(x)
out.append(x)
return out
def _iter_domains(value: Any) -> Iterable[str]:
"""Yield domains from str | list/tuple[str] | dict[*, str|list|tuple]."""
if value is None:
return
if isinstance(value, str):
yield value
elif isinstance(value, dict):
for val in value.values():
yield from _iter_domains(val)
elif isinstance(value, (list, tuple)):
for val in value:
yield from _iter_domains(val)
else:
raise AnsibleError(
"redirect_uris: domain value must be str, list/tuple[str], or dict mapping to those"
)
def build_redirect_uris(
domains: dict,
applications: dict,
tls_enabled: bool,
*,
wildcard: str = DEFAULT_WILDCARD,
features: Iterable[str] = DEFAULT_FEATURES,
dedup: bool = True,
) -> list[str]:
"""Registered redirect URIs for every SSO consumer, one per domain.
Args:
domains: app_id -> domain, list of domains, or nested mapping of those.
applications: merged applications view, used for feature gating and for
each consumer's ``server.tls.enabled`` override.
tls_enabled: global TLS default, overridden per consumer by
``resolve_enabled``; an .onion domain always resolves to plaintext.
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.
"""
if not isinstance(domains, dict):
raise AnsibleError(
"redirect_uris: 'domains' must be a dict mapping app_id -> domain or list of domains"
)
uris: list[str] = []
for app_id, domain_value in domains.items():
try:
has_feature = any(
bool(get(applications, app_id, f, False)) for f in features
)
except (AppConfigKeyError, ConfigEntryNotSetError):
has_feature = False
if not has_feature:
continue
for domain in _iter_domains(domain_value):
proto = (
"https"
if resolve_enabled(
applications.get(app_id) or {}, tls_enabled, primary_domain=domain
)
else "http"
)
try:
url = get_url({app_id: domain}, app_id, proto)
except Exception as exc:
raise AnsibleError(
f"redirect_uris: get_url failed for app '{app_id}' "
f"with domain '{domain}': {exc}"
) from exc
uris.append(f"{url}{wildcard}")
return _stable_dedup(uris) if dedup else uris
class LookupModule(LookupBase):
"""
Usage:
{{ lookup('redirect_uris') }}
{{ 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.
- parameters:
wildcard: suffix appended to every URI (default '/*')
features: config paths ORed to gate an application (default services.sso.enabled)
dedup: drop repeated URIs (default true)
"""
def run(
self,
terms: list[Any] | None,
variables: dict[str, Any] | None = None,
**kwargs: Any,
) -> list[list[str]]:
if terms:
raise AnsibleError("lookup('redirect_uris') expects no terms.")
variables = variables or getattr(self._templar, "available_variables", {}) or {}
if "TLS_ENABLED" not in variables:
raise AnsibleError("redirect_uris: TLS_ENABLED is not defined.")
loader = getattr(self, "_loader", None)
templar = getattr(self, "_templar", None)
domains = lookup_loader.get("domains", loader=loader, templar=templar).run(
[], variables=variables
)[0]
applications = lookup_loader.get(
"applications", loader=loader, templar=templar
).run([], variables=variables)[0]
features = kwargs.get("features") or DEFAULT_FEATURES
return [
build_redirect_uris(
domains,
applications,
bool(variables["TLS_ENABLED"]),
wildcard=kwargs.get("wildcard", DEFAULT_WILDCARD),
features=features,
dedup=bool(kwargs.get("dedup", True)),
)
]