fetch-secrets ran under umask 0227 for the files it writes, which made the mktemp file of grafana_secret_key mode 0400: the redirect of bao's stderr into it failed, every bao call in the helper answered 1, and the init container exited 1 before it stored or read the key. The umask now covers only the writes of put(), the helper chmods its own file, the script test runs under that umask, and a pod that does not start prints its init containers' logs.
82 lines
3.5 KiB
Nix
82 lines
3.5 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)"
|
|
# A caller's umask can leave the file unwritable to its owner.
|
|
chmod u+rw "$err"
|
|
|
|
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"
|
|
}
|
|
''
|