fix(front-proxy): accept the status codes an app declares, not just the triple

The front proxy hardcoded [200, 301, 302] and escalated to a full OpenResty redeploy on anything else. Apps that answer 403, 404 or 405 by design tripped it on every run: 19 escalations in one Nextcloud job at a measured median of 40.5s each.

roles/<app>/meta/server.yml already declares status_codes per canonical key, and sys-ctl-hlth-webserver already reads it. Move the resolution into utils/roles/applications/status_codes.py so both consumers share one definition of DEFAULT_OK, and expose it to templates as the accepted_status_codes filter.

The proxy gate takes the union of the default and the declaration rather than replacing it. Most declarations are narrower than the triple, so replacing would escalate vhosts that answer perfectly well. Contract checking stays with sys-ctl-hlth-webserver, which keeps replacement semantics.

_normalize_codes and _valid_http_code became dead in the health filter and are removed.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
kevinveenbirkenbachandClaude Opus 5 committed 2026-09-04 17:30:34 +02:00
1 parent bbfd82f893
commit c88e50ef17
5 files changed
+306 -51

No files matched your search

@@ -0,0 +1,157 @@
import unittest
from plugins.filter.status_codes import FilterModule
from utils.roles.applications.status_codes import (
DEFAULT_OK,
accepted_status_codes,
declared_status_codes,
)
SEAWEEDFS = {
"web-app-seaweedfs": {
"domains": {
"canonical": {
"api": "s3.example.org",
"filer": "filer.example.org",
"master": "master.example.org",
}
},
"server": {
"status_codes": {
"api": [301, 302, 403, 405],
"filer": [302, 403],
"master": [302, 403],
}
},
}
}
class TestDeclaredStatusCodes(unittest.TestCase):
def test_per_vhost_key_wins(self):
for domain, codes in (
("s3.example.org", [301, 302, 403, 405]),
("filer.example.org", [302, 403]),
("master.example.org", [302, 403]),
):
with self.subTest(domain=domain):
self.assertEqual(
declared_status_codes(SEAWEEDFS, "web-app-seaweedfs", domain),
codes,
)
def test_undeclared_app_declares_nothing(self):
apps = {"web-app-plain": {"domains": {"canonical": ["plain.example.org"]}}}
self.assertEqual(
declared_status_codes(apps, "web-app-plain", "plain.example.org"), []
)
def test_flat_canonical_uses_the_default_key(self):
apps = {
"web-app-prometheus": {
"domains": {"canonical": ["metrics.example.org"]},
"server": {"status_codes": {"default": [405]}},
}
}
self.assertEqual(
declared_status_codes(apps, "web-app-prometheus", "metrics.example.org"),
[405],
)
def test_unknown_domain_falls_back_to_the_declared_default(self):
apps = {
"web-app-x": {
"domains": {"canonical": {"api": "api.example.org"}},
"server": {"status_codes": {"api": [403], "default": [404]}},
}
}
self.assertEqual(
declared_status_codes(apps, "web-app-x", "other.example.org"), [404]
)
def test_a_key_without_its_own_codes_uses_the_default(self):
apps = {
"web-app-x": {
"domains": {"canonical": {"api": "api.example.org"}},
"server": {"status_codes": {"other": [403], "default": [404]}},
}
}
self.assertEqual(
declared_status_codes(apps, "web-app-x", "api.example.org"), [404]
)
def test_scalar_and_string_codes_are_normalized(self):
apps = {
"web-app-x": {
"domains": {"canonical": {"api": ["api.example.org"]}},
"server": {"status_codes": {"api": "403"}},
}
}
self.assertEqual(
declared_status_codes(apps, "web-app-x", "api.example.org"), [403]
)
def test_out_of_range_declarations_are_dropped(self):
apps = {
"web-app-x": {
"domains": {"canonical": {"api": "api.example.org"}},
"server": {"status_codes": {"api": [999, "nope"]}},
}
}
self.assertEqual(
declared_status_codes(apps, "web-app-x", "api.example.org"), []
)
class TestAcceptedStatusCodes(unittest.TestCase):
def test_filter_is_registered(self):
self.assertIn("accepted_status_codes", FilterModule().filters())
def test_declared_codes_are_added_to_the_default(self):
self.assertEqual(
accepted_status_codes(SEAWEEDFS, "web-app-seaweedfs", "filer.example.org"),
[200, 302, 301, 403],
)
def test_a_narrower_declaration_never_narrows_the_gate(self):
apps = {
"web-app-mailu": {
"domains": {"canonical": ["mail.example.org"]},
"server": {"status_codes": {"default": [200, 301]}},
}
}
self.assertEqual(
accepted_status_codes(apps, "web-app-mailu", "mail.example.org"),
DEFAULT_OK,
)
def test_a_disjoint_declaration_only_adds(self):
apps = {
"web-svc-mirror": {
"domains": {"canonical": ["mirror.example.org"]},
"server": {"status_codes": {"default": [404]}},
}
}
self.assertEqual(
accepted_status_codes(apps, "web-svc-mirror", "mirror.example.org"),
[200, 302, 301, 404],
)
def test_undeclared_app_gets_the_default(self):
apps = {"web-app-plain": {"domains": {"canonical": ["plain.example.org"]}}}
self.assertEqual(
accepted_status_codes(apps, "web-app-plain", "plain.example.org"),
DEFAULT_OK,
)
def test_the_result_is_a_copy(self):
first = accepted_status_codes(SEAWEEDFS, "web-app-seaweedfs", "unknown.org")
first.append(418)
self.assertEqual(
accepted_status_codes(SEAWEEDFS, "web-app-seaweedfs", "unknown.org"),
DEFAULT_OK,
)
if __name__ == "__main__":
unittest.main()