From dad288d555a03c26433b7471703e2abce222397b Mon Sep 17 00:00:00 2001 From: maia Date: Mon, 23 Nov 2026 12:43:37 +0100 Subject: [PATCH] swarm-otel, swarm-grafana: a delivery unit fails on no value yet The collector's and Grafana's OIDC delivery units exited 0 when the store held no value, so systemd did not retry them. On a fresh swarm authelia's mint writes the secret about 30 s after the first read, and only the refresh timer, ten minutes on, read it again. Both units now exit 75, and the store retry they already carry asks every 30 s until the mint lands. Closes #6790 --- .../swarm/credential-delivery.md | 11 +++++--- nix/host-modules/hive-squid-credential.nix | 3 +++ nix/host-modules/lib/attic-root-facts.nix | 3 +++ nix/host-modules/swarm-grafana.nix | 27 ++++++++++--------- nix/host-modules/swarm-otel.nix | 19 ++++++++----- nix/module-eval/grafana.nix | 18 +++++++++++++ nix/module-eval/swarm-otel-core.nix | 18 +++++++++++++ nix/vm-tests/full-setup.py | 11 ++++++++ 8 files changed, 88 insertions(+), 22 deletions(-) diff --git a/docs/4-implementation/swarm/credential-delivery.md b/docs/4-implementation/swarm/credential-delivery.md index 25f254dc4..b9121a4f1 100644 --- a/docs/4-implementation/swarm/credential-delivery.md +++ b/docs/4-implementation/swarm/credential-delivery.md @@ -65,8 +65,9 @@ the client it reads for unconditionally: authelia refuses a bearer-authz client with no audience, and every host publishes at least forgejo's `/metrics` as a scrape target, so the audience is never empty. -Until the store answers, the collector's secret file is empty: tmpfiles creates -it so `LoadCredential` has a source. The collector's unit starts through a +Until the store has the value, the collector's secret file is empty: tmpfiles +creates it so `LoadCredential` has a source. The unit fails and asks again +every 30 seconds while the store holds none, the same as Grafana's. The collector's unit starts through a wrapper that checks that file. On an empty one it loads the config without the `publishedScrapeTargets` jobs, so it still receives and still scrapes its own loopback targets, but asks authelia for no token. The prometheus receiver reads @@ -126,7 +127,11 @@ one delivery route: `swarm-bao-grafana-oidc.service` reads `swarm/services//oidc/client` out of the store, on **every** host that runs Grafana. Where authelia is doesn't enter into it — the mint in authelia's container writes that path whether the reader is a network away or in the -container next door. +container next door. On a fresh swarm the mint writes it a moment after the +reader's first attempt, so a read that finds no value yet fails and the unit +asks again every 30 seconds until the value is there, as the forge's and the +homeserver's do. A unit that finished there would wait for its refresh timer, +about ten minutes on. That's a deliberate refusal of the shortcut. Copying the plaintext straight out of authelia's tree when the two happen to share a host is one diff --git a/nix/host-modules/hive-squid-credential.nix b/nix/host-modules/hive-squid-credential.nix index 7485bc5c8..e15790701 100644 --- a/nix/host-modules/hive-squid-credential.nix +++ b/nix/host-modules/hive-squid-credential.nix @@ -85,6 +85,9 @@ in exit 1 fi if ! value="$(store_bao kv get -field=value ${lib.escapeShellArg "secret/swarm/hives/${hiveController.hiveName}/squid-selfcheck"} 2>"$STORE_ERR")"; then + # Done, not retried: a hive with no minted credential is a supported + # state (the self-check reports `credential=absent`), so there is + # nothing to wait for. The refresh timer reads the path again. if grep -qi 'no value found' "$STORE_ERR"; then echo "the swarm has not minted this hive's squid self-check credential yet; ${credentialFile} stays as it is." exit 0 diff --git a/nix/host-modules/lib/attic-root-facts.nix b/nix/host-modules/lib/attic-root-facts.nix index bfce4540c..e71da56ea 100644 --- a/nix/host-modules/lib/attic-root-facts.nix +++ b/nix/host-modules/lib/attic-root-facts.nix @@ -98,6 +98,9 @@ token="" fetch public-key key fetch pull-token token + # Done, not retried: a swarm with no cache has no facts to give, so a hive + # without them is a supported state and not a wait. The refresh timer reads + # the leaves again. if [ "$missing" = 1 ]; then echo "the swarm has not given this hive its cache facts yet; $dir stays as it is." exit 0 diff --git a/nix/host-modules/swarm-grafana.nix b/nix/host-modules/swarm-grafana.nix index d1599f6cb..797315c74 100644 --- a/nix/host-modules/swarm-grafana.nix +++ b/nix/host-modules/swarm-grafana.nix @@ -749,21 +749,21 @@ in # # It is a store reader, and this is what the store's readers do: a cert # login that fails LOUDLY, since every state it fails on is one a retry - # fixes, then a read that degrades QUIETLY, since no retry turns "no value - # there" into a value. + # fixes, then a read that fails the same way when the value is not there + # yet. # # ⚠️ An ABSENT secret is "not published yet", not a fault of this host's. # The mint runs in authelia's container and authelia mints on its # first boot, so early in a swarm's life there is legitimately nothing to - # read. + # read, and a retry finds it a moment later. A unit that exited 0 on it + # would not run again until the refresh timer, about ten minutes on. # Nothing here writes a stand-in: there is no such thing as a locally valid # OIDC client secret, and a placeholder would turn a Grafana that cannot # log anyone in into one whose token exchange is refused. # - # ⇒ So the absence surfaces at Grafana rather than here, and this unit's job - # is to leave the journal line that says which of the three it was: the - # secret not arriving is a Grafana nobody can log into, and a named cause is - # the only thing separating that from a mystery. + # ⇒ So the unit fails, and leaves the journal line that says which of the + # three it was: a secret that does not arrive is a Grafana nobody can log + # into, and a named cause is the only thing separating that from a mystery. systemd.services.swarm-bao-grafana-oidc = { description = "fetch Grafana's OIDC client secret from the swarm secret store"; # Ordered after the store where it runs here @@ -812,9 +812,9 @@ in ${ensureSecretDir} # `bao`'s own message is the only thing separating a missing value from - # a refused identity from an unreachable host. This unit's degraded - # mode is correct for all three, so it reports which one rather than - # asserting all three in a sentence of ours. + # a refused identity from an unreachable host. Retrying is right for + # all three, so this unit reports which one rather than asserting all + # three in a sentence of ours. err="$(mktemp)" trap 'rm -f "$err"' EXIT @@ -834,6 +834,9 @@ in fi export BAO_TOKEN + # 75 is EX_TEMPFAIL: not there yet, ask again. `Restart=on-failure` + # (./lib/store-retry.nix) asks every 30 s until the mint has written it, + # and the status line says "no value" rather than "no login" (1). if ! secret="$(bao kv get -field=value ${lib.escapeShellArg storeSecretPath} 2>"$err")"; then echo "swarm-bao did not return ${storeSecretPath}; Grafana has no OIDC client secret yet." >&2 if [ -s "$err" ]; then @@ -841,12 +844,12 @@ in else echo "bao failed without writing a diagnostic." >&2 fi - exit 0 + exit 75 fi if [ -z "$secret" ]; then echo "swarm-bao returned an empty ${storeSecretPath}; leaving the file as it is." >&2 - exit 0 + exit 75 fi # Written with a shell builtin, never handed to a program: `printf` is diff --git a/nix/host-modules/swarm-otel.nix b/nix/host-modules/swarm-otel.nix index 03829ffb6..5e9e6a3b8 100644 --- a/nix/host-modules/swarm-otel.nix +++ b/nix/host-modules/swarm-otel.nix @@ -677,8 +677,10 @@ in # # Shaped after ./swarm-grafana.nix's store reader: a cert login that fails # LOUDLY, since every state it fails on is one a retry fixes, then a read - # that degrades QUIETLY, since no retry turns "no value there" into a - # value. + # that fails the same way when the value is not there yet. Authelia's mint + # writes it a moment after a fresh swarm's first boot, so a retry does turn + # "no value there" into a value, and a unit that exited 0 on it would not + # run again until the refresh timer, about ten minutes on. # # ⚠️ Renders only where `haveClientIdentity` holds, unlike # ./swarm-grafana.nix's equivalent unit. That module refuses any host that @@ -740,9 +742,9 @@ in install -d -m 0700 ${lib.escapeShellArg collectorSecretDir} # `bao`'s own message is the only thing separating a missing value - # from a refused identity from an unreachable host. This unit's - # degraded mode is correct for all three, so it reports which one - # rather than asserting all three in a sentence of ours. + # from a refused identity from an unreachable host. Retrying is right + # for all three, so this unit reports which one rather than asserting + # all three in a sentence of ours. err="$(mktemp)" trap 'rm -f "$err"' EXIT @@ -762,6 +764,9 @@ in fi export BAO_TOKEN + # 75 is EX_TEMPFAIL: not there yet, ask again. `Restart=on-failure` + # (./lib/store-retry.nix) asks every 30 s until the mint has written it, + # and the status line says "no value" rather than "no login" (1). if ! secret="$(bao kv get -field=value ${lib.escapeShellArg storeSecretPath} 2>"$err")"; then echo "swarm-bao did not return ${storeSecretPath}; the collector has no OIDC client secret yet." >&2 if [ -s "$err" ]; then @@ -769,12 +774,12 @@ in else echo "bao failed without writing a diagnostic." >&2 fi - exit 0 + exit 75 fi if [ -z "$secret" ]; then echo "swarm-bao returned an empty ${storeSecretPath}; leaving the file as it is." >&2 - exit 0 + exit 75 fi # root-owned 0400, written with a shell builtin and never handed to a diff --git a/nix/module-eval/grafana.nix b/nix/module-eval/grafana.nix index 91165b454..452a4d529 100644 --- a/nix/module-eval/grafana.nix +++ b/nix/module-eval/grafana.nix @@ -351,6 +351,24 @@ let lib.hasInfix "secret/swarm/services/swarm-grafana/oidc/client" s && !(lib.hasInfix "secret/swarm/hives/" s); } + { + # Authelia's mint writes the secret a moment after a fresh swarm's first + # boot, so a read that finds nothing yet has to fail and be asked again. + # A unit that exits 0 there is done until the refresh timer, ten minutes + # on, and Grafana has no login in the meantime. + name = "grafana's OIDC delivery unit fails on no value yet, so the store retry asks again"; + ok = + let + u = grafanaRemoteAuthelia.systemd.services.swarm-bao-grafana-oidc; + afterRead = lib.last (lib.splitString "bao kv get" u.script); + beforeWrite = lib.head (lib.splitString "atomic_write_secret" afterRead); + in + # Both arms: the read that returns nothing and the one that returns an + # empty value. + lib.length (lib.splitString "exit 75" beforeWrite) == 3 + && !(lib.hasInfix "exit 0" beforeWrite) + && u.serviceConfig.Restart == "on-failure"; + } { # A copy on persistent disk outlives the secret's rotation and lands in # backups of the container's root. diff --git a/nix/module-eval/swarm-otel-core.nix b/nix/module-eval/swarm-otel-core.nix index 4f154210f..a1cdadf6f 100644 --- a/nix/module-eval/swarm-otel-core.nix +++ b/nix/module-eval/swarm-otel-core.nix @@ -133,6 +133,24 @@ let lib.hasInfix "secret/swarm/services/swarm-collector/oidc/client" s && !(lib.hasInfix "secret/swarm/hives/" s); } + { + # Authelia's mint writes the secret a moment after a fresh swarm's first + # boot, so a read that finds nothing yet has to fail and be asked again. + # A unit that exits 0 there is done until the refresh timer, ten minutes + # on, and the collector runs on an empty secret in the meantime. + name = "the collector's delivery unit fails on no value yet, so the store retry asks again"; + ok = + let + u = otelBaoRemoteAuthelia.systemd.services.swarm-bao-otel-oidc; + afterRead = lib.last (lib.splitString "bao kv get" u.script); + beforeWrite = lib.head (lib.splitString "atomic_write_secret" afterRead); + in + # Both arms: the read that returns nothing and the one that returns an + # empty value. + lib.length (lib.splitString "exit 75" beforeWrite) == 3 + && !(lib.hasInfix "exit 0" beforeWrite) + && u.serviceConfig.Restart == "on-failure"; + } { # A copy on persistent disk outlives the secret's rotation and lands in # backups of the container's root. diff --git a/nix/vm-tests/full-setup.py b/nix/vm-tests/full-setup.py index 2c042c0e8..e6902d39b 100644 --- a/nix/vm-tests/full-setup.py +++ b/nix/vm-tests/full-setup.py @@ -196,6 +196,17 @@ with subtest("authelia's mint has published the clients' secrets"): "swarm-bao-operator-viewer-policy", ) +with subtest("the swarm collector's client secret reaches the host"): + # `swarm-bao-otel-oidc` reads the mint's path once the store answers, and the mint writes + # it a moment after that first read on a fresh swarm. The unit has to fail on "no value yet" + # and ask again every 30 s. One that finished would wait for its refresh timer, ten minutes + # on, so the wait is shorter than that. + settle( + "[ -s /run/swarm-otel-oidc/swarm-collector.secret ]", + 300, + "swarm-bao-otel-oidc", + ) + with subtest("step 2: swarmctl user add"): machine.wait_until_succeeds( "swarmctl user add mara --display-name Mara --email mara@example.com --group admins "