Watch
0
0
Fork
You've already forked hyperhive
0
hyperhive/nix/host-modules/lib/grafana-secret-key.nix
atlas bd0cff47f9 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.
2026-11-28 10:01:39 +01:00

80 lines
3.4 KiB
Nix

# The store's value for Grafana's datasource-encryption key, seeded once from
# the file an earlier release generated in the container.
#
# Grafana cannot read datasource secrets encrypted with another key: it wraps
# the data keys in its `data_keys` table with this one. So a store with no
# value takes the file's, and a new key is minted only when there is no file
# either and the database holds no data key. A database that does hold one has
# been encrypted under a key this run cannot name, so a new key would make
# every secret in it unreadable, and the helper refuses instead of minting.
#
# The store is written with `-cas=0`, so a value it holds is never replaced and
# a lost race reads the winner's back. The file stays, unused.
#
# `grafana_secret_key` sets `GRAFANA_KEY` and returns 0, or returns 1 when the
# store does not answer or take a value, or when the refusal applies; nothing is
# minted then, and the reason is on stderr.
#
# Pure function — NOT a NixOS module. Call it from a script that has logged in
# (`BAO_TOKEN` set), with `bao`, `coreutils`, `grep`, `openssl` and `sqlite3`
# on its `PATH`:
#
# script = ''
# ${import ./lib/grafana-secret-key.nix { }}
# grafana_secret_key secret/swarm/services/foo/secret-key /path/to/seed /path/to/grafana.db openssl rand -hex 32 || exit 1
# '';
{ }:
''
# <store leaf> <seed file> <database> <mint command…>
grafana_secret_key() {
local leaf="$1" seed="$2" db="$3" candidate err rows readerr
shift 3
GRAFANA_KEY=""
err="$(mktemp)"
if ! GRAFANA_KEY="$(bao kv get -field=value "$leaf" 2>"$err")" || [ -z "$GRAFANA_KEY" ]; then
readerr="$(cat "$err")"
if [ -s "$seed" ]; then
candidate="$(tr -d '\n' < "$seed")"
echo "storing the key at $seed in $leaf."
else
# Read-only and immutable: the pod's Grafana is not running yet, and
# a journal left by an earlier crash is none of this check's to replay.
rows=0
if [ -e "$db" ]; then
if ! rows="$(sqlite3 -readonly "file:$db?immutable=1" 'SELECT count(*) FROM data_keys;' 2>"$err")"; then
if grep -q 'no such table' "$err"; then
rows=0
else
cat "$err" >&2
rm -f "$err"
echo "could not read $db to tell whether it holds encrypted data, and $leaf holds no key; minting nothing." >&2
GRAFANA_KEY=""
return 1
fi
fi
fi
if [ "$rows" != 0 ]; then
rm -f "$err"
echo "refusing to mint a datasource encryption key: $db holds $rows data key(s), which only the key it was created under can decrypt, $leaf holds no key (or did not answer) and there is no earlier key file at $seed (the store said: $readerr). Store that key at $leaf, or remove the database." >&2
GRAFANA_KEY=""
return 1
fi
candidate="$("$@")"
echo "no earlier key and no encrypted data; minting one in $leaf."
fi
if ! printf '%s' "$candidate" | bao kv put -cas=0 "$leaf" value=- >/dev/null 2>"$err"; then
cat "$err" >&2
echo "could not create $leaf; reading it back in case another run created it." >&2
fi
if ! GRAFANA_KEY="$(bao kv get -field=value "$leaf" 2>"$err")" || [ -z "$GRAFANA_KEY" ]; then
cat "$err" >&2
rm -f "$err"
echo "the store did not return $leaf." >&2
GRAFANA_KEY=""
return 1
fi
fi
rm -f "$err"
}
''