Each store runs with -httpAuth.* and a credential from the secret store, published to the namespace as a Secret by the host. The gateway's locations send it after auth_request has passed, and the collector's scrape and push take the same one, so a process on the node that bypasses the gateway is refused.
180 lines
7.2 KiB
Nix
180 lines
7.2 KiB
Nix
# The listener credential of the Victoria stores the cluster runs: the unit that
|
|
# creates it in the swarm secret store when the store holds none, hands it to
|
|
# the gateway's nginx and publishes it to the cluster as the Secret the store's
|
|
# pod loads its password from. The store's flags and the locations that send it
|
|
# are in ../swarm-victorialogs.nix and ../swarm-victoriametrics.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 stores this host runs, by the name `store-auth.nix` takes.
|
|
stores =
|
|
lib.optional deployCfg.victorialogs.enable "victorialogs"
|
|
++ lib.optional deployCfg.victoriametrics.enable "victoriametrics";
|
|
|
|
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)
|
|
]
|
|
}
|
|
'';
|
|
in
|
|
{
|
|
config = lib.mkIf (stores != [ ]) (
|
|
lib.mkMerge [
|
|
{
|
|
# 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.
|
|
systemd.tmpfiles.rules = [
|
|
"d ${storeAuth.dir} 0700 root root - -"
|
|
]
|
|
++ map (store: "f ${storeAuth.nginxFile store} 0600 root root -") stores;
|
|
}
|
|
|
|
(lib.mkIf (node.enable && k8sCfg.enable) {
|
|
assertions = [
|
|
{
|
|
assertion = haveIdentity;
|
|
message = ''
|
|
The ${lib.concatStringsSep " and " stores} 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 Victoria stores' listener credential in the swarm secret store if absent, and publish it";
|
|
wantedBy = [ "multi-user.target" ];
|
|
wants = [ "network-online.target" ];
|
|
after = [
|
|
"k3s.service"
|
|
"network-online.target"
|
|
]
|
|
++ storeContainer;
|
|
path = [
|
|
baoDeploy.package
|
|
pkgs.coreutils
|
|
pkgs.gnugrep
|
|
pkgs.kubectl
|
|
pkgs.openssl
|
|
pkgs.systemd
|
|
];
|
|
# ./lib/store-retry.nix: the login fails for as long as the store is
|
|
# sealed or down.
|
|
inherit (storeRetry) startLimitBurst startLimitIntervalSec;
|
|
serviceConfig = storeRetry.serviceConfig // {
|
|
Type = "oneshot";
|
|
RemainAfterExit = true;
|
|
UMask = "0077";
|
|
SyslogIdentifier = "swarm-bao-store-auth";
|
|
TimeoutStartSec = "15min";
|
|
};
|
|
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 0700 ${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>. 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.
|
|
reload=0
|
|
publish() {
|
|
local store="$1" leaf="$2" password_file="$3" nginx_file="$4" secret="$5"
|
|
local password directive
|
|
password="$(store_seeded "$leaf" /nonexistent mint_password)" || return 1
|
|
directive="proxy_set_header Authorization \"Basic $(printf '%s:%s' ${lib.escapeShellArg storeAuth.user} "$password" | base64 -w0)\";"
|
|
|
|
if secret_differs "$nginx_file" "$directive"; then
|
|
atomic_write_secret 0600 root:root "$nginx_file" "$directive"
|
|
reload=1
|
|
elif written_after_start nginx.service "$nginx_file"; then
|
|
reload=1
|
|
fi
|
|
if secret_differs "$password_file" "$password"; then
|
|
atomic_write_secret 0600 root:root "$password_file" "$password"
|
|
fi
|
|
echo "the credential of $store is at $nginx_file and $password_file; the Secret $secret follows."
|
|
}
|
|
|
|
store_init
|
|
store_login
|
|
if [ "$STORE_UP" != 1 ]; then
|
|
echo "could not log in to the swarm secret store; the stores' credentials are not published." >&2
|
|
exit 1
|
|
fi
|
|
${lib.concatMapStrings publish stores}
|
|
|
|
# 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}
|
|
'';
|
|
};
|
|
})
|
|
]
|
|
);
|
|
}
|