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
3 changed files with 429 additions and 91 deletions
|
|
@ -1298,7 +1298,7 @@ let
|
|||
in
|
||||
ds.jsonData.httpHeaderName1 == "Authorization"
|
||||
&& ds.secureJsonData.httpHeaderValue1 == "$__file{/run/grafana-secrets/metrics-bearer}"
|
||||
&& lib.hasInfix "put metrics-bearer \"Bearer $(seeded secret/${leaf})\"" fetch
|
||||
&& lib.hasInfix "bearer=\"$(seeded secret/${leaf})\"\nput metrics-bearer \"Bearer $bearer\"" fetch
|
||||
&& builtins.elem leaf (lib.findFirst (c: c.name == "grafana-auth") null metricsPod.containers).args
|
||||
&& lib.hasInfix "path \"secret/data/${leaf}\" {\n capabilities = [\"read\"]\n}" s.swarm-bao-metrics-auth-policy.script
|
||||
&& lib.hasInfix "path \"secret/data/${leaf}\" {\n capabilities = [\"create\", \"read\"]\n}" s.swarm-bao-grafana-oidc-policy.script;
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -2890,6 +2890,42 @@ let
|
|||
&& hasInfix "auth/jwt-k8s/role/swarm-grafana-oidc" script
|
||||
&& hasInfix "bound_subject=system:serviceaccount:hyperhive:swarm-grafana" script
|
||||
&& lib.elem "swarm-grafana-oidc" withNode.services.hyperhive.deploy.bao.k8sPrincipals
|
||||
# The earlier container's key file and the carried database reach the init
|
||||
# container alone, read-only: the file through a claim on a static local
|
||||
# volume (a `hostPath` is refused at this namespace's Pod Security
|
||||
# level), the database through the data volume's own mount. The init
|
||||
# container runs as the uid that owns them and as non-root.
|
||||
&&
|
||||
(lib.findFirst (m: m.name == "legacy-key") null init.volumeMounts) == {
|
||||
name = "legacy-key";
|
||||
mountPath = "/legacy-secret-key";
|
||||
readOnly = true;
|
||||
}
|
||||
&& (lib.findFirst (m: m.name == "state") null init.volumeMounts).readOnly
|
||||
&& !(lib.elem "legacy-key" (mountsOf main))
|
||||
&& !(lib.elem "legacy-key" (mountsOf side))
|
||||
&& !((lib.findFirst (m: m.name == "state") null side.volumeMounts) != null)
|
||||
&&
|
||||
(lib.findFirst (v: v.name == "legacy-key") null spec.volumes) == {
|
||||
name = "legacy-key";
|
||||
persistentVolumeClaim = {
|
||||
claimName = "swarm-grafana-legacy-key";
|
||||
readOnly = true;
|
||||
};
|
||||
}
|
||||
&& !(lib.any (v: v ? hostPath) spec.volumes)
|
||||
&&
|
||||
objects."swarm-grafana-legacy-key/persistentvolume".spec.local.path
|
||||
== "/var/lib/nixos-containers/swarm-grafana/var/lib/grafana-secret"
|
||||
&&
|
||||
objects."swarm-grafana-legacy-key/persistentvolume".spec.nodeAffinity.required.nodeSelectorTerms
|
||||
!= [ ]
|
||||
&&
|
||||
objects."swarm-grafana-legacy-key/persistentvolumeclaim".spec.volumeName
|
||||
== "swarm-grafana-legacy-key"
|
||||
&& init.securityContext.runAsUser == 196
|
||||
&& init.securityContext.runAsNonRoot
|
||||
&& !(side.securityContext ? runAsUser)
|
||||
# The listener credential comes from the Secret the host publishes.
|
||||
&& lib.any (s: (s.secret.name or null) == "swarm-grafana-credentials") credentials.projected.sources
|
||||
# The Service and the policy: only the node in, and out to DNS, the
|
||||
|
|
|
|||
Loading…
Reference in a new issue