diff --git a/docs/skills/k0s-sysext-ops.md b/docs/skills/k0s-sysext-ops.md index 7b806f80..b38569ae 100644 --- a/docs/skills/k0s-sysext-ops.md +++ b/docs/skills/k0s-sysext-ops.md @@ -46,6 +46,27 @@ k0s automatically applies all `.yaml` files under `/var/lib/k0s/manifests/argocd k0s kubectl get pods -A ``` +## KubeStellar Kiosk TLS Management + +The KubeStellar kiosk proxy TLS certificate (`cert.pem`) and private key (`key.pem`) are generated on first boot by `k0s-kiosk-tls.service`, which runs `/usr/share/k0s/kiosk-tls/generate-kiosk-tls.sh` (source: `files/k0s/kiosk-tls/generate-kiosk-tls.sh`) before `k0scontroller.service` starts. The private key is stored persistently in `/var/lib/k0s/kiosk/key.pem` with mode `0600` (the script runs under `umask 077`, so the key is never world-readable, even transiently), and the certificate is configured with the host's actual IP addresses in the Subject Alternative Names (SAN). `files/k0s/sysext/k0s-manifests.conf` seeds the kiosk static assets (`nginx.conf`, `kiosk-gate.js`, `kiosk-gate.css`) into this same directory on every boot, file-by-file rather than as a whole-directory copy, specifically so it never deletes `cert.pem`/`key.pem`. + +**Compromised build-time material is replaced automatically.** Sysext releases prior to `k0s-kiosk-tls.service` generated the certificate and key at image build time and shipped them inside the publicly-published release image, so those private keys are public knowledge. Hosts upgraded from such a release still carry the compromised pair in `/var/lib/k0s/kiosk`. The generator detects it — the build-time certificate carries the fixed SAN `DNS:localhost,DNS:*.local,IP:127.0.0.1,IP:10.0.2.15` and lacks the `OU=host-generated` subject marker the script adds — and regenerates the pair on the next boot (or on `systemctl restart k0s-kiosk-tls.service`). Host-generated and operator-provided certificates are never touched. + +The SAN is computed once, at generation time, from whatever IP addresses the host has at that moment (3650-day validity, no periodic refresh). On a DHCP host whose address later changes, the certificate will not include the new address — rotate manually (below) after an address change if browser cert warnings start appearing. + +To rotate or regenerate the TLS certificate and private key: + +```bash +# Remove the existing certificate and key from persistent storage +rm -f /var/lib/k0s/kiosk/key.pem /var/lib/k0s/kiosk/cert.pem + +# Trigger regeneration via the oneshot unit +systemctl restart k0s-kiosk-tls.service + +# Restart the controller service to reload kiosk assets +systemctl restart k0scontroller.service +``` + ## Troubleshooting - **Extension not merged**: Check `systemd-sysext status`. Verify the persistent image is `/var/lib/k0s/k0s.raw`; the boot activation unit copies it into `/run/extensions/k0s.raw`. diff --git a/docs/skills/k0s-sysext.md b/docs/skills/k0s-sysext.md index 1e38f94c..76b5078b 100644 --- a/docs/skills/k0s-sysext.md +++ b/docs/skills/k0s-sysext.md @@ -57,6 +57,8 @@ Design choices: | `elements/k0s/k0s-bin.bst` | Pins the upstream `k0s` binary SHA256; the release URL is derived from `include/k0s.yml`. | | `elements/oci/k0s-sysext.bst` | Builds the EROFS sysext image (`k0s-.raw`). | | `files/k0s/sysext/k0scontroller.service` | systemd unit for the k0s single-node controller/worker. Not enabled by default. | +| `files/k0s/sysext/k0s-kiosk-tls.service` | Oneshot systemd unit generating persistent kiosk TLS cert and key on first boot. | +| `files/k0s/kiosk-tls/generate-kiosk-tls.sh` | Generator run by `k0s-kiosk-tls.service`; also replaces the compromised build-time cert/key pair shipped by earlier releases. | | `files/k0s/sysext/extension-release.k0s` | Static sysext identity (`ID=_any`); `VERSION_ID=`/`ARCHITECTURE=` are appended at build time. | | `files/k0s/sysext/k0s-manifests.conf` | tmpfiles rule that copies declarative stacks to `/var/lib/k0s/manifests/`. | | `files/k0s/manifests/argocd/` | Raw YAML manifests for Argo CD. | diff --git a/elements/bluefin-server/os-stack.bst b/elements/bluefin-server/os-stack.bst index 074634d0..aecf386e 100644 --- a/elements/bluefin-server/os-stack.bst +++ b/elements/bluefin-server/os-stack.bst @@ -58,6 +58,9 @@ depends: # gpg binary required by systemd-sysupdate to verify SHA256SUMS.gpg - freedesktop-sdk.bst:components/gnupg.bst + # openssl CLI required for first-boot TLS certificate generation + - freedesktop-sdk.bst:components/openssl.bst + # First-Boot Credentials Provisioning - bluefin-server/os-creds-prov.bst diff --git a/elements/oci/k0s-sysext.bst b/elements/oci/k0s-sysext.bst index dc5bcdbc..b733fd39 100644 --- a/elements/oci/k0s-sysext.bst +++ b/elements/oci/k0s-sysext.bst @@ -3,8 +3,8 @@ description: | Produce a systemd-sysext extension image for k0s. Output: k0s-.raw, k0s-.raw.zst, and SHA256SUMS. - Contains /usr/bin/k0s, k0scontroller.service, tmpfiles.d/k0s-manifests.conf, - and /usr/share/k0s/manifests/ (Argo CD and KubeStellar). + Contains /usr/bin/k0s, k0scontroller.service, k0s-kiosk-tls.service, + tmpfiles.d/k0s-manifests.conf, and /usr/share/k0s/manifests/ (Argo CD and KubeStellar). (@): - include/arch.yml @@ -15,7 +15,6 @@ build-depends: - freedesktop-sdk.bst:components/erofs-utils.bst - freedesktop-sdk.bst:components/xz.bst - freedesktop-sdk.bst:components/zstd.bst - - freedesktop-sdk.bst:components/openssl.bst - filename: k0s/k0s-bin.bst config: location: / @@ -33,6 +32,9 @@ sources: - kind: local path: files/k0s/kubeflex directory: kubeflex-src + - kind: local + path: files/k0s/kiosk-tls + directory: kiosk-tls-src - kind: local path: files/os/issue.d/40-kubestellar.issue directory: issue-src @@ -67,6 +69,7 @@ config: # Stage systemd service cp -a sysext-src/k0scontroller.service sysext/usr/lib/systemd/system/ + cp -a sysext-src/k0s-kiosk-tls.service sysext/usr/lib/systemd/system/ # Stage extension release metadata cp -a sysext-src/extension-release.k0s sysext/usr/lib/extension-release.d/ @@ -90,14 +93,10 @@ config: cp -a kubeflex-src/. sysext/usr/share/k0s/kubeflex/ chmod 0755 sysext/usr/share/k0s/kubeflex/generate-postgres-secret.sh - # Generate self-signed TLS certificate for local kiosk proxy - openssl req -x509 -nodes -days 3650 -newkey rsa:2048 \ - -keyout sysext/usr/share/k0s/kiosk/key.pem \ - -out sysext/usr/share/k0s/kiosk/cert.pem \ - -subj "/CN=KubeStellar Console" \ - -addext "subjectAltName=DNS:localhost,DNS:*.local,IP:127.0.0.1,IP:10.0.2.15" - chmod 0600 sysext/usr/share/k0s/kiosk/key.pem - chmod 0644 sysext/usr/share/k0s/kiosk/cert.pem + # Stage kiosk first-boot TLS generator (run by k0s-kiosk-tls.service). + mkdir -p sysext/usr/share/k0s/kiosk-tls + cp -a kiosk-tls-src/. sysext/usr/share/k0s/kiosk-tls/ + chmod 0755 sysext/usr/share/k0s/kiosk-tls/generate-kiosk-tls.sh # Build EROFS image mkfs.erofs -d0 "${OUT}/${FNAME}" sysext diff --git a/files/k0s/kiosk-tls/generate-kiosk-tls.sh b/files/k0s/kiosk-tls/generate-kiosk-tls.sh new file mode 100755 index 00000000..3813cc70 --- /dev/null +++ b/files/k0s/kiosk-tls/generate-kiosk-tls.sh @@ -0,0 +1,64 @@ +#!/bin/bash +# First-boot generator for the KubeStellar kiosk TLS certificate and key. +# +# Run by k0s-kiosk-tls.service before k0scontroller.service. Writes key.pem +# (0600) and cert.pem (0644) into /var/lib/k0s/kiosk with the host's DNS name +# and IP addresses in the SAN. Idempotent: host-generated material is kept +# across boots so the certificate browsers have trusted stays stable. +# +# Earlier sysext releases generated cert.pem/key.pem at build time and shipped +# them inside the public release image, so those private keys are public +# knowledge. Build-time certificates carry exactly the SAN below (10.0.2.15 is +# the QEMU user-net guest address that was hardcoded at build time) and lack +# the OU=host-generated subject marker this script adds. When one is found on +# disk (seeded by the old directory-level tmpfiles rule on upgraded hosts), it +# is treated as compromised and regenerated in place. The marker keeps a QEMU +# user-net host whose own SAN legitimately matches from regenerating on every +# boot. +set -euo pipefail +umask 077 + +kiosk_dir="${KIOSK_TLS_DIR:-/var/lib/k0s/kiosk}" +key="$kiosk_dir/key.pem" +cert="$kiosk_dir/cert.pem" + +leaked_build_san="DNS:localhost,DNS:*.local,IPAddress:127.0.0.1,IPAddress:10.0.2.15" + +if [ -s "$key" ] && [ -s "$cert" ]; then + subject="$(openssl x509 -in "$cert" -noout -subject 2>/dev/null || true)" + existing_san="$(openssl x509 -in "$cert" -noout -ext subjectAltName 2>/dev/null | sed 1d | tr -d '[:space:]' || true)" + if [[ "$subject" == *host-generated* ]] || [ "$existing_san" != "$leaked_build_san" ]; then + exit 0 + fi + echo "k0s-kiosk-tls: replacing compromised build-time certificate (private key was published in the release image)" >&2 + rm -f "$key" "$cert" +fi + +mkdir -p "$kiosk_dir" +# tmpfiles.d owns this dir as 0755; keep that under our restrictive umask so +# the kiosk proxy can still read the static assets seeded next to the certs. +chmod 0755 "$kiosk_dir" + +SAN="DNS:localhost,DNS:*.local,IP:127.0.0.1" +if command -v hostname >/dev/null 2>&1; then + HN="$(hostname 2>/dev/null || true)" + if [ -n "$HN" ] && [ "$HN" != "localhost" ]; then + SAN="${SAN},DNS:${HN}" + fi +else + echo "k0s-kiosk-tls: hostname command not found, SAN has no host DNS name" >&2 +fi +if command -v ip >/dev/null 2>&1; then + for addr in $(ip -o addr show scope global 2>/dev/null | tr -s ' ' | cut -d ' ' -f4 | cut -d/ -f1); do + SAN="${SAN},IP:${addr}" + done +else + echo "k0s-kiosk-tls: ip command not found, SAN has no host IP address" >&2 +fi + +openssl req -x509 -nodes -days 3650 -newkey rsa:2048 \ + -keyout "$key" -out "$cert" \ + -subj "/CN=KubeStellar Console/OU=host-generated" \ + -addext "subjectAltName=${SAN}" +chmod 0600 "$key" +chmod 0644 "$cert" diff --git a/files/k0s/sysext/k0s-kiosk-tls.service b/files/k0s/sysext/k0s-kiosk-tls.service new file mode 100644 index 00000000..2c950bc1 --- /dev/null +++ b/files/k0s/sysext/k0s-kiosk-tls.service @@ -0,0 +1,11 @@ +[Unit] +Description=Generate persistent TLS certificate and key for KubeStellar kiosk +RequiresMountsFor=/var/lib/k0s +After=network-online.target systemd-tmpfiles-setup.service +Before=k0scontroller.service + +[Service] +Type=oneshot +RemainAfterExit=yes +StateDirectory=k0s +ExecStart=/usr/share/k0s/kiosk-tls/generate-kiosk-tls.sh diff --git a/files/k0s/sysext/k0s-manifests.conf b/files/k0s/sysext/k0s-manifests.conf index 33f9929c..d43dd2a7 100644 --- a/files/k0s/sysext/k0s-manifests.conf +++ b/files/k0s/sysext/k0s-manifests.conf @@ -2,4 +2,14 @@ d /var/lib/k0s/manifests 0755 root root - - C+ /var/lib/k0s/manifests/argocd - - - - /usr/share/k0s/manifests/argocd C+ /var/lib/k0s/manifests/kubestellar - - - - /usr/share/k0s/manifests/kubestellar -C+ /var/lib/k0s/kiosk - - - - /usr/share/k0s/kiosk +# Per-file, not whole-directory: k0s-kiosk-tls.service also writes +# cert.pem/key.pem into this directory, and they must survive every boot. +# A directory-level `C+` here would delete-then-recopy the whole tree from +# /usr/share/k0s/kiosk (which never contains cert.pem/key.pem), wiping the +# generated TLS material on every boot. Targeting each static asset file +# individually still propagates sysext upgrades to nginx.conf/kiosk-gate.* +# without touching anything else in the directory. +d /var/lib/k0s/kiosk 0755 root root - - +C+ /var/lib/k0s/kiosk/nginx.conf - - - - /usr/share/k0s/kiosk/nginx.conf +C+ /var/lib/k0s/kiosk/kiosk-gate.js - - - - /usr/share/k0s/kiosk/kiosk-gate.js +C+ /var/lib/k0s/kiosk/kiosk-gate.css - - - - /usr/share/k0s/kiosk/kiosk-gate.css diff --git a/files/k0s/sysext/k0scontroller.service b/files/k0s/sysext/k0scontroller.service index 618f2a64..7ea3458b 100644 --- a/files/k0s/sysext/k0scontroller.service +++ b/files/k0s/sysext/k0scontroller.service @@ -2,8 +2,8 @@ Description=k0s - Zero Friction Kubernetes Documentation=https://docs.k0sproject.io ConditionFileIsExecutable=/usr/bin/k0s -After=network-online.target -Wants=network-online.target +After=network-online.target k0s-kiosk-tls.service +Wants=network-online.target k0s-kiosk-tls.service [Service] Type=simple diff --git a/tests/unit/test_kubestellar_kiosk.py b/tests/unit/test_kubestellar_kiosk.py index ec608c72..1c329853 100644 --- a/tests/unit/test_kubestellar_kiosk.py +++ b/tests/unit/test_kubestellar_kiosk.py @@ -25,6 +25,10 @@ / "kubestellar" / "41-kubestellar-kiosk-proxy.yaml" ) +KIOSK_TLS_SERVICE = ROOT / "files" / "k0s" / "sysext" / "k0s-kiosk-tls.service" +KIOSK_TLS_SCRIPT = ROOT / "files" / "k0s" / "kiosk-tls" / "generate-kiosk-tls.sh" +K0S_CONTROLLER_SERVICE = ROOT / "files" / "k0s" / "sysext" / "k0scontroller.service" +OS_STACK = ROOT / "elements" / "bluefin-server" / "os-stack.bst" def test_kiosk_assets_are_packaged_and_seeded() -> None: @@ -34,12 +38,39 @@ def test_kiosk_assets_are_packaged_and_seeded() -> None: assert KIOSK_CONF.is_file() assert KIOSK_JS.is_file() assert KIOSK_CSS.is_file() - assert "freedesktop-sdk.bst:components/openssl.bst" in sysext - assert "keyout sysext/usr/share/k0s/kiosk/key.pem" in sysext + assert "freedesktop-sdk.bst:components/openssl.bst" not in sysext + assert "keyout" not in sysext + assert "cp -a sysext-src/k0s-kiosk-tls.service sysext/usr/lib/systemd/system/" in sysext + assert "path: files/k0s/kiosk-tls" in sysext + assert "directory: kiosk-tls-src" in sysext + assert "cp -a kiosk-tls-src/. sysext/usr/share/k0s/kiosk-tls/" in sysext + assert "chmod 0755 sysext/usr/share/k0s/kiosk-tls/generate-kiosk-tls.sh" in sysext assert "path: files/k0s/kiosk" in sysext assert "directory: kiosk-src" in sysext assert "cp -a kiosk-src/. sysext/usr/share/k0s/kiosk/" in sysext - assert "C+ /var/lib/k0s/kiosk - - - - /usr/share/k0s/kiosk" in tmpfiles + assert "C+ /var/lib/k0s/kiosk/nginx.conf - - - - /usr/share/k0s/kiosk/nginx.conf" in tmpfiles + assert "C+ /var/lib/k0s/kiosk/kiosk-gate.js - - - - /usr/share/k0s/kiosk/kiosk-gate.js" in tmpfiles + assert "C+ /var/lib/k0s/kiosk/kiosk-gate.css - - - - /usr/share/k0s/kiosk/kiosk-gate.css" in tmpfiles + + +def test_tmpfiles_never_wipes_the_generated_tls_material() -> None: + """A directory-level `C+ /var/lib/k0s/kiosk` would delete-then-recopy the + whole tree from /usr/share/k0s/kiosk on every boot, which never contains + cert.pem/key.pem -- wiping the TLS material k0s-kiosk-tls.service + generates into that same directory. Regression guard for that collision. + """ + directives = [ + line for line in TMPFILES.read_text(encoding="utf-8").splitlines() + if line.strip() and not line.strip().startswith("#") + ] + + assert not any("cert.pem" in line or "key.pem" in line for line in directives) + # No directive targets the bare directory (as opposed to a file inside it). + for line in directives: + if line.startswith("C+"): + assert line.split()[1] != "/var/lib/k0s/kiosk", ( + f"whole-directory C+ rule would delete cert.pem/key.pem: {line!r}" + ) def test_proxy_injects_only_csp_safe_same_origin_assets() -> None: @@ -107,3 +138,198 @@ def test_proxy_is_the_only_public_console_endpoint() -> None: "nginx@sha256:62223d644fa234c3a1cc785ee14242ec47a77364226f1c811d2f669f96dc2ac8" in proxy ) + + +def test_k0s_kiosk_tls_service_contract() -> None: + assert KIOSK_TLS_SERVICE.is_file(), "k0s-kiosk-tls.service is missing" + content = KIOSK_TLS_SERVICE.read_text(encoding="utf-8") + + assert "Type=oneshot" in content + assert "Before=k0scontroller.service" in content + assert "RequiresMountsFor=/var/lib/k0s" in content + assert "After=network-online.target systemd-tmpfiles-setup.service" in content + assert "StateDirectory=k0s" in content + assert "ExecStart=/usr/share/k0s/kiosk-tls/generate-kiosk-tls.sh" in content + + +def test_generate_kiosk_tls_script_contract() -> None: + assert KIOSK_TLS_SCRIPT.is_file(), "generate-kiosk-tls.sh is missing" + script = KIOSK_TLS_SCRIPT.read_text(encoding="utf-8") + + # key.pem must never be world-readable, even between creation and chmod. + assert "umask 077" in script + assert 'chmod 0600 "$key"' in script + assert 'chmod 0644 "$cert"' in script + assert "/CN=KubeStellar Console/OU=host-generated" in script + assert "DNS:localhost,DNS:*.local,IP:127.0.0.1" in script + assert "ip -o addr show scope global" in script + # Known-compromised build-time SAN (private key published in old releases). + assert "IPAddress:10.0.2.15" in script + + +def test_k0scontroller_orders_after_kiosk_tls() -> None: + assert K0S_CONTROLLER_SERVICE.is_file(), "k0scontroller.service is missing" + content = K0S_CONTROLLER_SERVICE.read_text(encoding="utf-8") + + assert "k0s-kiosk-tls.service" in content + assert "After=network-online.target k0s-kiosk-tls.service" in content + assert "Wants=network-online.target k0s-kiosk-tls.service" in content + + +def test_os_stack_includes_openssl() -> None: + content = OS_STACK.read_text(encoding="utf-8") + assert "freedesktop-sdk.bst:components/openssl.bst" in content + + +def _run_kiosk_tls_script(tmp_path: Path, state_dir: Path) -> tuple[int, str, str]: + """Run the shipped generator script against a fake root with stubbed tools. + + The openssl stub logs ``req`` invocations to a file and answers ``x509`` + inspection queries from ``subject=``/``san=`` lines the stub itself wrote + into the fake cert (tests may also pre-write such certs to simulate + existing material). + """ + import os + import subprocess + + bin_dir = tmp_path / "bin" + bin_dir.mkdir(exist_ok=True) + log = tmp_path / "openssl.args" + (bin_dir / "hostname").write_text("#!/bin/sh\necho testhost\n", encoding="utf-8") + (bin_dir / "ip").write_text( + "#!/bin/sh\n" + "printf '%s\\n' " + "'2: eth0 inet 192.0.2.10/24 brd 192.0.2.255 scope global dynamic eth0\\ valid_lft 3181sec preferred_lft 3181sec' " + "'2: eth0 inet6 2001:db8::10/64 scope global dynamic mngtmpaddr\\ valid_lft forever preferred_lft forever'\n", + encoding="utf-8", + ) + (bin_dir / "openssl").write_text( + "#!/bin/sh\n" + 'if [ "$1" = x509 ]; then\n' + " in=; prev=\n" + ' for a in "$@"; do [ "$prev" = -in ] && in=$a; prev=$a; done\n' + ' case " $* " in\n' + ' *" -subject "*) grep "^subject=" "$in" || true;;\n' + ' *" -ext "*)\n' + ' echo "X509v3 Subject Alternative Name:"\n' + ' grep "^san=" "$in" | sed "s/^san=/ /" || true;;\n' + " esac\n" + " exit 0\n" + "fi\n" + f"printf '%s\\n' \"$@\" > {log}\n" + "out=; key=; subj=; san=\n" + "while [ $# -gt 0 ]; do\n" + " case $1 in\n" + " -out) out=$2; shift;; -keyout) key=$2; shift;;\n" + " -subj) subj=$2; shift;; -addext) san=$2; shift;;\n" + " esac\n" + " shift\n" + "done\n" + '{ echo cert; echo "subject=$subj"; echo "san=${san#subjectAltName=}"; } > "$out"\n' + 'echo key > "$key"\n', + encoding="utf-8", + ) + for stub in ("hostname", "ip", "openssl"): + (bin_dir / stub).chmod(0o755) + + env = { + **os.environ, + "PATH": f"{bin_dir}:{os.environ['PATH']}", + "KIOSK_TLS_DIR": str(state_dir / "kiosk"), + } + proc = subprocess.run( + ["bash", str(KIOSK_TLS_SCRIPT)], + env=env, + capture_output=True, + text=True, + check=False, + ) + args = log.read_text(encoding="utf-8") if log.exists() else "" + return proc.returncode, args, proc.stderr + + +def test_k0s_kiosk_tls_script_puts_host_addresses_in_the_san(tmp_path: Path) -> None: + state_dir = tmp_path / "var" / "lib" / "k0s" + rc, openssl_args, _ = _run_kiosk_tls_script(tmp_path, state_dir) + + assert rc == 0, openssl_args + san = next( + arg for arg in openssl_args.splitlines() if arg.startswith("subjectAltName=") + ) + assert san == ( + "subjectAltName=DNS:localhost,DNS:*.local,IP:127.0.0.1," + "DNS:testhost,IP:192.0.2.10,IP:2001:db8::10" + ) + assert (state_dir / "kiosk" / "key.pem").read_text(encoding="utf-8") == "key\n" + cert = (state_dir / "kiosk" / "cert.pem").read_text(encoding="utf-8") + assert cert.startswith("cert\n") + assert "subject=/CN=KubeStellar Console/OU=host-generated" in cert + assert (state_dir / "kiosk" / "key.pem").stat().st_mode & 0o777 == 0o600 + assert (state_dir / "kiosk" / "cert.pem").stat().st_mode & 0o777 == 0o644 + + +def test_k0s_kiosk_tls_script_keeps_existing_host_material(tmp_path: Path) -> None: + state_dir = tmp_path / "var" / "lib" / "k0s" + kiosk = state_dir / "kiosk" + kiosk.mkdir(parents=True) + (kiosk / "key.pem").write_text("existing key\n", encoding="utf-8") + (kiosk / "cert.pem").write_text( + "existing cert\n" + "subject=/CN=KubeStellar Console/OU=host-generated\n" + "san=DNS:localhost,DNS:*.local,IPAddress:127.0.0.1,IPAddress:192.0.2.10\n", + encoding="utf-8", + ) + + rc, openssl_args, _ = _run_kiosk_tls_script(tmp_path, state_dir) + + assert rc == 0 + assert openssl_args == "", "openssl req must not run when key and cert already exist" + assert (kiosk / "key.pem").read_text(encoding="utf-8") == "existing key\n" + + +def test_k0s_kiosk_tls_script_keeps_operator_provided_material(tmp_path: Path) -> None: + """Certs without the marker are kept unless they match the leaked SAN.""" + state_dir = tmp_path / "var" / "lib" / "k0s" + kiosk = state_dir / "kiosk" + kiosk.mkdir(parents=True) + (kiosk / "key.pem").write_text("operator key\n", encoding="utf-8") + (kiosk / "cert.pem").write_text( + "operator cert\n" + "subject=/CN=console.example.org\n" + "san=DNS:console.example.org\n", + encoding="utf-8", + ) + + rc, openssl_args, _ = _run_kiosk_tls_script(tmp_path, state_dir) + + assert rc == 0 + assert openssl_args == "" + assert (kiosk / "key.pem").read_text(encoding="utf-8") == "operator key\n" + + +def test_k0s_kiosk_tls_script_replaces_leaked_build_time_material( + tmp_path: Path, +) -> None: + """Old releases shipped cert.pem/key.pem generated at build time inside the + public sysext image, so that private key is compromised. Upgraded hosts + still have it in /var/lib/k0s/kiosk; the script must detect the build-time + SAN (no host-generated marker) and regenerate in place. + """ + state_dir = tmp_path / "var" / "lib" / "k0s" + kiosk = state_dir / "kiosk" + kiosk.mkdir(parents=True) + (kiosk / "key.pem").write_text("leaked key\n", encoding="utf-8") + (kiosk / "cert.pem").write_text( + "leaked cert\n" + "subject=/CN=KubeStellar Console\n" + "san=DNS:localhost,DNS:*.local,IPAddress:127.0.0.1,IPAddress:10.0.2.15\n", + encoding="utf-8", + ) + + rc, openssl_args, stderr = _run_kiosk_tls_script(tmp_path, state_dir) + + assert rc == 0, stderr + assert "subjectAltName=" in openssl_args, "openssl req must regenerate the pair" + assert "compromised build-time certificate" in stderr + assert (kiosk / "key.pem").read_text(encoding="utf-8") == "key\n" + assert "host-generated" in (kiosk / "cert.pem").read_text(encoding="utf-8")