diff --git a/changelog.d/551-nginx-http2-switch.md b/changelog.d/551-nginx-http2-switch.md new file mode 100644 index 00000000..43d46d52 --- /dev/null +++ b/changelog.d/551-nginx-http2-switch.md @@ -0,0 +1,3 @@ +### Changed + +- The managed vhost's HTTPS server no longer serves HTTP/2 by default: the `cloudfall_nginx_site` role takes `cloudfall_nginx_site_http2` (bool, default `false`), and the next `domains` run renders `listen 443 ssl;` and `listen [::]:443 ssl;` with no `http2` on any Nginx version. Once the installed Nginx carries the HTTP/2 fix, set it to `true` in the inventory or with `--extra-vars cloudfall_nginx_site_http2=true` to render HTTP/2 as before (#551) diff --git a/engine/README.md b/engine/README.md index f9f6bed7..be57a1e2 100644 --- a/engine/README.md +++ b/engine/README.md @@ -371,7 +371,11 @@ The site role installs Nginx and Certbot, serves the ACME challenge webroot, and converges in two phases: until a certificate exists the route serves plain HTTP so the site stays reachable during cutover; once TLS material is present (or issuance is explicitly requested via `ISSUE_CERTIFICATES=true`) -HTTP redirects to HTTPS with TLS 1.2+. The proxy discards any +HTTP redirects to HTTPS with TLS 1.2+. The HTTPS server serves HTTP/1.1 +only by default; once the installed Nginx carries the HTTP/2 fix, turn +HTTP/2 on with `cloudfall_nginx_site_http2: true` in the inventory's host +or group vars, or pass `--extra-vars cloudfall_nginx_site_http2=true` to +`ansible-playbook`. The proxy discards any client-supplied forwarding chain and sends only the canonical `$remote_addr` as `X-Forwarded-For` and `X-Real-IP`, so upstream applications cannot be spoofed by forged headers. When TLS is required the diff --git a/engine/ansible/roles/cloudfall_nginx_site/defaults/main.yml b/engine/ansible/roles/cloudfall_nginx_site/defaults/main.yml index 17530a1e..edfbbafa 100644 --- a/engine/ansible/roles/cloudfall_nginx_site/defaults/main.yml +++ b/engine/ansible/roles/cloudfall_nginx_site/defaults/main.yml @@ -2,5 +2,8 @@ cloudfall_nginx_site_issue_certificate: false cloudfall_nginx_site_acme_webroot: /var/lib/letsencrypt cloudfall_nginx_site_receipt_directory: "" +# Off by default until the installed Nginx carries the HTTP/2 fix (#551); +# true renders HTTP/2 in the form the installed Nginx parses. +cloudfall_nginx_site_http2: false # Debian's Nginx runs its workers as www-data. cloudfall_nginx_site_worker_user: www-data diff --git a/engine/ansible/roles/cloudfall_nginx_site/meta/argument_specs.yml b/engine/ansible/roles/cloudfall_nginx_site/meta/argument_specs.yml index 7f17fa11..8f3af8b2 100644 --- a/engine/ansible/roles/cloudfall_nginx_site/meta/argument_specs.yml +++ b/engine/ansible/roles/cloudfall_nginx_site/meta/argument_specs.yml @@ -105,6 +105,9 @@ argument_specs: cloudfall_nginx_site_receipt_directory: type: str default: "" + cloudfall_nginx_site_http2: + type: bool + default: false cloudfall_nginx_site_worker_user: type: str default: www-data diff --git a/engine/ansible/roles/cloudfall_nginx_site/templates/domain-site.conf.j2 b/engine/ansible/roles/cloudfall_nginx_site/templates/domain-site.conf.j2 index 8ec49cad..56b05fe3 100644 --- a/engine/ansible/roles/cloudfall_nginx_site/templates/domain-site.conf.j2 +++ b/engine/ansible/roles/cloudfall_nginx_site/templates/domain-site.conf.j2 @@ -90,7 +90,14 @@ server { {% if cloudfall_nginx_site_tls_active %} server { -{% if cloudfall_nginx_site_version is version('1.25.1', '>=') %} +{# + `| bool` because a key=value `--extra-vars` hands the role the string + "false", which is truthy. +#} +{% if not cloudfall_nginx_site_http2 | bool %} + listen 443 ssl; + listen [::]:443 ssl; +{% elif cloudfall_nginx_site_version is version('1.25.1', '>=') %} listen 443 ssl; listen [::]:443 ssl; http2 on; diff --git a/engine/tests/test_nginx_site_routes.py b/engine/tests/test_nginx_site_routes.py index e4b148d1..26760e83 100644 --- a/engine/tests/test_nginx_site_routes.py +++ b/engine/tests/test_nginx_site_routes.py @@ -155,10 +155,21 @@ def _play(tmp_path: Path, name: str, play: dict[str, object]) -> str: return done.stdout +def _role_default(name: str) -> object: + defaults = yaml.safe_load((ROLE / "defaults" / "main.yml").read_text("utf-8")) + return cast("dict[str, object]", defaults)[name] + + def _render_all( - tmp_path: Path, domains: dict[str, dict[str, object]] + tmp_path: Path, + domains: dict[str, dict[str, object]], + overrides: dict[str, dict[str, object]], ) -> dict[str, str]: - """Render each Domain with TLS on and off on both Nginx versions.""" + """Render each Domain with TLS on and off on both Nginx versions. + + HTTP/2 is on, the shape the golden files hold, unless ``overrides`` + names the Domain's label. + """ output = tmp_path / "rendered" output.mkdir() tasks: list[dict[str, object]] = [] @@ -182,7 +193,9 @@ def _render_all( "cloudfall_nginx_site_acme_webroot": "/var/lib/letsencrypt", "cloudfall_nginx_site_tls_active": tls_active, "cloudfall_nginx_site_version": version, - }, + "cloudfall_nginx_site_http2": True, + } + | overrides.get(label, {}), } ) _play(tmp_path, "render", {"tasks": tasks}) @@ -200,6 +213,19 @@ def rendered(tmp_path_factory: pytest.TempPathFactory) -> dict[str, str]: "nested": _routed_domain(tmp_path, _NESTED_ROUTES), "reversed": _routed_domain(tmp_path, _NESTED_ROUTES_REVERSED), "no-root": _routed_domain(tmp_path, _NO_ROOT_ROUTES), + "role-default": _upstream_only_domain(), + "no-http2": _upstream_only_domain(), + "no-http2-extra-var": _upstream_only_domain(), + }, + { + "role-default": { + "cloudfall_nginx_site_http2": _role_default( + "cloudfall_nginx_site_http2" + ) + }, + "no-http2": {"cloudfall_nginx_site_http2": False}, + # What `--extra-vars cloudfall_nginx_site_http2=false` hands the role. + "no-http2-extra-var": {"cloudfall_nginx_site_http2": "false"}, }, ) @@ -318,6 +344,29 @@ def test_s003_7_an_upstream_only_domain_renders_byte_identical( assert rendered[f"upstream/{name}"] == golden +@pytest.mark.parametrize("nginx_version", NGINX_VERSIONS) +@pytest.mark.parametrize("label", ["role-default", "no-http2", "no-http2-extra-var"]) +def test_http2_off_drops_it_from_the_tls_server_on_every_version( + rendered: dict[str, str], label: str, nginx_version: str +) -> None: + """Only the listen lines differ from the default render (#551).""" + vhost = rendered[f"{label}/tls-{nginx_version}.conf"] + golden = (GOLDEN / f"tls-{nginx_version}.conf").read_text(encoding="utf-8") + tls = _servers(vhost)[1] + + assert tls.startswith( + "server {\n" + " listen 443 ssl;\n" + " listen [::]:443 ssl;\n" + " server_name crm.example.test www.crm.example.test;\n" + ) + assert "http2" not in vhost + assert vhost == golden.replace("ssl http2;", "ssl;").replace(" http2 on;\n", "") + assert rendered[f"{label}/http-{nginx_version}.conf"] == ( + GOLDEN / f"http-{nginx_version}.conf" + ).read_text(encoding="utf-8") + + # proves: S-003-2 @pytest.mark.parametrize("nginx_version", NGINX_VERSIONS) @pytest.mark.parametrize("tls_active", [True, False]) diff --git a/engine/tests/test_nginx_site_templates.py b/engine/tests/test_nginx_site_templates.py index 7736e92d..4ce7ccf7 100644 --- a/engine/tests/test_nginx_site_templates.py +++ b/engine/tests/test_nginx_site_templates.py @@ -5,6 +5,8 @@ from pathlib import Path import pytest +import yaml +from ansible.plugins.filter.core import FilterModule as AnsibleFilters # Aliased so pytest does not collect Ansible's plugin class as a test class. from ansible.plugins.test.core import TestModule as AnsibleTests @@ -35,7 +37,9 @@ def _domain() -> dict[str, object]: } -def _render(*, tls_active: bool, nginx_version: str = "1.26.3") -> str: +def _render( + *, tls_active: bool, nginx_version: str = "1.26.3", http2: object = True +) -> str: # trim_blocks matches the template module's default. environment = Environment( loader=FileSystemLoader(TEMPLATES), @@ -46,11 +50,13 @@ def _render(*, tls_active: bool, nginx_version: str = "1.26.3") -> str: ) # Ansible's own tests, so `is version(...)` compares as on the host. environment.tests.update(AnsibleTests().tests()) + environment.filters.update(AnsibleFilters().filters()) return environment.get_template("domain-site.conf.j2").render( cloudfall_nginx_site_domain=_domain(), cloudfall_nginx_site_acme_webroot="/var/lib/letsencrypt", cloudfall_nginx_site_tls_active=tls_active, cloudfall_nginx_site_version=nginx_version, + cloudfall_nginx_site_http2=http2, ) @@ -113,3 +119,30 @@ def test_nginx_from_1_25_1_gets_the_http2_directive(nginx_version: str) -> None: " server_name crm.example.test www.crm.example.test;\n" ) in rendered assert "ssl http2;" not in rendered + + +@pytest.mark.parametrize("http2", [False, "false"]) +@pytest.mark.parametrize("nginx_version", [DEBIAN_12_NGINX, "1.26.3"]) +def test_http2_off_renders_no_http2_on_either_side_of_1_25_1( + nginx_version: str, http2: object +) -> None: + """`"false"` is what a key=value `--extra-vars` hands the role (#551).""" + rendered = _render(tls_active=True, nginx_version=nginx_version, http2=http2) + + assert ( + "server {\n" + " listen 443 ssl;\n" + " listen [::]:443 ssl;\n" + " server_name crm.example.test www.crm.example.test;\n" + ) in rendered + assert "http2" not in rendered + + +def test_http2_defaults_off_in_the_role_and_its_argument_spec() -> None: + role = TEMPLATES.parent + defaults = yaml.safe_load((role / "defaults" / "main.yml").read_text("utf-8")) + specs = yaml.safe_load((role / "meta" / "argument_specs.yml").read_text("utf-8")) + option = specs["argument_specs"]["main"]["options"]["cloudfall_nginx_site_http2"] + + assert defaults["cloudfall_nginx_site_http2"] is False + assert option == {"type": "bool", "default": False}