From 0effa1670ace70b6e2d64d566debccc35b4e52bc Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Wed, 27 May 2026 00:12:14 +0200 Subject: [PATCH] fix(purge): remove www..conf redirect vhosts with entity purge `web-opt-rdr-www` generates a redirect vhost `/conf.d/servers/{http,https}/www..conf` for every deployed app's canonical and alias domains. Those redirect vhosts are not part of any app's `server.domains.{canonical,aliases}` list, so the entity-keyed purge primitive in `utils/cleanup/nginx_vhosts.py` did not yield them and `iter_vhost_files_for_entity` left them on disk. Between matrix-deploy rounds this leaked the redirect vhosts of the previous round's broader app set into the next round's narrower one. On run 26451828677 job 77879571696 (web-opt-rdr-www on debian) the round 1 -> round 2 transition round 1/2 inv=github-0 apps=[15 apps incl. web-svc-cdn, web-svc-simpleicons, web-svc-css, ...] round 2/2 inv=github-1 apps=[web-opt-rdr-www] dropped the bare `cdn.infinito.example.conf` etc. via the existing purge but kept the matching `www.cdn.infinito.example.conf`, `www.icon.infinito.example.conf`, ... behind. Round 2 only redeploys web-opt-rdr-www, whose `www_redirect_domains` lookup now only emits its own `www.w3redirect.` redirect, so nothing reissues the stale www-prefixed vhosts. `sys-ctl-hlth-csp` discovers domains by scanning .conf filenames in the nginx servers dir, so it still probed the stale `www..conf` hosts. nginx served them with a cert whose SAN no longer covered the hostname (the per-domain cert path baked into the conf had been removed alongside the bare vhost), the TLS handshake returned `net::ERR_SSL_UNRECOGNIZED_NAME_ALERT`, the CSP probe exited 11, and `sys-service-terminator : Apply state` failed after 10 retries. Extend `iter_vhost_files_for_entity` to also yield the `www..conf` variant for every domain it already yields, skipping domains that already begin with `www.` so a role that declares its canonical with a leading `www.` (web-opt-rdr-www's own `www.w3redirect.`) does not produce `www.www..conf`. The existing two- and three-step purge primitives (db / compose / dir / nginx) and the orchestrator in `scripts/container/purge/apps.sh` keep their entity-keyed contract; only the per-entity yield set widens. Coverage: - test_yields_www_redirect_variant: per-app www-prefixed file is yielded alongside the bare vhost. - test_www_prefixed_domain_not_double_prefixed: a www-prefixed canonical does not synthesize `www.www.` even when such a file exists on disk (defense in depth against a future regression that prepends unconditionally). - test_removes_www_redirect_variant: end-to-end purge removes both bare and www variant, leaves an unrelated www-prefixed file alone. Co-Authored-By: Claude Opus 4.7 --- tests/unit/utils/cleanup/test_nginx_vhosts.py | 62 +++++++++++++++++++ utils/cleanup/nginx_vhosts.py | 15 +++-- 2 files changed, 72 insertions(+), 5 deletions(-) diff --git a/tests/unit/utils/cleanup/test_nginx_vhosts.py b/tests/unit/utils/cleanup/test_nginx_vhosts.py index e8811aef74..ed685a2a9d 100644 --- a/tests/unit/utils/cleanup/test_nginx_vhosts.py +++ b/tests/unit/utils/cleanup/test_nginx_vhosts.py @@ -137,6 +137,44 @@ class TestIterVhostFiles(NginxVhostsTestBase, unittest.TestCase): ) self.assertEqual(got, []) + def test_yields_www_redirect_variant(self) -> None: + self._mk_role( + "web-svc-cdn", + application_id="web-svc-cdn", + canonical=["cdn.infinito.example"], + ) + bare = self._touch_vhost("cdn.infinito.example", "https") + redirect = self._touch_vhost("www.cdn.infinito.example", "https") + + got = sorted( + iter_vhost_files_for_entity( + "cdn", + nginx_dir=self.nginx_dir, + domain_primary="infinito.example", + roles_dir=self.roles_dir, + ) + ) + self.assertEqual(got, sorted([bare, redirect])) + + def test_www_prefixed_domain_not_double_prefixed(self) -> None: + self._mk_role( + "web-opt-rdr-www", + application_id="web-opt-rdr-www", + canonical=["www.w3redirect.infinito.example"], + ) + existing = self._touch_vhost("www.w3redirect.infinito.example", "https") + self._touch_vhost("www.www.w3redirect.infinito.example", "https") + + got = list( + iter_vhost_files_for_entity( + "opt-rdr-www", + nginx_dir=self.nginx_dir, + domain_primary="infinito.example", + roles_dir=self.roles_dir, + ) + ) + self.assertEqual(got, [existing]) + class TestPurgeVhostFiles(NginxVhostsTestBase, unittest.TestCase): def test_removes_only_matching_vhosts(self) -> None: @@ -183,6 +221,30 @@ class TestPurgeVhostFiles(NginxVhostsTestBase, unittest.TestCase): ) self.assertEqual(removed, []) + def test_removes_www_redirect_variant(self) -> None: + self._mk_role( + "web-svc-cdn", + application_id="web-svc-cdn", + canonical=["cdn.infinito.example"], + ) + bare = self._touch_vhost("cdn.infinito.example", "https") + redirect = self._touch_vhost("www.cdn.infinito.example", "https") + unrelated = self._touch_vhost("www.unrelated.infinito.example", "https") + + removed = sorted( + purge_vhost_files_for_entities( + ["cdn"], + nginx_dir=self.nginx_dir, + domain_primary="infinito.example", + roles_dir=self.roles_dir, + ) + ) + + self.assertEqual(removed, sorted([bare, redirect])) + self.assertFalse(bare.exists()) + self.assertFalse(redirect.exists()) + self.assertTrue(unrelated.exists()) + def test_multiple_entities_in_one_call(self) -> None: self._mk_role( "web-app-matomo", diff --git a/utils/cleanup/nginx_vhosts.py b/utils/cleanup/nginx_vhosts.py index 39ceb7a790..47ff3b33ac 100644 --- a/utils/cleanup/nginx_vhosts.py +++ b/utils/cleanup/nginx_vhosts.py @@ -61,7 +61,8 @@ def iter_vhost_files_for_entity( roles_dir: Path = ROLES_DIR, ) -> Iterable[Path]: """Yield existing vhost files (http + https) for every app belonging - to *entity*. Only yields files that exist on disk. + to *entity*, plus the ``www..conf`` redirect variant + generated by ``web-opt-rdr-www``. Only yields files that exist. """ apps = apps_for_entity(entity, roles_dir=roles_dir) if not apps: @@ -75,10 +76,14 @@ def iter_vhost_files_for_entity( if not app_conf: continue for domain in iter_app_domains(app_conf, include_aliases=True): - for proto in _PROTOCOLS: - conf_file = servers_dir / proto / f"{domain}.conf" - if conf_file.is_file(): - yield conf_file + domain_variants = [domain] + if not domain.startswith("www."): + domain_variants.append(f"www.{domain}") + for variant in domain_variants: + for proto in _PROTOCOLS: + conf_file = servers_dir / proto / f"{variant}.conf" + if conf_file.is_file(): + yield conf_file def purge_vhost_files_for_entities(