From 7b2dfcfac7b4131f973f4494d162144a61811af3 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Sun, 25 Oct 2026 11:04:28 +0100 Subject: [PATCH] fix(tls): serve a redirect to another host through the redirect role resolve_term follows a redirect source to its target so an alias inherits the TLS settings of the application it points at. A redirect to a domain another host serves ends that chain on a domain this host does not know, and the tls and cert lookups failed: galaxyserver full-1 stopped in web-opt-rdr-domains on 'tls: domain project.infinito.nexus not found' for its project.veen.world -> project.infinito.nexus redirect. When the chain ends outside the host's domains and web-opt-rdr-domains is deployed, the source now resolves to web-opt-rdr-domains with the source itself as primary domain, which is the role that serves its vhost and whose certificate bundle names it. Only the outermost call falls back, so the source asked for stays the primary domain across a longer chain; without the redirect role, or for a target this host serves, nothing changes. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../python/utils/certs/test_tls_common.py | 40 +++++++++++++++++++ utils/tls_common.py | 35 ++++++++++------ 2 files changed, 63 insertions(+), 12 deletions(-) diff --git a/tests/unit/python/utils/certs/test_tls_common.py b/tests/unit/python/utils/certs/test_tls_common.py index 461c17cc4f..0301535deb 100644 --- a/tests/unit/python/utils/certs/test_tls_common.py +++ b/tests/unit/python/utils/certs/test_tls_common.py @@ -176,6 +176,46 @@ class TestTlsCommon(unittest.TestCase): ) self.assertEqual((app_id, primary), ("web-app-b", "b.example")) + def test_resolve_term_redirect_to_another_host_is_served_by_the_redirect_role(self): + domains = dict(self.domains, **{"web-opt-rdr-domains": "rdr.example"}) + app_id, primary = resolve_term( + "Project.Old.Example", + domains=domains, + applications={}, + redirects={"project.old.example": "project.other-host.example"}, + forced_mode="auto", + err_prefix="t", + ) + self.assertEqual( + (app_id, primary), ("web-opt-rdr-domains", "project.old.example") + ) + + def test_resolve_term_foreign_chain_keeps_the_asked_source_as_primary(self): + domains = dict(self.domains, **{"web-opt-rdr-domains": "rdr.example"}) + app_id, primary = resolve_term( + "one.example", + domains=domains, + applications={}, + redirects={ + "one.example": "two.example", + "two.example": "other-host.example", + }, + forced_mode="auto", + err_prefix="t", + ) + self.assertEqual((app_id, primary), ("web-opt-rdr-domains", "one.example")) + + def test_resolve_term_foreign_target_fails_without_the_redirect_role(self): + with self.assertRaises(AnsibleError): + resolve_term( + "project.old.example", + domains=self.domains, + applications={}, + redirects={"project.old.example": "project.other-host.example"}, + forced_mode="auto", + err_prefix="t", + ) + def test_resolve_term_redirect_cycle_fails(self): with self.assertRaises(AnsibleError): resolve_term( diff --git a/utils/tls_common.py b/utils/tls_common.py index dbf8b8ab3b..60fe91c672 100644 --- a/utils/tls_common.py +++ b/utils/tls_common.py @@ -13,6 +13,7 @@ if TYPE_CHECKING: from collections.abc import Iterable AVAILABLE_FLAVORS = {"letsencrypt", "self_signed"} +REDIRECT_SERVING_APP = "web-opt-rdr-domains" def as_str(value: Any) -> str: @@ -217,6 +218,7 @@ def resolve_term( redirects: dict[str, str] | None = None, forced_mode: str, err_prefix: str, + _following: bool = False, ) -> tuple[str, str]: """ Returns (app_id, primary_domain) where primary_domain is normalized lower-case. @@ -234,6 +236,9 @@ def resolve_term( 3) If term is a domain found in neither but is a source in redirects (see redirect_targets), such as an AUTO_BUILD_ALIASES alias -> resolve its target, following a chain of redirects; a source is used once, so a cycle fails. + 4) If that chain ends on a domain this host does not serve, such as a redirect + to another host, and REDIRECT_SERVING_APP is in domains -> map the source to + REDIRECT_SERVING_APP, with the source itself as primary_domain. """ t = as_str(term) if not t: @@ -267,18 +272,24 @@ def resolve_term( app_id = resolve_app_id_for_domain(apps, bare) target = (redirects or {}).get(norm_domain(t)) if not app_id and target and target != norm_domain(t): - return resolve_term( - target, - domains=domains, - applications=applications, - redirects={ - source: dest - for source, dest in (redirects or {}).items() - if source != norm_domain(t) - }, - forced_mode="domain", - err_prefix=err_prefix, - ) + try: + return resolve_term( + target, + domains=domains, + applications=applications, + redirects={ + source: dest + for source, dest in (redirects or {}).items() + if source != norm_domain(t) + }, + forced_mode="domain", + err_prefix=err_prefix, + _following=True, + ) + except AnsibleError: + if _following or REDIRECT_SERVING_APP not in domains: + raise + return REDIRECT_SERVING_APP, norm_domain(t) if not app_id: raise AnsibleError( f"{err_prefix}: domain '{t}' not found (domains/applications)"