grafana: mint the admin password into the store and apply it on every start
Grafana's built-in admin kept the nixpkgs default password `admin`, and Grafana's API takes basic auth whatever `auth.disable_login_form` says, so the public vhost gave server-admin to `admin:admin`. `swarm-bao-grafana-admin` creates a random password in the store under Grafana's existing leaf when none is stored (`create` without `update`, so the first one is kept), then copies it into the container. `security.admin_password` reads it for a new database, and a `grafana cli admin reset-admin-password` in `grafana.service`'s `preStart` applies it to an existing one on every start. Closes #5138
This commit is contained in:
parent
cf4c4cde87
commit
993f5fd99d
7 changed files with 251 additions and 48 deletions
|
|
@ -66,6 +66,12 @@ let
|
|||
"swarm-bao-secret-publisher-policy"
|
||||
];
|
||||
};
|
||||
# ./swarm-grafana.nix, under Grafana's leaf, which may create the admin
|
||||
# password only once that policy is written.
|
||||
swarm-bao-grafana-admin = {
|
||||
here = deployCfg.grafana.enable;
|
||||
policies = [ "swarm-bao-grafana-oidc-policy" ];
|
||||
};
|
||||
# ./hive-matrix.nix, under matrix-ctl's leaf.
|
||||
swarm-bao-matrix-oidc = {
|
||||
here = deployCfg.matrix.enable;
|
||||
|
|
|
|||
|
|
@ -850,11 +850,17 @@ let
|
|||
# Singletons, so a list of one rather than a second shape: the granting unit
|
||||
# below takes a list either way and the units stay identical apart from
|
||||
# the objects they write.
|
||||
#
|
||||
# Grafana's leaf also creates its admin password (`swarm-bao-grafana-admin`
|
||||
# in ./swarm-grafana.nix). `create` without `update`, so a stored password is
|
||||
# never replaced.
|
||||
grafanaOidcReaders = [
|
||||
{
|
||||
name = "swarm-grafana-oidc";
|
||||
cn = baoDeploy.grafanaOidcCommonName;
|
||||
policyText = readStanza "${credentialMountPath}/data/swarm/services/${hyperhiveCfg.swarm.grafana.oidc.clientId}/oidc/client";
|
||||
policyText =
|
||||
readStanza "${credentialMountPath}/data/swarm/services/${hyperhiveCfg.swarm.grafana.oidc.clientId}/oidc/client"
|
||||
+ createStanza "${credentialMountPath}/data/swarm/services/${hyperhiveCfg.swarm.grafana.oidc.clientId}/admin/password";
|
||||
}
|
||||
];
|
||||
|
||||
|
|
|
|||
|
|
@ -253,6 +253,15 @@ let
|
|||
# the OIDC secret above, whose other reader is authelia's container.
|
||||
secretKeyPath = "/var/lib/grafana-secret/secret_key";
|
||||
|
||||
# The built-in `admin` account's password. Grafana's API takes basic auth
|
||||
# whatever the login form says, so this account is reachable from the public
|
||||
# vhost and its password cannot be the upstream default. Minted once into the
|
||||
# store by `swarm-bao-grafana-admin` below, which `swarm-bao.nix`'s
|
||||
# `grafanaOidcReaders` grants create and read on this one leaf.
|
||||
adminPasswordStorePath = "secret/swarm/services/${cfg.oidc.clientId}/admin/password";
|
||||
adminPasswordPath = "/var/lib/grafana-admin/password";
|
||||
hostAdminPasswordPath = "/var/lib/nixos-containers/${cfg.machine}${adminPasswordPath}";
|
||||
|
||||
socketPath = "${deployCfg.grafana.socketDir}/grafana.sock";
|
||||
|
||||
# Both static NixOS ids, and both checked rather than assumed: nginx has
|
||||
|
|
@ -656,6 +665,97 @@ in
|
|||
'';
|
||||
};
|
||||
|
||||
# The admin password: created in the store if it holds none, then copied
|
||||
# into the container, under Grafana's own leaf. Same shape as
|
||||
# `swarm-bao-nats-callout-xkey` in ./swarm-nats.nix. The grant is
|
||||
# `create` without `update`, so the first password stored is the one every
|
||||
# later run reads, and a race between two runs leaves one value.
|
||||
#
|
||||
# Grafana does not start without the file (`$__file{}` on a missing path is
|
||||
# a startup error), so a store that is unreachable on first boot keeps
|
||||
# Grafana down rather than up with the upstream default.
|
||||
systemd.services.swarm-bao-grafana-admin = {
|
||||
description = "create Grafana's admin password in the swarm secret store if absent and fetch it";
|
||||
after = lib.optionals baoDeploy.enable [
|
||||
"swarm-bao-pki.service"
|
||||
"container@${baoCfg.machine}.service"
|
||||
];
|
||||
wants = lib.optionals baoDeploy.enable [ "container@${baoCfg.machine}.service" ];
|
||||
requires = lib.optionals baoDeploy.enable [ "swarm-bao-pki.service" ];
|
||||
before = [ "container@${cfg.machine}.service" ];
|
||||
wantedBy = [
|
||||
"multi-user.target"
|
||||
"container@${cfg.machine}.service"
|
||||
];
|
||||
path = [
|
||||
baoDeploy.package
|
||||
pkgs.coreutils
|
||||
pkgs.openssl
|
||||
pkgs.systemd
|
||||
];
|
||||
inherit (storeRetry) startLimitBurst startLimitIntervalSec;
|
||||
serviceConfig = storeRetry.serviceConfig // {
|
||||
Type = "oneshot";
|
||||
RemainAfterExit = true;
|
||||
SyslogIdentifier = "swarm-bao-grafana-admin";
|
||||
TimeoutStartSec = 30;
|
||||
};
|
||||
environment = {
|
||||
BAO_ADDR = "https://${baoCfg.domain}:${toString baoCfg.port}";
|
||||
BAO_CLIENT_CERT = baoDeploy.grafanaOidcClientCertFile;
|
||||
BAO_CLIENT_KEY = baoDeploy.grafanaOidcClientKeyFile;
|
||||
}
|
||||
// lib.optionalAttrs (baoDeploy.serverCaFile != null) {
|
||||
BAO_CACERT = baoDeploy.serverCaFile;
|
||||
};
|
||||
script = ''
|
||||
set -euo pipefail
|
||||
|
||||
${atomicWriteSecret}
|
||||
${refreshConsumer}
|
||||
|
||||
err="$(mktemp)"
|
||||
trap 'rm -f "$err"' EXIT
|
||||
|
||||
if ! BAO_TOKEN="$(bao login -method=cert -token-only 2>"$err")"; then
|
||||
echo "could not log in to swarm-bao with Grafana's certificate; leaving Grafana's admin password file as it is." >&2
|
||||
cat "$err" >&2
|
||||
exit 1
|
||||
fi
|
||||
export BAO_TOKEN
|
||||
|
||||
# `bao kv get` exits 2 both for "nothing stored" and for a refusal, so
|
||||
# a failed read always tries the create, and the second read decides.
|
||||
# The value reaches `bao` on stdin, never in an argument.
|
||||
if ! password="$(bao kv get -field=value ${lib.escapeShellArg adminPasswordStorePath} 2>"$err")"; then
|
||||
minted="$(openssl rand -hex 32)"
|
||||
if ! printf '%s' "$minted" | bao kv put -cas=0 ${lib.escapeShellArg adminPasswordStorePath} value=- >/dev/null 2>"$err"; then
|
||||
echo "could not create ${adminPasswordStorePath}; reading it back in case another run created it." >&2
|
||||
cat "$err" >&2
|
||||
fi
|
||||
if ! password="$(bao kv get -field=value ${lib.escapeShellArg adminPasswordStorePath} 2>"$err")"; then
|
||||
echo "swarm-bao did not return ${adminPasswordStorePath}; Grafana does not start without it." >&2
|
||||
cat "$err" >&2
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
if [ -z "$password" ]; then
|
||||
echo "swarm-bao returned an empty ${adminPasswordStorePath}; leaving the file as it is." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Owner and mode as for the OIDC secret above, for its reasons.
|
||||
install -d -m 0755 ${lib.escapeShellArg (builtins.dirOf hostAdminPasswordPath)}
|
||||
if secret_differs ${lib.escapeShellArg hostAdminPasswordPath} "$password"; then
|
||||
atomic_write_secret 0400 ${lib.escapeShellArg "${toString config.ids.uids.grafana}:0"} ${lib.escapeShellArg hostAdminPasswordPath} "$password"
|
||||
fi
|
||||
|
||||
# Grafana's `preStart` applies the file to the database, so a file that
|
||||
# changed after it started needs a restart.
|
||||
refresh_consumer ${lib.escapeShellArg cfg.machine} grafana.service ${lib.escapeShellArg hostAdminPasswordPath}
|
||||
'';
|
||||
};
|
||||
|
||||
containers.${cfg.machine} = {
|
||||
autoStart = true;
|
||||
ephemeral = false;
|
||||
|
|
@ -681,7 +781,7 @@ in
|
|||
// caTrust.bindMount;
|
||||
|
||||
config =
|
||||
{ ... }:
|
||||
{ config, ... }:
|
||||
{
|
||||
imports = [
|
||||
../container-modules/swarm-container.nix
|
||||
|
|
@ -752,6 +852,32 @@ in
|
|||
'';
|
||||
};
|
||||
|
||||
# Upstream applies `security.admin_password` only when it creates the
|
||||
# database, so a database created before that setting keeps the
|
||||
# password it was created with. This resets it from the delivered
|
||||
# file on every start, before the server listens.
|
||||
#
|
||||
# `grafana cli` must open the database the server opens, which the
|
||||
# server's config.ini names. nixpkgs keeps that file private, so it
|
||||
# is rendered again from the same settings with nixpkgs' formatter.
|
||||
systemd.services.grafana.preStart =
|
||||
let
|
||||
grafana = config.services.grafana;
|
||||
configFile =
|
||||
(pkgs.formats.ini {
|
||||
listToValue = lib.concatMapStringsSep " " (lib.generators.mkValueStringDefault { });
|
||||
mkKeyValue = lib.generators.mkKeyValueDefault {
|
||||
mkValueString = v: if v == null then "" else lib.generators.mkValueStringDefault { } v;
|
||||
} "=";
|
||||
}).generate
|
||||
"config.ini"
|
||||
grafana.settings;
|
||||
in
|
||||
lib.mkAfter ''
|
||||
${lib.getExe' grafana.package "grafana"} cli --homepath ${grafana.dataDir} --config ${configFile} \
|
||||
admin reset-admin-password --password-from-stdin < ${adminPasswordPath}
|
||||
'';
|
||||
|
||||
# The group Grafana chgrps its socket to. Declared here because a
|
||||
# container has its own user database — the host knowing gid 60 as
|
||||
# `nginx` means nothing in here, only the number crosses.
|
||||
|
|
@ -837,10 +963,14 @@ in
|
|||
# whole host fails to build. The unit below generates it.
|
||||
security.secret_key = "$__file{${secretKeyPath}}";
|
||||
|
||||
# No local password path, ever. This is not tidiness: Grafana
|
||||
# ships an `admin`/`admin` account and this vhost is on the
|
||||
# public gateway, so SSO is the only way in — not merely the
|
||||
# preferred one.
|
||||
# The password a newly created database gets; the `preStart`
|
||||
# above gives an existing one the same. Written by
|
||||
# `swarm-bao-grafana-admin` on the host.
|
||||
security.admin_password = "$__file{${adminPasswordPath}}";
|
||||
|
||||
# No password box on the public login page: people log in through
|
||||
# SSO. This hides the form only; the API still takes basic auth,
|
||||
# which is why the admin password above is minted.
|
||||
#
|
||||
# Not conditional on anything, and the block below is not
|
||||
# either, which is what keeps the two from ever disagreeing.
|
||||
|
|
|
|||
|
|
@ -557,13 +557,28 @@ let
|
|||
# for this one is the OTHER service's: `services/*` would have granted
|
||||
# both, and the two are separate principals precisely because a
|
||||
# dashboard is not entitled to a collector's credential.
|
||||
name = "the Grafana OIDC reader's grant is Grafana's own client secret and nothing else";
|
||||
#
|
||||
# The admin password is the one leaf it may create, and without `update`:
|
||||
# a grant that could replace it would let a second run rotate the
|
||||
# password Grafana's database already holds.
|
||||
name = "the Grafana reader reads its own client secret and creates its admin password, and nothing else";
|
||||
ok =
|
||||
let
|
||||
s = baoGrantHere.systemd.services.swarm-bao-grafana-oidc-policy.script;
|
||||
in
|
||||
lib.hasInfix "path \"secret/data/swarm/services/swarm-grafana/oidc/client\" {" s
|
||||
&& lib.hasInfix "capabilities = [\"read\"]" s
|
||||
grantsIn s == [
|
||||
{
|
||||
path = "secret/data/swarm/services/swarm-grafana/oidc/client";
|
||||
caps = [ "read" ];
|
||||
}
|
||||
{
|
||||
path = "secret/data/swarm/services/swarm-grafana/admin/password";
|
||||
caps = [
|
||||
"create"
|
||||
"read"
|
||||
];
|
||||
}
|
||||
]
|
||||
&& !(lib.hasInfix "secret/data/swarm/agents" s)
|
||||
&& !(lib.hasInfix "secret/data/swarm/hives" s)
|
||||
&& !(lib.hasInfix "secret/data/swarm/services/*" s)
|
||||
|
|
@ -827,6 +842,20 @@ let
|
|||
&& !(noReaders ? swarm-bao-matrix-oidc)
|
||||
&& !(noReaders ? swarm-bao-nats-callout-xkey);
|
||||
}
|
||||
{
|
||||
# The admin password is created under Grafana's leaf, so its unit waits
|
||||
# for the unit writing that leaf's role and policy; before it, the create
|
||||
# is refused. The second arm is the absence control.
|
||||
name = "grafana's admin password unit is ordered after grafana's policy unit";
|
||||
ok =
|
||||
let
|
||||
u = baoGrantWithConsumers.systemd.services.swarm-bao-grafana-admin;
|
||||
in
|
||||
lib.elem "swarm-bao-grafana-oidc-policy.service" u.after
|
||||
&& lib.elem "swarm-bao-grafana-oidc-policy.service" u.wants
|
||||
&& !(lib.elem "swarm-bao-grafana-oidc-policy.service" u.requires)
|
||||
&& !(baoGrantNoReaders.systemd.services ? swarm-bao-grafana-admin);
|
||||
}
|
||||
{
|
||||
# The ordering is set apart from each reader's own definition, so it can
|
||||
# define a reader by itself: `after` on a unit nothing else declares is a
|
||||
|
|
@ -959,6 +988,7 @@ let
|
|||
[
|
||||
"swarm-services-cert"
|
||||
"swarm-bao-forwarder-oidc"
|
||||
"swarm-bao-grafana-admin"
|
||||
]
|
||||
++ policyReaders
|
||||
);
|
||||
|
|
|
|||
|
|
@ -123,9 +123,8 @@ let
|
|||
{
|
||||
# This fixture enables grafana and NOT authelia, so the login-form toggle
|
||||
# has to follow "grafana requires SSO" rather than "both services are on
|
||||
# this host". Grafana ships an
|
||||
# `admin`/`admin` account and its vhost is on the public gateway, so a
|
||||
# password box there is a way in whatever the topology.
|
||||
# this host". Its vhost is on the public gateway, so a password box there
|
||||
# is a way in whatever the topology.
|
||||
name = "grafana disables its local login form even where authelia is not on this host";
|
||||
ok =
|
||||
grafanaOldPath.containers.swarm-grafana.config.services.grafana.settings.auth.disable_login_form;
|
||||
|
|
@ -196,6 +195,36 @@ let
|
|||
&& !(grafanaRefusedFor grafanaRemoteAuthelia "services.hyperhive.swarm.authelia.url")
|
||||
&& !(grafanaRefusedFor grafanaRemoteAuthelia "deploy.bao.grafanaOidcClientCertFile");
|
||||
}
|
||||
{
|
||||
# Grafana's API takes basic auth whatever the login form says, so the
|
||||
# built-in admin's password is the only thing between the public vhost
|
||||
# and server-admin. Upstream sets it only when it creates the database,
|
||||
# so both halves are pinned: the setting a new database reads, and the
|
||||
# reset every start applies to whichever database exists. Without the
|
||||
# reset, a database created before the setting keeps `admin`.
|
||||
#
|
||||
# The reset's `--config` must be the file the server reads, or it resets
|
||||
# a different database: compared by store path against the server's own
|
||||
# command line.
|
||||
name = "grafana's admin password is minted into the store and applied to new and existing databases";
|
||||
ok =
|
||||
let
|
||||
unit = grafanaRemoteAuthelia.systemd.services.swarm-bao-grafana-admin;
|
||||
grafana = grafanaRemoteAuthelia.containers.swarm-grafana.config.services.grafana;
|
||||
service = grafanaRemoteAuthelia.containers.swarm-grafana.config.systemd.services.grafana;
|
||||
resetConfig = lib.head (
|
||||
lib.head (lib.filter lib.isList (builtins.split "--config ([^ ]+)" service.preStart))
|
||||
);
|
||||
in
|
||||
lib.hasInfix "bao kv put -cas=0 secret/swarm/services/swarm-grafana/admin/password value=-" unit.script
|
||||
&& lib.hasInfix "openssl rand -hex 32" unit.script
|
||||
&& lib.hasInfix "atomic_write_secret 0400 196:0 /var/lib/nixos-containers/swarm-grafana/var/lib/grafana-admin/password" unit.script
|
||||
&& lib.elem "container@swarm-grafana.service" unit.before
|
||||
&& unit.environment.BAO_CLIENT_CERT == "/etc/pki/bao-grafana-oidc.pem"
|
||||
&& grafana.settings.security.admin_password == "$__file{/var/lib/grafana-admin/password}"
|
||||
&& lib.hasInfix "admin reset-admin-password --password-from-stdin < /var/lib/grafana-admin/password" service.preStart
|
||||
&& lib.hasInfix "-config ${resetConfig}" service.script;
|
||||
}
|
||||
{
|
||||
# Same 403-not-a-miss reason as the matrix and queue arms below: the
|
||||
# reader's grant covers the `services` prefix, so a path outside it is
|
||||
|
|
|
|||
Loading…
Reference in a new issue