The Deployment swarm-grafana replaces the swarm-grafana container: one replica on a static local volume that is the container's data directory, an init container that fetches the OIDC client secret and creates the administrator password, the datasources' bearer and the encryption key in the secret store, and a sidecar that keeps the controller's Viewer token. The datasources reach the stores through the gateway with that bearer, and Grafana's /metrics asks for the stores' kind of credential, which the vhost sends. The five host units, the journal forwarder, its client glue and the unix-socket directory go with the container.
269 lines
12 KiB
Nix
269 lines
12 KiB
Nix
# The listener credentials of the Victoria stores and Grafana the cluster runs and
|
|
# of the queue's exporter: the unit that creates each in the swarm secret store when it
|
|
# holds none, hands it to the gateway's nginx and publishes it to the cluster as
|
|
# the Secret the pod reads it from (the store's password, the exporter's bcrypt
|
|
# hash). The flags and the locations that send the credential are in
|
|
# ../swarm-victorialogs.nix, ../swarm-victoriametrics.nix and ../swarm-grafana.nix,
|
|
# the exporter's in ../swarm-nats.nix; ../lib/store-auth.nix names what they share.
|
|
# docs/4-implementation/k8s/runtime.md has the design.
|
|
{
|
|
pkgs,
|
|
lib,
|
|
config,
|
|
...
|
|
}:
|
|
let
|
|
hyperhiveCfg = config.services.hyperhive;
|
|
deployCfg = hyperhiveCfg.deploy;
|
|
k8sCfg = hyperhiveCfg.k8s;
|
|
node = deployCfg.k8s;
|
|
baoCfg = hyperhiveCfg.swarm.bao;
|
|
baoDeploy = deployCfg.bao;
|
|
|
|
storeAuth = import ../lib/store-auth.nix;
|
|
storeRetry = import ../lib/store-retry.nix { };
|
|
|
|
# The gateway's group: nginx's master and workers run as its user, and read
|
|
# the files `include`d into the store locations.
|
|
nginxGroup = config.services.nginx.group;
|
|
|
|
# The pods this host runs that take the credential, by the name `store-auth.nix` takes.
|
|
stores =
|
|
lib.optional deployCfg.victorialogs.enable "victorialogs"
|
|
++ lib.optional deployCfg.victoriametrics.enable "victoriametrics"
|
|
++ lib.optional deployCfg.grafana.enable "grafana";
|
|
|
|
# The queue's exporter runs in the queue's pod.
|
|
exporterOn = deployCfg.nats.enable;
|
|
exporter = storeAuth.exporter;
|
|
credentialNames = stores ++ lib.optional exporterOn "the queue's exporter";
|
|
|
|
haveIdentity =
|
|
baoDeploy.storeAuthClientCertFile != null && baoDeploy.storeAuthClientKeyFile != null;
|
|
|
|
# Where the store runs on this host, the first login waits for its container.
|
|
# No `wants`: a pull would start the container mid-switch on the old closure.
|
|
storeContainer = lib.optional baoDeploy.enable "container@${baoCfg.machine}.service";
|
|
|
|
kubeconfig = "/etc/rancher/k3s/k3s.yaml";
|
|
|
|
publish = store: ''
|
|
publish ${
|
|
lib.escapeShellArgs [
|
|
store
|
|
"secret/${storeAuth.leaf store}"
|
|
(storeAuth.passwordFile store)
|
|
(storeAuth.nginxFile store)
|
|
(storeAuth.secretName store)
|
|
storeAuth.user
|
|
]
|
|
}
|
|
'';
|
|
in
|
|
{
|
|
config = lib.mkIf (stores != [ ] || exporterOn) (
|
|
lib.mkMerge [
|
|
{
|
|
# The group exists on a host where the NixOS nginx unit is off too, since
|
|
# the files below are made with it.
|
|
users.groups.${nginxGroup} = { };
|
|
|
|
# The files exist before nginx first reads its configuration, because
|
|
# an `include` of a missing file is a fatal error there. `f` creates
|
|
# a file and never overwrites one, and an empty file sets no header:
|
|
# the store then refuses the request, which is the only thing a
|
|
# gateway without the credential can get from it. The directory and
|
|
# the nginx files are the gateway's group's to read, which nginx needs
|
|
# to start; the passwords and the hash stay in files only root reads.
|
|
systemd.tmpfiles.rules = [
|
|
"d ${storeAuth.dir} 0750 root ${nginxGroup} - -"
|
|
]
|
|
++ map (store: "f ${storeAuth.nginxFile store} 0640 root ${nginxGroup} -") stores
|
|
++ lib.optional exporterOn "f ${exporter.nginxFile} 0640 root ${nginxGroup} -";
|
|
}
|
|
|
|
(lib.mkIf (node.enable && k8sCfg.enable) {
|
|
assertions = [
|
|
{
|
|
assertion = haveIdentity;
|
|
message = ''
|
|
The ${lib.concatStringsSep " and " credentialNames} the cluster runs takes a credential, which a unit
|
|
creates in the swarm secret store and publishes under the identity
|
|
services.hyperhive.deploy.bao.storeAuthClientCertFile and
|
|
storeAuthClientKeyFile name (the leaf the store mints where
|
|
services.hyperhive.deploy.bao.enable is set on this host).
|
|
'';
|
|
}
|
|
];
|
|
|
|
systemd.services.swarm-bao-store-auth = lib.mkIf haveIdentity {
|
|
description = "create the stores', Grafana's and the queue exporter's listener credentials in the swarm secret store if absent, and publish them";
|
|
wantedBy = [ "multi-user.target" ];
|
|
wants = [ "network-online.target" ];
|
|
after = [
|
|
"k3s.service"
|
|
"network-online.target"
|
|
]
|
|
++ storeContainer;
|
|
path = [
|
|
baoDeploy.package
|
|
pkgs.apacheHttpd
|
|
pkgs.coreutils
|
|
pkgs.gnugrep
|
|
pkgs.gnused
|
|
pkgs.kubectl
|
|
pkgs.openssl
|
|
pkgs.systemd
|
|
];
|
|
# ./lib/store-retry.nix: the login fails for as long as the store is
|
|
# sealed or down.
|
|
#
|
|
# `Type = "simple"`, as `hyperhive-k8s-ca-trust` is (./runtime.nix):
|
|
# the start job is done once the script has forked. A oneshot's
|
|
# start job stays pending through its restarts, so a boot or a
|
|
# switch with the store sealed held `multi-user.target` and
|
|
# `switch-to-configuration` for the whole retry budget. Nothing
|
|
# orders after this unit. `RemainAfterExit` keeps it `active
|
|
# (exited)` once the credential is out, so `SubState=exited` is the
|
|
# state that says so: `is-active` is true while the script still runs.
|
|
inherit (storeRetry) startLimitBurst startLimitIntervalSec;
|
|
serviceConfig = storeRetry.serviceConfig // {
|
|
Type = "simple";
|
|
RemainAfterExit = true;
|
|
UMask = "0077";
|
|
SyslogIdentifier = "swarm-bao-store-auth";
|
|
};
|
|
environment = {
|
|
BAO_ADDR = "https://${baoCfg.domain}:${toString baoCfg.port}";
|
|
BAO_CLIENT_CERT = baoDeploy.storeAuthClientCertFile;
|
|
BAO_CLIENT_KEY = baoDeploy.storeAuthClientKeyFile;
|
|
KUBECONFIG = kubeconfig;
|
|
}
|
|
// lib.optionalAttrs (baoDeploy.serverCaFile != null) {
|
|
BAO_CACERT = baoDeploy.serverCaFile;
|
|
};
|
|
script = ''
|
|
set -euo pipefail
|
|
${import ../lib/store-seeded-secret.nix { }}
|
|
${import ../lib/atomic-write-secret.nix { }}
|
|
${import ../lib/refresh-consumer.nix { }}
|
|
|
|
install -d -m 0750 -o root -g ${lib.escapeShellArg nginxGroup} ${storeAuth.dir}
|
|
|
|
# A password of 64 hex digits, as Grafana's bearer is.
|
|
mint_password() {
|
|
openssl rand -hex 32
|
|
}
|
|
|
|
# <store> <leaf> <password file> <nginx file> <secret> <user>. The
|
|
# store's value wins and a leaf that holds none is created, so the
|
|
# first password stored is the one every later run reads. The nginx
|
|
# file is written first: it is what the gateway needs before the
|
|
# store answers anyone, and it needs nothing of the cluster.
|
|
# `password_changed` tells the exporter's hash step that the
|
|
# password file was rewritten.
|
|
reload=0
|
|
password_changed=0
|
|
publish() {
|
|
local store="$1" leaf="$2" password_file="$3" nginx_file="$4" secret="$5" user="$6"
|
|
local password directive
|
|
password="$(store_seeded "$leaf" /nonexistent mint_password)" || return 1
|
|
directive="proxy_set_header Authorization \"Basic $(printf '%s:%s' "$user" "$password" | base64 -w0)\";"
|
|
|
|
if secret_differs "$nginx_file" "$directive"; then
|
|
atomic_write_secret 0640 root:${nginxGroup} "$nginx_file" "$directive"
|
|
reload=1
|
|
elif written_after_start nginx.service "$nginx_file"; then
|
|
reload=1
|
|
fi
|
|
# A file that already holds the value keeps the owner it was written with.
|
|
chown root:${nginxGroup} "$nginx_file"
|
|
chmod 0640 "$nginx_file"
|
|
if secret_differs "$password_file" "$password"; then
|
|
atomic_write_secret 0600 root:root "$password_file" "$password"
|
|
password_changed=1
|
|
fi
|
|
echo "the credential of $store is at $nginx_file and $password_file; the Secret $secret follows."
|
|
}
|
|
|
|
# <password file> <hash file>. The exporter takes the hash of its
|
|
# password on its command line, so that is all the cluster is given.
|
|
# `htpasswd` reads the password from stdin, never from an argument,
|
|
# and writes `$2y$`, which the exporter would take for a plain
|
|
# password (it treats only `$2a$` as bcrypt): the prefix is
|
|
# rewritten, and a hash that is not `$2a$<cost>$` and 60 characters
|
|
# stops the unit. A new hash only when there is none or the
|
|
# password changed, since each carries a fresh salt.
|
|
exporter_hash() {
|
|
local password_file="$1" hash_file="$2" hash
|
|
if [ "$password_changed" != 1 ] && [ -s "$hash_file" ]; then
|
|
return 0
|
|
fi
|
|
hash="$(htpasswd -niBC ${toString exporter.cost} x < "$password_file" | cut -d: -f2- | sed 's/^[$]2y[$]/$2a$/')"
|
|
case "$hash" in
|
|
'${exporter.hashPrefix}${toString exporter.cost}$'*) ;;
|
|
*)
|
|
echo 'htpasswd did not give a $2a$<cost>$ hash' >&2
|
|
return 1
|
|
;;
|
|
esac
|
|
if [ "''${#hash}" != 60 ]; then
|
|
echo "the hash is ''${#hash} characters, not 60" >&2
|
|
return 1
|
|
fi
|
|
atomic_write_secret 0600 root:root "$hash_file" "$hash"
|
|
}
|
|
|
|
store_init
|
|
store_login
|
|
if [ "$STORE_UP" != 1 ]; then
|
|
echo "could not log in to the swarm secret store; the listener credentials are not published." >&2
|
|
exit 1
|
|
fi
|
|
${lib.concatMapStrings publish stores}
|
|
${lib.optionalString exporterOn ''
|
|
publish ${
|
|
lib.escapeShellArgs [
|
|
"nats-exporter"
|
|
"secret/${exporter.leaf}"
|
|
exporter.passwordFile
|
|
exporter.nginxFile
|
|
exporter.secretName
|
|
exporter.user
|
|
]
|
|
}
|
|
exporter_hash ${
|
|
lib.escapeShellArgs [
|
|
exporter.passwordFile
|
|
exporter.hashFile
|
|
]
|
|
}
|
|
''}
|
|
|
|
# A running nginx reads its files at a reload; one that is not
|
|
# running reads them as it starts.
|
|
if [ "$reload" = 1 ] && systemctl is-active --quiet nginx.service; then
|
|
systemctl reload nginx.service
|
|
fi
|
|
|
|
# A pod that loads the Secret waits in ContainerCreating until this
|
|
# has run.
|
|
timeout 600 ${pkgs.bash}/bin/bash -c ${lib.escapeShellArg "until kubectl get namespace ${k8sCfg.namespace} >/dev/null 2>&1; do sleep 2; done"}
|
|
${lib.concatMapStrings (store: ''
|
|
kubectl --namespace ${k8sCfg.namespace} create secret generic ${storeAuth.secretName store} \
|
|
--from-file=${storeAuth.credentialId}=${storeAuth.passwordFile store} \
|
|
--dry-run=client --output=yaml \
|
|
| kubectl --namespace ${k8sCfg.namespace} apply --server-side --force-conflicts --filename=-
|
|
'') stores}
|
|
${lib.optionalString exporterOn ''
|
|
kubectl --namespace ${k8sCfg.namespace} create secret generic ${exporter.secretName} \
|
|
--from-literal=${exporter.hashKey}="$(< ${exporter.hashFile})" \
|
|
--dry-run=client --output=yaml \
|
|
| kubectl --namespace ${k8sCfg.namespace} apply --server-side --force-conflicts --filename=-
|
|
''}
|
|
'';
|
|
};
|
|
})
|
|
]
|
|
);
|
|
}
|