From bc336d1188bd1a14c5a6f09efdf75bb3d4083b60 Mon Sep 17 00:00:00 2001 From: mnovak Date: Sun, 8 Nov 2026 10:49:42 +0100 Subject: [PATCH] [repo] NixOS and docs: Caddy admin socket for changes under Caddy, polkit start/stop only, edge never stops restarting, openssl in the VM tests, beside-Caddy without secrets in argv, pinned Ianitor rev, X-Served-By trial marker --- deploy/nixos/module.nix | 15 +++++++-- deploy/nixos/tests/default.nix | 8 ++++- deploy/test-access.sh | 15 +++++---- docs/api.md | 26 +++++++++++---- docs/deploy/beside-caddy.md | 46 +++++++++++++++++--------- docs/deploy/configuration-server.patch | 8 ++--- docs/ui-api.md | 6 ++-- 7 files changed, 86 insertions(+), 38 deletions(-) diff --git a/deploy/nixos/module.nix b/deploy/nixos/module.nix index 8812af5..dbfe896 100644 --- a/deploy/nixos/module.nix +++ b/deploy/nixos/module.nix @@ -4,7 +4,9 @@ # services.ianitor: ianitor-edge (the only public listener), ianitor-control (loopback), # caddy-fallback (the same routes as Caddy JSON), the engine-select unit, the per-boot UI proof, -# and a polkit rule that lets control start, stop and restart exactly the two engine units. +# and a polkit rule that lets control start and stop exactly the two engine units. While Caddy +# serves, control loads changes into it through its admin socket (/run/caddy-fallback/admin.sock, +# group ianitor, 0660). # # Secrets never enter the Nix store: every credential is a *path string* to a file outside the # store (sops-nix, agenix, Custos provisioning, ...), loaded with systemd LoadCredential; an @@ -66,6 +68,8 @@ let compile_caddy = compile // { state_dir = "${stateRoot}/caddy-state"; access_log = "/var/log/ianitor/caddy-access.log"; + # Changes keep working under Caddy: control loads them through this socket. + caddy_admin_socket = "/run/caddy-fallback/admin.sock"; }; ui = { @@ -298,6 +302,8 @@ in systemd.services.ianitor-edge = { description = "Ianitor edge (Pingora)"; + # Never give up restarting the only public listener. + startLimitIntervalSec = 0; after = [ "network-online.target" "ianitor-control.service" "ianitor-ui-proof.service" ]; wants = [ "network-online.target" ]; requires = [ "ianitor-ui-proof.service" ]; @@ -342,6 +348,8 @@ in Group = "ianitor"; ExecStart = "${cfg.caddyPackage}/bin/caddy run --config ${stateRoot}/caddy/caddy.json"; LoadCredential = loadCreds cfg.credentials ++ [ "ui-proof:${runProof}/ui-proof" ]; + RuntimeDirectory = "caddy-fallback"; + RuntimeDirectoryMode = "0750"; ReadWritePaths = [ "${stateRoot}/caddy-state" "/var/log/ianitor" ]; AmbientCapabilities = [ "CAP_NET_BIND_SERVICE" ]; CapabilityBoundingSet = [ "CAP_NET_BIND_SERVICE" ]; @@ -370,7 +378,8 @@ in ''; }; - # Control may start, stop and restart exactly the two engine units, nothing else. + # Control may start and stop exactly the two engine units, nothing else. (polkit is new on a + # server that had no other use for it.) security.polkit.enable = true; security.polkit.extraConfig = '' polkit.addRule(function (action, subject) { @@ -378,7 +387,7 @@ in var unit = action.lookup("unit"); var verb = action.lookup("verb"); if ((unit == "ianitor-edge.service" || unit == "caddy-fallback.service") && - (verb == "start" || verb == "stop" || verb == "restart")) { + (verb == "start" || verb == "stop")) { return polkit.Result.YES; } } diff --git a/deploy/nixos/tests/default.nix b/deploy/nixos/tests/default.nix index 6e89f9d..52840f5 100644 --- a/deploy/nixos/tests/default.nix +++ b/deploy/nixos/tests/default.nix @@ -51,7 +51,7 @@ let wantedBy = [ "multi-user.target" ]; serviceConfig.ExecStart = "${pkgs.python3}/bin/python3 -m http.server 8000 --bind 127.0.0.1 --directory /etc"; }; - environment.systemPackages = [ pkgs.curl pkgs.jq ]; + environment.systemPackages = [ pkgs.curl pkgs.jq pkgs.openssl ]; }; # Python helpers shared by the test scripts. @@ -151,6 +151,12 @@ in assert server.succeed("cat /var/lib/ianitor/engine").strip() == "caddy" server.wait_until_succeeds("curl -s -o /dev/null -w '%{http_code}' --cacert ${testRoot}/domi-root.crt --resolve app.test:443:127.0.0.1 https://app.test/ | grep -q 200") + # Changes keep working while Caddy serves: loaded through its admin socket and watched. + r = api(server, t, "POST", "/hosts", {"host": { + "hostname": "two.test", "upstreams": ["http://127.0.0.1:8000"], "tls": "domi", "auth": "none"}}) + assert wait_apply(server, t, r["apply"]["id"])["state"] == "kept" + server.succeed("curl -s -o /dev/null -w '%{http_code}' --cacert ${testRoot}/domi-root.crt --resolve two.test:443:127.0.0.1 https://two.test/ | grep -q 200") + api(server, t, "POST", "/engine", {"target": "pingora"}) server.wait_until_succeeds( f"curl -s -H 'Authorization: Bearer {t}' http://127.0.0.1:7878/api/v1/engine | jq -e '.running == \"pingora\" and .state == \"running\"'" diff --git a/deploy/test-access.sh b/deploy/test-access.sh index d9fa4a5..a12e4fb 100755 --- a/deploy/test-access.sh +++ b/deploy/test-access.sh @@ -15,8 +15,9 @@ # --public HOST must answer 2xx or 3xx (not a sign-in redirect) # --protected HOST without a session, must redirect to --auth-host (302/303) or answer 401 # --auth-host HOST Authelia's portal name, for --protected -# --via-ianitor HOST must answer 2xx/3xx, and the answer must not come from Caddy alone -# (Ianitor adds no Via; Caddy adds "Via: 1.1 Caddy" once, so exactly one) +# --via-ianitor HOST must answer 2xx/3xx with "X-Served-By: ianitor": the trial hosts set that +# response header in their Ianitor route (configuration-server.patch), so +# an answer Caddy gives on its own fails # --control-port N control's port (default 7878): must NOT answer from outside # --insecure accept a certificate the client does not trust (staging: tls internal) # @@ -57,7 +58,7 @@ failures=0 pass() { printf 'ok %s\n' "$1"; } fail() { printf 'FAIL %s\n' "$1"; failures=$((failures + 1)); } -# status, location and the number of Via headers for https://HOST/ sent to the server +# the response head of https://HOST/ sent to the server probe() { curl -sS -o /dev/null -D - --max-time 15 "${insecure[@]}" \ --resolve "$1:443:$server" "https://$1/" 2>/dev/null | tr -d '\r' @@ -88,9 +89,11 @@ done for h in "${via[@]}"; do head=$(probe "$h") code=$(printf '%s\n' "$head" | awk 'NR==1 {print $2}') - vias=$(printf '%s\n' "$head" | grep -ci '^via:' || true) - if [[ "$code" =~ ^[23][0-9][0-9]$ ]]; then - pass "through Ianitor $h answers $code (Via headers: $vias)" + served=$(printf '%s\n' "$head" | awk 'tolower($1)=="x-served-by:" {print tolower($2)}') + if [[ "$code" =~ ^[23][0-9][0-9]$ ]] && [ "$served" = "ianitor" ]; then + pass "through Ianitor $h answers $code (X-Served-By: ianitor)" + elif [[ "$code" =~ ^[23][0-9][0-9]$ ]]; then + fail "$h answers $code, but not through Ianitor (no X-Served-By: ianitor)" else fail "through Ianitor $h answers '${code:-nothing}'" fi diff --git a/docs/api.md b/docs/api.md index 2b08a93..cf7faca 100644 --- a/docs/api.md +++ b/docs/api.md @@ -305,18 +305,32 @@ curl -s -H "$H" -H 'Content-Type: application/json' -X POST "$API/engine" -d '{" ``` Control writes `/var/lib/ianitor/engine` first, stops the serving unit, starts the other one (polkit -allows exactly `ianitor-edge.service` and `caddy-fallback.service`, verbs start/stop/restart) and +allows exactly `ianitor-edge.service` and `caddy-fallback.service`, verbs start and stop) and waits until it serves; if it does not within 20 s, it starts the old one again and reports -`engine.switch_failed`. The switch waits for a running apply (same lock). Refusals: -`engine.unmanaged` (no `[engine]` in control.toml), `engine.same`, `engine.switching`, +`engine.switch_failed`. The switch waits for a running apply (same lock, first come first +served: a queued switch runs right after the apply in progress, before any apply queued after +it). Back on Pingora, edge is reloaded so it serves what was applied while Caddy served. +Refusals: `engine.unmanaged` (no `[engine]` in control.toml), `engine.same`, `engine.switching`, `engine.not_ready` (Caddy's settings cannot be built from the current routes). -While Caddy serves (decisions Q3, Q4): -- changes are paused: `overview.paused_reason = "engine.caddy"`, writes get `409 control.paused`; +While Caddy serves (decisions Q3, Q4; Mark 2026-11-08): +- **changes keep working**: applies, Undo and Restore go through the same pipeline. Both outputs + are compiled; the trial is loaded into Caddy through its admin API (`POST /load` on the Unix + socket `[compile_caddy] caddy_admin_socket`, `/run/caddy-fallback/admin.sock` under NixOS), the + touched hosts are probed *through Caddy* for the watch window, and a revert loads the previous + `caddy.json` again. Without `caddy_admin_socket` an apply under Caddy fails with + `apply.failed.reload` and nothing changes; - **share links are off**: minting is `409 share.engine_caddy`, and a visitor with a link gets the normal sign-in (fails closed). Caddy cannot check a share token and control never serves visitors; a validator Caddy calls through `forward_auth` is planned before v1.1; -- device revocations restart caddy-fallback (Caddy reads the allow list when it starts). +- device issues and revocations reload Caddy through the same socket (Caddy reads the allow list + when a config is loaded). + +A revocation is in force only once the serving engine has the new list. If the list cannot be +written or the engine does not take it, `DELETE /certs/devices/{id}` answers `503 +pki.devices_unwritten` (the revocation itself is recorded), `GET /certs` carries +`devices_problem` until it is fixed, and control retries every 2 s; repeating the DELETE is +safe and nudges the engine again. Edge also re-reads the device files by itself every 5 s. ## All calls diff --git a/docs/deploy/beside-caddy.md b/docs/deploy/beside-caddy.md index 0104ef1..9edfec6 100644 --- a/docs/deploy/beside-caddy.md +++ b/docs/deploy/beside-caddy.md @@ -11,7 +11,12 @@ cut-over. This is **prepared, not applied**: Mark runs every command below himse snapshot. - Caddy is the only listener on 80/443 and the only service beyond loopback (decision D-21a); every backend binds 127.0.0.1. Secrets come from sops-nix (`/run/secrets/...`). -- There is **no Authelia** in the repository; hosts rely on their applications' own logins. +- Authelia is not in the repository: production runs it, configured outside Configuration_Server + (Mark, 2026-11-08). Ianitor keeps forward-auth; the admin UI's host must sit behind it, and + control refuses a ui-proof header rule on any other host. The two trial hosts use no sign-in + (their applications have none today). +- The module turns on `security.polkit` (control may start and stop exactly `ianitor-edge` and + `caddy-fallback`). The server had no other use for polkit, so that service is new there. - Lowest-risk hosts: `flowers.stage.marek-novak.cz` (a static presentation app, nginx on 127.0.0.1:8091) and `esp32.stage.marek-novak.cz` (a static web IDE, 127.0.0.1:8093, used only by Mark). Both are on the staging mirror, "the one machine safe to break". @@ -51,12 +56,16 @@ In Configuration_Server, on a branch, per the repository's workflow: git switch -c feature/ianitor-beside-caddy git apply /path/to/Ianitor/docs/deploy/configuration-server.patch mkdir -p hyperv-infra/ianitor && cp /path/to/domi-root.crt hyperv-infra/ianitor/domi-root.crt -# Two new secrets (values generated, never typed into a file in the repository): +# Two new secrets, generated and piped (never in a command line, a file in the repository or +# the shell history). Needs `sops set --value-stdin`; with an older sops, use `sops edit`: cd hyperv-infra -sops set secrets/secrets.yaml '["ianitor"]["share-link-key"]' "\"$(head -c 32 /dev/urandom | base64 -w0)\"" -sops set secrets/secrets.yaml '["ianitor"]["pki-password"]' "\"$(head -c 32 /dev/urandom | base64 -w0)\"" -# Pin Ianitor to the reviewed commit: -nix flake lock --override-input ianitor "git+https://gitea.marek-novak.cz/mnovak/Ianitor.git?rev=" +for k in share-link-key pki-password; do + { printf '"'; head -c 32 /dev/urandom | base64 -w0; printf '"'; } | + sops set --value-stdin secrets/secrets.yaml "[\"ianitor\"][\"$k\"]" +done +# Pin Ianitor to the reviewed commit (the patch carries MERGED_COMMIT as a placeholder): +sed -i "s/MERGED_COMMIT//" flake.nix +nix flake lock --update-input ianitor nix flake check --no-build git add -A && git commit ``` @@ -73,9 +82,11 @@ On the mirror, the first intermediate and the two hosts: ```sh sudo -u ianitor-control ianitor-control token create --name mark-laptop --config /etc/ianitor/control.toml -# -> save the token in Custos; then, from the laptop: -ssh -L 7878:127.0.0.1:7878 mirror -TOKEN=ian_...; API=http://127.0.0.1:7878/api/v1; H="Authorization: Bearer $TOKEN" +# -> save the token in Custos; then, from the laptop. The token goes into a header file that only +# you can read, so it is never in a command line (ps) or the shell history: +ssh -fN -L 7878:127.0.0.1:7878 mirror +H=@$HOME/.ianitor-header; API=http://127.0.0.1:7878/api/v1 +( umask 077; IFS= read -rs t; printf 'Authorization: Bearer %s\n' "$t" > ~/.ianitor-header ) # paste the token curl -s -H "$H" -X POST "$API/certs/intermediate/renewal" | jq -r .csr_pem > req.csr curl -s -H "$H" "$API/pki-requests/$(curl -s -H "$H" $API/overview | jq -r .intermediate.renewal.request_id)/ext" > req.ext # offline: openssl x509 -req -in req.csr -CA domi-root.crt -CAkey domi-root.key -days 365 -sha256 -extfile req.ext -out int.crt @@ -86,10 +97,13 @@ for h in flowers esp32; do done # each is kept or reverted within 60 s; GET $API/applies/ ``` -(The web UI needs the admin host behind Authelia, which this repository does not have. For the -trial, run it locally against the tunnel: `IANITOR_CONTROL=http://127.0.0.1:7878 -IANITOR_DEV_TOKEN=$TOKEN pnpm --filter web dev`; the dev proxy adds the token, the browser never -holds it.) +(The web UI needs the admin host behind Authelia; for the trial, run it locally against the +tunnel. The token goes in through the environment, never the command line, and the dev proxy +adds it, so the browser never holds it: + + IANITOR_CONTROL=http://127.0.0.1:7878 \ + IANITOR_DEV_TOKEN="$(sed 's/^Authorization: Bearer //' ~/.ianitor-header)" pnpm --filter web dev +) Then, from the desktop (192.168.100.140): @@ -99,8 +113,10 @@ deploy/test-access.sh --server 192.168.100.166 --insecure \ --via-ianitor flowers.stage.marek-novak.cz --via-ianitor esp32.stage.marek-novak.cz ``` -`--insecure` because the stage names use Caddy's `tls internal` (or the stage wildcard). It must -print `all checks passed`; the two-week run (§1.12) starts then. +`--insecure` because the stage names use Caddy's `tls internal` (or the stage wildcard). Each +`--via-ianitor` host must answer with `X-Served-By: ianitor` (set by its route in the patch), so +an answer from Caddy alone fails. It must print `all checks passed`; the two-week run (§1.12) +starts then. Rollback at any time: `git revert` the commit, `Deploy-Staging.ps1 -Mode Switch`. Ianitor's state stays in /var/lib/ianitor and is ignored. diff --git a/docs/deploy/configuration-server.patch b/docs/deploy/configuration-server.patch index 0350ceb..a003568 100644 --- a/docs/deploy/configuration-server.patch +++ b/docs/deploy/configuration-server.patch @@ -9,8 +9,8 @@ index 0d30b91..4302dc7 100644 + + # Ianitor (the Pingora reverse proxy), beside Caddy on two low-risk hosts. Its packages are + # built from its own pinned nixpkgs; only the module is evaluated against this one. Pin to a -+ # reviewed commit (rev=...) before switching production. -+ ianitor.url = "git+https://gitea.marek-novak.cz/mnovak/Ianitor.git?ref=main"; ++ # reviewed commit: MERGED_COMMIT is replaced by the merged hash before the first lock. ++ ianitor.url = "git+https://gitea.marek-novak.cz/mnovak/Ianitor.git?ref=main&rev=MERGED_COMMIT"; }; outputs = { self, nixpkgs, sops-nix, nixpkgs-gitea, ... }@inputs: { @@ -113,7 +113,7 @@ index 0000000..cd23016 + # The trial hosts, as Ianitor route files. They are applied through the API after the first + # start (docs/deploy/beside-caddy.md in the Ianitor repo); listed here so the review sees them. + environment.etc."ianitor/trial-hosts.json".text = builtins.toJSON [ -+ { hostname = "flowers.${stage}"; upstreams = [ "http://127.0.0.1:8091" ]; tls = "domi"; auth = "none"; health_check = { path = "/"; interval_secs = 30; }; } -+ { hostname = "esp32.${stage}"; upstreams = [ "http://127.0.0.1:8093" ]; tls = "domi"; auth = "none"; health_check = { path = "/"; interval_secs = 30; }; } ++ { hostname = "flowers.${stage}"; upstreams = [ "http://127.0.0.1:8091" ]; tls = "domi"; auth = "none"; health_check = { path = "/"; interval_secs = 30; }; response_headers = [ { action = "set"; name = "X-Served-By"; value = "ianitor"; } ]; } ++ { hostname = "esp32.${stage}"; upstreams = [ "http://127.0.0.1:8093" ]; tls = "domi"; auth = "none"; health_check = { path = "/"; interval_secs = 30; }; response_headers = [ { action = "set"; name = "X-Served-By"; value = "ianitor"; } ]; } + ]; +} diff --git a/docs/ui-api.md b/docs/ui-api.md index d905cc1..6b76411 100644 --- a/docs/ui-api.md +++ b/docs/ui-api.md @@ -527,8 +527,8 @@ Recorded by the Phase 3 implementation (branch `phase-3`); docs/api.md has the c admin host, as a share link when that host has share links on. - **G18**: `GET/POST /engine` (shape in docs/api.md). Differences from §1.9: no `fallback_ready {compared, matched}` (control cannot run the differential suite at runtime; it reports - `{built_at, current}`), and **changes are paused while Caddy serves** (`paused_reason = - "engine.caddy"`). + `{built_at, current}`). Changes keep working while Caddy serves (Mark 2026-11-08): they are + loaded into Caddy and watched through it; only share links are off. - **G19**: the UI shows "No websockets" only when off, and "Open to anyone" for `auth = none` (Q11). - **PKI** (§1.7, §1.8, §3.6): built as drawn, with two differences while Custos lacks the operations: device requests are approved on the server (`pki approve `, the UI shows the @@ -538,4 +538,4 @@ Recorded by the Phase 3 implementation (branch `phase-3`); docs/api.md has the c 10 minutes. - New host field `device_certs` (bool): only Domi device certificates on the allow list get in. - `overview.paused_reason` gives the underlying code (`apply.needs_attention`, - `control.files_unwritten`, `repo.*`, `engine.caddy`) next to `paused {code: "control.paused"}`. + `control.files_unwritten`, `repo.*`) next to `paused {code: "control.paused"}`.