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:
parent
9f62e71dae
commit
bd0cff47f9
1 changed files with 429 additions and 91 deletions
|
|
@ -282,7 +282,7 @@ let
|
|||
start = r.scripts.start;
|
||||
in
|
||||
lib.hasInfix "bao kv put -cas=0 \"$1\" value=-" init
|
||||
&& lib.hasInfix "put admin-password \"$(seeded secret/swarm/services/swarm-grafana/admin/password)\"" init
|
||||
&& lib.hasInfix "admin=\"$(seeded secret/swarm/services/swarm-grafana/admin/password)\"\nput admin-password \"$admin\"" init
|
||||
&& lib.hasInfix "openssl rand -hex 32" init
|
||||
&& r.settings.security.admin_password == "$__file{/run/grafana-secrets/admin-password}"
|
||||
&& lib.hasInfix "admin reset-admin-password --password-from-stdin < /run/grafana-secrets/admin-password" start
|
||||
|
|
@ -291,22 +291,50 @@ let
|
|||
}
|
||||
{
|
||||
# Grafana's datasource-encryption key reaches Grafana from the store. A
|
||||
# different key leaves Grafana unable to read its stored datasource
|
||||
# secrets, so the store's value wins, `-cas=0` never replaces one, and a
|
||||
# lost race reads the winner's back. No file of an earlier release is
|
||||
# read: a host that never held a stored key gets a new one, and the
|
||||
# provisioned datasources are written again at start.
|
||||
name = "grafana's datasource key is created in the store once and delivered from the pod's memory";
|
||||
# different key leaves Grafana unable to read what the key it was
|
||||
# created under encrypted (the data keys in its database), so the
|
||||
# store's value wins, then the file the container generated, and a new
|
||||
# key is minted only when there is neither and the database holds no
|
||||
# data key. `-cas=0` never replaces a value, and a lost race reads the
|
||||
# winner's back. The file is read from the pod's read-only mount and
|
||||
# nothing writes it.
|
||||
name = "grafana's datasource key is the store's, else the earlier container's file, else a new one only over a database with no data keys, and the file is only read";
|
||||
ok =
|
||||
let
|
||||
r = rendered grafanaRemoteAuthelia;
|
||||
init = r.scripts.fetch;
|
||||
at = needle: lib.stringLength (lib.head (lib.splitString needle init));
|
||||
call = "grafana_secret_key secret/swarm/services/swarm-grafana/secret-key /legacy-secret-key/secret_key /var/lib/grafana/grafana.db openssl rand -hex 32 || exit 1";
|
||||
# The helper's text is part of the script: store, then file, then
|
||||
# the check of the database, then the mint.
|
||||
readStore = "bao kv get -field=value \"$leaf\"";
|
||||
readFile = "tr -d '\\n' < \"$seed\"";
|
||||
readDb = "sqlite3 -readonly \"file:$db?immutable=1\" 'SELECT count(*) FROM data_keys;'";
|
||||
mint = "candidate=\"$(\"$@\")\"";
|
||||
legacyUses = lib.length (lib.splitString "/legacy-secret-key" init) - 1;
|
||||
in
|
||||
lib.hasInfix "put secret-key \"$(seeded secret/swarm/services/swarm-grafana/secret-key)\"" init
|
||||
&& lib.hasInfix "value=\"$(stored \"$1\")\"" init
|
||||
lib.hasInfix call init
|
||||
&& lib.hasInfix "put secret-key \"$GRAFANA_KEY\"" init
|
||||
&& at readStore < at readFile
|
||||
&& at readFile < at readDb
|
||||
&& at readDb < at mint
|
||||
&& lib.hasInfix "refusing to mint a datasource encryption key" init
|
||||
&& lib.hasInfix "if [ \"$rows\" != 0 ]; then" init
|
||||
&& lib.hasInfix "return 1" init
|
||||
&& lib.hasInfix "no such table" init
|
||||
&& lib.hasInfix "kv put -cas=0 \"$leaf\" value=-" init
|
||||
# The earlier file is named once, as the call's argument, and the
|
||||
# script has no command that writes, moves or removes it.
|
||||
&& legacyUses == 1
|
||||
&& !(lib.hasInfix "/var/lib/nixos-containers" init)
|
||||
&& !(lib.hasInfix "rm -f \"$seed\"" init)
|
||||
&& !(lib.hasInfix "> \"$seed\"" init)
|
||||
&& r.settings.security.secret_key == "$__file{/run/grafana-secrets/secret-key}"
|
||||
&& !(lib.hasInfix "/var/lib/grafana-secret" init)
|
||||
&& !(lib.any (lib.hasInfix "/var/lib/grafana-secret") grafanaRemoteAuthelia.systemd.tmpfiles.rules);
|
||||
# The directory the read-only volume sits on has to exist.
|
||||
&& lib.elem "d /var/lib/nixos-containers/swarm-grafana/var/lib/grafana-secret - - - -" grafanaRemoteAuthelia.systemd.tmpfiles.rules
|
||||
&& !(lib.any (lib.hasInfix "/var/lib/grafana-secret") (
|
||||
lib.filter (lib.hasPrefix "r ") grafanaRemoteAuthelia.systemd.tmpfiles.rules
|
||||
));
|
||||
}
|
||||
{
|
||||
# swarm-controller reads alerts with this token, so the account it
|
||||
|
|
@ -389,8 +417,8 @@ let
|
|||
persistent = s: lib.hasInfix "/var/lib/nixos-containers" s || lib.hasInfix "/var/lib/grafana-" s;
|
||||
in
|
||||
lib.hasInfix "put swarm-grafana.secret \"$oidc\"" init
|
||||
&& lib.hasInfix "put metrics-bearer \"Bearer $(seeded secret/swarm/services/swarm-grafana/metrics/bearer)\"" init
|
||||
&& lib.hasInfix "umask 0277" init
|
||||
&& lib.hasInfix "bearer=\"$(seeded secret/swarm/services/swarm-grafana/metrics/bearer)\"\nput metrics-bearer \"Bearer $bearer\"" init
|
||||
&& lib.hasInfix "umask 0227" init
|
||||
&& lib.hasInfix "d=/run/grafana-secrets" init
|
||||
&& !(persistent init)
|
||||
&& !(persistent r.scripts.controllerToken)
|
||||
|
|
|
|||
Loading…
Reference in a new issue