# `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. # `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 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" ''