diff --git a/.github/workflows/compatibility.yml b/.github/workflows/compatibility.yml index a623f3214d..7a71bbc1a1 100644 --- a/.github/workflows/compatibility.yml +++ b/.github/workflows/compatibility.yml @@ -12,6 +12,7 @@ on: - 'docker/Dockerfile.compat' - 'docker/entrypoint.sh' - 'docker/healthcheck.sh' + - 'docker/test_healthcheck.py' - 'docker/run-docker-tests.sh' - 'docker/test-run-docker-tests.sh' - 'docker/test-compat-socket.sh' @@ -50,6 +51,9 @@ jobs: - name: Test Docker runner failure aggregation run: bash docker/test-run-docker-tests.sh + - name: Test container healthcheck listener + run: python3 -m unittest discover -s docker -p test_healthcheck.py + compat-aws-cli-shim: name: compat / aws CLI endpoint resolution runs-on: ubuntu-24.04-arm diff --git a/docker/healthcheck.sh b/docker/healthcheck.sh index 84e628dd14..7fa7b708fe 100755 --- a/docker/healthcheck.sh +++ b/docker/healthcheck.sh @@ -1,6 +1,53 @@ #!/bin/bash # Container health probe. Bash only, because ubi9-micro has no curl. -exec 3<>/dev/tcp/127.0.0.1/4566 || exit 1 +trim() { + local value=$1 + value=${value#"${value%%[![:space:]]*}"} + printf '%s' "${value%"${value##*[![:space:]]}"}" +} + +read_gateway_listener() { + local gateway=${1%%,*} + local ipv6_pattern='^\[([^]]+)\](:([0-9]+))?$' + gateway=$(trim "$gateway") + gateway_host= + gateway_port= + if [[ $gateway =~ $ipv6_pattern ]]; then + gateway_host=${BASH_REMATCH[1]} + gateway_port=${BASH_REMATCH[3]} + elif [[ $gateway == *:* ]]; then + gateway_host=$(trim "${gateway%%:*}") + gateway_port=$(trim "${gateway#*:}") + else + gateway_host=$gateway + fi +} + +host=${QUARKUS_HTTP_HOST:-} +public_port=${FLOCI_PORT:-} +tls_enabled=${FLOCI_TLS_ENABLED:-} +# Match LocalStackParity: a nonempty Floci setting wins, and parity can be disabled. +if [[ ${LOCALSTACK_PARITY:-} != false ]]; then + read_gateway_listener "${GATEWAY_LISTEN:-}" + host=${host:-$gateway_host} + public_port=${public_port:-${EDGE_PORT:-$gateway_port}} + if [[ -z $tls_enabled && ${USE_SSL:-} == 1 ]]; then + tls_enabled=true + fi +fi +# Probe the configured listener. Wildcard binds are reached over loopback. +host=${host:-127.0.0.1} +case "$host" in + 0.0.0.0) host=127.0.0.1 ;; + ::|\[::\]) host=::1 ;; +esac +port=${QUARKUS_HTTP_PORT:-${public_port:-4566}} +# Floci TLS serves HTTP and HTTPS on its public multiplexed port. Its private +# Quarkus HTTP backend requires a PROXY preface, so do not probe that directly. +if [[ ${tls_enabled,,} == true ]]; then + port=${public_port:-4566} +fi +exec 3<>"/dev/tcp/$host/$port" || exit 1 printf 'GET /_floci/init HTTP/1.0\r\nHost: localhost\r\n\r\n' >&3 IFS= read -r -d '' response <&3 [[ $response == "HTTP/1."?" 200"* ]] || exit 1 diff --git a/docker/test_healthcheck.py b/docker/test_healthcheck.py new file mode 100644 index 0000000000..79d87b8346 --- /dev/null +++ b/docker/test_healthcheck.py @@ -0,0 +1,199 @@ +"""Run with python3 -m unittest discover -s docker -p 'test_healthcheck.py'.""" +import os +from pathlib import Path +import socket +import subprocess +import threading +import unittest + + +SCRIPT = Path(__file__).with_name("healthcheck.sh") + + +class HealthcheckTest(unittest.TestCase): + def probe( + self, + settings: dict[str, str], + host: str = "127.0.0.1", + status: int = 200, + body: str = '{"ready":true}', + ) -> tuple[subprocess.CompletedProcess[bytes], list[bytes]]: + """Probe an owned loopback listener, replacing PORT in each setting.""" + with socket.socket(socket.AF_INET6 if ":" in host else socket.AF_INET, socket.SOCK_STREAM) as listener: + listener.bind((host, 0)) + listener.listen(1) + listener.settimeout(2) + port = str(listener.getsockname()[1]) + env = {key: value for key, value in os.environ.items() + if not key.startswith(("QUARKUS_HTTP_", "FLOCI_PORT", "FLOCI_TLS_ENABLED")) + and key not in ("USE_SSL", "LOCALSTACK_PARITY", "EDGE_PORT", "GATEWAY_LISTEN")} + env.update({key: value.replace("PORT", port) for key, value in settings.items()}) + requests: list[bytes] = [] + + def serve() -> None: + try: + connection, _ = listener.accept() + except socket.timeout: + return + with connection: + connection.settimeout(2) + request = b"" + while b"\r\n\r\n" not in request: + chunk = connection.recv(4096) + if not chunk: + break + request += chunk + requests.append(request) + connection.sendall(f"HTTP/1.0 {status} Test\r\nContent-Length: {len(body)}\r\n\r\n{body}".encode()) + + thread = threading.Thread(target=serve) + thread.start() + try: + result = subprocess.run(["bash", str(SCRIPT)], env=env, capture_output=True, timeout=3) + finally: + thread.join(timeout=3) + self.assertFalse(thread.is_alive()) + return result, requests + + def test_configured_interface_and_http_port(self): + result, requests = self.probe({"QUARKUS_HTTP_HOST": "127.0.0.2", "QUARKUS_HTTP_PORT": "PORT"}, host="127.0.0.2") + self.assertEqual(0, result.returncode, result.stderr) + self.assertIn(b"GET /_floci/init HTTP/1.0", requests[0]) + + def test_floci_port(self): + result, _ = self.probe({"FLOCI_PORT": "PORT"}) + self.assertEqual(0, result.returncode, result.stderr) + + def test_quarkus_port_precedes_floci_port(self): + result, _ = self.probe({"QUARKUS_HTTP_PORT": "PORT", "FLOCI_PORT": "1"}) + self.assertEqual(0, result.returncode, result.stderr) + + def test_tls_uses_public_multiplexed_port(self): + result, _ = self.probe({"FLOCI_TLS_ENABLED": "TRUE", "FLOCI_PORT": "PORT", "QUARKUS_HTTP_PORT": "1", "QUARKUS_HTTP_SSL_PORT": "2"}) + self.assertEqual(0, result.returncode, result.stderr) + + def test_localstack_tls_uses_public_multiplexed_port(self): + for floci_setting in (None, ""): + with self.subTest(floci_setting=floci_setting): + settings = {"USE_SSL": "1", "FLOCI_PORT": "PORT", "QUARKUS_HTTP_PORT": "1"} + if floci_setting is not None: + settings["FLOCI_TLS_ENABLED"] = floci_setting + result, _ = self.probe(settings) + self.assertEqual(0, result.returncode, result.stderr) + + def test_explicit_floci_tls_setting_precedes_localstack_tls(self): + for floci_setting in ("false", " \t"): + with self.subTest(floci_setting=floci_setting): + result, _ = self.probe({"FLOCI_TLS_ENABLED": floci_setting, "USE_SSL": "1", + "QUARKUS_HTTP_PORT": "PORT", "FLOCI_PORT": "1"}) + self.assertEqual(0, result.returncode, result.stderr) + + def test_disabled_parity_ignores_localstack_tls(self): + result, _ = self.probe({"LOCALSTACK_PARITY": "false", "USE_SSL": "1", + "QUARKUS_HTTP_PORT": "PORT", "FLOCI_PORT": "1"}) + self.assertEqual(0, result.returncode, result.stderr) + + def test_explicit_floci_tls_works_with_parity_disabled(self): + result, _ = self.probe({"LOCALSTACK_PARITY": "false", "FLOCI_TLS_ENABLED": "true", + "QUARKUS_HTTP_PORT": "1", "FLOCI_PORT": "PORT"}) + self.assertEqual(0, result.returncode, result.stderr) + + def test_edge_port(self): + for floci_port in (None, ""): + with self.subTest(floci_port=floci_port): + settings = {"EDGE_PORT": "PORT"} + if floci_port is not None: + settings["FLOCI_PORT"] = floci_port + result, _ = self.probe(settings) + self.assertEqual(0, result.returncode, result.stderr) + + def test_gateway_host_and_port(self): + result, _ = self.probe({"GATEWAY_LISTEN": "127.0.0.2:PORT"}, host="127.0.0.2") + self.assertEqual(0, result.returncode, result.stderr) + + def test_gateway_wildcard_uses_loopback(self): + result, _ = self.probe({"GATEWAY_LISTEN": "0.0.0.0:PORT"}) + self.assertEqual(0, result.returncode, result.stderr) + + def test_gateway_uses_first_listener_and_trims_whitespace(self): + result, _ = self.probe({"GATEWAY_LISTEN": " \t127.0.0.2 : PORT ,127.0.0.1:1"}, + host="127.0.0.2") + self.assertEqual(0, result.returncode, result.stderr) + + def test_gateway_port_without_host(self): + result, _ = self.probe({"GATEWAY_LISTEN": ":PORT"}) + self.assertEqual(0, result.returncode, result.stderr) + + def test_gateway_host_without_port(self): + for gateway in ("127.0.0.2", "127.0.0.2:"): + with self.subTest(gateway=gateway): + result, _ = self.probe({"GATEWAY_LISTEN": gateway, "FLOCI_PORT": "PORT"}, + host="127.0.0.2") + self.assertEqual(0, result.returncode, result.stderr) + + def test_gateway_ipv6_host_and_port(self): + for gateway in ("[::1]:PORT", "[::]:PORT"): + with self.subTest(gateway=gateway): + result, _ = self.probe({"GATEWAY_LISTEN": gateway}, host="::1") + self.assertEqual(0, result.returncode, result.stderr) + + def test_gateway_ipv6_host_without_port(self): + result, _ = self.probe({"GATEWAY_LISTEN": "[::1]", "FLOCI_PORT": "PORT"}, host="::1") + self.assertEqual(0, result.returncode, result.stderr) + + def test_edge_port_precedes_gateway_port(self): + result, _ = self.probe({"EDGE_PORT": "PORT", "GATEWAY_LISTEN": "0.0.0.0:1"}) + self.assertEqual(0, result.returncode, result.stderr) + + def test_explicit_floci_port_precedes_aliases(self): + result, _ = self.probe({"FLOCI_PORT": "PORT", "EDGE_PORT": "1", "GATEWAY_LISTEN": ":2"}) + self.assertEqual(0, result.returncode, result.stderr) + + def test_explicit_quarkus_port_precedes_aliases_without_tls(self): + result, _ = self.probe({"QUARKUS_HTTP_PORT": "PORT", "EDGE_PORT": "1", + "GATEWAY_LISTEN": ":2"}) + self.assertEqual(0, result.returncode, result.stderr) + + def test_explicit_host_precedes_gateway_host(self): + result, _ = self.probe({"QUARKUS_HTTP_HOST": "127.0.0.1", + "GATEWAY_LISTEN": "127.0.0.2:PORT"}) + self.assertEqual(0, result.returncode, result.stderr) + + def test_empty_explicit_host_allows_gateway_host(self): + result, _ = self.probe({"QUARKUS_HTTP_HOST": "", "GATEWAY_LISTEN": "127.0.0.2:PORT"}, + host="127.0.0.2") + self.assertEqual(0, result.returncode, result.stderr) + + def test_disabled_parity_ignores_listener_aliases(self): + result, _ = self.probe({"LOCALSTACK_PARITY": "false", "FLOCI_PORT": "PORT", + "EDGE_PORT": "1", "GATEWAY_LISTEN": "127.0.0.2:2"}) + self.assertEqual(0, result.returncode, result.stderr) + + def test_tls_uses_aliased_public_port(self): + for tls in ({"FLOCI_TLS_ENABLED": "true"}, {"USE_SSL": "1"}): + for listener in ({"EDGE_PORT": "PORT"}, {"GATEWAY_LISTEN": "127.0.0.2:PORT"}): + with self.subTest(tls=tls, listener=listener): + result, _ = self.probe({"QUARKUS_HTTP_HOST": "127.0.0.2", + "QUARKUS_HTTP_PORT": "1", **tls, **listener}, + host="127.0.0.2") + self.assertEqual(0, result.returncode, result.stderr) + + def test_wildcard_uses_loopback(self): + result, _ = self.probe({"QUARKUS_HTTP_HOST": "0.0.0.0", "FLOCI_PORT": "PORT"}) + self.assertEqual(0, result.returncode, result.stderr) + + def test_ipv6_wildcard_uses_loopback(self): + result, _ = self.probe({"QUARKUS_HTTP_HOST": "::", "FLOCI_PORT": "PORT"}, host="::1") + self.assertEqual(0, result.returncode, result.stderr) + + def test_failure_status_stays_unhealthy(self): + result, requests = self.probe({"FLOCI_PORT": "PORT"}, status=503) + self.assertTrue(requests) + self.assertNotEqual(0, result.returncode) + + def test_closed_port_stays_unhealthy(self): + with socket.socket() as reserved: + reserved.bind(("127.0.0.1", 0)) + env = dict(os.environ, QUARKUS_HTTP_HOST="127.0.0.1", QUARKUS_HTTP_PORT=str(reserved.getsockname()[1]), FLOCI_TLS_ENABLED="false") + result = subprocess.run(["bash", str(SCRIPT)], env=env, capture_output=True, timeout=3) + self.assertNotEqual(0, result.returncode)