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

@ -549,13 +549,32 @@ dials the pod's ClusterIP Service by name, as the stores' vhosts do, so
which covers a database migration after an upgrade.
- **The init container.** `fetch-secrets` logs in to the secret store with the
pod's ServiceAccount token under the jwt role `swarm-grafana-oidc`, and writes
four files, mode 0400, to the pod's in-memory `runtime` volume: the OIDC client
secret, the administrator password, the bearer of the datasources and the
datasource encryption key. It reads the client secret from
`swarm/services/<client id>/oidc/client`, which authelia's mint stores, and
exits non-zero when the store holds none, so the pod waits in `Init` and retries
until the mint has written it. The other three it creates when the store holds
none (a lost race reads the winner's value) and never replaces.
four files, mode 0440 and group-readable by the pod's group, to the pod's
in-memory `runtime` volume: the OIDC client secret, the administrator password,
the bearer of the datasources and the datasource encryption key. It reads the
client secret from `swarm/services/<client id>/oidc/client`, which authelia's
mint stores, and exits non-zero when the store holds none, so the pod waits in
`Init` and retries until the mint has written it. The administrator password and
the bearer it creates when the store holds none (a lost race reads the winner's
value) and never replaces. The next item gives the order for the encryption key.
- **The encryption key.** Grafana wraps the data keys in its database's
`data_keys` table with this key, and only the key that made a database
decrypts them, so the init container never makes a new key for a
database that holds any. It takes, in this order: the store's value at
`swarm/services/<client id>/secret-key`; else the file the earlier container
generated at `/var/lib/nixos-containers/swarm-grafana/var/lib/grafana-secret/secret_key`,
which it stores in the leaf; else a new key, stored the same way, but only when
the carried `grafana.db` is absent or holds no data key. With a database that
holds data keys, no file and an empty leaf, it exits non-zero and prints the
cause and the leaf: the fix is to store the key that made that database
at the leaf. It writes every value with `-cas=0`, so a later start reads the
store's. The file reaches the init container read-only, through a claim on a
static local volume on its directory (`swarm-grafana-legacy-key`, pinned like
the data volume), because the namespace's Pod Security level refuses a
`hostPath`; the init container also mounts the data directory read-only, and
runs as the uid that owns both (the container's `grafana` user). `sqlite3`
reads the database read-only and immutable. The file seeds a leaf that's
still empty: once every host's leaf holds a key, nothing reads it.
- **Rotation.** Grafana reads the four files once, when it starts. A replaced
OIDC client secret, administrator password or bearer takes effect when the pod
restarts: `kubectl -n hyperhive rollout restart deployment/swarm-grafana`. No