diff --git a/deploy/nixos/module.nix b/deploy/nixos/module.nix index c10a36a..12af0fb 100644 --- a/deploy/nixos/module.nix +++ b/deploy/nixos/module.nix @@ -25,7 +25,9 @@ let # user can put a socket there while control restarts. uiSocket = "/run/ianitor-control/ui.sock"; # The front proxy's socket (Caddy beside Ianitor): its directory is setgid ianitor-proxy, so - # the socket either engine makes there belongs to that group. + # the socket either engine makes there belongs to that group, and sticky, so a group member + # (the front proxy) cannot unlink or replace it; each engine removes a stale socket as root + # before it starts and after it stops (an engine switch hands the path over). proxyDir = "/run/ianitor-proxy"; proxySocket = "${proxyDir}/edge.sock"; toml = pkgs.formats.toml { }; @@ -168,6 +170,17 @@ in example = [ "caddy" ]; description = "The front proxy's users, added to the ianitor-proxy group so they can connect."; }; + services = mkOption { + type = types.listOf types.str; + default = [ ]; + example = [ "caddy" ]; + description = '' + The front proxy's systemd services, given the ianitor-proxy group in their unit + (SupplementaryGroups). A group added to a user only reaches a running service when it + restarts, and a reload (Caddy's enableReload) keeps the old groups: putting the group in + the unit changes the unit, so the next switch restarts the service. + ''; + }; }; trustedProxies = mkOption { type = types.listOf types.str; @@ -269,7 +282,7 @@ in }; }; - config = mkIf cfg.enable { + config = mkIf cfg.enable (lib.mkMerge [ { assertions = [ { @@ -310,7 +323,7 @@ in "d ${stateRoot}/caddy-state 0700 caddy-fallback ianitor - -" "f ${stateRoot}/engine 0644 ianitor-control ianitor - ${cfg.defaultEngine}" "d /var/log/ianitor 0770 ianitor-edge ianitor - -" - ] ++ lib.optional cfg.edge.proxySocket.enable "d ${proxyDir} 2770 root ianitor-proxy - -"; + ] ++ lib.optional cfg.edge.proxySocket.enable "d ${proxyDir} 3770 root ianitor-proxy - -"; environment.systemPackages = [ pkg ]; # The non-secret config, so the CLI (token, pki) finds it: --config /etc/ianitor/control.toml. @@ -377,6 +390,11 @@ in "--share-revocations ${stateRoot}/edge/share-revocations.json" ]; LoadCredential = loadCreds edgeCreds ++ [ "ui-proof:${runProof}/ui-proof" ]; + SupplementaryGroups = lib.optional cfg.edge.proxySocket.enable "ianitor-proxy"; + # The sticky directory lets only a socket's owner unlink it: clear the other engine's + # (or a crashed run's) as root, before start and after stop. + ExecStartPre = lib.optional cfg.edge.proxySocket.enable "+${pkgs.coreutils}/bin/rm -f ${proxySocket}"; + ExecStopPost = lib.optional cfg.edge.proxySocket.enable "+${pkgs.coreutils}/bin/rm -f ${proxySocket}"; RuntimeDirectory = "ianitor-edge"; RuntimeDirectoryMode = "0750"; ReadWritePaths = [ "${stateRoot}/edge-state" "/var/log/ianitor" ] @@ -407,7 +425,9 @@ in # The admin socket (and its runtime directory) belong to this group; "ianitor" for the # certificates and logs. Group = "ianitor-caddy-admin"; - SupplementaryGroups = [ "ianitor" ]; + SupplementaryGroups = [ "ianitor" ] ++ lib.optional cfg.edge.proxySocket.enable "ianitor-proxy"; + ExecStartPre = lib.optional cfg.edge.proxySocket.enable "+${pkgs.coreutils}/bin/rm -f ${proxySocket}"; + ExecStopPost = lib.optional cfg.edge.proxySocket.enable "+${pkgs.coreutils}/bin/rm -f ${proxySocket}"; ExecStart = "${cfg.caddyPackage}/bin/caddy run --config ${stateRoot}/caddy/caddy.json"; LoadCredential = loadCreds cfg.credentials ++ [ "ui-proof:${runProof}/ui-proof" ]; RuntimeDirectory = "caddy-fallback"; @@ -478,5 +498,14 @@ in networking.firewall.allowedTCPPorts = mkIf cfg.edge.openFirewall ( map (a: lib.toInt (lib.last (lib.splitString ":" a))) [ cfg.edge.httpListen cfg.edge.httpsListen ] ); - }; + } + + # The front proxy's services get the socket's group in their unit (edge.proxySocket.services), + # so the switch that adds it restarts them. + (mkIf cfg.edge.proxySocket.enable { + systemd.services = lib.genAttrs cfg.edge.proxySocket.services (_: { + serviceConfig.SupplementaryGroups = [ "ianitor-proxy" ]; + }); + }) + ]); } diff --git a/deploy/nixos/tests/default.nix b/deploy/nixos/tests/default.nix index c0e06c8..0c71e01 100644 --- a/deploy/nixos/tests/default.nix +++ b/deploy/nixos/tests/default.nix @@ -268,6 +268,10 @@ in assert wait_apply(server, t, r["apply"]["id"])["state"] == "kept" server.wait_until_succeeds("test -S /run/ianitor-proxy/edge.sock") assert server.succeed("stat -c '%a %G' /run/ianitor-proxy/edge.sock").strip() == "660 ianitor-proxy" + # Sticky and setgid: the front proxy may connect but not unlink or replace edge's socket. + assert server.succeed("stat -c '%a %U %G' /run/ianitor-proxy").strip() == "3770 root ianitor-proxy" + server.fail("runuser -u front -- rm -f /run/ianitor-proxy/edge.sock") + server.succeed("test -S /run/ianitor-proxy/edge.sock") code = server.succeed( "runuser -u front -- curl -s -o /dev/null -w '%{http_code}' --unix-socket /run/ianitor-proxy/edge.sock http://app.test/" ).strip() diff --git a/docs/deploy/beside-caddy.md b/docs/deploy/beside-caddy.md index e0949a7..5a0ea1f 100644 --- a/docs/deploy/beside-caddy.md +++ b/docs/deploy/beside-caddy.md @@ -32,6 +32,11 @@ visitor ── Caddy :443 (as today) ── unix:/run/ianitor-proxy/edge.sock whose only members are edge, the Caddy fallback and Caddy (`proxySocket.users = [ "caddy" ]`). Edge serves the HTTPS hosts there in plain HTTP (Caddy terminated TLS) and **believes the X-Forwarded-For Caddy sends**, so the visit log and the share-link throttle see the visitor. + Edge always tells the upstream `X-Forwarded-Proto: https` for requests on the socket, so the + socket is for a front proxy that serves HTTPS only (Caddy redirects plain HTTP itself); a front + proxy that forwarded plain-HTTP requests there would have them reported as HTTPS. + The directory is sticky (3770), so Caddy, a member of the group, can connect but cannot unlink + or replace edge's socket; each engine clears a stale socket as root when it starts and stops. No other local user can connect, so none can claim an address; no loopback TCP port is involved, and `trustedProxies` stays off. The Caddy fallback listens on the same socket (compile option `proxy_socket`, a third Caddy server with `trusted_proxies_unix`), so an engine @@ -88,6 +93,18 @@ scripts/staging/Deploy-Staging.ps1 -Mode Test scripts/staging/Deploy-Staging.ps1 -Mode Switch ``` +After the first switch, make sure Caddy runs with its new group (QC phase 4): a reload, which is what NixOS's +Caddy module does when only its config changes, keeps a process's old groups, and edge's socket then answers +Caddy with EACCES (502 for the two hosts). The patch puts the group into Caddy's unit +(`proxySocket.services = [ "caddy" ]`), which changes the unit, so the switch should restart Caddy; check it, and +restart by hand if not: + +```sh +gid=$(getent group ianitor-proxy | cut -d: -f3) +grep "^Groups:" /proc/$(systemctl show -p MainPID --value caddy)/status | grep -qw "$gid" \ + || sudo systemctl restart caddy +``` + On the mirror, the first intermediate and the two hosts: ```sh diff --git a/docs/deploy/configuration-server.patch b/docs/deploy/configuration-server.patch index bf6c8e1..6738330 100644 --- a/docs/deploy/configuration-server.patch +++ b/docs/deploy/configuration-server.patch @@ -99,7 +99,7 @@ index 0000000..cd23016 + httpsListen = "127.0.0.1:18443"; + openFirewall = false; + # Caddy reaches edge here; only Caddy joins the socket's group. -+ proxySocket = { enable = true; users = [ "caddy" ]; }; ++ proxySocket = { enable = true; users = [ "caddy" ]; services = [ "caddy" ]; }; + }; + control.listen = "127.0.0.1:7878"; + shareLinkKeyFile = config.sops.secrets."ianitor/share-link-key".path;