# Cases for ./grafana-secret-key.nix. Each case sets up the store's leaf, the # seed file, the database and the stub's behaviour, runs `grafana_secret_key` # the way the init container's script does, and compares its status, the key it # hands over and what the store holds afterwards. set -uo pipefail failures=0 leaf=secret/swarm/services/t/secret-key # setup setup() { case=$1 dir=$(mktemp -d) mkdir "$dir/kv" export FAKE_BAO_KV=$dir/kv FAKE_BAO_LOG=$dir/bao.log unset FAKE_BAO_DENY_READ FAKE_BAO_DENY_WRITE FAKE_BAO_RACE : >"$FAKE_BAO_LOG" seed=$dir/secret_key db=$dir/grafana.db stored=$FAKE_BAO_KV/$(printf '%s' "$leaf" | tr / _) } mint() { printf 'minted-key\n' } # A database as Grafana leaves it: with `data_keys` rows, with the table and no # rows, or with other tables only. db_with_keys() { sqlite3 "$db" "CREATE TABLE data_keys (name TEXT); INSERT INTO data_keys VALUES ('a'), ('b');"; } db_without_rows() { sqlite3 "$db" 'CREATE TABLE data_keys (name TEXT);'; } db_without_table() { sqlite3 "$db" 'CREATE TABLE other (name TEXT);'; } # expect expect() { ( PATH="$FAKE_BAO_BIN:$PATH" export BAO_TOKEN=s.fake # The init container's umask: the helper's own temporary file must still # be writable to it. umask 0227 # shellcheck source=/dev/null . "$HELPER" rc=0 grafana_secret_key "$leaf" "$seed" "$db" mint >"$dir/out" 2>"$dir/err" || rc=$? printf '%s' "$rc" >"$dir/rc" printf '%s' "$GRAFANA_KEY" >"$dir/key" ) held=$(cat "$stored" 2>/dev/null) if [ "$(cat "$dir/rc")" != "$1" ]; then fail "exited $(cat "$dir/rc"), wanted $1: $(cat "$dir/err")"; fi if [ "$(cat "$dir/key")" != "$2" ]; then fail "handed over '$(cat "$dir/key")', wanted '$2'"; fi if [ "$held" != "$3" ]; then fail "the store holds '$held', wanted '$3'"; fi } fail() { echo "FAILED: $case: $*" >&2 failures=$((failures + 1)) } setup "an empty store takes the seed's key" printf 'seed-key\n' >"$seed" expect 0 seed-key seed-key grep -q '^kv put -cas=0 ' "$FAKE_BAO_LOG" || fail "no create-only write" [ -e "$seed" ] || fail "the seed file was removed" setup "an empty store takes the seed's key over a database that holds data keys" printf 'seed-key\n' >"$seed" db_with_keys expect 0 seed-key seed-key setup "a store that holds a key wins over the seed, and is not written" printf 'seed-key\n' >"$seed" printf 'store-key' >"$stored" expect 0 store-key store-key ! grep -q '^kv put' "$FAKE_BAO_LOG" || fail "wrote to the store" setup "a store that holds a key wins over a database that holds data keys, and is not written" printf 'store-key' >"$stored" db_with_keys expect 0 store-key store-key ! grep -q '^kv put' "$FAKE_BAO_LOG" || fail "wrote to the store" setup "with no seed, an empty store and no database, a key is minted and stored" expect 0 minted-key minted-key setup "with no seed and an empty store, a database with the table and no rows takes a minted key" db_without_rows expect 0 minted-key minted-key setup "with no seed and an empty store, a database without the table takes a minted key" db_without_table expect 0 minted-key minted-key setup "with no seed and an empty store, a database that holds data keys is refused and nothing is minted" db_with_keys expect 1 "" "" grep -q 'refusing to mint' "$dir/err" || fail "the refusal does not say so: $(cat "$dir/err")" grep -q "$leaf" "$dir/err" || fail "the refusal does not name the leaf" grep -q '2 data key' "$dir/err" || fail "the refusal does not count the keys" ! grep -q '^kv put' "$FAKE_BAO_LOG" || fail "wrote to the store" setup "with no seed and an empty store, a database that cannot be read is refused and nothing is minted" printf 'this is not a database, and is longer than a page header needs to be to be refused as one\n' >"$db" expect 1 "" "" grep -q 'minting nothing' "$dir/err" || fail "the refusal does not say so: $(cat "$dir/err")" setup "a store that refuses the read stores the seed and hands over nothing" printf 'seed-key\n' >"$seed" export FAKE_BAO_DENY_READ=1 expect 1 "" seed-key setup "a store that refuses the read, with a database that holds data keys and no seed, mints nothing" db_with_keys export FAKE_BAO_DENY_READ=1 expect 1 "" "" setup "a store that refuses the write hands over nothing" export FAKE_BAO_DENY_WRITE=1 expect 1 "" "" setup "a store that holds an empty value hands over nothing" : >"$stored" expect 1 "" "" setup "a lost race takes the winner's key" printf 'seed-key\n' >"$seed" export FAKE_BAO_RACE=other-key expect 0 other-key other-key echo "$failures failures" [ "$failures" = 0 ]