diff --git a/docs/4-implementation/k8s/runtime.md b/docs/4-implementation/k8s/runtime.md index 2148e83c3..bee7d5a90 100644 --- a/docs/4-implementation/k8s/runtime.md +++ b/docs/4-implementation/k8s/runtime.md @@ -370,7 +370,10 @@ writes to the store and Grafana that reads it. nginx's main process reads its configuration as root, so the file needs no other reader, and it's never in the nix store), which the vhost's store locations `include`, then reloads nginx, and publishes the password as the - Secret the way the host publishes the CA ConfigMap. The files stay on disk, as the cluster keeps the Secret on disk, so a reboot with + Secret the way the host publishes the CA ConfigMap. The unit is `Type=simple` + and retries every 30 seconds, so a boot or a switch with the secret store still + sealed doesn't wait for it; the credential is out once the unit is `active (exited)`. + The files stay on disk, as the cluster keeps the Secret on disk, so a reboot with the secret store still sealed leaves the gateway sending the credential the pod expects. Nothing rotates the value by itself: to rotate it, delete the leaf's metadata with a token that may and diff --git a/nix/host-modules/k8s/store-credentials.nix b/nix/host-modules/k8s/store-credentials.nix index b734fde80..980fe28f3 100644 --- a/nix/host-modules/k8s/store-credentials.nix +++ b/nix/host-modules/k8s/store-credentials.nix @@ -106,13 +106,21 @@ in ]; # ./lib/store-retry.nix: the login fails for as long as the store is # sealed or down. + # + # `Type = "simple"`, as `hyperhive-k8s-ca-trust` is (./runtime.nix): + # the start job is done once the script has forked. A oneshot's + # start job stays pending through its restarts, so a boot or a + # switch with the store sealed held `multi-user.target` and + # `switch-to-configuration` for the whole retry budget. Nothing + # orders after this unit. `RemainAfterExit` keeps it `active + # (exited)` once the credential is out, so `SubState=exited` is the + # state that says so: `is-active` is true while the script still runs. inherit (storeRetry) startLimitBurst startLimitIntervalSec; serviceConfig = storeRetry.serviceConfig // { - Type = "oneshot"; + Type = "simple"; RemainAfterExit = true; UMask = "0077"; SyslogIdentifier = "swarm-bao-store-auth"; - TimeoutStartSec = "15min"; }; environment = { BAO_ADDR = "https://${baoCfg.domain}:${toString baoCfg.port}"; diff --git a/nix/module-eval/k8s.nix b/nix/module-eval/k8s.nix index 527443e43..c3e340dd1 100644 --- a/nix/module-eval/k8s.nix +++ b/nix/module-eval/k8s.nix @@ -2293,6 +2293,40 @@ let # Without the queue there is no file for it. && !(lib.elem rule noNode.systemd.tmpfiles.rules); } + { + # A oneshot's start job stays pending through its restarts, so while the + # secret store is sealed a boot and a switch waited on the unit for its + # whole retry budget. The start job of a `simple` unit is done at fork. + name = "the stores' credential unit starts at fork, so a sealed secret store holds neither multi-user.target nor a switch, and its retries go on"; + ok = + let + services = allLocal.systemd.services; + unit = services.swarm-bao-store-auth; + orderedAfter = + name: + lib.filterAttrs ( + _: s: lib.elem name (s.after or [ ]) || lib.elem name (s.requires or [ ]) + ) services; + in + unit.serviceConfig.Type == "simple" + # It is still started at boot, and still restarts on failure for as + # long as the store is down: the retry budget is the shared one. + && unit.wantedBy == [ "multi-user.target" ] + && unit.serviceConfig.Restart == "on-failure" + && unit.startLimitBurst == 2880 + # `active (exited)` after the publish, so `SubState=exited` says the + # credential is out. + && unit.serviceConfig.RemainAfterExit + # A start timeout would be a second, unrelated budget on a unit whose + # start job is done at fork. + && !(unit.serviceConfig ? TimeoutStartSec) + # Nothing orders after it, so none of the units that depend on the + # credential is delayed by it: the gateway reads the file it writes at + # its next reload and the pods wait in `ContainerCreating` for the + # Secret. The control: the lookup finds the units ordered after k3s. + && orderedAfter "swarm-bao-store-auth.service" == { } + && orderedAfter "k3s.service" != { }; + } { # The two `auth_request` responders hold no state and log in under one # role, so they are one pod with one account that may run on any node. diff --git a/nix/vm-tests/k8s-nats.nix b/nix/vm-tests/k8s-nats.nix index 66a97ce4e..e5d4db7fa 100644 --- a/nix/vm-tests/k8s-nats.nix +++ b/nix/vm-tests/k8s-nats.nix @@ -416,6 +416,17 @@ pkgs.testers.runNixOSTest { raise + def store_credential_published() -> None: + # The unit is `Type=simple`: `is-active` is true while its script is still + # running and `systemctl restart` returns at fork, so `SubState=exited` + # is what says the files and the Secrets are out. + settle( + '[ "$(systemctl show --property=SubState --value swarm-bao-store-auth.service)" = exited ]', + 900, + ["swarm-bao-store-auth.service"], + ) + + def pods() -> list[str]: return machine.succeed(f"kubectl -n {NS} get pods --selector app.kubernetes.io/name={SERVICE} -o name || true").split() @@ -849,7 +860,9 @@ pkgs.testers.runNixOSTest { machine.succeed(f": > {EXPORTER_CONF} && systemctl reload metrics-gateway") status, _ = gateway_until(401) assert status == 401, status - machine.succeed("systemctl restart swarm-bao-store-auth.service && systemctl reload metrics-gateway") + machine.succeed("systemctl restart swarm-bao-store-auth.service") + store_credential_published() + machine.succeed("systemctl reload metrics-gateway") assert gateway_until(200)[0] == 200 with subtest("a leaf that has to be issued again is issued beside the running server, which reloads it for new connections"): diff --git a/nix/vm-tests/k8s-services.nix b/nix/vm-tests/k8s-services.nix index e7a6fac51..984102bc0 100644 --- a/nix/vm-tests/k8s-services.nix +++ b/nix/vm-tests/k8s-services.nix @@ -649,6 +649,17 @@ pkgs.testers.runNixOSTest { raise + def store_credential_published() -> None: + # The unit is `Type=simple`: `is-active` is true while its script is still + # running and `systemctl restart` returns at fork, so `SubState=exited` + # is what says the files and the Secrets are out. + settle( + '[ "$(systemctl show --property=SubState --value swarm-bao-store-auth.service)" = exited ]', + 900, + ["swarm-bao-store-auth.service"], + ) + + def service_pods(service: str) -> list[str]: out = machine.succeed(f"kubectl -n {NS} get pods --selector app.kubernetes.io/name={service} -o name || true") return out.split() @@ -959,7 +970,7 @@ pkgs.testers.runNixOSTest { machine.succeed(f"test -L {MANIFESTS}/{name}.yaml") # The pod waits for the Secret the unit publishes; the gateway reads its # include file as it reloads. - settle('[ "$(systemctl is-active swarm-bao-store-auth.service)" = active ]', 900, ["swarm-bao-store-auth.service"]) + store_credential_published() machine.succeed("systemctl reload logs-gateway") vl_pod = wait_pod_ready(VL) auth_pod = wait_pod_ready(LOGS_AUTH) @@ -1137,7 +1148,9 @@ pkgs.testers.runNixOSTest { assert status == 401, (status, body[:200]) # The unit writes the file again, and the same request is granted: the # control that the 401 is the missing credential. - machine.succeed("systemctl restart swarm-bao-store-auth.service && systemctl reload logs-gateway") + machine.succeed("systemctl restart swarm-bao-store-auth.service") + store_credential_published() + machine.succeed("systemctl reload logs-gateway") assert machine.succeed(f"cat {STORE_CONF}") == saved wait_for_record(AGENT_TOKEN, "switch", "written before the switch") @@ -1521,7 +1534,9 @@ pkgs.testers.runNixOSTest { break time.sleep(1) assert status == 401, (status, body[:200]) - machine.succeed("systemctl restart swarm-bao-store-auth.service && systemctl reload logs-gateway") + machine.succeed("systemctl restart swarm-bao-store-auth.service") + store_credential_published() + machine.succeed("systemctl reload logs-gateway") assert machine.succeed(f"cat {VM_CONF}") == saved wait_for_series(AGENT_TOKEN, "written_before_the_switch")