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

View file

@ -1,45 +1,67 @@
# 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, so a store
# with no value takes the file's, and a new key is minted only when there is no
# file either. 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 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 log in, answer or take a value; nothing is minted then.
# 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 unit's `script`, with
# `bao` and `coreutils` on its `path` and the `BAO_*` client environment set:
# 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 openssl rand -hex 32 || exit 1
# 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> <mint command…>
# <store leaf> <seed file> <database> <mint command…>
grafana_secret_key() {
local leaf="$1" seed="$2" candidate err
shift 2
local leaf="$1" seed="$2" db="$3" candidate err rows readerr
shift 3
GRAFANA_KEY=""
err="$(mktemp)"
if ! BAO_TOKEN="$(bao login -method=cert -token-only 2>"$err")"; then
cat "$err" >&2
rm -f "$err"
echo "could not log in to the swarm secret store." >&2
return 1
fi
export BAO_TOKEN
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; minting one in $leaf."
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