Watch
0
0
Fork
You've already forked hyperhive
0

keep the container's secret key through the pod's first start

Grafana wraps the data keys of its database with secret_key, and a different
key makes them undecryptable, which grafana cli's re-encrypt cannot repair
(it decrypts with the current key first). So the init container takes the
store's key, else the file the container generated, else a new key, and a new
key only over a database that holds no data_keys row; otherwise it exits
non-zero naming the leaf. The file reaches it read-only through a claim on a
static local volume, because the namespace refuses a hostPath.
This commit is contained in:
atlas 2026-11-28 00:21:37 +01:00
commit bd0cff47f9
2 changed files with 429 additions and 91 deletions

View file

@ -1,7 +1,7 @@
# `checks.script-test-grafana-secret-key` — runs
# ../host-modules/lib/grafana-secret-key.nix's `grafana_secret_key` against a
# stub `bao` that keeps its leaves in files. The cases are in
# ./grafana-secret-key.sh.
# stub `bao` that keeps its leaves in files and a real `sqlite3` over small
# databases. The cases are in ./grafana-secret-key.sh.
#
# What runs is the rendered helper, sourced as the unit's script includes it.
{
@ -9,20 +9,16 @@
...
}:
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`, and answer as bao does: a missing
# `kv get` and `kv put -cas=0` act on a file per leaf under `FAKE_BAO_KV`, and answer as bao does: a missing
# leaf is "No value found at <path>" on stderr, status 2. `FAKE_BAO_DENY_READ`
# and `FAKE_BAO_DENY_WRITE` answer 403 as a policy that grants neither does.
# and `FAKE_BAO_DENY_WRITE` answer 403 as a policy that grants neither does. A call without the pod's
# login token (`BAO_TOKEN`, set by the cases) answers 97.
# `FAKE_BAO_RACE` has another run create the leaf with that value between
# this one's read and its write. 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; }
[ "$3" = -field=value ] || { echo "fake bao: get without -field=value" >&2; exit 95; }
@ -60,7 +56,11 @@ pkgs.runCommand "hyperhive-script-test-grafana-secret-key"
import ../host-modules/lib/grafana-secret-key.nix { }
);
FAKE_BAO_BIN = "${fakeBao}/bin";
nativeBuildInputs = [ pkgs.coreutils ];
nativeBuildInputs = [
pkgs.coreutils
pkgs.gnugrep
pkgs.sqlite
];
}
''
${pkgs.bash}/bin/bash ${./grafana-secret-key.sh}

View file

@ -1,7 +1,7 @@
# Cases for ./grafana-secret-key.nix. Each case sets up the store's leaf, the
# seed file and the stub's behaviour, runs `grafana_secret_key` the way the
# unit's script does, and compares its status, the key it hands over and what
# the store holds afterwards.
# 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
@ -12,10 +12,11 @@ setup() {
case=$1
dir=$(mktemp -d)
mkdir "$dir/kv"
export FAKE_BAO_KV=$dir/kv FAKE_BAO_LOG=$dir/bao.log FAKE_BAO_LOGIN_RC=0
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 / _)
}
@ -23,15 +24,21 @@ 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 <status> <key> <what the store holds, "" for nothing>
expect() {
(
PATH="$FAKE_BAO_BIN:$PATH"
unset BAO_TOKEN
export BAO_TOKEN=s.fake
# shellcheck source=/dev/null
. "$HELPER"
rc=0
grafana_secret_key "$leaf" "$seed" mint >/dev/null 2>"$dir/err" || rc=$?
grafana_secret_key "$leaf" "$seed" "$db" mint >"$dir/out" 2>"$dir/err" || rc=$?
printf '%s' "$rc" >"$dir/rc"
printf '%s' "$GRAFANA_KEY" >"$dir/key"
)
@ -52,26 +59,57 @@ 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 "with no seed and an empty store, a key is minted and stored"
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 "a store that refuses the login mints nothing, with or without a seed"
export FAKE_BAO_LOGIN_RC=1
expect 1 "" ""
printf 'seed-key\n' >"$seed"
expect 1 "" ""
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 "a store that refuses the read stores and hands over nothing"
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 "" ""