From df9cd8c7e66f8da406eaa777022caaa260e35dc7 Mon Sep 17 00:00:00 2001 From: maia Date: Tue, 24 Nov 2026 18:19:26 +0100 Subject: [PATCH] archive: an empty prefix lists as no users, not as a failure swarm-archive-users and hive-archive-backup read "No value found" on stderr to tell a prefix that holds nothing from an error. With `kv list -format=json`, bao answers such a prefix with status 2, `{}` on stdout and nothing on stderr, so on a swarm whose archive has no users yet the unit failed every 30 seconds forever, and every switch reported it. store_list, in the shared store helper, prints the names as a JSON array and `[]` for a prefix that holds nothing: failed, empty stderr, stdout `{}`. It is not decided on status 2 alone, which a refused token, a sealed store and an unreachable store end with as well (their error is on stderr). The two scripts use it. The script test runs it against a stub that answers as bao does, with controls for a refusal, a sealed store and a warning beside an empty answer, and the archive VM test's stub no longer prints the table format's message for a JSON list. full-setup waits for the users file to be rendered on a fresh swarm. Closes #6840 --- nix/host-modules/lib/archive-backup.nix | 15 ++---- nix/host-modules/lib/archive-server.nix | 17 ++----- nix/host-modules/lib/store-seeded-secret.nix | 26 ++++++++++ nix/script-tests/store-seeded-secret.nix | 18 +++++++ nix/script-tests/store-seeded-secret.sh | 51 +++++++++++++++++++- nix/vm-tests/full-setup.py | 11 +++++ nix/vm-tests/swarm-archive.nix | 6 ++- 7 files changed, 119 insertions(+), 25 deletions(-) diff --git a/nix/host-modules/lib/archive-backup.nix b/nix/host-modules/lib/archive-backup.nix index 1b1b24572..0bf675798 100644 --- a/nix/host-modules/lib/archive-backup.nix +++ b/nix/host-modules/lib/archive-backup.nix @@ -136,17 +136,10 @@ in otlp_gauges hive-archive-backup "agent=$agent" "''${metrics[@]}" } - # The agents the controller granted this hive; none yet is "No value found". - rc=0 - granted="$(store_bao kv list -format=json "$grants" 2>"$STORE_ERR")" || rc=$? - if [ "$rc" != 0 ]; then - if grep -q 'No value found' "$STORE_ERR"; then - granted='[]' - else - cat "$STORE_ERR" >&2 - echo "archive backup failed: could not list $grants" >&2 - exit 1 - fi + # The agents the controller granted this hive; none yet lists as `[]`. + if ! granted="$(store_list "$grants")"; then + echo "archive backup failed: could not list $grants" >&2 + exit 1 fi granted="$(printf '%s' "$granted" | jq -r '.[]')" diff --git a/nix/host-modules/lib/archive-server.nix b/nix/host-modules/lib/archive-server.nix index 241e134d0..2d06836e8 100644 --- a/nix/host-modules/lib/archive-server.nix +++ b/nix/host-modules/lib/archive-server.nix @@ -185,18 +185,11 @@ in store_bao kv put -cas=0 ${lib.escapeShellArg readyPath} ready=1 >/dev/null fi - # A prefix that holds nothing is "No value found"; it means no agent has a - # user yet, not a failure. - rc=0 - names="$(store_bao kv list -format=json ${lib.escapeShellArg usersPrefix} 2>"$STORE_ERR")" || rc=$? - if [ "$rc" != 0 ]; then - if grep -q 'No value found' "$STORE_ERR"; then - names='[]' - else - cat "$STORE_ERR" >&2 - echo "could not list ${usersPrefix}; $file stays as it is." >&2 - exit 1 - fi + # A prefix that holds nothing lists as `[]`: no agent has a user yet, which + # is no failure (`store_list` says how the store answers that). + if ! names="$(store_list ${lib.escapeShellArg usersPrefix})"; then + echo "could not list ${usersPrefix}; $file stays as it is." >&2 + exit 1 fi content="" diff --git a/nix/host-modules/lib/store-seeded-secret.nix b/nix/host-modules/lib/store-seeded-secret.nix index 691d4aaed..c7e1deb29 100644 --- a/nix/host-modules/lib/store-seeded-secret.nix +++ b/nix/host-modules/lib/store-seeded-secret.nix @@ -8,6 +8,9 @@ # race reads the winner's back. A read of the leaf that fails for any reason # but "No value found" prints the store's error and neither mints nor writes. # +# `store_list` lists a prefix and answers `[]` for one that holds nothing; see +# it for why that is not the "No value found" of a table-format list. +# # `store_prefetch` reads many leaves concurrently, and the first `store_read` of # a leaf returns that answer, status and error included, instead of asking # again, so what a caller decides from a read is the same. Later reads of the @@ -54,6 +57,29 @@ fi } + # Prints the names under the prefix as bao's JSON array, `[]` for a + # prefix that holds nothing, and returns 0. Any other answer prints the + # store's error on stderr and returns 1. + # + # ⚠️ With `-format=json`, bao answers a prefix that holds nothing with status + # 2, `{}` on stdout and NOTHING on stderr. The "No value found" message + # belongs to the table format, so a grep for it never matches here. And status + # 2 alone is no answer: a refused token, a sealed store and a store that is not + # there end with status 2 too, with their error on stderr and nothing on + # stdout. So "nothing there" is exactly: failed, empty stderr, stdout `{}`. + store_list() { + local out rc=0 + out="$(store_bao kv list -format=json "$1" 2>"$STORE_ERR")" || rc=$? + if [ "$rc" = 0 ]; then + printf '%s\n' "$out" + elif [ ! -s "$STORE_ERR" ] && [ "$(printf '%s' "$out" | tr -d '[:space:]')" = '{}' ]; then + echo '[]' + else + cat "$STORE_ERR" >&2 + return 1 + fi + } + # … Starts the first read of each leaf now, at most STORE_PREFETCH_JOBS # at a time, and returns when all have ended. Each read is bounded by # `store_bao`, and its answer is kept for `store_read`. Does nothing without diff --git a/nix/script-tests/store-seeded-secret.nix b/nix/script-tests/store-seeded-secret.nix index cf39b40fb..dcc905bc2 100644 --- a/nix/script-tests/store-seeded-secret.nix +++ b/nix/script-tests/store-seeded-secret.nix @@ -15,6 +15,11 @@ let # `FAKE_BAO_READ_TIMEOUT` is `timeout` ending the read: status 124, silent. # `FAKE_BAO_READ_5XX` is a server error on the read. A leaf nobody wrote is # "No value found at ", status 2, as `bao kv get` answers it. + # `kv list -format=json` answers `FAKE_BAO_LIST` (a JSON array) when it is + # set, and as bao answers a prefix that holds nothing when it is not: status + # 2, `{}`, nothing on stderr. `FAKE_BAO_LIST_DENY` and `FAKE_BAO_LIST_SEALED` + # are the errors, which end with status 2 as well but print on stderr, and + # `FAKE_BAO_LIST_NOISY` is an empty answer with a line on stderr. # `FAKE_BAO_RACE` has another run create the leaf between this one's read and # its write. `FAKE_BAO_READ_DELAY` holds each read that long and records, in # `$FAKE_BAO_KV/.peak`, how many reads were in flight with it. Every call is @@ -47,6 +52,19 @@ let [ -e "$leaf" ] || { echo "No value found at secret/data/x" >&2; exit 2; } cat "$leaf" ;; + "kv list") + [ "''${BAO_TOKEN-}" = s.fake ] || { echo "fake bao: kv without the login's token" >&2; exit 97; } + [ "$3" = -format=json ] || { echo "fake bao: kv list without -format=json" >&2; exit 98; } + # As bao answers: a refusal and a sealed store end with status 2, the + # error on stderr and nothing on stdout. + [ -z "''${FAKE_BAO_LIST_DENY-}" ] || { printf 'Error listing %s: Code: 403. Errors:\n\n* 1 error occurred:\n\t* permission denied\n' "$4" >&2; exit 2; } + [ -z "''${FAKE_BAO_LIST_SEALED-}" ] || { echo "Error making API request. Code: 503. Errors: * Vault is sealed" >&2; exit 2; } + # An empty answer with something on stderr is not "nothing there". + [ -z "''${FAKE_BAO_LIST_NOISY-}" ] || { echo "fake bao: a warning" >&2; echo '{}'; exit 2; } + # A prefix that holds nothing: status 2, `{}`, nothing on stderr. + [ -n "''${FAKE_BAO_LIST-}" ] || { echo '{}'; exit 2; } + printf '%s\n' "$FAKE_BAO_LIST" + ;; "kv put") [ "''${BAO_TOKEN-}" = s.fake ] || { echo "fake bao: kv without the login's token" >&2; exit 97; } [ "$3" = -cas=0 ] || { echo "fake bao: put without -cas=0" >&2; exit 96; } diff --git a/nix/script-tests/store-seeded-secret.sh b/nix/script-tests/store-seeded-secret.sh index 3494300df..16a0d7421 100644 --- a/nix/script-tests/store-seeded-secret.sh +++ b/nix/script-tests/store-seeded-secret.sh @@ -22,7 +22,7 @@ setup() { dir=$(mktemp -d) mkdir "$dir/kv" export FAKE_BAO_KV=$dir/kv FAKE_BAO_LOG=$dir/bao.log FAKE_BAO_LOGIN_RC=0 - unset FAKE_BAO_DENY_READ FAKE_BAO_DENY_WRITE FAKE_BAO_RACE FAKE_BAO_READ_TIMEOUT FAKE_BAO_READ_5XX FAKE_BAO_READ_DELAY PREFETCH + unset FAKE_BAO_DENY_READ FAKE_BAO_DENY_WRITE FAKE_BAO_RACE FAKE_BAO_READ_TIMEOUT FAKE_BAO_READ_5XX FAKE_BAO_READ_DELAY FAKE_BAO_LIST FAKE_BAO_LIST_DENY FAKE_BAO_LIST_SEALED FAKE_BAO_LIST_NOISY PREFETCH : >"$FAKE_BAO_LOG" earlier=$dir/earlier minted=$dir/minted @@ -298,6 +298,55 @@ peak=$(sort -n "$FAKE_BAO_KV/.peak" | tail -1) if [ "$peak" -gt 8 ]; then fail "$peak reads in flight, wanted at most 8"; fi if [ "$peak" -lt 2 ]; then fail "$peak read in flight, the reads did not overlap"; fi +# run_list — `store_list` of the prefix with the stub first on PATH, +# under the same `set -e` the callers' scripts run with. Sets $rc and $got. +run_list() { + got=$( + set -e + PATH="$FAKE_BAO_BIN:$PATH" + unset BAO_TOKEN + # shellcheck source=/dev/null + . "$HELPER" + store_init + store_login 2>>"$dir/err" + store_list "$1" 2>"$dir/err" + ) + rc=$? +} + +setup "a prefix with names lists them" +export FAKE_BAO_LIST='["agent1","agent2"]' +run_list secret/swarm/services/archive/users +expect 0 '["agent1","agent2"]' + +# bao's `kv list -format=json` of a prefix that holds nothing is status 2, `{}` +# and an empty stderr; its "No value found" is the table format's, so a script +# that greps stderr for it fails the unit forever on a swarm with no users yet. +setup "a prefix that holds nothing lists as an empty array, and nothing is printed on stderr" +run_list secret/swarm/services/archive/users +expect 0 '[]' +if [ -s "$dir/err" ]; then fail "wrote on stderr: $(cat "$dir/err")"; fi + +# Status 2 alone is no answer: the same status ends a refused token and a +# sealed store, and reading either as "nothing there" would empty the users file. +setup "a refused token is an error, with the store's message" +export FAKE_BAO_LIST_DENY=1 +run_list secret/swarm/services/archive/users +expect 1 "" +err_has "permission denied" + +setup "an empty answer with something on stderr is an error" +export FAKE_BAO_LIST_NOISY=1 +run_list secret/swarm/services/archive/users +expect 1 "" +err_has "a warning" + +setup "a sealed store is an error, with the store's message" +export FAKE_BAO_LIST_SEALED=1 +run_list secret/swarm/services/archive/users +expect 1 "" +err_has "Vault is sealed" + if [ "$failures" != 0 ]; then echo "$failures of $count cases failed" >&2 exit 1 diff --git a/nix/vm-tests/full-setup.py b/nix/vm-tests/full-setup.py index fdfcad627..0648fbdf5 100644 --- a/nix/vm-tests/full-setup.py +++ b/nix/vm-tests/full-setup.py @@ -239,6 +239,17 @@ with subtest("the receiver-socket check is active once the collector has recover "swarm-otel", ) +with subtest("the archive's users file is rendered while no agent has a user yet"): + # No agent has a user until the controller grants one, so `kv list` of the users prefix + # answers as bao answers an empty prefix: status 2, `{}` on stdout, nothing on stderr. The + # unit has to read that as "no users yet" and finish; one that reads it as an error fails + # every 30 s forever and every switch reports it. + settle( + "journalctl -u swarm-archive-users --no-pager | grep -q 'Finished render'", + 600, + "swarm-archive-users", + ) + with subtest("step 2: swarmctl user add"): machine.wait_until_succeeds( "swarmctl user add mara --display-name Mara --email mara@example.com --group admins " diff --git a/nix/vm-tests/swarm-archive.nix b/nix/vm-tests/swarm-archive.nix index 066593fb9..b8384ffda 100644 --- a/nix/vm-tests/swarm-archive.nix +++ b/nix/vm-tests/swarm-archive.nix @@ -40,8 +40,12 @@ let case "$1 $2" in "login -method=cert") echo faketoken; exit 0 ;; "kv list") + # As bao answers `kv list -format=json` for a prefix that holds + # nothing: status 2, `{}` on stdout, nothing on stderr. (Its "No value + # found" message is the table format's.) + [ "$3" = -format=json ] || { echo "fake bao: kv list without -format=json" >&2; exit 98; } path="$4" - [ -d "$store/$path" ] || { echo "No value found at $path" >&2; exit 2; } + [ -d "$store/$path" ] || { echo '{}'; exit 2; } ls "$store/$path" | ${pkgs.jq}/bin/jq -R . | ${pkgs.jq}/bin/jq -s . exit 0 ;; "kv put")