# Cases for ../host-modules/lib/attic-init.nix. Each case runs the key script or # the cache script against stubs that record every call, over a store kept in # files, and compares the record and what the script left behind. set -uo pipefail failures=0 count=0 case="setup" fail() { echo "FAILED: $case: $*" >&2 failures=$((failures + 1)) } dir=$(mktemp -d) mkdir -p "$dir/bin" "$dir/store" "$dir/secrets" # The scripts name their files under @DIR@, which is this run's directory. for s in ENSURE_JWT PROVISION; do sed "s|@DIR@|$dir|g" "${!s}" >"$dir/$s.sh" export "$s=$dir/$s.sh" done calls=$dir/calls export DIR=$dir # The store is a directory with one file per path. `get` exits 2 on a miss, as # `bao kv get` does. `put -cas=0` refuses an existing path; without it a put # overwrites. $RACE_PATH makes another run create that path just before our put # lands. cat >"$dir/bin/bao" <>"$calls" if [ "\${LOGIN_FAIL:-0}" = 1 ]; then echo denied >&2; exit 2; fi echo store-token ;; "kv get") path=\${4} f=\$(file "\$path") echo "bao get \$path" >>"$calls" [ -f "\$f" ] || exit 2 cat "\$f" ;; "kv put") if [ "\$3" = "-cas=0" ]; then cas=1; path=\$4; else cas=0; path=\$3; fi f=\$(file "\$path") echo "bao put cas=\$cas \$path" >>"$calls" value=\$(cat) if [ "\${RACE_PATH:-}" = "\$path" ] && [ ! -f "\$f" ]; then printf '%s' raced >"\$f" fi if [ "\$cas" = 1 ] && [ -f "\$f" ]; then echo "check-and-set parameter did not match" >&2; exit 1; fi printf '%s' "\$value" >"\$f" ;; esac STUB # The key is a fixed word, so the file the script writes is predictable. cat >"$dir/bin/openssl" <>"$calls" echo FAKEPEM STUB # A token carries the subject and a serial, so a case can tell which one a call # used. The key comes in the environment, as the real tool takes it. cat >"$dir/bin/atticadm" <&2; exit 1; } echo "atticadm \$*" >>"$calls" n=\$(grep -c '^atticadm' "$calls") sub=\$2 [ "\$1" = "-f" ] && sub=\$5 echo "tok-\$sub-\$n" STUB # The cache exists once \`cache create\` ran. Each call records the token the # client config names. cat >"$dir/bin/attic" <>"$calls" case "\$1 \$2" in "cache info") [ -f "\$DIR/cache-exists" ] && echo " Public Key: grid:FAKEKEY" >&2 ;; "cache create") touch "\$DIR/cache-exists" ;; *) exit 1 ;; esac STUB chmod +x "$dir/bin/"* cat >"$dir/bin/curl" <>"$calls" [ "\${CURL_FAIL:-0}" != 1 ] STUB printf '#!%s\nexit 0\n' "$BASH" >"$dir/bin/sleep" # A container that is not running has nothing to refresh. printf '#!%s\nexit 1\n' "$BASH" >"$dir/bin/systemctl" chmod +x "$dir/bin/"* export PATH="$dir/bin:$PATH" # n : how many recorded calls match. n() { grep -c -- "$1" "$calls" || true; } # run