From d84d87b46cef5d343e5415c71ca35cc35179ee25 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Thu, 19 Nov 2026 15:34:39 +0000 Subject: [PATCH] fix(svc-cache-package): serve HTTP/2 on TLS listeners behind the platform switch MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The package-cache frontend rendered its TLS server blocks without an http2 directive, so re-enabling WEBSERVER_HTTP2_ENABLED after the OpenResty CVE fix would still leave every cached HTTPS host on HTTP/1.1. The template now emits http2 on; for TLS listeners when the switch is on, fail-closed for string overrides from extra vars. The dev-stack renderer reads the same switch from group_vars/all/04_webserver.yml, so both renderers stay in step. Plain listeners never get HTTP/2. Refs #2245, builds on #2237. 🎯 Fix: 93 % 💥 Break: 3 % 💸 Tokens: – ⏱️ Velocity: – 🛡️ Security: 100 % 🏆 Score: 90 🚦 Gate: ✅ 90/5, tokens and velocity <= 100, security 100 % Created-with: https://s.veen.world/skills Co-Authored-By: Claude Opus 5.5 --- .../templates/upstreams.conf.j2 | 3 ++ tests/unit/python/utils/cache/test_render.py | 38 +++++++++++++++++++ utils/cache/render.py | 6 +++ 3 files changed, 47 insertions(+) diff --git a/roles/svc-cache-package/templates/upstreams.conf.j2 b/roles/svc-cache-package/templates/upstreams.conf.j2 index 1e8127131a..0328b9045e 100644 --- a/roles/svc-cache-package/templates/upstreams.conf.j2 +++ b/roles/svc-cache-package/templates/upstreams.conf.j2 @@ -27,6 +27,9 @@ server { listen {{ listener.port }}{{ ' ssl' if listener.tls else '' }}; server_name {{ host.host }}; {% if listener.tls %} +{% if WEBSERVER_HTTP2_ENABLED | string | lower in ['true', 'yes', 'on', '1'] %} + http2 on; +{% endif %} ssl_certificate {{ CACHE_PACKAGE_CERTS_DIR }}/{{ host.host }}.crt; ssl_certificate_key {{ CACHE_PACKAGE_CERTS_DIR }}/{{ host.host }}.key; {% set repo = CACHE_PACKAGE_REPOS[host.repo] %} diff --git a/tests/unit/python/utils/cache/test_render.py b/tests/unit/python/utils/cache/test_render.py index 559c25f7f0..32ea5290cd 100644 --- a/tests/unit/python/utils/cache/test_render.py +++ b/tests/unit/python/utils/cache/test_render.py @@ -11,6 +11,7 @@ import stat import tempfile import unittest from pathlib import Path +from unittest import mock from utils.cache.hosts import host_records from utils.cache.render import _write, upstreams_conf @@ -88,5 +89,42 @@ class TestOllamaRegistry(unittest.TestCase): self.assertNotIn("location /v2/", block) +def _servers(http2: bool | str) -> tuple[list[str], list[str]]: + """Return the TLS and the plain server blocks of the rendered frontend. + + Args: + http2: the value ``WEBSERVER_HTTP2_ENABLED`` is rendered with. + """ + with mock.patch("utils.cache.render._webserver_http2", return_value=http2): + blocks = upstreams_conf().split("server {")[1:] + tls = [block for block in blocks if " ssl;" in block] + return tls, [block for block in blocks if " ssl;" not in block] + + +class TestHttp2(unittest.TestCase): + def test_no_listener_serves_http2_while_the_switch_is_off(self) -> None: + tls, plain = _servers(False) + + self.assertTrue(tls) + self.assertFalse([block for block in tls + plain if "http2" in block]) + + def test_every_tls_listener_serves_http2_once_the_switch_is_on(self) -> None: + tls, _plain = _servers(True) + + self.assertTrue(tls) + self.assertEqual([block for block in tls if "\n\thttp2 on;\n" not in block], []) + + def test_a_string_false_from_extra_vars_keeps_http2_off(self) -> None: + tls, _plain = _servers("false") + + self.assertFalse([block for block in tls if "http2" in block]) + + def test_a_plain_listener_never_serves_http2(self) -> None: + _tls, plain = _servers(True) + + self.assertTrue(plain) + self.assertFalse([block for block in plain if "http2" in block]) + + if __name__ == "__main__": unittest.main() diff --git a/utils/cache/render.py b/utils/cache/render.py index 8e041f6737..e8e888f917 100644 --- a/utils/cache/render.py +++ b/utils/cache/render.py @@ -33,6 +33,7 @@ if TYPE_CHECKING: ROLE = "svc-cache-package" ROLE_DIR = PROJECT_ROOT / "roles" / ROLE ROLE_VARS = ROLE_DIR / ROLE_FILE_VARS_MAIN +WEBSERVER_VARS = PROJECT_ROOT / "group_vars" / "all" / "04_webserver.yml" TEMPLATE_DIRS = (ROLE_DIR / "templates", PROJECT_ROOT / "compose" / "templates") UPSTREAMS_TEMPLATE = "upstreams.conf.j2" CONSUMER_TEMPLATE = "cache-consumer.yml.jinja" @@ -92,10 +93,15 @@ def _render(template: str, **context) -> str: return env.get_template(template).render(**context) +def _webserver_http2() -> bool: + return load_yaml_any(str(WEBSERVER_VARS))["WEBSERVER_HTTP2_ENABLED"] + + def upstreams_conf() -> str: """Return the nginx frontend configuration the declarations produce.""" return _render( UPSTREAMS_TEMPLATE, + WEBSERVER_HTTP2_ENABLED=_webserver_http2(), CACHE_PACKAGE_UPSTREAMS=list(host_records().values()), CACHE_PACKAGE_REPOS=repo_records(), CACHE_PACKAGE_BACKEND=BACKEND,