From 6bb1dafcc5e79e6387666593ae6c7dbfc853ef64 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Sun, 25 Oct 2026 08:11:33 +0100 Subject: [PATCH] refactor(web-opt-rdr-domains): read host-wide redirects from the role config Host-wide redirects such as a legacy zone came from the top-level inventory variable redirect_domain_mappings, which only the role default declared. Once the role input moved to web_opt_rdr_domains_mappings nothing declared it any more and the inventory validation refused the deploy, because it does not see the lookup's vars_ read. The redirects now live where the role's other settings are: services.rdr-domains.redirects in meta/services.yml, empty by default and set per inventory under applications.web-opt-rdr-domains, and current_play_redirect_domains reads them through the config lookup. They still win over the application aliases, and a caller that includes the role without its own web_opt_rdr_domains_mappings still renders the merged CURRENT_PLAY_REDIRECT_DOMAINS. Inventories move their redirect_domain_mappings list unchanged to applications.web-opt-rdr-domains.services.rdr-domains.redirects. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../lookup/current_play_redirect_domains.py | 13 ++- roles/web-opt-rdr-domains/README.md | 22 ++-- roles/web-opt-rdr-domains/meta/services.yml | 1 + .../test_current_play_redirect_domains.py | 100 ++++++++++-------- 4 files changed, 79 insertions(+), 57 deletions(-) diff --git a/plugins/lookup/current_play_redirect_domains.py b/plugins/lookup/current_play_redirect_domains.py index caabdc6fe1..a6593ee42c 100644 --- a/plugins/lookup/current_play_redirect_domains.py +++ b/plugins/lookup/current_play_redirect_domains.py @@ -121,7 +121,8 @@ class LookupModule(LookupBase): ``keep_path: false`` to drop the request URI from its 301. Merges, in order: - 1. user-supplied ``redirect_domain_mappings`` (if any) + 1. ``services.rdr-domains.redirects`` of ``web-opt-rdr-domains``, set per + inventory under ``applications`` 2. ``{DOMAIN_PRIMARY -> DOMAIN_HOMEPAGE}`` (skipped when equal); non-empty result triggers ``sys-stk-front-base`` to auto-load ``web-opt-rdr-domains`` so openresty has an SNI match for @@ -140,7 +141,9 @@ class LookupModule(LookupBase): Steps 1-2 win over conflicting step-3 entries. """ - def _run_lookup(self, name: str, variables: dict[str, Any] | None) -> Any: + def _run_lookup( + self, name: str, variables: dict[str, Any] | None, *terms: str + ) -> Any: plugin = lookup_loader.get( name, loader=getattr(self, "_loader", None), @@ -148,7 +151,7 @@ class LookupModule(LookupBase): ) if plugin is None: return None - result = plugin.run([], variables=variables) + result = plugin.run(list(terms), variables=variables) return result[0] if result else None def _node_onion(self, vars_: Mapping[str, Any]) -> str: @@ -190,7 +193,9 @@ class LookupModule(LookupBase): domain_homepage = self._resolve_str(vars_.get("DOMAIN_HOMEPAGE")) auto_build_aliases = bool(vars_.get("AUTO_BUILD_ALIASES", False)) - user_mappings = vars_.get("redirect_domain_mappings") or [] + user_mappings = self._run_lookup( + "config", vars_, "web-opt-rdr-domains", "services.rdr-domains.redirects" + ) if not isinstance(user_mappings, list): user_mappings = [] diff --git a/roles/web-opt-rdr-domains/README.md b/roles/web-opt-rdr-domains/README.md index d311573998..6019ec48c6 100644 --- a/roles/web-opt-rdr-domains/README.md +++ b/roles/web-opt-rdr-domains/README.md @@ -8,18 +8,24 @@ This Ansible role configures NGINX to perform 301 redirects from one domain to a This role configures Nginx to perform 301 redirects based on a list of source→target domain mappings. -Extra mappings go into the inventory as `redirect_domain_mappings`. A source may be a whole zone through `*.`, and `keep_path: false` sends every request to the bare target instead of appending its path: +Extra mappings go into the inventory as `services.rdr-domains.redirects` of this role. A source may be a whole zone through `*.`, and `keep_path: false` sends every request to the bare target instead of appending its path: ```yaml -redirect_domain_mappings: - - source: legacy.example - target: example.org - keep_path: false - - source: "*.legacy.example" - target: example.org - keep_path: false +applications: + web-opt-rdr-domains: + services: + rdr-domains: + redirects: + - source: legacy.example + target: example.org + keep_path: false + - source: "*.legacy.example" + target: example.org + keep_path: false ``` +A caller that includes the role with its own `web_opt_rdr_domains_mappings` renders only that list; without it the role renders `CURRENT_PLAY_REDIRECT_DOMAINS`, which holds these redirects, the primary domain's redirect to the homepage and every application alias. + ## Cosmos The diagram places Redirector in the Infinito.Nexus cosmos: the components it deploys (capabilities), the central services it consumes (dependencies), and its outward reach (federation and bridged external networks). diff --git a/roles/web-opt-rdr-domains/meta/services.yml b/roles/web-opt-rdr-domains/meta/services.yml index 2a32f5c2d5..a22a1db449 100644 --- a/roles/web-opt-rdr-domains/meta/services.yml +++ b/roles/web-opt-rdr-domains/meta/services.yml @@ -4,6 +4,7 @@ rdr-domains: modes: host: enabled: true + redirects: [] lifecycle: beta min_storage: "50MB" cpus: "0.1" diff --git a/tests/unit/python/plugins/lookup/test_current_play_redirect_domains.py b/tests/unit/python/plugins/lookup/test_current_play_redirect_domains.py index 2cd4420990..b597479a63 100644 --- a/tests/unit/python/plugins/lookup/test_current_play_redirect_domains.py +++ b/tests/unit/python/plugins/lookup/test_current_play_redirect_domains.py @@ -19,13 +19,20 @@ class CurrentPlayRedirectDomainsLookupTests(unittest.TestCase): lm._loader = None return lm - def _patch_lookups(self, *, deployed=None, current_play_apps=None): + def _patch_lookups(self, *, deployed=None, current_play_apps=None, redirects=None): + def _config(terms, variables=None): + if terms == ["web-opt-rdr-domains", "services.rdr-domains.redirects"]: + return [[] if redirects is None else redirects] + return [None] + def _get(name, *args, **kwargs): plugin = MagicMock() if name == "deployment": plugin.run.return_value = [{"deployed": list(deployed or [])}] elif name == "applications_current_play": plugin.run.return_value = [current_play_apps or {}] + elif name == "config": + plugin.run.side_effect = _config else: plugin.run.return_value = [None] return plugin @@ -41,39 +48,33 @@ class CurrentPlayRedirectDomainsLookupTests(unittest.TestCase): result = lm.run(terms=[], variables={})[0] self.assertEqual(result, []) - def test_user_redirect_domain_mappings_preserved(self): + def test_configured_redirects_preserved(self): lm = self._make_lookup() - variables = { - "redirect_domain_mappings": [ - {"source": "old.example.com", "target": "new.example.com"}, - ], - } - with self._patch_lookups(deployed=[], current_play_apps={}): - result = lm.run(terms=[], variables=variables)[0] + redirects = [{"source": "old.example.com", "target": "new.example.com"}] + with self._patch_lookups( + deployed=[], current_play_apps={}, redirects=redirects + ): + result = lm.run(terms=[], variables={})[0] self.assertEqual( result, [{"source": "old.example.com", "target": "new.example.com"}], ) - def test_user_wildcard_mapping_keeps_its_keep_path_flag(self): + def test_configured_wildcard_keeps_its_keep_path_flag(self): lm = self._make_lookup() - variables = { - "redirect_domain_mappings": [ - { - "source": "*.old.example", - "target": "new.example", - "keep_path": False, - }, - ], - } - with self._patch_lookups(deployed=[], current_play_apps={}): - result = lm.run(terms=[], variables=variables)[0] + redirects = [ + {"source": "*.old.example", "target": "new.example", "keep_path": False}, + ] + with self._patch_lookups( + deployed=[], current_play_apps={}, redirects=redirects + ): + result = lm.run(terms=[], variables={})[0] self.assertEqual( result, [{"source": "*.old.example", "target": "new.example", "keep_path": False}], ) - def test_user_mapping_values_are_templated(self): + def test_configured_values_are_templated(self): class _ResolvingTemplar: def __init__(self, mapping): self._mapping = mapping @@ -90,13 +91,13 @@ class CurrentPlayRedirectDomainsLookupTests(unittest.TestCase): } ) lm._loader = None - variables = { - "redirect_domain_mappings": [ - {"source": "*.{{ OLD_DOMAIN }}", "target": "{{ DOMAIN_PRIMARY }}"}, - ], - } - with self._patch_lookups(deployed=[], current_play_apps={}): - result = lm.run(terms=[], variables=variables)[0] + redirects = [ + {"source": "*.{{ OLD_DOMAIN }}", "target": "{{ DOMAIN_PRIMARY }}"}, + ] + with self._patch_lookups( + deployed=[], current_play_apps={}, redirects=redirects + ): + result = lm.run(terms=[], variables={})[0] self.assertEqual(result, [{"source": "*.old.example", "target": "new.example"}]) def test_primary_redirect_appended_when_both_domains_set(self): @@ -180,7 +181,7 @@ class CurrentPlayRedirectDomainsLookupTests(unittest.TestCase): result, ) - def test_user_mapping_overrides_per_app_mapping_on_same_source(self): + def test_configured_redirect_overrides_per_app_mapping_on_same_source(self): lm = self._make_lookup() apps = { "web-app-foo": { @@ -190,14 +191,11 @@ class CurrentPlayRedirectDomainsLookupTests(unittest.TestCase): } } } - variables = { - "DOMAIN_PRIMARY": "example.com", - "AUTO_BUILD_ALIASES": False, - "redirect_domain_mappings": [ - {"source": "alt.example.com", "target": "OVERRIDE.example.com"}, - ], - } - with self._patch_lookups(deployed=[], current_play_apps=apps): + variables = {"DOMAIN_PRIMARY": "example.com", "AUTO_BUILD_ALIASES": False} + redirects = [{"source": "alt.example.com", "target": "OVERRIDE.example.com"}] + with self._patch_lookups( + deployed=[], current_play_apps=apps, redirects=redirects + ): result = lm.run(terms=[], variables=variables)[0] for entry in result: if entry["source"] == "alt.example.com": @@ -206,7 +204,7 @@ class CurrentPlayRedirectDomainsLookupTests(unittest.TestCase): else: self.fail("expected entry with source alt.example.com") - def test_combined_user_primary_and_per_app(self): + def test_combined_configured_primary_and_per_app(self): lm = self._make_lookup() apps = { "web-app-foo": { @@ -220,12 +218,12 @@ class CurrentPlayRedirectDomainsLookupTests(unittest.TestCase): "DOMAIN_PRIMARY": "infinito.test", "DOMAIN_HOMEPAGE": "infinito.nexus", "AUTO_BUILD_ALIASES": False, - "redirect_domain_mappings": [ - {"source": "legacy.example.com", "target": "current.example.com"}, - ], } + redirects = [{"source": "legacy.example.com", "target": "current.example.com"}] with self._patch_lookups( - deployed=["web-opt-rdr-domains"], current_play_apps=apps + deployed=["web-opt-rdr-domains"], + current_play_apps=apps, + redirects=redirects, ): result = lm.run(terms=[], variables=variables)[0] sources = {entry["source"]: entry["target"] for entry in result} @@ -233,9 +231,21 @@ class CurrentPlayRedirectDomainsLookupTests(unittest.TestCase): self.assertEqual(sources.get("infinito.test"), "infinito.nexus") self.assertEqual(sources.get("alt-foo.example.com"), "foo.example.com") - def test_non_list_redirect_domain_mappings_treated_as_empty(self): + def test_non_list_configured_redirects_treated_as_empty(self): lm = self._make_lookup() - variables = {"redirect_domain_mappings": "not-a-list"} + with self._patch_lookups( + deployed=[], current_play_apps={}, redirects="not-a-list" + ): + result = lm.run(terms=[], variables={})[0] + self.assertEqual(result, []) + + def test_the_inventory_variable_is_no_longer_read(self): + lm = self._make_lookup() + variables = { + "redirect_domain_mappings": [ + {"source": "old.example.com", "target": "new.example.com"}, + ], + } with self._patch_lookups(deployed=[], current_play_apps={}): result = lm.run(terms=[], variables=variables)[0] self.assertEqual(result, [])