diff --git a/nix/host-modules/lib/grafana-secret-key.nix b/nix/host-modules/lib/grafana-secret-key.nix index 05d51ae5a..50e9f1690 100644 --- a/nix/host-modules/lib/grafana-secret-key.nix +++ b/nix/host-modules/lib/grafana-secret-key.nix @@ -31,6 +31,8 @@ shift 3 GRAFANA_KEY="" err="$(mktemp)" + # A caller's umask can leave the file unwritable to its owner. + chmod u+rw "$err" if ! GRAFANA_KEY="$(bao kv get -field=value "$leaf" 2>"$err")" || [ -z "$GRAFANA_KEY" ]; then readerr="$(cat "$err")" diff --git a/nix/host-modules/swarm-grafana.nix b/nix/host-modules/swarm-grafana.nix index ec418840c..31a7b7a7e 100644 --- a/nix/host-modules/swarm-grafana.nix +++ b/nix/host-modules/swarm-grafana.nix @@ -479,13 +479,15 @@ let fetch = pkgs.writeShellScriptBin "swarm-grafana-fetch" '' export PATH=${lib.makeBinPath tools} set -euo pipefail - umask 0227 ${login} ${grafanaSecretKey} d=${lib.escapeShellArg secretDir} put() { - printf '%s' "$2" > "$d/$1.new" + # Group-readable (0440): the pod's group is how Grafana reads what the init + # container, running as another uid, wrote. Only the writes take the umask: + # a temporary file made under it could not be written to again. + ( umask 0227; printf '%s' "$2" > "$d/$1.new" ) mv -f "$d/$1.new" "$d/$1" } stored() { diff --git a/nix/module-eval/grafana.nix b/nix/module-eval/grafana.nix index 5f87b8e47..35970f769 100644 --- a/nix/module-eval/grafana.nix +++ b/nix/module-eval/grafana.nix @@ -418,7 +418,9 @@ let in lib.hasInfix "put swarm-grafana.secret \"$oidc\"" init && lib.hasInfix "bearer=\"$(seeded secret/swarm/services/swarm-grafana/metrics/bearer)\"\nput metrics-bearer \"Bearer $bearer\"" init - && lib.hasInfix "umask 0227" init + && lib.hasInfix "( umask 0227; printf '%s' \"$2\" > \"$d/$1.new\" )" init + # Only the writes take it: a temporary file made under it is not writable to its owner. + && !(lib.hasInfix "\numask 0227" init) && lib.hasInfix "d=/run/grafana-secrets" init && !(persistent init) && !(persistent r.scripts.controllerToken) diff --git a/nix/script-tests/grafana-secret-key.sh b/nix/script-tests/grafana-secret-key.sh index b4893efe7..ed35dfe4a 100644 --- a/nix/script-tests/grafana-secret-key.sh +++ b/nix/script-tests/grafana-secret-key.sh @@ -35,6 +35,9 @@ expect() { ( PATH="$FAKE_BAO_BIN:$PATH" export BAO_TOKEN=s.fake + # The init container's umask: the helper's own temporary file must still + # be writable to it. + umask 0227 # shellcheck source=/dev/null . "$HELPER" rc=0 diff --git a/nix/vm-tests/k8s-services.nix b/nix/vm-tests/k8s-services.nix index 8757867d0..affed5bed 100644 --- a/nix/vm-tests/k8s-services.nix +++ b/nix/vm-tests/k8s-services.nix @@ -865,6 +865,13 @@ pkgs.testers.runNixOSTest { diagnose(["hyperhive-k8s-ca-trust.service"]) print(machine.execute(f"kubectl -n {NS} describe pod {name}")[1]) print(machine.execute(f"kubectl -n {NS} logs {name} --tail=60")[1]) + # An init container that fails keeps the pod out of Running, and its + # output is the only thing that says why: the default container has + # none yet. + for init in machine.execute(f"kubectl -n {NS} get pod {name} -o jsonpath='{{.spec.initContainers[*].name}}'")[1].split(): + for previous in ("", "--previous"): + print(f"--- init container {init} {previous}") + print(machine.execute(f"kubectl -n {NS} logs {name} -c {init} {previous} --tail=60 2>&1")[1]) raise return name