The script test's stub gains the two remaining rows of bao's table: a token with no access to the mount (403 on the preflight) and a store that is not there. Both end with status 2 and an error on stderr, and both must be errors, not an empty list.
101 lines
5.3 KiB
Nix
101 lines
5.3 KiB
Nix
# `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 <path>", 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_NOMOUNT` is a token with no access to the mount,
|
|
# `FAKE_BAO_LIST_DOWN` a store that is not there, 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_NOMOUNT-}" ] || { printf 'Error making API request.\n\nURL: GET http://127.0.0.1:8200/v1/sys/internal/ui/mounts/secret\nCode: 403. Errors:\n\n* preflight capability check returned 403, please ensure client'"'"'s policies grant access to path "secret/"\n' >&2; exit 2; }
|
|
[ -z "''${FAKE_BAO_LIST_DOWN-}" ] || { echo 'Get "http://127.0.0.1:8200/v1/sys/internal/ui/mounts/secret": dial tcp 127.0.0.1:8200: connect: connection refused' >&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"
|
|
''
|