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
7 changed files with 1308 additions and 1031 deletions

View file

@ -877,11 +877,10 @@ let
lib.hasInfix "/logs-auth.pem ]" s && lib.hasInfix "swarm-logs-auth \"\" clientAuth" s;
}
{
# Read-only on every agent's queue secret, the one leaf Grafana's host
# creates and the store container forwarder's client secret, and nothing
# else: this principal checks presented bearers, and its host fetches
# one secret for the container.
name = "the metrics store responder's grant is agents' queue secrets and log/metric grants, Grafana's metrics bearer and its container forwarder's client secret, read-only, and nothing else";
# Read-only on every agent's queue secret and mirrored grant and the one
# leaf Grafana's host creates, and nothing else: this principal checks
# presented bearers.
name = "the metrics store responder's grant is every agent's queue secret and log/metric grant and Grafana's bearer, read-only, and nothing else";
ok =
let
s = baoGrantHere.systemd.services.swarm-bao-metrics-auth-policy.script;
@ -899,10 +898,6 @@ let
path = "secret/data/swarm/services/swarm-grafana/metrics/bearer";
caps = [ "read" ];
}
{
path = "secret/data/swarm/services/swarm-victoriametrics-collector/oidc/client";
caps = [ "read" ];
}
]
&& lib.hasInfix "auth/cert/certs/swarm-metrics-auth" s
&& lib.hasInfix "allowed_common_names=swarm-metrics-auth" s

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

View file

@ -53,6 +53,7 @@ let
deploy.swarm-controller.enable = false;
deploy.nats.enable = false;
deploy.victorialogs.enable = false;
deploy.victoriametrics.enable = false;
}
);
@ -376,18 +377,18 @@ let
{
# The images are the ones the manifests run, one per rendered service, the
# controller's listener leaf, the queue's three more (server, exporter,
# init), the log store's and its two responders': the fourteen are the
# control that the list is not empty.
# init), the log store's and its two responders', and the metrics store's and
# its two: the seventeen are the control that the list is not empty.
name = "the node loads the image of every service the manifests run, and k3s's own";
ok =
let
loaded = map toString withNode.services.k3s.images;
rendered = map toString withNode.system.build.k8sImages;
in
lib.length rendered == 14
lib.length rendered == 17
&& lib.all (i: lib.elem i loaded) rendered
&& lib.elem (toString withNode.services.k3s.package.airgap-images) loaded
&& lib.length loaded == 15;
&& lib.length loaded == 18;
}
{
# The all-local host's assertions are the control: they all hold.
@ -618,9 +619,16 @@ let
allRoles = map lib.head (
lib.filter lib.isList (builtins.split "auth/jwt-k8s/role/([a-z-]+)" script)
);
# The queue's two roles share one account, and the log store's
# responders hold one role of their own: a case each.
roles = lib.filter (r: !(lib.hasPrefix "swarm-nats" r) && r != "swarm-logs-auth") allRoles;
# The queue's two roles share one account, and the log and metrics stores'
# responders hold one role each of their own: a case each.
roles = lib.filter (
r:
!(lib.hasPrefix "swarm-nats" r)
&& !(lib.elem r [
"swarm-logs-auth"
"swarm-metrics-auth"
])
) allRoles;
ttl = "token_ttl=1h \\\n token_max_ttl=1h";
roleText = name: ''
bao write auth/jwt-k8s/role/${name} \
@ -796,16 +804,26 @@ let
&& !(onCluster.systemd.services ? "container@swarm-claude-login")
&& !(onCluster.containers ? swarm-nats)
&& !(onCluster.systemd.services ? "container-nats")
# The log store and its responders: no container, no unit, no socket
# directory. The metrics store beside it is the control that the lookups
# find what a host still runs.
# The log and metrics stores and their responders: no container, no
# unit, no socket directory. The Grafana container beside them, the CA
# publisher and a runtime directory of the host's own are the controls
# that the lookups find what a host still runs.
&& !(onCluster.containers ? swarm-victorialogs)
&& !(onCluster.systemd.services ? "container@swarm-victorialogs")
&& !(onCluster.systemd.services ? swarm-logs-auth)
&& !(onCluster.systemd.services ? swarm-logs-grafana-auth)
&& !(lib.any (lib.hasPrefix "d /run/swarm-victorialogs ") onCluster.systemd.tmpfiles.rules)
&& onCluster.containers ? swarm-victoriametrics
&& onCluster.systemd.services ? swarm-metrics-auth
&& !(onCluster.containers ? swarm-victoriametrics)
&& !(onCluster.systemd.services ? "container@swarm-victoriametrics")
&& !(onCluster.systemd.services ? swarm-metrics-auth)
&& !(onCluster.systemd.services ? swarm-metrics-agent-auth)
&& !(onCluster.systemd.services ? swarm-victoriametrics-forwarder-oidc)
&& !(onCluster.systemd.timers ? swarm-victoriametrics-forwarder-oidc)
&& !(lib.any (lib.hasPrefix "d /run/swarm-victoriametrics") onCluster.systemd.tmpfiles.rules)
&& onCluster.containers ? swarm-grafana
&& onCluster.systemd.services ? "container@swarm-grafana"
&& onCluster.systemd.services ? hyperhive-k8s-ca-trust
&& lib.any (lib.hasPrefix "d /run/swarm-") onCluster.systemd.tmpfiles.rules
&&
manifestNames onCluster == [
"coredns-custom"
@ -814,11 +832,13 @@ let
"swarm-claude-login"
"swarm-controller"
"swarm-logs-auth"
"swarm-metrics-auth"
"swarm-nats"
"swarm-notify-router"
"swarm-review-classifier"
"swarm-stt"
"swarm-victorialogs"
"swarm-victoriametrics"
]
&&
map (o: o.kind) onCluster.services.k3s.manifests.hyperhive-namespace.content == [
@ -854,6 +874,15 @@ let
"Service"
"ServiceAccount"
]
&&
lib.sort lib.lessThan (map (o: o.kind) onCluster.services.k3s.manifests.swarm-metrics-auth.content)
== [
"Deployment"
"NetworkPolicy"
"Service"
"Service"
"ServiceAccount"
]
&&
lib.sort lib.lessThan (map (o: o.kind) onCluster.services.k3s.manifests.swarm-victorialogs.content)
== [
@ -862,6 +891,16 @@ let
"PersistentVolume"
"PersistentVolumeClaim"
"Service"
]
&&
lib.sort lib.lessThan (
map (o: o.kind) onCluster.services.k3s.manifests.swarm-victoriametrics.content
) == [
"Deployment"
"NetworkPolicy"
"PersistentVolume"
"PersistentVolumeClaim"
"Service"
];
}
{
@ -1077,18 +1116,20 @@ let
ports = [ (tcp 4222) ];
}
]
# The namespace's default, the controller (its listener), the log store
# and its responders (listeners), the queue and this pod have a policy:
# the others have no egress and no listener, and rely on the default.
# The namespace's default, the controller (its listener), the log and metrics
# stores and their responders (listeners), the queue and this pod have a
# policy: the others have no egress and no listener, and rely on the default.
&&
lib.sort lib.lessThan (lib.filter (n: lib.hasSuffix "/networkpolicy" n) (lib.attrNames objects))
== [
"default-deny-ingress/networkpolicy"
"swarm-controller/networkpolicy"
"swarm-logs-auth/networkpolicy"
"swarm-metrics-auth/networkpolicy"
"swarm-nats/networkpolicy"
"swarm-stt/networkpolicy"
"swarm-victorialogs/networkpolicy"
"swarm-victoriametrics/networkpolicy"
];
}
{
@ -1908,7 +1949,7 @@ let
{
# The checker is the control that it finds a violation: the same pod with
# a host port, a kept capability or a missing seccomp profile is flagged,
# and the workloads it reads are all seven.
# and the workloads it reads are all eleven.
name = "the namespace enforces Pod Security restricted, and no pod the module renders breaks it";
ok =
let
@ -1982,7 +2023,7 @@ let
&& ns."pod-security.kubernetes.io/warn" == "restricted"
&& ns."pod-security.kubernetes.io/audit" == "restricted"
&& ns."app.kubernetes.io/part-of" == "hyperhive"
&& lib.length specs == 9
&& lib.length specs == 11
&& lib.all (s: problems s == [ ]) specs
&& problems withHostPort != [ ]
&& problems withCaps != [ ]
@ -2164,6 +2205,224 @@ let
&& !(spec ? initContainers)
&& !(lib.any (v: v.name == "tls") spec.volumes);
}
{
# The data is the container's own directory, so an upgrade moves the
# process and not the bytes. The path is the plain one, never the
# DynamicUser layout's `private/` directory. Every arm has a control: the
# log store's pod, which has no startup probe, and the pods that have no
# volume, no selector or no ServiceAccount.
name = "the metrics store's pod is the daemon on a static local volume that is its old container's data directory, with a startup probe and no store identity";
ok =
let
spec = podSpec "swarm-victoriametrics";
c = container "swarm-victoriametrics";
dir = "/var/lib/nixos-containers/swarm-victoriametrics/var/lib/victoriametrics";
pv = objects."swarm-victoriametrics/persistentvolume";
pvc = objects."swarm-victoriametrics/persistentvolumeclaim";
dep = objects."swarm-victoriametrics/deployment".spec;
label = "hyperhive.darkest.space/state-victoriametrics";
probe = {
path = "/health";
port = "api";
};
in
c.args == [
"-storageDataPath=/var/lib/victoriametrics"
"-httpListenAddr=:8428"
"-retentionPeriod=5y"
]
&& c.livenessProbe.httpGet == probe
&& c.readinessProbe.httpGet == probe
# The store opens its storage before it serves HTTP: liveness waits for
# ten minutes of tries.
&& c.startupProbe.httpGet == probe
&& c.startupProbe.failureThreshold == 60
&& !((container "swarm-victorialogs") ? startupProbe)
&&
c.ports == [
{
name = "api";
containerPort = 8428;
}
]
&& dep.replicas == 1
&& dep.strategy.type == "Recreate"
&& pv.spec.local.path == dir
&& !(lib.hasInfix "/private/" pv.spec.local.path)
&& pv.spec.persistentVolumeReclaimPolicy == "Retain"
&& pvc.spec.volumeName == pv.metadata.name
&&
pv.spec.nodeAffinity.required.nodeSelectorTerms == [
{
matchExpressions = [
{
key = label;
operator = "In";
values = [ "true" ];
}
];
}
]
&& spec.nodeSelector == { ${label} = "true"; }
&&
(lib.findFirst (v: v.name == "state") null spec.volumes).persistentVolumeClaim.claimName
== pvc.metadata.name
&& spec.securityContext.fsGroupChangePolicy == "OnRootMismatch"
&& !(spec ? initContainers)
&& !(lib.any (v: v.name == "tls") spec.volumes)
&& lib.any (v: v.name == "tls") (podSpec "swarm-controller").volumes
&& !(spec ? serviceAccountName)
&& !(objects ? "swarm-victoriametrics/serviceaccount")
&& !(lib.any (v: v.name == "bao-token" || v.name == "credentials") spec.volumes)
&& (podSpec "swarm-stt") ? serviceAccountName
&&
objects."swarm-victoriametrics/service".spec.ports == [
{
name = "api";
protocol = "TCP";
port = 8428;
targetPort = "api";
}
]
&& objects."swarm-victoriametrics/networkpolicy".spec.policyTypes == [ "Ingress" ]
&& !(objects."swarm-victoriametrics/networkpolicy".spec ? ingress)
&& lib.elem "d ${dir} - - - -" onCluster.systemd.tmpfiles.rules
&& !(lib.any (lib.hasPrefix "d ${dir} ") noNode.systemd.tmpfiles.rules);
}
{
# The metrics store's responders: one unpinned pod, one account, two
# containers whose Services each reach their own. The log store's pod is
# the control for the shape; a `--socket` would be a listener no Service
# reaches.
name = "the metrics store's responders are one unpinned pod under one account, reached by the node alone, with the store and DNS as its only way out";
ok =
let
spec = podSpec "swarm-metrics-auth";
named = n: lib.findFirst (c: c.name == n) null spec.containers;
agents = named "swarm-metrics-auth";
grafana = named "grafana-auth";
policy = objects."swarm-metrics-auth/networkpolicy".spec;
s = withNode.systemd.services;
script = s.swarm-bao-k8s-jwt-policy.script;
podLabels = {
"app.kubernetes.io/name" = "swarm-metrics-auth";
"app.kubernetes.io/part-of" = "hyperhive";
};
service = n: objects.${n}.spec;
envOfC = c: lib.listToAttrs (map (e: lib.nameValuePair e.name e.value) c.env);
in
map (c: c.name) spec.containers == [
"swarm-metrics-auth"
"grafana-auth"
]
&&
agents.args == [
"--listen"
"0.0.0.0:8083"
"--store-cert-role"
"swarm-metrics-auth"
"--surface"
"metrics"
"--verdict-ttl"
"60"
]
&&
grafana.args == [
"--listen"
"0.0.0.0:8084"
"--store-cert-role"
"swarm-metrics-auth"
"--store-path"
"swarm/services/swarm-grafana/metrics/bearer"
]
&& !(lib.elem "--socket" (agents.args ++ grafana.args))
# Each container logs in with the pod's token, on the same role.
&& (envOfC agents).BAO_JWT_ROLE == "swarm-metrics-auth"
&& (envOfC grafana).BAO_JWT_ROLE == "swarm-metrics-auth"
&& !(lib.any (e: lib.hasPrefix "BAO_CLIENT" e.name) (agents.env ++ grafana.env))
&& grafana.livenessProbe.httpGet.port == "grafana-health"
&& spec.serviceAccountName == "swarm-metrics-auth"
&& objects."swarm-metrics-auth/serviceaccount".metadata.name == "swarm-metrics-auth"
&& hasInfix "bound_subject=system:serviceaccount:hyperhive:swarm-metrics-auth" script
&& hasInfix "auth/jwt-k8s/role/swarm-metrics-auth" script
&& hasInfix "token_type=batch" s.swarm-bao-metrics-auth-policy.script
&& lib.elem "swarm-metrics-auth" withNode.services.hyperhive.deploy.bao.k8sPrincipals
# No state label: the store's pod is pinned, this one is not.
&& !(spec ? nodeSelector)
&& (podSpec "swarm-victoriametrics") ? nodeSelector
&& (service "swarm-metrics-auth/service").selector == podLabels
&&
(service "swarm-metrics-auth/service").ports == [
{
name = "api";
protocol = "TCP";
port = 8083;
targetPort = "api";
}
]
&& (service "swarm-metrics-grafana-auth/service").selector == podLabels
&&
(service "swarm-metrics-grafana-auth/service").ports == [
{
name = "api";
protocol = "TCP";
port = 8084;
targetPort = "grafana-api";
}
]
&&
policy.policyTypes == [
"Ingress"
"Egress"
]
&& !(policy ? ingress)
&& lib.length policy.egress == 2
&&
(lib.elemAt policy.egress 1).ports == [
{
protocol = "TCP";
port = 8208;
}
]
&& !(objects."swarm-victoriametrics/networkpolicy".spec ? egress)
&& !(spec ? initContainers)
&& !(lib.any (v: v.name == "tls") spec.volumes);
}
{
# A named `targetPort` resolves to the first container of the pod that
# declares the name, so a second container declaring it too is reached by
# the wrong Service. The first clauses are the control: the lookup finds
# the Services whose pods hold two containers, and the store's, and
# finds the name once in each.
name = "every Service that targets a port by name reaches a name that exactly one container of its pod declares";
ok =
let
all = lib.attrValues objects;
services = lib.filter (o: o.kind == "Service") all;
deployments = lib.filter (o: o.kind == "Deployment") all;
named = lib.filter (svc: lib.any (p: builtins.isString p.targetPort) svc.spec.ports) services;
podOf =
svc: lib.findFirst (d: d.spec.template.metadata.labels == svc.spec.selector) null deployments;
declared =
pod: port:
lib.length (
lib.filter (p: p.name == port) (lib.concatMap (c: c.ports or [ ]) pod.spec.template.spec.containers)
);
reached = lib.concatMap (
svc:
map (p: {
service = svc.metadata.name;
count = declared (podOf svc) p.targetPort;
}) (lib.filter (p: builtins.isString p.targetPort) svc.spec.ports)
) named;
countOf = n: (lib.findFirst (r: r.service == n) { count = 0; } reached).count;
in
lib.length (lib.filter (svc: lib.length (podOf svc).spec.template.spec.containers > 1) named) >= 4
&& countOf "swarm-metrics-grafana-auth" == 1
&& countOf "swarm-logs-grafana-auth" == 1
&& countOf "swarm-victoriametrics" == 1
&& lib.all (r: r.count == 1) reached;
}
{
# The host without the node is the control: it labels nothing.
name = "the node labels itself as the entry node, and with a state label for each service whose data the cluster keeps";
@ -2181,6 +2440,7 @@ let
"hyperhive.darkest.space/state-nats=true"
"hyperhive.darkest.space/state-ci-metrics=true"
"hyperhive.darkest.space/state-victorialogs=true"
"hyperhive.darkest.space/state-victoriametrics=true"
]
)
&&
@ -2190,6 +2450,7 @@ let
"hyperhive.darkest.space/state-nats" = "true";
"hyperhive.darkest.space/state-ci-metrics" = "true";
"hyperhive.darkest.space/state-victorialogs" = "true";
"hyperhive.darkest.space/state-victoriametrics" = "true";
}
&& noNode.services.k3s.nodeLabel == [ ];
}

View file

@ -188,10 +188,6 @@ let
machine = "hive-matrix";
option = "matrix";
}
{
machine = "swarm-victoriametrics";
option = "victoriametrics";
}
];
ownClientIdOf = h: svc: h.services.hyperhive.swarm.${svc.option}.otel.clientId;
@ -799,72 +795,6 @@ let
ok =
!shared.matrixNoBaoIdentity.containers.hive-matrix.config.services.opentelemetry-collector.enable;
}
{
# The metrics store's container has no network: the forwarder dials the
# relay on loopback and reads a secret the host fetched and bound in.
name = "the metrics store container forwards its journal through the relay as its own client";
ok =
let
h = servicesHost;
container = h.containers.swarm-victoriametrics;
c = container.config;
s = c.services.opentelemetry-collector.settings;
otel = h.services.hyperhive.swarm.otel;
clientId = h.services.hyperhive.swarm.victoriametrics.otel.clientId;
secret = "/run/swarm-victoriametrics-forwarder-oidc/${clientId}.secret";
fetch = h.systemd.services.swarm-victoriametrics-forwarder-oidc;
in
c.services.opentelemetry-collector.enable
&& s.exporters.otlphttp.endpoint == "http://127.0.0.1:4380/swarm-victoriametrics"
&& s.extensions."oauth2client/swarm".token_url == "http://127.0.0.1:4380/api/oidc/token"
&& s.extensions."oauth2client/swarm".client_id == clientId
&& s.extensions."oauth2client/swarm".endpoint_params.audience == clientId
&& otel.serviceProducers.swarm-victoriametrics.clientId == clientId
&&
(lib.findFirst (a: a.key == "service.name") null s.processors.resource.attributes).value
== "victoriametrics"
&& lib.elem "oidc-client-secret:${secret}" c.systemd.services.opentelemetry-collector.serviceConfig.LoadCredential
&& !(c.systemd.services ? swarm-journal-forwarder-oidc)
&& container.bindMounts."/run/swarm-victoriametrics-forwarder-oidc".isReadOnly
&& !(lib.elem "--link-journal=host" (container.extraFlags or [ ]))
&&
fetch.environment.BAO_CLIENT_CERT
== h.services.hyperhive.deploy.victoriametrics.authBaoClientCertFile
&&
fetch.environment.BAO_CLIENT_KEY == h.services.hyperhive.deploy.victoriametrics.authBaoClientKeyFile
&& lib.hasInfix "bao kv get -field=value secret/swarm/services/${clientId}/oidc/client" fetch.script
&& lib.hasInfix "refresh_consumer swarm-victoriametrics opentelemetry-collector.service" fetch.script;
}
{
# The relay: a loopback listener in the container, a proxy to the bound
# socket, and the host nginx server on that socket, which opens the
# forwarder's two routes and nothing else.
name = "the metrics store's egress relay leads loopback to the collector's route and authelia's token endpoint only";
ok =
let
h = servicesHost;
container = h.containers.swarm-victoriametrics;
c = container.config;
sock = "/run/swarm-victoriametrics-egress/egress.sock";
relay = "swarm-victoriametrics-egress-relay";
nginx = h.services.nginx.appendHttpConfig;
otelDomain = h.services.hyperhive.swarm.otel.domain;
autheliaDomain = h.services.hyperhive.swarm.authelia.domain;
in
c.systemd.sockets.${relay}.listenStreams == [ "127.0.0.1:4380" ]
&&
lib.hasSuffix "/lib/systemd/systemd-socket-proxyd ${sock}"
c.systemd.services.${relay}.serviceConfig.ExecStart
&& !container.bindMounts."/run/swarm-victoriametrics-egress".isReadOnly
&& lib.hasInfix "listen unix:${sock};" nginx
&& lib.hasInfix "location / {\n return 403;" nginx
&& lib.hasInfix "location ^~ /swarm-victoriametrics/ {\n proxy_pass https://${otelDomain}" nginx
&& lib.hasInfix "proxy_ssl_name ${otelDomain};" nginx
&& lib.hasInfix "location = /api/oidc/token {\n proxy_pass https://${autheliaDomain};" nginx
&& lib.hasInfix "proxy_ssl_name ${autheliaDomain};" nginx
&& lib.elem "-/run/swarm-victoriametrics-egress" h.systemd.services.nginx.serviceConfig.ReadWritePaths
&& lib.hasInfix "rm -f ${sock}" h.systemd.services.nginx.preStart;
}
{
# Authelia's forwarder reads its secret from the store under the
# publisher's identity, handed in for both units. The IdP host with no

View file

@ -231,7 +231,6 @@ let
"swarm-grafana-collector"
"swarm-forge-collector"
"swarm-matrix-collector"
"swarm-victoriametrics-collector"
"swarm-authelia-collector"
"swarm-attic-collector"
];

View file

@ -278,7 +278,7 @@ let
m = locs."= /metrics" or null;
in
m != null
&& m.proxyPass == "http://unix:/run/swarm-victoriametrics/victoriametrics.sock:/metrics"
&& m.proxyPass == "http://$swarm_metrics_store_upstream/metrics$is_args$args"
&& lib.hasInfix "auth_request /__metrics_authelia;" m.extraConfig
&& !(lib.hasInfix "error_page" m.extraConfig)
&& lib.hasInfix "internal;" locs."= /__metrics_authelia".extraConfig

View file

@ -203,7 +203,6 @@ let
"swarm-ci-runner"
"swarm-grafana"
"swarm-otel"
"swarm-victoriametrics"
];
# A container's start limit, in seconds.
@ -248,7 +247,6 @@ let
"forgejo-secrets.service"
"swarm-bao-token.service"
"swarm-bridge-dns.service"
"victoriametrics-state-migrate.service"
];
suffixes = [
"-ca-bundle.service"
@ -264,7 +262,6 @@ let
"grafana.service"
"opentelemetry-collector.service"
"tuwunel.service"
"victoriametrics.service"
];
}
];
@ -311,22 +308,6 @@ let
[ ]
) hyperhiveContainers;
# The metrics store on a build older than its socket needs. Only its
# `assertions` are read, and the package is a stub carrying a version.
oldMetricsStore = hive {
deploy.victoriametrics.enable = true;
deploy.victoriametrics.package = pkgs.emptyDirectory // {
version = "1.152.0";
};
};
# Failing assertions first, then their messages: a message is only
# well-defined when its assertion fails (nixpkgs' fileSystems one throws
# otherwise).
failedMetricsStoreVersion =
host:
lib.filter (lib.hasInfix "services.hyperhive.deploy.victoriametrics.package must be") (
map (a: a.message) (lib.filter (a: !a.assertion) host.assertions)
);
cases = [
{
# `lib.all` over an empty set holds vacuously, so the roster is counted
@ -770,7 +751,6 @@ let
"swarm-authelia"
"swarm-bao"
"swarm-grafana"
"swarm-victoriametrics"
"swarm-otel"
"hive-matrix"
"hive-forge"
@ -818,7 +798,7 @@ let
# that lost a container can't pass by comparing fewer of them.
name = "every service container restates its host-side privateNetwork";
ok =
lib.length (lib.attrNames serviceContainers) == 10
lib.length (lib.attrNames serviceContainers) == 9
&& lib.all (
c: c.privateNetwork == (c.config.services.hyperhive.swarmContainer.privateNetwork or null)
) (lib.attrValues serviceContainers);
@ -847,46 +827,6 @@ let
in
c.networking.firewall.enable && c.networking.resolvconf.enable;
}
{
# No interface but loopback, so the socket is the store's only listener
# the host can reach. Every location that forwards to the store goes
# through it; the internal auth subrequests are the only other upstreams.
# The read, push and scrape locations are named so an empty filter
# can't pass.
name = "the metrics store runs on a netns of its own and the gateway reaches it over its socket";
ok =
let
host = serviceContainersWithCi;
c = host.containers.swarm-victoriametrics;
dir = "/run/swarm-victoriametrics";
sock = "${dir}/victoriametrics.sock";
viaSock = path: "http://unix:${sock}:${path}";
locs =
host.services.nginx.virtualHosts.${host.services.hyperhive.swarm.victoriametrics.domain}.locations;
toStore = map (l: l.proxyPass) (
lib.filter (l: l.proxyPass != null && !(lib.hasInfix "internal;" l.extraConfig)) (
lib.attrValues locs
)
);
unit = c.config.systemd.services.victoriametrics;
nginxGid = host.ids.gids.nginx;
in
c.privateNetwork
&& c.hostBridge == null
&& c.hostAddress == null
&& c.config.services.victoriametrics.listenAddress == "unix:${sock}"
&& locs."= /api/v1/query".proxyPass == viaSock "/api/v1/query"
&& locs."= /opentelemetry/api/v1/push".proxyPass == viaSock "/opentelemetry/api/v1/push"
&& locs."= /metrics".proxyPass == viaSock "/metrics"
&& lib.all (lib.hasPrefix (viaSock "")) toStore
&& c.bindMounts.${dir}.hostPath == dir
&& builtins.elem "d ${dir} 0770 root ${toString nginxGid} - -" host.systemd.tmpfiles.rules
&& c.config.users.groups.gateway-nginx.gid == nginxGid
&& unit.serviceConfig.Group == "gateway-nginx"
&& !unit.serviceConfig.DynamicUser
&& unit.serviceConfig.UMask == "0007"
&& lib.hasInfix "--unix-socket ${sock}" unit.postStart;
}
{
# On the bridge by DHCP, like the CI runner, and its socket the only
# listener the gateway dials. `address` unset is what keeps tuwunel off
@ -1373,38 +1313,9 @@ let
&& !verdict (lib.replaceStrings [ "%header{x-swarm-otel-route}" ] [ "" ] script)
&& !verdict (lib.replaceStrings [ ''[ "$seen" = "$route" ] && '' ] [ "" ] script);
}
{
# The DynamicUser layout keeps the history under /var/lib/private; the
# static user's StateDirectory is the plain path. The move runs as its
# own unit ahead of the store.
name = "the metrics store's history moves from the DynamicUser path before the store starts";
ok =
let
cfg = serviceContainers.swarm-victoriametrics.config;
m = cfg.systemd.services.victoriametrics-state-migrate;
in
cfg.services.victoriametrics.stateDir == "victoriametrics"
&& builtins.elem "victoriametrics.service" m.before
&& builtins.elem "victoriametrics.service" m.requiredBy
&& lib.hasInfix "old=/var/lib/private/victoriametrics" m.script
&& lib.hasInfix "new=/var/lib/victoriametrics" m.script
&& lib.hasInfix ''mv "$old" "$new"'' m.script
&& lib.hasInfix ''chown -R victoriametrics:gateway-nginx "$new"'' m.script;
}
{
# The control is the default package on the all-services host.
name = "a metrics store package older than 1.153.0 fails evaluation";
ok =
let
old = failedMetricsStoreVersion oldMetricsStore;
in
lib.length old == 1
&& lib.hasInfix "1.152.0" (lib.head old)
&& failedMetricsStoreVersion serviceContainersWithCi == [ ];
}
{
name = "a service container that sets no stateVersion of its own is on 26.05";
ok = serviceContainers.swarm-victoriametrics.config.system.stateVersion == "26.05";
ok = serviceContainers.swarm-grafana.config.system.stateVersion == "26.05";
}
{
name = "a service container that pins its own stateVersion keeps it";