# `checks.script-test-store-seeded-secret` — runs # ../host-modules/lib/store-seeded-secret.nix's `store_seeded` against a stub # `bao` that keeps its leaves in files. The cases are in # ./store-seeded-secret.sh. # # What runs is the rendered helper, sourced as its callers' scripts include it. { pkgs, ... }: let # `login` answers from `FAKE_BAO_LOGIN_RC`. `kv get` and `kv put -cas=0` act # on a file per leaf under `FAKE_BAO_KV`; `FAKE_BAO_DENY_READ` and # `FAKE_BAO_DENY_WRITE` answer 403 as a policy that grants neither does, and # `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 # logged without the value `put` reads from stdin. fakeBao = pkgs.writeShellScriptBin "bao" '' echo "$*" >> "$FAKE_BAO_LOG" case "$1 $2" in "login -method=cert") [ "$FAKE_BAO_LOGIN_RC" = 0 ] || { echo "fake bao: certificate refused" >&2; exit 2; } printf 's.fake' ;; "kv get") [ "''${BAO_TOKEN-}" = s.fake ] || { echo "fake bao: kv without the login's token" >&2; exit 97; } leaf="$FAKE_BAO_KV/$(printf '%s' "$4" | tr / _)" [ -z "''${FAKE_BAO_READ_TIMEOUT-}" ] || exit 124 if [ -n "''${FAKE_BAO_READ_DELAY-}" ]; then touch "$FAKE_BAO_KV/.inflight.$$" ls "$FAKE_BAO_KV"/.inflight.* | wc -l >> "$FAKE_BAO_KV/.peak" sleep "$FAKE_BAO_READ_DELAY" rm -f "$FAKE_BAO_KV/.inflight.$$" fi [ -z "''${FAKE_BAO_READ_5XX-}" ] || { echo "Error reading secret/data/x: Error making API request. Code: 503. Errors: * Vault is sealed" >&2; exit 2; } [ -z "''${FAKE_BAO_DENY_READ-}" ] || { echo "fake bao: permission denied" >&2; exit 2; } if [ -n "''${FAKE_BAO_RACE-}" ] && [ ! -e "$FAKE_BAO_KV/.raced" ]; then touch "$FAKE_BAO_KV/.raced" printf '%s' "$FAKE_BAO_RACE" > "$leaf" echo "No value found at secret/data/x" >&2 exit 2 fi [ -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; } leaf="$FAKE_BAO_KV/$(printf '%s' "$4" | tr / _)" [ -z "''${FAKE_BAO_DENY_WRITE-}" ] || { echo "fake bao: permission denied" >&2; exit 2; } [ ! -e "$leaf" ] || { echo "fake bao: check-and-set parameter did not match" >&2; exit 2; } cat > "$leaf" ;; *) echo "fake bao: unexpected call: $*" >&2 exit 98 ;; esac ''; in pkgs.runCommand "hyperhive-script-test-store-seeded-secret" { HELPER = pkgs.writeText "store-seeded-secret.sh" ( import ../host-modules/lib/store-seeded-secret.nix { } ); FAKE_BAO_BIN = "${fakeBao}/bin"; nativeBuildInputs = [ pkgs.coreutils pkgs.gnugrep ]; } '' ${pkgs.bash}/bin/bash ${./store-seeded-secret.sh} touch "$out" ''