the queue's exporter asks every request for a login the gateway supplies
This commit is contained in:
parent
f54aae7927
commit
893b693b46
6 changed files with 488 additions and 133 deletions
|
|
@ -46,7 +46,7 @@ let
|
|||
swarm-bao-ci-runner = deployCfg.ci-runner.enable;
|
||||
# ./k8s/store-credentials.nix
|
||||
swarm-bao-store-auth =
|
||||
(deployCfg.victorialogs.enable || deployCfg.victoriametrics.enable)
|
||||
(deployCfg.victorialogs.enable || deployCfg.victoriametrics.enable || deployCfg.nats.enable)
|
||||
&& deployCfg.k8s.enable
|
||||
&& hyperhiveCfg.k8s.enable
|
||||
&& havePair baoDeploy.storeAuthClientCertFile baoDeploy.storeAuthClientKeyFile;
|
||||
|
|
|
|||
|
|
@ -1,9 +1,11 @@
|
|||
# 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.
|
||||
# The listener credentials of the Victoria stores 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 stores' flags and the locations that send the credential are in
|
||||
# ../swarm-victorialogs.nix and ../swarm-victoriametrics.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,
|
||||
|
|
@ -26,6 +28,11 @@ let
|
|||
lib.optional deployCfg.victorialogs.enable "victorialogs"
|
||||
++ lib.optional deployCfg.victoriametrics.enable "victoriametrics";
|
||||
|
||||
# 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;
|
||||
|
||||
|
|
@ -43,12 +50,13 @@ let
|
|||
(storeAuth.passwordFile store)
|
||||
(storeAuth.nginxFile store)
|
||||
(storeAuth.secretName store)
|
||||
storeAuth.user
|
||||
]
|
||||
}
|
||||
'';
|
||||
in
|
||||
{
|
||||
config = lib.mkIf (stores != [ ]) (
|
||||
config = lib.mkIf (stores != [ ] || exporterOn) (
|
||||
lib.mkMerge [
|
||||
{
|
||||
# The files exist before nginx first reads its configuration, because
|
||||
|
|
@ -59,7 +67,8 @@ in
|
|||
systemd.tmpfiles.rules = [
|
||||
"d ${storeAuth.dir} 0700 root root - -"
|
||||
]
|
||||
++ map (store: "f ${storeAuth.nginxFile store} 0600 root root -") stores;
|
||||
++ map (store: "f ${storeAuth.nginxFile store} 0600 root root -") stores
|
||||
++ lib.optional exporterOn "f ${exporter.nginxFile} 0600 root root -";
|
||||
}
|
||||
|
||||
(lib.mkIf (node.enable && k8sCfg.enable) {
|
||||
|
|
@ -67,7 +76,7 @@ in
|
|||
{
|
||||
assertion = haveIdentity;
|
||||
message = ''
|
||||
The ${lib.concatStringsSep " and " stores} the cluster runs takes a credential, which a unit
|
||||
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
|
||||
|
|
@ -77,7 +86,7 @@ in
|
|||
];
|
||||
|
||||
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";
|
||||
description = "create the Victoria stores' 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 = [
|
||||
|
|
@ -87,8 +96,10 @@ in
|
|||
++ storeContainer;
|
||||
path = [
|
||||
baoDeploy.package
|
||||
pkgs.apacheHttpd
|
||||
pkgs.coreutils
|
||||
pkgs.gnugrep
|
||||
pkgs.gnused
|
||||
pkgs.kubectl
|
||||
pkgs.openssl
|
||||
pkgs.systemd
|
||||
|
|
@ -125,17 +136,20 @@ in
|
|||
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.
|
||||
# <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"
|
||||
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' ${lib.escapeShellArg storeAuth.user} "$password" | base64 -w0)\";"
|
||||
directive="proxy_set_header Authorization \"Basic $(printf '%s:%s' "$user" "$password" | base64 -w0)\";"
|
||||
|
||||
if secret_differs "$nginx_file" "$directive"; then
|
||||
atomic_write_secret 0600 root:root "$nginx_file" "$directive"
|
||||
|
|
@ -145,10 +159,39 @@ in
|
|||
fi
|
||||
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
|
||||
|
|
@ -156,6 +199,24 @@ in
|
|||
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.
|
||||
|
|
@ -172,6 +233,12 @@ in
|
|||
--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=-
|
||||
''}
|
||||
'';
|
||||
};
|
||||
})
|
||||
|
|
|
|||
|
|
@ -28,4 +28,26 @@ rec {
|
|||
# The Secret the store's pod loads its credentials from
|
||||
# (../k8s/render.nix names it `<service>-credentials`).
|
||||
secretName = store: "swarm-${store}-credentials";
|
||||
|
||||
# The queue's exporter takes the same kind of login, with a difference: its
|
||||
# flag takes no file, so the pod gets the password's bcrypt hash from the
|
||||
# Secret `exporter.secretName` (key `hashKey`, environment variable
|
||||
# `hashEnv`, argument `-http_pass`), and the gateway's `/metrics` location
|
||||
# for the queue sends the password itself in the header `exporter.nginxFile`
|
||||
# sets. The password and the hash are files only root reads. `cost` is NATS's
|
||||
# own default for `nats server passwd`; the exporter compares with
|
||||
# `bcrypt.CompareHashAndPassword`, which reads the cost from the hash, and
|
||||
# only for a value that starts with `$2a$`.
|
||||
exporter = {
|
||||
user = "swarm-exporter";
|
||||
leaf = "swarm/services/nats/exporter-auth";
|
||||
passwordFile = "${dir}/nats.password";
|
||||
nginxFile = "${dir}/nats.conf";
|
||||
hashFile = "${dir}/nats.hash";
|
||||
secretName = "swarm-nats-exporter-auth";
|
||||
hashKey = "hash";
|
||||
hashEnv = "EXPORTER_HASH";
|
||||
cost = 11;
|
||||
hashPrefix = "$2a$";
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -366,7 +366,7 @@ let
|
|||
# (`swarm-bao-store-auth`, ./k8s/store-credentials.nix): it creates each
|
||||
# credential when the store holds none and reads it back, `create` without
|
||||
# `update`, so a stored value is never replaced. One principal for both
|
||||
# stores, a leaf each.
|
||||
# stores and the queue's exporter, a leaf each.
|
||||
storeAuthWriters = [
|
||||
{
|
||||
name = "swarm-store-auth";
|
||||
|
|
@ -376,7 +376,8 @@ let
|
|||
[
|
||||
"victorialogs"
|
||||
"victoriametrics"
|
||||
];
|
||||
]
|
||||
+ createReadStanza "${credentialMountPath}/data/${storeAuth.exporter.leaf}";
|
||||
}
|
||||
];
|
||||
|
||||
|
|
|
|||
|
|
@ -2051,10 +2051,12 @@ in
|
|||
default = "swarm-store-auth";
|
||||
description = ''
|
||||
Subject the store's `swarm-store-auth` cert-auth role accepts: the
|
||||
identity the unit that publishes the log store's and the metrics store's
|
||||
listener credentials presents from the host (./k8s/store-credentials.nix).
|
||||
Its grant is create and read on `swarm/services/victorialogs/store-auth`
|
||||
and `swarm/services/victoriametrics/store-auth`, and nothing else.
|
||||
identity the unit that publishes the log store's, the metrics store's and the
|
||||
queue exporter's listener credentials presents from the host
|
||||
(./k8s/store-credentials.nix). Its grant is create and read on
|
||||
`swarm/services/victorialogs/store-auth`,
|
||||
`swarm/services/victoriametrics/store-auth` and
|
||||
`swarm/services/nats/exporter-auth`, and nothing else.
|
||||
|
||||
⚠️ Reserved as a hive name by ./swarm.nix, like its siblings.
|
||||
'';
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ let
|
|||
machine = "swarm-nats";
|
||||
|
||||
labels = import ./k8s/labels.nix;
|
||||
storeAuth = import ./lib/store-auth.nix;
|
||||
|
||||
# The container's journal forwarder pushes to its own receiver on the swarm
|
||||
# collector, the route `serviceProducers.swarm-nats` in
|
||||
|
|
@ -1006,7 +1007,30 @@ let
|
|||
(toString deployCfg.nats.metricsPort)
|
||||
]
|
||||
++ exporterFlags
|
||||
++ [ monitorUrl ];
|
||||
++ [
|
||||
# Every request to `/metrics` needs this login. The exporter takes
|
||||
# the password only on its command line and compares it as bcrypt
|
||||
# for a value that starts with `$2a$`, so the argument is the hash
|
||||
# the host's unit publishes (./k8s/store-credentials.nix), which
|
||||
# Kubernetes substitutes from the environment. The gateway's
|
||||
# `/metrics` location sends the password. A Secret that does not
|
||||
# exist keeps the container from starting; it does not leave the
|
||||
# exporter open.
|
||||
"-http_user"
|
||||
storeAuth.exporter.user
|
||||
"-http_pass"
|
||||
"$(${storeAuth.exporter.hashEnv})"
|
||||
monitorUrl
|
||||
];
|
||||
env = [
|
||||
{
|
||||
name = storeAuth.exporter.hashEnv;
|
||||
valueFrom.secretKeyRef = {
|
||||
name = storeAuth.exporter.secretName;
|
||||
key = storeAuth.exporter.hashKey;
|
||||
};
|
||||
}
|
||||
];
|
||||
# The port the metrics Service targets.
|
||||
ports = [
|
||||
{
|
||||
|
|
@ -1049,9 +1073,10 @@ let
|
|||
};
|
||||
};
|
||||
|
||||
# The exporter answers a request with every client's name, so only the
|
||||
# node may read it: the Service is reached by name from the host's
|
||||
# gateway, and the policy admits the node's own traffic (which the
|
||||
# The exporter answers a request with every client's name, so it asks every
|
||||
# request for a login (`-http_user`, `-http_pass` above) and only the node
|
||||
# reaches it: the Service is reached by name from the host's gateway, which
|
||||
# sends the login, and the policy admits the node's own traffic (which the
|
||||
# cluster's policy engine always lets in, ./k8s/render.nix) and, from
|
||||
# anywhere else, the client port alone.
|
||||
objects."${machine}/service" = {
|
||||
|
|
@ -1177,7 +1202,8 @@ in
|
|||
the swarm collector's authelia check.
|
||||
|
||||
This is the exporter container's port in the pod, which the Service `swarm-nats-metrics` publishes on the same
|
||||
number and only the node reaches.
|
||||
number. Only the node reaches it, and it answers a request only with the
|
||||
login in `lib/store-auth.nix`.
|
||||
|
||||
⚠️ This and {option}`services.hyperhive.deploy.nats.monitorPort` are two
|
||||
more claims on a port
|
||||
|
|
@ -1399,6 +1425,9 @@ in
|
|||
resolver ${networkCfg.bridgeIp} valid=10s ipv6=off;
|
||||
set $swarm_nats_metrics_upstream ${metricsHost}:${toString deployCfg.nats.metricsPort};
|
||||
auth_request /__nats_metrics_authz;
|
||||
# The exporter's login in place of the caller's `Authorization`;
|
||||
# an empty file sends none and the exporter answers 401.
|
||||
include ${storeAuth.exporter.nginxFile};
|
||||
'';
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue