Watch
0
0
Fork
You've already forked hyperhive
0

run the metrics store and its auth responders as pods

This commit is contained in:
atlas 2026-11-27 09:11:27 +01:00 • committed by maia
commit 07a004fad1

View file

@ -1173,7 +1173,7 @@ let
let
locs = allLocal.services.nginx.virtualHosts."metrics.t.local".locations;
toStore = lib.filterAttrs (
_: l: l.proxyPass != null && lib.hasPrefix "http://unix:/run/swarm-victoriametrics/" l.proxyPass
_: l: l.proxyPass != null && lib.hasPrefix "http://$swarm_metrics_store_upstream" l.proxyPass
) locs;
open = lib.filterAttrs (_: l: !(lib.hasInfix "auth_request " l.extraConfig)) toStore;
in
@ -1188,19 +1188,22 @@ let
# as a 200), the switch rewrites to the map's pick, an agent bearer
# reaches the agents' responder (listed before the catch-all bearer arm,
# since a map takes the first matching regex), any other bearer reaches
# Grafana's, each socket bound by its unit, and anything else goes to
# authelia. vmui is a browser's, so it redirects to the login.
name = "the metrics read API sends an agent bearer to swarm-metrics-agent-auth, another bearer to swarm-metrics-auth and a session to authelia";
# Grafana's, each hop dials the Service of the container that listens
# there, and anything else goes to authelia. vmui is a browser's, so it
# redirects to the login.
name = "the metrics read API sends an agent bearer to the agents' responder, another bearer to Grafana's and a session to authelia";
ok =
let
l = allLocal.services.nginx.virtualHosts."metrics.t.local".locations;
http = allLocal.services.nginx.appendHttpConfig;
unit = allLocal.systemd.services.swarm-metrics-auth;
agentUnit = allLocal.systemd.services.swarm-metrics-agent-auth;
agentArm = "\"~^Bearer swarm-agent\\.\" /__metrics_agent_auth;";
bearerArm = "\"~^Bearer \" /__metrics_grafana_auth;";
v = allLocal.services.hyperhive.deploy.victoriametrics;
logs = allLocal.services.hyperhive.deploy.victorialogs;
agentUnit = v.authUnit;
pod =
allLocal.services.hyperhive.k8s.rendered.services.swarm-metrics-auth.objects."swarm-metrics-auth/deployment".spec.template.spec;
grafana = lib.findFirst (c: c.name == "grafana-auth") null pod.containers;
agentArm = "\"~^Bearer swarm-agent\\.\" /__metrics_agent_auth;";
bearerArm = "\"~^Bearer \" /__metrics_grafana_auth;";
reads = lib.filterAttrs (n: _: lib.hasPrefix "= /api/v1/" n || lib.hasPrefix "^~ /api/v1/" n) l;
in
reads ? "= /api/v1/query_range"
@ -1218,18 +1221,28 @@ let
lib.hasInfix "rewrite ^ \$swarm_metrics_read_auth_target last;"
l."= /__metrics_read_auth".extraConfig
&& lib.hasInfix "internal;" l."= /__metrics_grafana_auth".extraConfig
&& l."= /__metrics_grafana_auth".proxyPass == "http://unix:/run/swarm-metrics-auth/auth.sock:/"
&& lib.hasInfix "--socket /run/swarm-metrics-auth/auth.sock" unit.serviceConfig.ExecStart
&& l."= /__metrics_grafana_auth".proxyPass == "http://$swarm_metrics_grafana_auth_upstream/"
&&
lib.hasInfix
"set $swarm_metrics_grafana_auth_upstream swarm-metrics-grafana-auth.hyperhive.svc.cluster.local:8084;"
l."= /__metrics_grafana_auth".extraConfig
&& builtins.elem "--listen" grafana.args
&& builtins.elem "0.0.0.0:8084" grafana.args
&& builtins.elem "swarm-metrics-auth" grafana.args
&& lib.hasInfix "internal;" l."= /__metrics_agent_auth".extraConfig
&& l."= /__metrics_agent_auth".proxyPass == "http://unix:/run/swarm-metrics-agent-auth/auth.sock:/"
&& lib.hasInfix "--socket /run/swarm-metrics-agent-auth/auth.sock" agentUnit.serviceConfig.ExecStart
&& l."= /__metrics_agent_auth".proxyPass == "http://$swarm_metrics_agent_auth_upstream/"
&&
lib.hasInfix
"set $swarm_metrics_agent_auth_upstream swarm-metrics-auth.hyperhive.svc.cluster.local:8083;"
l."= /__metrics_agent_auth".extraConfig
&& hasStorePath "${v.agentAuthPackage}/bin/swarm-logs-auth " agentUnit.serviceConfig.ExecStart
&& !(hasStorePath "${logs.authPackage}" agentUnit.serviceConfig.ExecStart)
&& hasStorePath "${v.authPackage}/bin/swarm-metrics-auth " unit.serviceConfig.ExecStart
&& lib.hasInfix "--listen 0.0.0.0:8083" agentUnit.serviceConfig.ExecStart
&& !(lib.hasInfix "--socket" agentUnit.serviceConfig.ExecStart)
&& lib.hasInfix "--store-cert-role swarm-metrics-auth" agentUnit.serviceConfig.ExecStart
&& lib.hasInfix "--surface metrics" agentUnit.serviceConfig.ExecStart
&& lib.hasInfix "--verdict-ttl ${toString v.agentAuthVerdictTtl}" agentUnit.serviceConfig.ExecStart
&& agentUnit.serviceConfig.LoadCredential == unit.serviceConfig.LoadCredential
&& pod.serviceAccountName == "swarm-metrics-auth"
&& lib.hasInfix "limit_req_zone $binary_remote_addr zone=swarm_metrics_agent_auth:1m rate=${v.agentAuthRate};" http
&&
lib.hasInfix "limit_req zone=swarm_metrics_agent_auth burst=${toString v.agentAuthBurst} nodelay;"
@ -1239,6 +1252,37 @@ let
&& lib.hasInfix "error_page 401 =302" l."^~ /vmui/".extraConfig
&& l."= /__metrics_authelia".proxyPass == "https://auth.t.local/api/authz/auth-request";
}
{
# The store sees the path nginx matched. An exact location names its own
# path after the store's variable and carries the query string; a prefix
# location writes no URI part, because `$uri` is decoded and a decoded
# line break would split the request line. The first clause is the
# control: the lookups find the locations this is about.
name = "the metrics vhost's exact locations name the store's path and its prefix locations forward the request's own";
ok =
let
l = allLocal.services.nginx.virtualHosts."metrics.t.local".locations;
store = "http://$swarm_metrics_store_upstream";
exact = lib.filterAttrs (
n: _: lib.hasPrefix "= /" n && l.${n}.proxyPass != null && lib.hasPrefix store l.${n}.proxyPass
) l;
prefixes = lib.filterAttrs (n: _: lib.hasPrefix "^~ " n) l;
in
l."= /api/v1/query".proxyPass == "${store}/api/v1/query$is_args$args"
&& l."= /opentelemetry/api/v1/push".proxyPass == "${store}/opentelemetry/api/v1/push$is_args$args"
&& l."= /metrics".proxyPass == "${store}/metrics$is_args$args"
&& l."= /vmui".proxyPass == "${store}/vmui$is_args$args"
&& lib.length (lib.attrNames exact) > 15
&& lib.all (n: l.${n}.proxyPass == "${store}${lib.removePrefix "= " n}$is_args$args") (
lib.attrNames exact
)
&&
lib.attrNames prefixes == [
"^~ /api/v1/label/"
"^~ /vmui/"
]
&& lib.all (n: l.${n}.proxyPass == store) (lib.attrNames prefixes);
}
{
# Grafana's host creates the bearer and its datasource sends it; the
# store's responder reads it. Three modules spell the path, and the
@ -1252,13 +1296,15 @@ let
lib.findFirst (d: d.uid == "swarm-victoriametrics") null
allLocal.containers.swarm-grafana.config.services.grafana.provision.datasources.settings.datasources;
bearerUnit = allLocal.systemd.services.swarm-bao-grafana-metrics-bearer;
metricsPod =
allLocal.services.hyperhive.k8s.rendered.services.swarm-metrics-auth.objects."swarm-metrics-auth/deployment".spec.template.spec;
s = allLocal.systemd.services;
in
ds.jsonData.httpHeaderName1 == "Authorization"
&& ds.secureJsonData.httpHeaderValue1 == "$__file{/run/swarm-grafana-secrets/metrics-bearer}"
&& lib.hasInfix "bao kv put -cas=0 secret/${leaf} value=-" bearerUnit.script
&& lib.hasInfix "/run/swarm-grafana-secrets/metrics-bearer \"Bearer $bearer\"" bearerUnit.script
&& lib.hasInfix "--store-path ${leaf}" s.swarm-metrics-auth.serviceConfig.ExecStart
&& builtins.elem leaf (lib.findFirst (c: c.name == "grafana-auth") null metricsPod.containers).args
&& lib.hasInfix "path \"secret/data/${leaf}\" {\n capabilities = [\"read\"]\n}" s.swarm-bao-metrics-auth-policy.script
&& lib.hasInfix "path \"secret/data/${leaf}\" {\n capabilities = [\"create\", \"read\"]\n}" s.swarm-bao-grafana-oidc-policy.script;
}
@ -1289,18 +1335,20 @@ let
];
}
{
# The responder reads the store as itself, with the leaf the store's
# host mints for it.
name = "swarm-metrics-auth presents its own store leaf";
# The responders read the store as themselves, with the leaf the store's
# host mints for them.
name = "the metrics store's responders present their own store leaf";
ok =
let
v = allLocal.services.hyperhive.deploy.victoriametrics;
lc = allLocal.systemd.services.swarm-metrics-auth.serviceConfig.LoadCredential;
lc = v.authUnit.serviceConfig.LoadCredential;
in
lib.hasSuffix "/metrics-auth.pem" v.authBaoClientCertFile
&& lib.hasSuffix "/metrics-auth-key.pem" v.authBaoClientKeyFile
&& builtins.elem "bao-client.pem:${v.authBaoClientCertFile}" lc
&& builtins.elem "bao-client-key.pem:${v.authBaoClientKeyFile}" lc
# The pod's unit names the ids and no host path.
&& builtins.elem "bao-client.pem" lc
&& builtins.elem "bao-client-key.pem" lc
&& !(lib.any (lib.hasInfix ":") lc)
&& lib.hasInfix "/metrics-auth.pem ]" allLocal.systemd.services.swarm-bao-pki.script;
}
{
@ -1409,7 +1457,7 @@ let
# `/run/private/` (0700 root:root), which nginx cannot traverse, so a
# socket there answers every proxied request with a 502. Each `unix:`
# upstream under `/run/<dir>/` is matched to the units creating `<dir>`.
# The first clauses are the control: the match finds the metrics store's responders.
# The first clauses are the control.
name = "no unix socket nginx proxies to sits in a DynamicUser's preserved runtime directory";
ok =
let
@ -1443,8 +1491,19 @@ let
"no"
]);
in
owners ? swarm-metrics-auth
&& owners ? swarm-metrics-agent-auth
# The controls: the match finds the sockets the gateway's vhosts still
# dial, and the predicate flags a unit that is exactly the hazard and
# passes one that is not.
lib.elem "swarm-grafana" upstreamDirs
&& lib.elem "swarm-otel" upstreamDirs
&& hidden {
DynamicUser = true;
RuntimeDirectoryPreserve = "yes";
}
&& !hidden {
DynamicUser = false;
RuntimeDirectoryPreserve = "yes";
}
&& !(lib.any (u: hidden u.serviceConfig) (lib.attrValues owners));
}
{
@ -2892,7 +2951,6 @@ let
"swarm-builder"
"swarm-grafana"
"swarm-otel"
"swarm-victoriametrics"
]
&& lib.all (carries allLocal) names
&& withCi.containers ? swarm-ci-runner