From 457aea86cabefce0a766c8b42b0ab2dfcfddc58c Mon Sep 17 00:00:00 2001 From: mnovak Date: Sun, 8 Nov 2026 12:04:14 +0100 Subject: [PATCH] [repo] NixOS and docs: ianitor-caddy-guard with caddy-fallback, caddy-revert after every control stop, admin socket only for control, Caddy trial deadline in the engine-switch VM test, trusted proxies stay off beside Caddy --- deploy/nixos/module.nix | 33 +++++++++++++++++++++++--- deploy/nixos/tests/default.nix | 21 ++++++++++++++++ docs/api.md | 24 ++++++++++++++----- docs/deploy/beside-caddy.md | 8 +++++++ docs/deploy/configuration-server.patch | 4 +--- docs/ui-api.md | 3 ++- 6 files changed, 80 insertions(+), 13 deletions(-) diff --git a/deploy/nixos/module.nix b/deploy/nixos/module.nix index 282defc..34699d1 100644 --- a/deploy/nixos/module.nix +++ b/deploy/nixos/module.nix @@ -151,7 +151,7 @@ in type = types.listOf types.str; default = [ ]; example = [ "127.0.0.1" ]; - description = "Proxies in front of edge whose X-Forwarded-For names the client (beside Caddy: 127.0.0.1), for the share-link throttle and the access log. Off by default."; + description = "Proxies in front of edge whose X-Forwarded-For names the client (beside Caddy: 127.0.0.1), for the share-link throttle and the access log. Off by default: on a loopback listener any local process could then claim any address (docs/deploy/beside-caddy.md)."; }; }; @@ -252,7 +252,9 @@ in }) userCreds; users.groups.ianitor = { }; - users.users.ianitor-control = { isSystemUser = true; group = "ianitor"; home = "${stateRoot}/control"; }; + # Only control (and Caddy itself) may reach Caddy's admin socket; edge is not in it. + users.groups.ianitor-caddy-admin = { }; + users.users.ianitor-control = { isSystemUser = true; group = "ianitor"; extraGroups = [ "ianitor-caddy-admin" ]; home = "${stateRoot}/control"; }; users.users.ianitor-edge = { isSystemUser = true; group = "ianitor"; }; users.users.caddy-fallback = { isSystemUser = true; group = "ianitor"; }; @@ -300,6 +302,10 @@ in User = "ianitor-control"; Group = "ianitor"; ExecStart = "${pkg}/bin/ianitor-control --config ${controlToml}"; + # After every stop, a crash included: a trial still loaded into Caddy goes back to the + # last good caddy.json at once (ianitor_control::caddy_guard). It runs after a crash + # too, so no separate OnFailure= unit is needed. + ExecStopPost = "${pkg}/bin/ianitor-control caddy-revert --config ${controlToml}"; LoadCredential = loadCreds controlCreds ++ [ "ui-proof:${runProof}/ui-proof" ]; ReadWritePaths = [ stateRoot ]; Restart = "on-failure"; @@ -352,7 +358,10 @@ in }; serviceConfig = hardening // { User = "caddy-fallback"; - Group = "ianitor"; + # The admin socket (and its runtime directory) belong to this group; "ianitor" for the + # certificates and logs. + Group = "ianitor-caddy-admin"; + SupplementaryGroups = [ "ianitor" ]; ExecStart = "${cfg.caddyPackage}/bin/caddy run --config ${stateRoot}/caddy/caddy.json"; LoadCredential = loadCreds cfg.credentials ++ [ "ui-proof:${runProof}/ui-proof" ]; RuntimeDirectory = "caddy-fallback"; @@ -367,6 +376,24 @@ in }; }; + # The deadline of a trial under Caddy, outside control (a hung control included): once a + # second it reverts a trial past its deadline to the last good caddy.json. Runs with Caddy. + systemd.services.ianitor-caddy-guard = { + description = "Ianitor: deadline for changes tried on Caddy"; + partOf = [ "caddy-fallback.service" ]; + after = [ "caddy-fallback.service" ]; + wantedBy = [ "caddy-fallback.service" ]; + environment.IANITOR_LOG = "info"; + serviceConfig = hardening // { + User = "ianitor-control"; + Group = "ianitor"; + ExecStart = "${pkg}/bin/ianitor-control caddy-guard --config ${controlToml}"; + ReadWritePaths = [ "${stateRoot}/control" ]; + Restart = "always"; + RestartSec = 1; + }; + }; + # At boot: start the engine the UI chose last (ROADMAP §3). systemd.services.ianitor-engine-select = { description = "Ianitor: start the chosen engine"; diff --git a/deploy/nixos/tests/default.nix b/deploy/nixos/tests/default.nix index 52840f5..f239d08 100644 --- a/deploy/nixos/tests/default.nix +++ b/deploy/nixos/tests/default.nix @@ -157,6 +157,27 @@ in 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") + # The trial's deadline under Caddy does not depend on control (fix round 2, B2): the guard + # runs with Caddy, only control may use Caddy's admin socket, and a control killed during + # the watch leaves Caddy on the last good config (ExecStopPost=caddy-revert). + server.succeed("systemctl is-active ianitor-caddy-guard.service") + server.fail("runuser -u ianitor-edge -- test -w /run/caddy-fallback/admin.sock") + server.succeed("runuser -u ianitor-control -- test -w /run/caddy-fallback/admin.sock") + r = api(server, t, "POST", "/hosts", {"host": { + "hostname": "three.test", "upstreams": ["http://127.0.0.1:8000"], "tls": "domi", "auth": "none"}}) + three = r["apply"]["id"] + for _ in range(60): + if api(server, t, "GET", f"/applies/{three}").get("state") == "watching": + break + time.sleep(0.5) + assert visit(server, "three.test") == "200", "the trial serves" + server.succeed("systemctl kill --signal=KILL ianitor-control.service") + server.wait_until_fails("curl -s -o /dev/null -w '%{http_code}' --cacert ${testRoot}/domi-root.crt --resolve three.test:443:127.0.0.1 https://three.test/ | grep -q 200", timeout=10) + server.wait_for_unit("ianitor-control.service") + server.wait_until_succeeds(f"curl -sf -H 'Authorization: Bearer {t}' http://127.0.0.1:7878/api/v1/overview") + assert wait_apply(server, t, three)["state"] == "reverted" + assert visit(server, "two.test") == "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/docs/api.md b/docs/api.md index a0344d1..00f72f4 100644 --- a/docs/api.md +++ b/docs/api.md @@ -316,15 +316,27 @@ Refusals: `engine.unmanaged` (no `[engine]` in control.toml), `engine.same`, `en 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; + socket `[compile_caddy] caddy_admin_socket`, `/run/caddy-fallback/admin.sock` under NixOS, + group `ianitor-caddy-admin`: control and Caddy, not edge), and a revert loads the previous + `caddy.json` again. Every load sends `Cache-Control: must-revalidate`, so Caddy reads its + certificate and device files again even when the config is unchanged. The watch judges health + as edge does: control probes the upstreams itself with edge's rules (health check: `GET `, + Host = the upstream, 2xx; none: `GET /`, Host = the hostname, below 500), and a probe *through* + Caddy only asks that Caddy answers at all (any status, its sign-in redirect included). Each + round also checks that Caddy still runs the trial (`GET /config/`); if anything else reloaded it, + the change is undone as `apply.reverted.edge_lost`. Without `caddy_admin_socket` an apply under + Caddy fails with `apply.failed.reload` and nothing changes; +- **an unconfirmed trial never outlives control**, as with edge: control arms a deadline + (`caddy-trial.json` in its state directory: watch + margin) before loading the trial and claims + it before confirming. `ianitor-caddy-guard` (a unit that runs with caddy-fallback, + `ianitor-control caddy-guard`) reverts a trial past its deadline, which covers a hung control; + ianitor-control's `ExecStopPost=ianitor-control caddy-revert` reverts at once after any stop or + crash. Whoever renames the deadline file first wins, so a trial is either kept or reverted; +- device issues and revocations wait for a running apply, then reload Caddy and check that it + serves the certificate on disk for a Domi host (else `devices_problem`, retried); - **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 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 diff --git a/docs/deploy/beside-caddy.md b/docs/deploy/beside-caddy.md index 9edfec6..1aa6e03 100644 --- a/docs/deploy/beside-caddy.md +++ b/docs/deploy/beside-caddy.md @@ -32,6 +32,14 @@ visitor ── Caddy :443 (as today) ── https://127.0.0.1:18443 Ianitor edg - Caddy keeps the public side for the two names and hands them to Ianitor over TLS; Ianitor serves them with Domi certificates, which Caddy checks against the Domi root (a public file). - Every other host is untouched. Rollback: revert the patch and switch (below). +- Visitor addresses: Ianitor sees every visit coming from 127.0.0.1 (Caddy), so the share-link + throttle counts all visitors together and the visit log shows 127.0.0.0. The option + `services.ianitor.edge.trustedProxies = [ "127.0.0.1" ]` would make edge believe Caddy's + `X-Forwarded-For` instead, but edge listens on a TCP port on loopback that **any local user or + process can reach**, and any of them could then claim any address. It stays **off** for the + trial (the two hosts have no share links and no visit log); turn it on only on a machine where + every local account is trusted. (A Unix-socket listener for edge, which Caddy alone could reach, + is the proper fix; not built yet.) ## The patch diff --git a/docs/deploy/configuration-server.patch b/docs/deploy/configuration-server.patch index 5dc3eb3..a003568 100644 --- a/docs/deploy/configuration-server.patch +++ b/docs/deploy/configuration-server.patch @@ -67,7 +67,7 @@ new file mode 100644 index 0000000..cd23016 --- /dev/null +++ b/hyperv-infra/modules/ianitor.nix -@@ -0,0 +1,51 @@ +@@ -0,0 +1,49 @@ +{ config, lib, inputs, ... }: + +# Ianitor beside Caddy (Ianitor ROADMAP §1.12, Phase 3): two low-risk hosts, flowers and esp32, @@ -100,8 +100,6 @@ index 0000000..cd23016 + httpListen = "127.0.0.1:18080"; + httpsListen = "127.0.0.1:18443"; + openFirewall = false; -+ # Caddy in front, on loopback: its X-Forwarded-For names the visitor (throttle, logs). -+ trustedProxies = [ "127.0.0.1" ]; + }; + control.listen = "127.0.0.1:7878"; + shareLinkKeyFile = config.sops.secrets."ianitor/share-link-key".path; diff --git a/docs/ui-api.md b/docs/ui-api.md index b629129..0d1cb08 100644 --- a/docs/ui-api.md +++ b/docs/ui-api.md @@ -545,4 +545,5 @@ 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.*`) next to `paused {code: "control.paused"}`. + `control.files_unwritten`, `repo.*`) next to `paused {code: "control.paused"}`, and + `overview.paused_detail` the technical detail behind it (for "Show details").