737 lines
30 KiB
Nix
737 lines
30 KiB
Nix
# The swarm's metrics store: one VictoriaMetrics for the whole swarm and the
|
||
# gateway's `auth_request` responders for it, each a pod in the host's cluster
|
||
# (./k8s/render.nix, docs/4-implementation/k8s/runtime.md).
|
||
#
|
||
# A LOCAL time-series database rather than only an external sink, and that is
|
||
# the point rather than a convenience: the swarm dashboard has to stay
|
||
# readable when the outside world is unreachable. Same failure-domain property
|
||
# the hive-status KV has — a view of the system must not depend on the system
|
||
# it is viewing being healthy.
|
||
#
|
||
# It is the collector that feeds this (the gateway OTEL collector), not the
|
||
# agents directly: one ingest point per swarm, authenticated there.
|
||
#
|
||
# The store's own listener takes writes and admin calls beside its queries, and
|
||
# asks every request for the credential in ./lib/store-auth.nix: each location
|
||
# below that dials it replaces the caller's `Authorization` with that credential
|
||
# once its own check has passed, so a caller's bearer never reaches the store.
|
||
# Its Service admits only the node's own traffic, and the vhost below, which
|
||
# dials it by name, is the way in for everything the swarm does.
|
||
{
|
||
pkgs,
|
||
lib,
|
||
config,
|
||
...
|
||
}:
|
||
let
|
||
cfg = config.services.hyperhive.swarm.victoriametrics;
|
||
deployCfg = config.services.hyperhive.deploy;
|
||
hyperhiveCfg = config.services.hyperhive;
|
||
gatewayCfg = hyperhiveCfg.gateway;
|
||
networkCfg = hyperhiveCfg.network;
|
||
k8sCfg = hyperhiveCfg.k8s;
|
||
baoCfg = hyperhiveCfg.swarm.bao;
|
||
inherit (import ../images { inherit pkgs lib; })
|
||
mkServiceImage
|
||
wgetOnly
|
||
loopbackProbe
|
||
;
|
||
storeAuth = import ./lib/store-auth.nix;
|
||
|
||
# The workloads, as the cluster names them: the store, and the one pod that
|
||
# holds both responders. Each has a Service of its name, which the gateway
|
||
# dials.
|
||
storeService = "swarm-victoriametrics";
|
||
authService = "swarm-metrics-auth";
|
||
grafanaAuthService = "swarm-metrics-grafana-auth";
|
||
storePort = 8428;
|
||
authPort = 8083;
|
||
grafanaAuthPort = 8084;
|
||
# The Grafana responder's own `/healthz` and `/readyz`, beside the agents'
|
||
# responder's on 8080 in the same pod.
|
||
grafanaAuthHealthPort = 8085;
|
||
dataDir = "/var/lib/victoriametrics";
|
||
hostDataDir = "/var/lib/nixos-containers/swarm-victoriametrics${dataDir}";
|
||
|
||
serviceHost = service: "${service}.${k8sCfg.namespace}.svc.${deployCfg.k8s.clusterDomain}";
|
||
|
||
# What a location needs to dial a Service: the host's resolver, which hands
|
||
# the cluster's domain to CoreDNS, and the name in a variable, so that nginx
|
||
# looks it up per request. A name written into `proxy_pass` gets one lookup
|
||
# when the configuration loads, which fails the load while the cluster is
|
||
# down. Each role has a variable of its own, so that an auth subrequest's
|
||
# `set` can never change where the request it checks is sent.
|
||
viaService = variable: service: port: ''
|
||
resolver ${networkCfg.bridgeIp} valid=10s ipv6=off;
|
||
set ${variable} ${serviceHost service}:${toString port};
|
||
'';
|
||
storeVariable = "$swarm_metrics_store_upstream";
|
||
agentAuthVariable = "$swarm_metrics_agent_auth_upstream";
|
||
grafanaAuthVariable = "$swarm_metrics_grafana_auth_upstream";
|
||
# The store's locations dial it with the store's credential in place of the
|
||
# caller's `Authorization`. The file is empty until the unit that mints the
|
||
# credential has run (./k8s/store-credentials.nix), and the store then refuses
|
||
# the request. The responders' locations never include it.
|
||
viaStore =
|
||
viaService storeVariable storeService storePort
|
||
+ "include ${storeAuth.nginxFile "victoriametrics"};\n";
|
||
|
||
# The store's read API, which the vhost forwards to a caller holding
|
||
# Grafana's bearer, an agent's own bearer, or an operator's session.
|
||
#
|
||
# Grafana's Prometheus datasource reads the query, series, label, metadata,
|
||
# buildinfo, exemplar, rules and alerts routes; the store answers the last
|
||
# three with fixed placeholders. vmui (`uiLocations` below) is static files
|
||
# plus the query API and the remaining read-only routes here. Paths read off
|
||
# the pinned build's `app/vmselect/main.go` and vmui's `src/`.
|
||
readPaths = [
|
||
"/api/v1/query"
|
||
"/api/v1/query_range"
|
||
"/api/v1/query_exemplars"
|
||
"/api/v1/series"
|
||
"/api/v1/labels"
|
||
"/api/v1/metadata"
|
||
"/api/v1/rules"
|
||
"/api/v1/alerts"
|
||
"/api/v1/status/buildinfo"
|
||
"/api/v1/export"
|
||
"/api/v1/export/csv"
|
||
"/api/v1/status/tsdb"
|
||
"/api/v1/status/metric_names_stats"
|
||
"/api/v1/status/top_queries"
|
||
"/api/v1/status/active_queries"
|
||
"/expand-with-exprs"
|
||
"/prettify-query"
|
||
"/metric-relabel-debug"
|
||
];
|
||
# The store serves only `<name>/values` under `/api/v1/label/`.
|
||
readPrefixes = [ "/api/v1/label/" ];
|
||
|
||
# `proxy_pass` with a variable and a URI part sends that URI and not the
|
||
# request's own, so an exact location names its own path and the store
|
||
# receives the path nginx matched, with the request's query string. A prefix
|
||
# location writes no URI part, so the request's path and query reach the store
|
||
# as the caller sent them: `$uri` is decoded, and a decoded line break in it
|
||
# would split the request line.
|
||
toStoreAt = path: "http://${storeVariable}${path}$is_args$args";
|
||
toStoreAsSent = "http://${storeVariable}";
|
||
|
||
# Cache directives for the push route's authelia subrequest. The key is the
|
||
# `Authorization` header and the route, and only a 200 is stored, so a 401
|
||
# or 403 always reaches authelia again. A request without an `Authorization`
|
||
# header skips the cache both ways (`swarm_metrics_ingest_nocache` in
|
||
# `appendHttpConfig`), so a cached grant is never read by a caller that
|
||
# presented no token. The `Cookie` header is dropped from the subrequest,
|
||
# because authelia also accepts a session cookie and the key does not cover
|
||
# it. `proxy_ignore_headers` makes the `proxy_cache_valid` line the only
|
||
# lifetime, whatever cache headers authelia sends.
|
||
ingestAuthzCache = ''
|
||
proxy_set_header Cookie "";
|
||
proxy_cache swarm_metrics_ingest_authz;
|
||
proxy_cache_key "$http_authorization|$http_host$request_uri";
|
||
proxy_cache_valid 200 60s;
|
||
proxy_cache_lock on;
|
||
proxy_cache_lock_timeout 30s;
|
||
proxy_cache_use_stale updating;
|
||
proxy_cache_bypass $swarm_metrics_ingest_nocache;
|
||
proxy_no_cache $swarm_metrics_ingest_nocache;
|
||
proxy_ignore_headers Cache-Control Expires Set-Cookie Vary;
|
||
'';
|
||
|
||
# Bare `auth_request`, no login redirect: a machine caller handed
|
||
# `error_page 401 =302` follows it and reads authelia's login page as a 200
|
||
# holding no series. `/__metrics_read_auth` picks the check from the
|
||
# caller's `Authorization` (`appendHttpConfig` below).
|
||
readAuth = ''
|
||
auth_request /__metrics_read_auth;
|
||
'';
|
||
readLocations =
|
||
let
|
||
exact = path: {
|
||
proxyPass = toStoreAt path;
|
||
extraConfig = viaStore + readAuth;
|
||
};
|
||
prefix = {
|
||
proxyPass = toStoreAsSent;
|
||
extraConfig = viaStore + readAuth;
|
||
};
|
||
in
|
||
lib.listToAttrs (
|
||
map (p: lib.nameValuePair "= ${p}" (exact p)) readPaths
|
||
++ map (p: lib.nameValuePair "^~ ${p}" prefix) readPrefixes
|
||
);
|
||
|
||
# The same three lines as `swarmAuthRequest` in ./swarm-victorialogs.nix,
|
||
# for the same browser: an operator without a session is sent to log in.
|
||
swarmAuthRequest = ''
|
||
auth_request /__metrics_authelia;
|
||
auth_request_set $target_url $scheme://$http_host$request_uri;
|
||
error_page 401 =302 https://${hyperhiveCfg.swarm.authelia.domain}/?rd=$target_url;
|
||
'';
|
||
# vmui's static files and UI config. Only a browser loads these, and its
|
||
# queries then go to the read locations above with the same session cookie.
|
||
uiLocations = {
|
||
"= /vmui" = {
|
||
proxyPass = toStoreAt "/vmui";
|
||
extraConfig = viaStore + swarmAuthRequest;
|
||
};
|
||
"^~ /vmui/" = {
|
||
proxyPass = toStoreAsSent;
|
||
extraConfig = viaStore + swarmAuthRequest;
|
||
};
|
||
};
|
||
|
||
# The store's unit: the daemon, its flags and its state directory, which is
|
||
# all the pod is rendered from. Nothing evaluates a systemd unit for it.
|
||
storeUnit = {
|
||
serviceConfig = {
|
||
ExecStart = lib.concatStringsSep " " [
|
||
"${deployCfg.victoriametrics.package}/bin/victoria-metrics"
|
||
"-storageDataPath=${dataDir}"
|
||
"-httpListenAddr=:${toString storePort}"
|
||
"-retentionPeriod=${deployCfg.victoriametrics.retentionPeriod}"
|
||
# Every route but `/health` and the few built-in ones that answer a
|
||
# fixed text asks for it (docs/4-implementation/k8s/runtime.md).
|
||
"-httpAuth.username=${storeAuth.user}"
|
||
"-httpAuth.password=file://%d/${storeAuth.credentialId}"
|
||
];
|
||
LoadCredential = [ storeAuth.credentialId ];
|
||
StateDirectory = "victoriametrics";
|
||
};
|
||
environment = { };
|
||
path = [ ];
|
||
};
|
||
|
||
# The store identity the responders' units name by credential id: the pod
|
||
# logs in with its ServiceAccount token instead, and mounts the CA file.
|
||
metricsAuthStoreIds = [
|
||
"bao-client.pem"
|
||
"bao-client-key.pem"
|
||
]
|
||
++ lib.optional (deployCfg.bao.serverCaFile != null) "bao-ca.pem";
|
||
|
||
# The path Grafana's host creates the bearer at, under the KV mount. The
|
||
# same leaf as `grafanaMetricsBearerLeaf` in ./swarm-bao.nix.
|
||
grafanaBearerStorePath = "swarm/services/${hyperhiveCfg.swarm.grafana.oidc.clientId}/metrics/bearer";
|
||
|
||
# The agents' `auth_request` responder, the `swarm-logs-auth` binary on the
|
||
# metrics surface. The renderer names its credentials directory after the
|
||
# binary, which is what `authCredential` spells.
|
||
authDaemon = "swarm-logs-auth";
|
||
authCredential = id: "/run/credentials/${authDaemon}.service/${id}";
|
||
authUnit = {
|
||
serviceConfig = {
|
||
ExecStart = lib.concatStringsSep " " [
|
||
"${deployCfg.victoriametrics.agentAuthPackage}/bin/${authDaemon}"
|
||
"--listen 0.0.0.0:${toString authPort}"
|
||
# The role ./swarm-bao.nix writes on the store's `cert` mount.
|
||
"--store-cert-role swarm-metrics-auth"
|
||
"--surface metrics"
|
||
"--verdict-ttl ${toString deployCfg.victoriametrics.agentAuthVerdictTtl}"
|
||
];
|
||
LoadCredential = metricsAuthStoreIds;
|
||
};
|
||
environment = {
|
||
BAO_ADDR = "https://${baoCfg.domain}:${toString baoCfg.port}";
|
||
BAO_CLIENT_CERT = authCredential "bao-client.pem";
|
||
BAO_CLIENT_KEY = authCredential "bao-client-key.pem";
|
||
}
|
||
// lib.optionalAttrs (builtins.elem "bao-ca.pem" metricsAuthStoreIds) {
|
||
BAO_CACERT = authCredential "bao-ca.pem";
|
||
};
|
||
path = [ ];
|
||
};
|
||
|
||
# What the responders' pod holds besides the agents' responder: Grafana's
|
||
# responder, `swarm-metrics-auth`, pointed at the bearer Grafana's
|
||
# datasources share. It logs in under the pod's one ServiceAccount, with the
|
||
# environment and mounts of the first container.
|
||
authPod =
|
||
{
|
||
env,
|
||
volumeMounts,
|
||
metaFor,
|
||
securityContext,
|
||
}:
|
||
let
|
||
image = mkServiceImage {
|
||
name = grafanaAuthService;
|
||
package = deployCfg.victoriametrics.authPackage;
|
||
bin = "swarm-metrics-auth";
|
||
extraContents = [ wgetOnly ];
|
||
};
|
||
in
|
||
{
|
||
containers = [
|
||
{
|
||
name = "grafana-auth";
|
||
image = "${image.imageName}:${image.imageTag}";
|
||
imagePullPolicy = "IfNotPresent";
|
||
args = [
|
||
"--listen"
|
||
"0.0.0.0:${toString grafanaAuthPort}"
|
||
"--store-cert-role"
|
||
"swarm-metrics-auth"
|
||
"--store-path"
|
||
grafanaBearerStorePath
|
||
];
|
||
env = lib.mapAttrsToList (name: value: { inherit name value; }) (
|
||
lib.filterAttrs (n: _: lib.hasPrefix "BAO_" n) env
|
||
// {
|
||
SWARM_METRICS_AUTH_HEALTH_ADDR = "127.0.0.1:${toString grafanaAuthHealthPort}";
|
||
}
|
||
);
|
||
# Not `api`: a Service resolves a named port to the first
|
||
# container of the pod that declares the name, which is the agents'
|
||
# responder's.
|
||
ports = [
|
||
{
|
||
name = "grafana-api";
|
||
containerPort = grafanaAuthPort;
|
||
}
|
||
];
|
||
livenessProbe = loopbackProbe grafanaAuthHealthPort "/healthz";
|
||
readinessProbe = loopbackProbe grafanaAuthHealthPort "/readyz";
|
||
volumeMounts = lib.filter (
|
||
m:
|
||
lib.elem m.name [
|
||
"credentials"
|
||
"bao-token"
|
||
"tmp"
|
||
]
|
||
) volumeMounts;
|
||
inherit securityContext;
|
||
}
|
||
];
|
||
|
||
# The pod's policy, from ./k8s/render.nix, covers this port too: only the
|
||
# node reaches it.
|
||
objects."${grafanaAuthService}/service" = {
|
||
apiVersion = "v1";
|
||
kind = "Service";
|
||
metadata = metaFor authService // {
|
||
name = grafanaAuthService;
|
||
};
|
||
spec = {
|
||
type = "ClusterIP";
|
||
selector = (metaFor authService).labels;
|
||
ports = [
|
||
{
|
||
name = "api";
|
||
protocol = "TCP";
|
||
port = grafanaAuthPort;
|
||
targetPort = "grafana-api";
|
||
}
|
||
];
|
||
};
|
||
};
|
||
|
||
images = [ image ];
|
||
};
|
||
in
|
||
{
|
||
# What the store IS from any hive's point of view, the name it answers on, is
|
||
# `swarm.victoriametrics` in ./swarm-victoriametrics-service.nix.
|
||
# `enable`, `package` and `retentionPeriod` are decisions of the host that
|
||
# runs it and live under `deploy.*`.
|
||
|
||
# Retention is a property of the store this host runs, not something the
|
||
# swarm has to agree on: it is read only where the pod is defined, and a hive
|
||
# that is a *client* of the metrics store never consults it.
|
||
options.services.hyperhive.deploy.victoriametrics.package = lib.mkOption {
|
||
type = lib.types.package;
|
||
default = pkgs.victoriametrics;
|
||
defaultText = lib.literalExpression "pkgs.victoriametrics";
|
||
description = "VictoriaMetrics package to run.";
|
||
};
|
||
|
||
options.services.hyperhive.deploy.victoriametrics.retentionPeriod = lib.mkOption {
|
||
type = lib.types.str;
|
||
default = "5y";
|
||
example = "90d";
|
||
description = ''
|
||
How long samples are kept.
|
||
|
||
Deliberately a high default rather than a required option: the two
|
||
failure directions are not symmetric. Too long fills a disk, which
|
||
is visible and recoverable by lowering this; too short **destroys
|
||
history**, silently and permanently. So the safe default is generous
|
||
and an operator lowers it once they have measured how fast this swarm
|
||
actually accumulates data.
|
||
'';
|
||
};
|
||
|
||
options.services.hyperhive.deploy.victoriametrics.authPackage = lib.mkOption {
|
||
type = lib.types.package;
|
||
defaultText = lib.literalExpression "hyperhive.packages.\${system}.swarm-metrics-auth";
|
||
description = ''
|
||
The `swarm-metrics-auth` package, run as the second container of the
|
||
responders' pod: the gateway's `auth_request` responder that checks the
|
||
bearer Grafana's metrics datasource presents on the store's query API
|
||
against the secret store.
|
||
'';
|
||
};
|
||
|
||
options.services.hyperhive.deploy.victoriametrics.agentAuthPackage = lib.mkOption {
|
||
type = lib.types.package;
|
||
defaultText = lib.literalExpression "hyperhive.packages.\${system}.swarm-logs-auth";
|
||
description = ''
|
||
The `swarm-logs-auth` package, run as the first container of the
|
||
responders' pod: the gateway's `auth_request` responder that checks an
|
||
agent's `swarm-agent.<agent>.<secret>` bearer on the read API against the
|
||
secret store.
|
||
|
||
⚠️ Not folded into `authPackage` above: that one is
|
||
`swarm-metrics-auth`, a different binary. Same split as
|
||
{option}`services.hyperhive.deploy.nats.authPackage`.
|
||
'';
|
||
};
|
||
|
||
options.services.hyperhive.deploy.victoriametrics.authBaoClientCertFile = lib.mkOption {
|
||
type = lib.types.nullOr lib.types.str;
|
||
default = null;
|
||
example = "/var/lib/swarm-bao-pki/metrics-auth.pem";
|
||
description = ''
|
||
Client certificate of the metrics store's responders. Its subject must be
|
||
{option}`services.hyperhive.deploy.bao.metricsAuthCommonName`.
|
||
|
||
Required when `deploy.victoriametrics.enable` is set; unset fails at
|
||
eval. ./glue-metrics-auth-bao-identity.nix points it at the leaf
|
||
./glue-bao-tls.nix mints on a host that runs the store; on any other
|
||
metrics-store host it is a file the operator places.
|
||
|
||
A path, never a value.
|
||
'';
|
||
};
|
||
|
||
options.services.hyperhive.deploy.victoriametrics.authBaoClientKeyFile = lib.mkOption {
|
||
type = lib.types.nullOr lib.types.str;
|
||
default = null;
|
||
example = "/var/lib/swarm-bao-pki/metrics-auth-key.pem";
|
||
description = ''
|
||
Private key for {option}`services.hyperhive.deploy.victoriametrics.authBaoClientCertFile`.
|
||
A path, never a value.
|
||
'';
|
||
};
|
||
|
||
options.services.hyperhive.deploy.victoriametrics.agentAuthVerdictTtl = lib.mkOption {
|
||
type = lib.types.ints.unsigned;
|
||
default = 60;
|
||
description = ''
|
||
Seconds the agents' responder grants an agent bearer it has verified
|
||
again without reading the secret store. A denial is never remembered.
|
||
|
||
This is also how long a re-minted or deleted queue secret keeps working
|
||
at the metrics store's read API after its last successful check. It is
|
||
equally how long a revoked `metrics` permission keeps working. `0` checks
|
||
the store on every query.
|
||
'';
|
||
};
|
||
|
||
options.services.hyperhive.deploy.victoriametrics.agentAuthRate = lib.mkOption {
|
||
type = lib.types.str;
|
||
default = "50r/s";
|
||
example = "30r/m";
|
||
description = ''
|
||
nginx `limit_req` rate, per client address, of read API requests
|
||
carrying a `swarm-agent.` bearer, which nginx checks with the agents'
|
||
responder. Each such request that the responder has
|
||
not granted within
|
||
{option}`services.hyperhive.deploy.victoriametrics.agentAuthVerdictTtl`
|
||
costs a secret store login and read, whether the bearer is right or not.
|
||
|
||
Keyed by client address, so every agent on one host shares one budget.
|
||
Requests over the rate and its burst are answered 403.
|
||
'';
|
||
};
|
||
|
||
options.services.hyperhive.deploy.victoriametrics.agentAuthBurst = lib.mkOption {
|
||
type = lib.types.ints.positive;
|
||
default = 500;
|
||
description = ''
|
||
Requests a client address may make above
|
||
{option}`services.hyperhive.deploy.victoriametrics.agentAuthRate` before
|
||
nginx answers 403, served without delay.
|
||
'';
|
||
};
|
||
|
||
options.services.hyperhive.deploy.victoriametrics.unit = lib.mkOption {
|
||
type = lib.types.raw;
|
||
readOnly = true;
|
||
internal = true;
|
||
default = storeUnit;
|
||
description = ''
|
||
The store's unit, the `serviceConfig`, `environment` and `path` that
|
||
../host-modules/k8s/render.nix renders its pod from.
|
||
'';
|
||
};
|
||
|
||
options.services.hyperhive.deploy.victoriametrics.authUnit = lib.mkOption {
|
||
type = lib.types.raw;
|
||
readOnly = true;
|
||
internal = true;
|
||
default = authUnit;
|
||
description = ''
|
||
The agents' responder's unit, which the first container of the
|
||
responders' pod is rendered from.
|
||
'';
|
||
};
|
||
|
||
options.services.hyperhive.deploy.victoriametrics.pod = lib.mkOption {
|
||
type = lib.types.raw;
|
||
readOnly = true;
|
||
internal = true;
|
||
default = authPod;
|
||
description = ''
|
||
What the responders' pod holds besides the agents' responder: a function
|
||
of that container that ../host-modules/k8s/render.nix calls, giving
|
||
Grafana's responder, its Service and its image.
|
||
'';
|
||
};
|
||
|
||
options.services.hyperhive.deploy.victoriametrics.servicePort = lib.mkOption {
|
||
type = lib.types.port;
|
||
readOnly = true;
|
||
internal = true;
|
||
default = storePort;
|
||
description = "The port the store's Service publishes.";
|
||
};
|
||
|
||
options.services.hyperhive.deploy.victoriametrics.authServicePort = lib.mkOption {
|
||
type = lib.types.port;
|
||
readOnly = true;
|
||
internal = true;
|
||
default = authPort;
|
||
description = "The port the agents' responder's Service publishes.";
|
||
};
|
||
|
||
options.services.hyperhive.deploy.victoriametrics.hostDataDir = lib.mkOption {
|
||
type = lib.types.str;
|
||
readOnly = true;
|
||
internal = true;
|
||
default = hostDataDir;
|
||
description = ''
|
||
The store's data directory on the host, which the pod's volume is. A host
|
||
without it gets the directory made.
|
||
'';
|
||
};
|
||
|
||
config = lib.mkIf deployCfg.victoriametrics.enable {
|
||
assertions = [
|
||
{
|
||
assertion =
|
||
deployCfg.victoriametrics.authBaoClientCertFile != null
|
||
&& deployCfg.victoriametrics.authBaoClientKeyFile != null;
|
||
message = ''
|
||
services.hyperhive.deploy.victoriametrics.authBaoClientCertFile and
|
||
authBaoClientKeyFile must both be set.
|
||
|
||
The metrics store's responders read Grafana's metrics bearer and
|
||
agents' queue secrets from the swarm's secret store with this
|
||
certificate. They default to the leaf the store mints where
|
||
services.hyperhive.deploy.bao.enable is set on this host; elsewhere,
|
||
issue a leaf whose CN is
|
||
services.hyperhive.deploy.bao.metricsAuthCommonName and name it in
|
||
both options.
|
||
'';
|
||
}
|
||
];
|
||
|
||
# The gateway name, inside `deployCfg.victoriametrics.enable` — that
|
||
# guard is the load-bearing part. Every hive in a swarm may know this
|
||
# store exists, but only the host that RUNS it may claim the name; a
|
||
# client hive declaring the vhost would answer for a service it does not
|
||
# have.
|
||
services.hyperhive.gateway.localNames = [ cfg.domain ];
|
||
|
||
# This host serves the vhost and resolves the cluster's names for it
|
||
# through the hive's dnsmasq.
|
||
services.hyperhive.gateway.enable = lib.mkDefault true;
|
||
services.hyperhive.gateway.dns.enable = lib.mkDefault true;
|
||
|
||
# Which check a read location's `auth_request` gets. `auth_request` takes a
|
||
# fixed URI, so it names `/__metrics_read_auth`, which rewrites to the
|
||
# target this map picks: an agent's `swarm-agent.` bearer goes to the
|
||
# agents' check, any other bearer to Grafana's, anything else (an
|
||
# operator's session cookie) to authelia. A map tries its regexes in
|
||
# order, so the agent arm comes first.
|
||
services.nginx.appendHttpConfig = ''
|
||
map $http_authorization $swarm_metrics_read_auth_target {
|
||
"~^Bearer swarm-agent\." /__metrics_agent_auth;
|
||
"~^Bearer " /__metrics_grafana_auth;
|
||
default /__metrics_authelia;
|
||
}
|
||
limit_req_zone $binary_remote_addr zone=swarm_metrics_agent_auth:1m rate=${deployCfg.victoriametrics.agentAuthRate};
|
||
map $http_authorization $swarm_metrics_ingest_nocache {
|
||
"" 1;
|
||
default 0;
|
||
}
|
||
'';
|
||
|
||
# The zone behind `ingestAuthzCache`.
|
||
services.nginx.proxyCachePath.swarm_metrics_ingest_authz = {
|
||
enable = true;
|
||
keysZoneName = "swarm_metrics_ingest_authz";
|
||
keysZoneSize = "1m";
|
||
maxSize = "10m";
|
||
inactive = "10m";
|
||
};
|
||
|
||
# `forceSSL`: authelia answers a plain-http auth subrequest
|
||
# with 400, which `auth_request` cannot read as anything but a broken
|
||
# check. Same as ./swarm-victorialogs.nix's vhost.
|
||
services.hyperhive.gateway.vhosts."${cfg.domain}" = {
|
||
forceSSL = true;
|
||
locations =
|
||
readLocations
|
||
// uiLocations
|
||
// {
|
||
# Nothing reaches the store unless a location here names it. The
|
||
# store serves writes and admin calls (`/api/v1/import`,
|
||
# `/api/v1/admin/tsdb/delete_series`, ...) on the same listener as
|
||
# its queries, and asks for its credential on all of them.
|
||
"/".return = "403";
|
||
|
||
# The swarm UI's quick link opens the bare domain. The store's own
|
||
# index page links mostly to paths refused above, so send it to vmui.
|
||
"= /".return = "302 /vmui/";
|
||
|
||
# The swarm collector's ingest route. `=` so it outranks the `/`
|
||
# prefix above.
|
||
#
|
||
# ⚠️ No `error_page 401 =302` here, and its absence is the point: a
|
||
# redirect is right for a browser and wrong for a pusher, which would
|
||
# follow it and POST its batch at a login page that answers 200 —
|
||
# ingest reporting healthy while storing nothing.
|
||
"= /opentelemetry/api/v1/push" = {
|
||
proxyPass = toStoreAt "/opentelemetry/api/v1/push";
|
||
extraConfig = viaStore + ''
|
||
auth_request /__metrics_ingest_authz;
|
||
'';
|
||
};
|
||
|
||
# The store's own health, for the swarm collector wherever it runs:
|
||
# served on the query listener, so no exporter and no extra port. `=`
|
||
# for the same reason as the push route, and no `error_page` for the
|
||
# same reason: a scraper would parse a login page as metrics.
|
||
#
|
||
# The collector requests exactly this URL, declared as
|
||
# `publishedScrapeTargets.victoriametrics` in ./swarm-otel-service.nix;
|
||
# the two must agree, since that string is also the token's audience.
|
||
"= /metrics" = {
|
||
proxyPass = toStoreAt "/metrics";
|
||
extraConfig = viaStore + ''
|
||
auth_request /__metrics_authelia;
|
||
'';
|
||
};
|
||
|
||
# `rewrite … last` re-matches locations, so the subrequest lands in
|
||
# one of the two internal locations named by the map.
|
||
"= /__metrics_read_auth" = {
|
||
extraConfig = ''
|
||
internal;
|
||
rewrite ^ $swarm_metrics_read_auth_target last;
|
||
'';
|
||
};
|
||
|
||
# Grafana's bearer, checked by `swarm-metrics-auth`. The
|
||
# `Authorization` header rides the subrequest unchanged. No rate limit
|
||
# in front: the responder reads the store at most once per its cache
|
||
# TTL, whatever the bearers.
|
||
"= /__metrics_grafana_auth" = {
|
||
proxyPass = "http://${grafanaAuthVariable}/";
|
||
extraConfig = ''
|
||
${viaService grafanaAuthVariable grafanaAuthService grafanaAuthPort}
|
||
internal;
|
||
proxy_pass_request_body off;
|
||
proxy_set_header Content-Length "";
|
||
'';
|
||
};
|
||
|
||
# An agent's bearer, checked by the agents' responder. Any
|
||
# well-formed agent bearer the responder has not recently granted
|
||
# costs a store login and read, so the rate per client address is
|
||
# capped here. 403 when over: `auth_request` passes only 401 and 403
|
||
# through, and `swarm-metrics` retries a 401.
|
||
"= /__metrics_agent_auth" = {
|
||
proxyPass = "http://${agentAuthVariable}/";
|
||
extraConfig = ''
|
||
${viaService agentAuthVariable authService authPort}
|
||
internal;
|
||
limit_req zone=swarm_metrics_agent_auth burst=${toString deployCfg.victoriametrics.agentAuthBurst} nodelay;
|
||
limit_req_status 403;
|
||
proxy_pass_request_body off;
|
||
proxy_set_header Content-Length "";
|
||
'';
|
||
};
|
||
|
||
# The push route's copy of the subrequest below, plus the verdict
|
||
# cache. Separate because `/__metrics_authelia` also serves `/metrics`
|
||
# and vmui, which must not be cached.
|
||
"= /__metrics_ingest_authz" = {
|
||
proxyPass = "https://${hyperhiveCfg.swarm.authelia.domain}/api/authz/auth-request";
|
||
recommendedProxySettings = false;
|
||
extraConfig = ''
|
||
internal;
|
||
${gatewayCfg.lib.verifiedProxyTo hyperhiveCfg.swarm.authelia.domain}
|
||
proxy_pass_request_body off;
|
||
proxy_set_header Content-Length "";
|
||
proxy_set_header X-Original-Method $request_method;
|
||
proxy_set_header X-Original-URL $scheme://$http_host$request_uri;
|
||
proxy_set_header X-Forwarded-Proto $scheme;
|
||
proxy_set_header X-Forwarded-Host $http_host;
|
||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||
${ingestAuthzCache}
|
||
'';
|
||
};
|
||
|
||
# The authelia subrequest. For `/metrics` it admits only the collector's
|
||
# client; for the read and vmui locations
|
||
# it admits an operator's session (`metricsStoreRules` in
|
||
# ./swarm-authelia.nix). Same target and header set as the sibling log
|
||
# store's, which took them from `swarm-ui.nix` — `X-Original-URL` and
|
||
# `X-Original-Method` are what authelia's auth-request implementation
|
||
# reads, and the address it compares the token's audience against.
|
||
"= /__metrics_authelia" = {
|
||
proxyPass = "https://${hyperhiveCfg.swarm.authelia.domain}/api/authz/auth-request";
|
||
# nixpkgs appends its OWN `Host $host` after extraConfig, which
|
||
# would override verifiedProxyTo's — see the comment on
|
||
# verifiedProxyTo in hive-gateway/vhost-lib.nix.
|
||
recommendedProxySettings = false;
|
||
extraConfig = ''
|
||
internal;
|
||
${gatewayCfg.lib.verifiedProxyTo hyperhiveCfg.swarm.authelia.domain}
|
||
proxy_pass_request_body off;
|
||
proxy_set_header Content-Length "";
|
||
proxy_set_header X-Original-Method $request_method;
|
||
proxy_set_header X-Original-URL $scheme://$http_host$request_uri;
|
||
proxy_set_header X-Forwarded-Proto $scheme;
|
||
proxy_set_header X-Forwarded-Host $http_host;
|
||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||
'';
|
||
};
|
||
};
|
||
};
|
||
|
||
# The pod's volume is the store's data directory, which the rule makes when
|
||
# it is missing. It leaves an existing directory's owner and mode alone: the
|
||
# pod's group is given the tree when the pod starts.
|
||
systemd.tmpfiles.rules = [ "d ${hostDataDir} - - - -" ];
|
||
|
||
# The pods reach the secret store through the hive bridge.
|
||
services.hyperhive.network.enable = lib.mkDefault true;
|
||
};
|
||
|
||
# 🔑 OTLP ingest needs no flag. Measured against the pinned 1.146.0 rather
|
||
# than inferred from the module's option list, which has no OTLP switch and
|
||
# so reads as though the feature were absent: the running server answers
|
||
# `POST /opentelemetry/api/v1/push` with 200 (a nonexistent path answers
|
||
# 400, so that 200 means the route exists). The `-opentelemetry.*` flags
|
||
# only tune naming and limits.
|
||
#
|
||
# ⚠️ That endpoint asks for the store's credential like every other route: the
|
||
# vhost's ingest location authenticates the collector and then replaces its
|
||
# `Authorization` with it. The collector is still the only writer, but it
|
||
# arrives by the swarm name rather than over loopback, because it need not
|
||
# share a host with this store.
|
||
}
|