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,