Watch
0
0
Fork
You've already forked hyperhive
0

run grafana as a pod in the cluster

The Deployment swarm-grafana replaces the swarm-grafana container: one
replica on a static local volume that is the container's data directory,
an init container that fetches the OIDC client secret and creates the
administrator password, the datasources' bearer and the encryption key in
the secret store, and a sidecar that keeps the controller's Viewer token.
The datasources reach the stores through the gateway with that bearer, and
Grafana's /metrics asks for the stores' kind of credential, which the vhost
sends. The five host units, the journal forwarder, its client glue and the
unix-socket directory go with the container.
This commit is contained in:
atlas 2026-11-27 22:51:25 +01:00
commit 9f62e71dae
7 changed files with 1938 additions and 1953 deletions

View file

@ -101,7 +101,6 @@ let
baoGrantWithConsumers = hiveWithController {
deploy.bao.enable = true;
deploy.bao.bootstrapTokenFile = "/run/secrets/bao-bootstrap.token";
deploy.grafana.enable = true;
deploy.swarm-otel.enable = true;
};
@ -131,7 +130,6 @@ let
# Every reader against a store it does not run, each with a leaf placed by
# hand. The deployment in which there is no local policy unit to wait for.
baoRemoteReaders = hiveWithController {
deploy.grafana.enable = true;
deploy.swarm-otel.enable = true;
deploy.bao.clientCertFile = "/etc/pki/bao-client.pem";
deploy.bao.clientKeyFile = "/etc/pki/bao-client-key.pem";
@ -144,7 +142,6 @@ let
# The readers ./glue-bao-readers-policy-order.nix orders after their policy
# units.
policyReaders = [
"swarm-bao-grafana-oidc"
"swarm-bao-otel-oidc"
];
@ -706,7 +703,7 @@ let
# run rotate a value Grafana or the metrics store already holds. The
# controller's Grafana token is the one leaf it may replace, which it
# does when Grafana refuses the stored token.
name = "the Grafana reader reads its own and its forwarder's client secrets, creates its admin password, metrics bearer and datasource key, and writes the controller's Grafana token, and nothing else";
name = "the Grafana reader reads its own client secret, creates its admin password, metrics bearer and datasource key, and writes the controller's Grafana token, and nothing else";
ok =
let
s = baoGrantHere.systemd.services.swarm-bao-grafana-oidc-policy.script;
@ -716,10 +713,6 @@ let
path = "secret/data/swarm/services/swarm-grafana/oidc/client";
caps = [ "read" ];
}
{
path = "secret/data/swarm/services/swarm-grafana-collector/oidc/client";
caps = [ "read" ];
}
{
path = "secret/data/swarm/services/swarm-grafana/admin/password";
caps = [
@ -997,7 +990,7 @@ let
lib.all (p: p != null) own
&& !(lib.any (p: lib.elem p hiveLeaf) own)
&& lib.length (lib.unique own) == lib.length own
&& presents "swarm-bao-grafana-oidc" b.grafanaOidcClientCertFile b.grafanaOidcClientKeyFile
&& presents "swarm-bao-otel-oidc" b.otelOidcClientCertFile b.otelOidcClientKeyFile
&& presents "swarm-bao-forwarder-oidc" b.forwarderOidcClientCertFile b.forwarderOidcClientKeyFile;
}
@ -1098,29 +1091,6 @@ let
&& !(noReaders ? swarm-bao-matrix-oidc)
&& !(noReaders ? swarm-bao-nats-callout-xkey);
}
{
# The admin password and the controller's Grafana token are written under
# Grafana's leaf, so their units wait for the unit writing that leaf's
# role and policy; before it, the write is refused. The last arm is the
# absence control.
name = "grafana's admin password and controller token units are ordered after grafana's policy unit";
ok =
lib.all
(
unit:
let
u = baoGrantWithConsumers.systemd.services.${unit};
in
lib.elem "swarm-bao-grafana-oidc-policy.service" u.after
&& lib.elem "swarm-bao-grafana-oidc-policy.service" u.wants
&& !(lib.elem "swarm-bao-grafana-oidc-policy.service" u.requires)
&& !(baoGrantNoReaders.systemd.services ? ${unit})
)
[
"swarm-bao-grafana-admin"
"swarm-bao-grafana-controller-token"
];
}
{
# The ordering is set apart from each reader's own definition, so it can
# define a reader by itself: `after` on a unit nothing else declares is a
@ -1312,9 +1282,6 @@ let
[
"swarm-services-cert"
"swarm-bao-forwarder-oidc"
"swarm-bao-grafana-admin"
"swarm-bao-grafana-metrics-bearer"
"swarm-bao-grafana-controller-token"
]
++ policyReaders
);
@ -2245,8 +2212,9 @@ let
}
{
# Create without update: the first value stored is the one every later
# run reads, and no run replaces it. Two leaves are the stores', the third the exporter's.
name = "the store credential publisher's grant is create and read on the two stores' and the queue exporter's credentials and nothing else, and the PKI unit signs its leaf";
# run reads, and no run replaces it. Three leaves are the stores' and
# Grafana's, the fourth the exporter's.
name = "the store credential publisher's grant is create and read on the two stores', Grafana's and the queue exporter's credentials and nothing else, and the PKI unit signs its leaf";
ok =
let
s = baoGrantHere.systemd.services.swarm-bao-store-auth-policy.script;
@ -2262,12 +2230,17 @@ let
"create"
"read"
]
&&
(grantFor grants "secret/data/swarm/services/grafana/store-auth").caps == [
"create"
"read"
]
&&
(grantFor grants "secret/data/swarm/services/nats/exporter-auth").caps == [
"create"
"read"
]
&& lib.length grants == 3
&& lib.length grants == 4
&& lib.hasInfix "auth/cert/certs/swarm-store-auth" s
&& lib.hasInfix "allowed_common_names=swarm-store-auth" s
&& lib.hasInfix "token_policies=swarm-store-auth" s
@ -3217,11 +3190,6 @@ let
machine = baoGrantWithConsumers.services.hyperhive.swarm.otel.machine;
consumer = "opentelemetry-collector.service";
}
{
fetch = "swarm-bao-grafana-oidc";
machine = baoGrantWithConsumers.services.hyperhive.swarm.grafana.machine;
consumer = "grafana.service";
}
{
fetch = "swarm-bao-forwarder-oidc";
machine = baoGrantWithConsumers.services.hyperhive.swarm.bao.machine;

View file

@ -195,10 +195,6 @@ let
# changes (../host-modules/lib/refresh-consumer.nix), each on a fixture
# that renders it.
storeRefreshed = [
{
unit = "swarm-bao-grafana-oidc";
machine = allLocal;
}
{
unit = "swarm-bao-otel-oidc";
machine = allLocal;
@ -1284,7 +1280,7 @@ let
&& lib.all (n: l.${n}.proxyPass == store) (lib.attrNames prefixes);
}
{
# Grafana's host creates the bearer and its datasource sends it; the
# Grafana's init container creates the bearer and its datasource sends it; the
# store's responder reads it. Three modules spell the path, and the
# store's grants name it twice: if any one drifts, Grafana sends a value
# the responder never reads, or one of them is refused by the store.
@ -1294,16 +1290,15 @@ let
leaf = "swarm/services/${allLocal.services.hyperhive.swarm.grafana.oidc.clientId}/metrics/bearer";
ds =
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;
allLocal.services.hyperhive.deploy.grafana.rendered.provision.datasources.settings.datasources;
fetch = allLocal.services.hyperhive.deploy.grafana.rendered.scripts.fetch;
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
&& ds.secureJsonData.httpHeaderValue1 == "$__file{/run/grafana-secrets/metrics-bearer}"
&& lib.hasInfix "put metrics-bearer \"Bearer $(seeded secret/${leaf})\"" fetch
&& 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;
@ -1316,7 +1311,7 @@ let
ok =
let
datasources =
allLocal.containers.swarm-grafana.config.services.grafana.provision.datasources.settings.datasources;
allLocal.services.hyperhive.deploy.grafana.rendered.provision.datasources.settings.datasources;
metrics = lib.findFirst (d: d.uid == "swarm-victoriametrics") null datasources;
logs = lib.findFirst (d: d.uid == "swarm-victorialogs") null datasources;
leaf = "swarm/services/${allLocal.services.hyperhive.swarm.grafana.oidc.clientId}/metrics/bearer";
@ -1494,7 +1489,7 @@ let
# 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-authelia" upstreamDirs
&& lib.elem "swarm-otel" upstreamDirs
&& hidden {
DynamicUser = true;
@ -2862,11 +2857,11 @@ let
{
# Control: a store fetch with no timer still renders `RemainAfterExit`,
# so the cases below read a real value rather than a missing one.
name = "Grafana's admin-password fetch stays active after it exits and has no timer";
name = "the store credential unit stays active after it exits and has no timer";
ok =
allLocal.systemd.services ? swarm-bao-grafana-admin
&& remainsAfterExit allLocal.systemd.services.swarm-bao-grafana-admin
&& !(allLocal.systemd.timers ? swarm-bao-grafana-admin);
allLocal.systemd.services ? swarm-bao-store-auth
&& remainsAfterExit allLocal.systemd.services.swarm-bao-store-auth
&& !(allLocal.systemd.timers ? swarm-bao-store-auth);
}
{
# A timer whose unit does not exist fails to load, so each timer is
@ -2949,7 +2944,6 @@ let
"swarm-authelia"
"swarm-bao"
"swarm-builder"
"swarm-grafana"
"swarm-otel"
]
&& lib.all (carries allLocal) names

View file

@ -31,14 +31,6 @@ let
deploy.grafana.package = grafanaStub;
};
# A host that tries to move the socket directory; reading the option throws.
grafanaSocketDirSet = hive {
deploy.grafana.enable = true;
deploy.grafana.socketDir = "/run/test-grafana-sock";
deploy.grafana.plugins = [ ];
deploy.grafana.package = grafanaStub;
};
# Same pairing `nix/module-eval/bao-otel-collector.nix` uses for its own
# cases: the store's container with its own collector on, so
# `.containers.swarm-bao.config` carries a real, evaluated
@ -104,6 +96,9 @@ let
#
# Matched on the option name the message names, not on its prose, so the
# wording stays rewordable: the option name is the part an operator has to
# What a host's Grafana pod is rendered from.
rendered = m: m.services.hyperhive.deploy.grafana.rendered;
# act on, and a message that stopped naming it would be the actual defect.
grafanaRefusedFor =
m: option:
@ -115,24 +110,34 @@ let
) m.assertions;
cases = [
{
# The socket's directory is its access control, so it must stay dedicated
# to this one socket. `readOnly` is what makes that inexpressible: a
# definition anywhere — here at the `deploy` path — has to fail the eval,
# and an unset host has to render the default into nginx's upstream.
name = "grafana's socket directory cannot be set and renders its default";
# The UI is reached by name, through the gateway, so the vhost dials the
# pod's Service and the host binds nothing for it: no socket directory
# option, no directory of it. The Service's name is in a variable that
# nginx resolves per request.
name = "grafana has no socket directory and its vhost dials the pod's Service by name";
ok =
!(builtins.tryEval grafanaSocketDirSet.services.hyperhive.deploy.grafana.socketDir).success
let
g = grafanaHere.services.hyperhive.swarm.grafana;
locs = grafanaHere.services.nginx.virtualHosts.${g.domain}.locations;
in
!(grafanaHere.services.hyperhive.deploy.grafana ? socketDir)
&& locs."= /metrics".proxyPass == "http://$swarm_grafana_upstream/metrics$is_args$args"
&& locs."/".proxyPass == "http://$swarm_grafana_upstream"
&&
grafanaHere.services.nginx.virtualHosts.${grafanaHere.services.hyperhive.swarm.grafana.domain}.locations."= /metrics".proxyPass
== "http://unix:/run/swarm-grafana/grafana.sock:/metrics";
lib.hasInfix "set $swarm_grafana_upstream swarm-grafana.hyperhive.svc.cluster.local:3000;"
locs."/".extraConfig
&& !(lib.any (lib.hasInfix "/run/swarm-grafana") grafanaHere.systemd.tmpfiles.rules);
}
{
# Grafana serves `/metrics` unauthenticated, so the published name gates
# it on the collector's auth subrequest: no `error_page 401 =302` (a
# scraper would parse a login page as metrics) and no `/__hive_authelia`.
# `=` is what catches the normalised variants and still leaves the rest
# of Grafana proxied.
name = "grafana's published vhost serves /metrics only behind the collector's auth subrequest";
# Grafana serves `/metrics` to anyone who reaches the port, so the
# published name gates it on the collector's auth subrequest: no
# `error_page 401 =302` (a scraper would parse a login page as metrics)
# and no `/__hive_authelia`. `=` is what catches the normalised variants
# and still leaves the rest of Grafana proxied. After that check the
# vhost sends the listener credential the pod's `[metrics]` section asks
# for; `/` sends none, because Grafana's own login is the credential
# there.
name = "grafana's published vhost serves /metrics only behind the collector's auth subrequest, with the pod's credential";
ok =
let
g = grafanaHere.services.hyperhive.swarm.grafana;
@ -144,10 +149,43 @@ let
&& locs."= /metrics".return == null
&& checksOf locs."= /metrics" == [ "auth_request /__grafana_metrics_authz;" ]
&& !(lib.hasInfix "error_page" locs."= /metrics".extraConfig)
&& lib.hasInfix "include /var/lib/swarm-store-auth/grafana.conf;" locs."= /metrics".extraConfig
&& !(lib.hasInfix "swarm-store-auth" locs."/".extraConfig)
&& authz.proxyPass == "https://auth.t.local/api/authz/auth-request"
&& lib.hasInfix "internal;" authz.extraConfig
&& !(grafanaHere.services.nginx.virtualHosts ? "grafana-metrics")
&& lib.hasPrefix "http://unix:" locs."/".proxyPass;
&& !(grafanaHere.services.nginx.virtualHosts ? "grafana-metrics");
}
{
# The credential the vhost sends is the one the pod's settings ask for:
# the same user name, the password from the Secret's credential, and the
# endpoint on. Grafana reads both keys at start (`pkg/setting/setting.go`
# in the pinned release), and the pod's login is the only other way in to
# `/metrics`.
name = "grafana's metrics endpoint asks for the listener credential the vhost sends";
ok =
let
s = (rendered grafanaHere).settings;
in
s.metrics.enabled
&& s.metrics.basic_auth_username == "swarm-store"
&& s.metrics.basic_auth_password == "$__file{/run/credentials/swarm-grafana.service/store-auth}"
&& lib.elem "store-auth" grafanaHere.services.hyperhive.deploy.grafana.unit.serviceConfig.LoadCredential;
}
{
# The UI and the API are Grafana's own to authenticate: no anonymous
# access, a Viewer or otherwise, and the listener is the pod's port on all
# its addresses because the Service is what reaches it.
name = "grafana takes no anonymous access and listens on the pod's port";
ok =
let
s = (rendered grafanaHere).settings;
in
!(s ? "auth.anonymous")
&& !(s."auth.basic".enabled or true == false)
&& s.server.protocol == "http"
&& s.server.http_addr == "0.0.0.0"
&& s.server.http_port == 3000
&& s.server.root_url == "https://grafana.t.local/";
}
{
# This fixture enables grafana and NOT authelia, so the login-form toggle
@ -155,7 +193,7 @@ let
# this host". Its vhost is on the public gateway, so a password box there
# is a way in whatever the topology.
name = "grafana disables its local login form even where authelia is not on this host";
ok = grafanaHere.containers.swarm-grafana.config.services.grafana.settings.auth.disable_login_form;
ok = (rendered grafanaHere).settings.auth.disable_login_form;
}
{
# The absence class this whole file is for, and the reported defect in one
@ -169,7 +207,7 @@ let
name = "grafana's OIDC block names the swarm's IdP, wherever that IdP runs";
ok =
let
oauth = m: m.containers.swarm-grafana.config.services.grafana.settings."auth.generic_oauth";
oauth = m: (rendered m).settings."auth.generic_oauth";
remote = oauth grafanaRemoteAuthelia;
local = oauth grafanaWithAuthelia;
in
@ -181,25 +219,28 @@ let
}
{
# 🩸 The arm that guards the ruling this slice landed under. There is ONE
# delivery route: the store reader, on every host that runs Grafana. The
# negative names the deleted unit rather than a generic absence, because
# the way this regresses is someone re-adding the co-located copy as an
# optimisation — a second writer of one path, and a second shape of "the
# secret is wrong" to debug.
name = "grafana's OIDC secret has exactly one delivery unit, the store reader, in both topologies";
# delivery route: the pod's init container, with the store, on every host
# that runs Grafana. The negatives name the deleted units rather than a
# generic absence, because the way this regresses is someone re-adding a
# co-located copy as an optimisation — a second writer of one path, and a
# second shape of "the secret is wrong" to debug.
name = "grafana's OIDC secret has exactly one delivery route, the pod's init container, in both topologies";
ok =
let
local = grafanaWithAuthelia.systemd.services;
remote = grafanaRemoteAuthelia.systemd.services;
init = m: (rendered m).scripts.fetch;
in
local ? swarm-bao-grafana-oidc
&& remote ? swarm-bao-grafana-oidc
lib.hasInfix "secret/swarm/services/swarm-grafana/oidc/client" (init grafanaWithAuthelia)
&& lib.hasInfix "secret/swarm/services/swarm-grafana/oidc/client" (init grafanaRemoteAuthelia)
&& !(local ? swarm-bao-grafana-oidc)
&& !(remote ? swarm-bao-grafana-oidc)
&& !(local ? swarm-grafana-oidc-secret)
&& !(remote ? swarm-grafana-oidc-secret);
}
{
# What the deleted warning became. The shape is unchanged — a Grafana host
# holding no store leaf — but silence there is a container nobody can log
# holding no store leaf — but silence there is a pod nobody can log
# into for a reason no log names, and a warning is read back by nothing.
# The second arm is what makes this a refusal about the IDENTITY: this
# fixture names an IdP, so a message about `swarm.authelia.url` here would
@ -232,110 +273,75 @@ let
# reset, a database created before the setting keeps `admin`.
#
# The reset's `--config` must be the file the server reads, or it resets
# a different database: compared by store path against the server's own
# command line.
name = "grafana's admin password is minted into the store and applied to new and existing databases";
# a different database: the same store path in both commands.
name = "grafana's admin password is created in the store and applied to new and existing databases";
ok =
let
unit = grafanaRemoteAuthelia.systemd.services.swarm-bao-grafana-admin;
grafana = grafanaRemoteAuthelia.containers.swarm-grafana.config.services.grafana;
service = grafanaRemoteAuthelia.containers.swarm-grafana.config.systemd.services.grafana;
resetConfig = lib.head (
lib.head (lib.filter lib.isList (builtins.split "--config ([^ ]+)" service.preStart))
);
r = rendered grafanaRemoteAuthelia;
init = r.scripts.fetch;
start = r.scripts.start;
in
lib.hasInfix "bao kv put -cas=0 secret/swarm/services/swarm-grafana/admin/password value=-" unit.script
&& lib.hasInfix "openssl rand -hex 32" unit.script
&& lib.hasInfix "atomic_write_secret 0400 196:0 /run/swarm-grafana-secrets/admin-password" unit.script
&& lib.elem "container@swarm-grafana.service" unit.before
&& unit.environment.BAO_CLIENT_CERT == "/etc/pki/bao-grafana-oidc.pem"
&& grafana.settings.security.admin_password == "$__file{/run/swarm-grafana-secrets/admin-password}"
&& lib.hasInfix "admin reset-admin-password --password-from-stdin < /run/swarm-grafana-secrets/admin-password" service.preStart
&& lib.hasInfix "-config ${resetConfig}" service.script;
lib.hasInfix "bao kv put -cas=0 \"$1\" value=-" init
&& lib.hasInfix "put admin-password \"$(seeded secret/swarm/services/swarm-grafana/admin/password)\"" init
&& lib.hasInfix "openssl rand -hex 32" init
&& r.settings.security.admin_password == "$__file{/run/grafana-secrets/admin-password}"
&& lib.hasInfix "admin reset-admin-password --password-from-stdin < /run/grafana-secrets/admin-password" start
&& lib.hasInfix "--config ${r.configFile}" start
&& lib.length (lib.splitString "--config ${r.configFile}" start) == 3;
}
{
# Grafana's datasource-encryption key reaches Grafana from the store,
# seeded from the file an earlier release generated in the container,
# which stays on disk. A different key leaves Grafana unable to read its
# stored datasource secrets, so nothing may mint ahead of that seed, and
# the superseded in-container generator must be gone: left in place it
# would write a second key beside the delivered one. The last two arms
# are the presence controls for the absence arms: the guest's `grafana`
# unit is found by the same lookup, and the admin password's legacy file
# is among the removals.
name = "grafana's datasource key is seeded into the store from the earlier file, delivered from /run, and the earlier file is kept";
# Grafana's datasource-encryption key reaches Grafana from the store. A
# different key leaves Grafana unable to read its stored datasource
# secrets, so the store's value wins, `-cas=0` never replaces one, and a
# lost race reads the winner's back. No file of an earlier release is
# read: a host that never held a stored key gets a new one, and the
# provisioned datasources are written again at start.
name = "grafana's datasource key is created in the store once and delivered from the pod's memory";
ok =
let
m = grafanaRemoteAuthelia;
unit = m.systemd.services.swarm-bao-grafana-secret-key;
guest = m.containers.swarm-grafana.config;
earlier = "/var/lib/nixos-containers/swarm-grafana/var/lib/grafana-secret/secret_key";
r = rendered grafanaRemoteAuthelia;
init = r.scripts.fetch;
in
lib.hasInfix "bao kv put -cas=0 \"\$leaf\" value=-" unit.script
&& lib.hasInfix "grafana_secret_key secret/swarm/services/swarm-grafana/secret-key ${earlier} openssl rand -hex 32" unit.script
&& lib.length (lib.splitString earlier unit.script) == 2
&& lib.hasInfix "atomic_write_secret 0400 196:0 /run/swarm-grafana-secrets/secret-key" unit.script
&& lib.elem "container@swarm-grafana.service" unit.before
&& unit.environment.BAO_CLIENT_CERT == "/etc/pki/bao-grafana-oidc.pem"
&&
guest.services.grafana.settings.security.secret_key
== "$__file{/run/swarm-grafana-secrets/secret-key}"
&& !(guest.systemd.services ? swarm-grafana-secret-key)
&& guest.systemd.services ? grafana
&& !(lib.any (lib.hasInfix "/var/lib/grafana-secret") m.systemd.tmpfiles.rules)
&& lib.any (lib.hasInfix "/var/lib/grafana-admin") m.systemd.tmpfiles.rules
&& lib.elem "r /var/lib/nixos-containers/swarm-grafana/var/lib/grafana-admin/password" m.systemd.tmpfiles.rules;
lib.hasInfix "put secret-key \"$(seeded secret/swarm/services/swarm-grafana/secret-key)\"" init
&& lib.hasInfix "value=\"$(stored \"$1\")\"" init
&& r.settings.security.secret_key == "$__file{/run/grafana-secrets/secret-key}"
&& !(lib.hasInfix "/var/lib/grafana-secret" init)
&& !(lib.any (lib.hasInfix "/var/lib/grafana-secret") grafanaRemoteAuthelia.systemd.tmpfiles.rules);
}
{
# swarm-controller reads alerts with this token, so the account it
# belongs to is created and kept a Viewer, the token lands at the path
# the controller's grant reads, and an attempt against a Grafana or a
# store that never answers ends and retries. The timer re-runs it, so
# the unit must not stay active.
name = "the controller's Grafana token is a Viewer account's, stored under Grafana's leaf, and each attempt is bounded";
# the controller's grant reads, and a call against a Grafana or a store
# that never answers ends and is retried on the next pass.
name = "the controller's Grafana token is a Viewer account's, stored under Grafana's leaf, and each call is bounded";
ok =
let
unit = grafanaRemoteAuthelia.systemd.services.swarm-bao-grafana-controller-token;
s = unit.script;
s = (rendered grafanaRemoteAuthelia).scripts.controllerToken;
in
!(lib.elem (toString (unit.serviceConfig.TimeoutStartSec or "infinity")) [
"infinity"
"0"
])
&& lib.hasInfix "curl --silent --show-error --connect-timeout 5 --max-time 15" s
lib.hasInfix "curl --silent --show-error --connect-timeout 5 --max-time 15" s
&& lib.hasInfix "--data '{\"name\":\"swarm-controller\",\"role\":\"Viewer\",\"isDisabled\":false}'" s
&& lib.hasInfix "--data '{\"role\":\"Viewer\",\"isDisabled\":false}'" s
&& lib.hasInfix "bao kv put secret/swarm/controller/swarm-controller/grafana/token value=-" s
&& lib.hasInfix "--unix-socket /run/swarm-grafana/grafana.sock" s
&& unit.environment.BAO_CLIENT_CERT == "/etc/pki/bao-grafana-oidc.pem"
&& unit.serviceConfig.Restart == "on-failure"
&& !(unit.serviceConfig.RemainAfterExit or false)
&& lib.elem "swarm-bao-grafana-admin.service" unit.after
&& grafanaRemoteAuthelia.systemd.timers ? swarm-bao-grafana-controller-token;
&& lib.hasInfix "base=http://127.0.0.1:3000" s
&& !(lib.hasInfix "--unix-socket" s)
&& lib.hasInfix "sleep 300" s;
}
{
# The container is ready when the guest's boot finishes, which can be
# before Grafana listens, so the unit waits for the socket inside its own
# start job, ahead of every call that needs it. The wait is bounded and
# sits inside the unit's own timeout, or systemd kills the job first.
name = "the controller's Grafana token waits a bounded time for Grafana's socket before its first call";
# The pod is ready when its containers start, which can be before Grafana
# listens, so the sidecar asks `/api/health` first and sleeps and asks
# again when it does not answer. The pass is only called on the answer.
name = "the controller's Grafana token sidecar calls Grafana only after /api/health answers";
ok =
let
unit = grafanaRemoteAuthelia.systemd.services.swarm-bao-grafana-controller-token;
s = unit.script;
parts = lib.splitString "if ! retry_in_job " s;
budget = lib.toInt (lib.head (lib.splitString " " (lib.elemAt parts 1)));
s = (rendered grafanaRemoteAuthelia).scripts.controllerToken;
at = needle: lib.stringLength (lib.head (lib.splitString needle s));
loop = lib.last (lib.splitString "while true; do" s);
in
lib.length parts == 2
&& lib.hasInfix "retry_in_job() {" s
&& lib.hasInfix "${toString budget} grafana_listening; then" s
&& lib.hasInfix "http://localhost/api/health" s
&& budget > 0
&& budget <= 120
&& unit.serviceConfig.TimeoutStartSec == "4min"
&& at "retry_in_job ${toString budget} grafana_listening" < at "no Grafana admin password at"
&& at "retry_in_job ${toString budget} grafana_listening" < at "status=\"$(grafana \"$admin\" GET";
lib.hasInfix "\"$base/api/health\"" loop
&& lib.hasInfix "pass || echo" loop
&& at "\"$base/api/health\"" > at "while true; do"
&& at "pass || echo" > at "\"$base/api/health\""
&& lib.hasInfix "sleep 5" loop;
}
{
# Same 403-not-a-miss reason as the matrix and queue arms below: the
@ -346,7 +352,7 @@ let
name = "grafana's OIDC secret is read from the prefix the mint writes";
ok =
let
s = grafanaRemoteAuthelia.systemd.services.swarm-bao-grafana-oidc.script;
s = (rendered grafanaRemoteAuthelia).scripts.fetch;
in
lib.hasInfix "secret/swarm/services/swarm-grafana/oidc/client" s
&& !(lib.hasInfix "secret/swarm/hives/" s);
@ -354,67 +360,54 @@ let
{
# Authelia's mint writes the secret a moment after a fresh swarm's first
# boot, so a read that finds nothing yet has to fail and be asked again.
# A unit that exits 0 there is done until the refresh timer, ten minutes
# on, and Grafana has no login in the meantime.
name = "grafana's OIDC delivery unit fails on no value yet, so the store retry asks again";
# An init container that exits 0 there starts Grafana with no login: the
# init fails, and the kubelet starts it again.
name = "grafana's init container fails on no OIDC value yet, so the pod starts it again";
ok =
let
u = grafanaRemoteAuthelia.systemd.services.swarm-bao-grafana-oidc;
afterRead = lib.last (lib.splitString "bao kv get" u.script);
beforeWrite = lib.head (lib.splitString "atomic_write_secret" afterRead);
s = (rendered grafanaRemoteAuthelia).scripts.fetch;
afterRead = lib.last (lib.splitString "oidc=\"$(stored" s);
beforeWrite = lib.head (lib.splitString "put swarm-grafana.secret" afterRead);
in
# Both arms: the read that returns nothing and the one that returns an
# empty value.
lib.length (lib.splitString "exit 75" beforeWrite) == 3
lib.hasInfix "exit 1" beforeWrite
&& !(lib.hasInfix "exit 0" beforeWrite)
&& u.serviceConfig.Restart == "on-failure";
&& lib.hasInfix "set -euo pipefail" s;
}
{
# A copy on persistent disk outlives the secret's rotation and lands in
# backups of the container's root.
name = "grafana's OIDC secret, admin password and metrics bearer are written to the host's /run only, and the old copies removed";
# backups. The pod writes them to its memory-backed runtime directory,
# which the settings name, and the host keeps no copy, no unit and no
# directory for them.
name = "grafana's OIDC secret, admin password, bearer and key are written to the pod's memory only, and the host has no copy";
ok =
let
m = grafanaRemoteAuthelia;
r = rendered m;
services = m.systemd.services;
mount = m.containers.swarm-grafana.bindMounts."/run/swarm-grafana-secrets";
rules = m.systemd.tmpfiles.rules;
guest = m.containers.swarm-grafana.config.services.grafana;
init = r.scripts.fetch;
persistent = s: lib.hasInfix "/var/lib/nixos-containers" s || lib.hasInfix "/var/lib/grafana-" s;
oidc = services.swarm-bao-grafana-oidc.script;
admin = services.swarm-bao-grafana-admin.script;
bearer = services.swarm-bao-grafana-metrics-bearer.script;
# The bind source must exist when the store refuses the login, so the
# directory is made ahead of it in every delivery script.
dirBeforeLogin =
s:
lib.hasInfix "install -d -m 0755 /run/swarm-grafana-secrets" (
lib.head (lib.splitString "bao login -method=cert" s)
);
in
lib.hasInfix "atomic_write_secret 0400 196:0 /run/swarm-grafana-secrets/swarm-grafana.secret" oidc
&& lib.hasInfix "atomic_write_secret 0400 196:0 /run/swarm-grafana-secrets/metrics-bearer" bearer
&& dirBeforeLogin oidc
&& dirBeforeLogin admin
&& dirBeforeLogin bearer
&& !(persistent oidc)
&& !(persistent admin)
&& !(persistent bearer)
&& !(persistent services.swarm-bao-grafana-controller-token.script)
&& lib.hasInfix "< /run/swarm-grafana-secrets/admin-password" services.swarm-bao-grafana-controller-token.script
&& mount.hostPath == "/run/swarm-grafana-secrets"
&& mount.isReadOnly
lib.hasInfix "put swarm-grafana.secret \"$oidc\"" init
&& lib.hasInfix "put metrics-bearer \"Bearer $(seeded secret/swarm/services/swarm-grafana/metrics/bearer)\"" init
&& lib.hasInfix "umask 0277" init
&& lib.hasInfix "d=/run/grafana-secrets" init
&& !(persistent init)
&& !(persistent r.scripts.controllerToken)
&& lib.hasInfix "< /run/grafana-secrets/admin-password" r.scripts.controllerToken
&&
guest.settings."auth.generic_oauth".client_secret
== "$__file{/run/swarm-grafana-secrets/swarm-grafana.secret}"
r.settings."auth.generic_oauth".client_secret
== "$__file{/run/grafana-secrets/swarm-grafana.secret}"
&& lib.any (
d: d.secureJsonData.httpHeaderValue1 or null == "$__file{/run/swarm-grafana-secrets/metrics-bearer}"
) guest.provision.datasources.settings.datasources
&& lib.elem "d /run/swarm-grafana-secrets 0755 root root -" rules
&& lib.elem "r /var/lib/nixos-containers/swarm-grafana/var/lib/grafana-oidc/swarm-grafana.secret" rules
&& lib.elem "r /var/lib/nixos-containers/swarm-grafana/var/lib/grafana-admin/password" rules
&& lib.elem "r /var/lib/nixos-containers/swarm-grafana/var/lib/grafana-metrics/bearer" rules
&& !(lib.any (lib.hasPrefix "f /run/swarm-grafana-secrets") rules);
d: d.secureJsonData.httpHeaderValue1 or null == "$__file{/run/grafana-secrets/metrics-bearer}"
) r.provision.datasources.settings.datasources
&& !(services ? swarm-bao-grafana-admin)
&& !(services ? swarm-bao-grafana-metrics-bearer)
&& !(services ? swarm-bao-grafana-secret-key)
&& !(services ? swarm-bao-grafana-controller-token)
&& !(m.systemd.timers ? swarm-bao-grafana-controller-token)
&& !(lib.any (lib.hasInfix "swarm-grafana-secrets") rules)
&& !(m.containers ? swarm-grafana);
}
{
# Both halves of the co-location assumption, which was one host's
@ -440,7 +433,7 @@ let
name = "the forge dashboard ships, and its CI runs tab queries the metric names the CI run poller and the controller record";
ok =
let
provider = lib.head grafanaWithAuthelia.containers.swarm-grafana.config.services.grafana.provision.dashboards.settings.providers;
provider = lib.head grafanaWithAuthelia.services.hyperhive.deploy.grafana.rendered.provision.dashboards.settings.providers;
shipped = lib.attrNames provider.options.path.entries;
board = builtins.fromJSON (builtins.readFile ../host-modules/swarm-grafana/dashboards/forge.json);
exprs = lib.concatMap (el: map (q: q.spec.query.spec.expr or "") el.spec.data.spec.queries) (
@ -484,7 +477,7 @@ let
name = "the review classifier dashboard ships, and queries the metric names the controller and the classifier record";
ok =
let
provider = lib.head grafanaWithAuthelia.containers.swarm-grafana.config.services.grafana.provision.dashboards.settings.providers;
provider = lib.head grafanaWithAuthelia.services.hyperhive.deploy.grafana.rendered.provision.dashboards.settings.providers;
shipped = lib.attrNames provider.options.path.entries;
board = builtins.fromJSON (
builtins.readFile ../host-modules/swarm-grafana/dashboards/review-classifier.json
@ -644,7 +637,7 @@ let
name = "the builds dashboard ships, and reads the fields hive-c0re logs on a container build";
ok =
let
provider = lib.head grafanaWithAuthelia.containers.swarm-grafana.config.services.grafana.provision.dashboards.settings.providers;
provider = lib.head grafanaWithAuthelia.services.hyperhive.deploy.grafana.rendered.provision.dashboards.settings.providers;
shipped = lib.attrNames provider.options.path.entries;
board = builtins.fromJSON (builtins.readFile ../host-modules/swarm-grafana/dashboards/builds.json);
exprs = map (e: (lib.head e.spec.data.spec.queries).spec.query.spec.expr) (
@ -687,7 +680,7 @@ let
name = "the host dashboard ships, and every panel query filters on host";
ok =
let
provider = lib.head grafanaWithAuthelia.containers.swarm-grafana.config.services.grafana.provision.dashboards.settings.providers;
provider = lib.head grafanaWithAuthelia.services.hyperhive.deploy.grafana.rendered.provision.dashboards.settings.providers;
shipped = lib.attrNames provider.options.path.entries;
board = builtins.fromJSON (builtins.readFile ../host-modules/swarm-grafana/dashboards/host.json);
exprs = lib.concatMap (p: map (t: t.expr) p.targets) board.panels;
@ -708,7 +701,7 @@ let
let
h = grafanaHere.services.hyperhive;
groups =
grafanaHere.containers.swarm-grafana.config.services.grafana.provision.alerting.rules.settings.groups;
grafanaHere.services.hyperhive.deploy.grafana.rendered.provision.alerting.rules.settings.groups;
rules = lib.concatMap (g: g.rules) groups;
expr =
uid:
@ -751,7 +744,7 @@ let
name = "the egress dashboard ships, and reads the field layout of squid's log lines";
ok =
let
provider = lib.head grafanaWithAuthelia.containers.swarm-grafana.config.services.grafana.provision.dashboards.settings.providers;
provider = lib.head grafanaWithAuthelia.services.hyperhive.deploy.grafana.rendered.provision.dashboards.settings.providers;
shipped = lib.attrNames provider.options.path.entries;
board = builtins.fromJSON (builtins.readFile ../host-modules/swarm-grafana/dashboards/egress.json);
exprs = map (e: (lib.head e.spec.data.spec.queries).spec.query.spec.expr) (
@ -845,7 +838,7 @@ let
ok =
let
datasources =
grafanaWithAuthelia.containers.swarm-grafana.config.services.grafana.provision.datasources.settings.datasources;
grafanaWithAuthelia.services.hyperhive.deploy.grafana.rendered.provision.datasources.settings.datasources;
metrics = lib.findFirst (d: d.uid == "swarm-victoriametrics") null datasources;
logs = lib.findFirst (d: d.uid == "swarm-victorialogs") null datasources;
in
@ -862,7 +855,7 @@ let
name = "the logs datasource's plugin is handed Grafana's environment";
ok =
let
grafana = grafanaWithAuthelia.containers.swarm-grafana.config.services.grafana;
grafana = grafanaWithAuthelia.services.hyperhive.deploy.grafana.rendered;
logs = lib.findFirst (
d: d.uid == "swarm-victorialogs"
) null grafana.provision.datasources.settings.datasources;
@ -888,7 +881,7 @@ let
ok =
let
sources =
grafanaHere.containers.swarm-grafana.config.services.grafana.provision.datasources.settings.datasources;
grafanaHere.services.hyperhive.deploy.grafana.rendered.provision.datasources.settings.datasources;
logs = lib.head (lib.filter (d: d.uid == "swarm-victorialogs") sources);
rules = logs.jsonData.logLevelRules or [ ];
# `overwrite_text` makes the stored text the OpenTelemetry short name
@ -916,7 +909,7 @@ let
ok =
let
groups =
grafanaHere.containers.swarm-grafana.config.services.grafana.provision.alerting.rules.settings.groups;
grafanaHere.services.hyperhive.deploy.grafana.rendered.provision.alerting.rules.settings.groups;
rules = lib.concatMap (g: g.rules) groups;
rule = lib.findFirst (r: r.uid == "hh-agent-turn-failed") null rules;
expr = (lib.head rule.data).model.expr;
@ -934,7 +927,7 @@ let
ok =
let
groups =
grafanaHere.containers.swarm-grafana.config.services.grafana.provision.alerting.rules.settings.groups;
grafanaHere.services.hyperhive.deploy.grafana.rendered.provision.alerting.rules.settings.groups;
rules = lib.concatMap (g: g.rules) groups;
rule = lib.findFirst (r: r.uid == "hh-review-classifier-run-failed") null rules;
expr = (lib.head rule.data).model.expr;
@ -960,7 +953,7 @@ let
ok =
let
groups =
grafanaHere.containers.swarm-grafana.config.services.grafana.provision.alerting.rules.settings.groups;
grafanaHere.services.hyperhive.deploy.grafana.rendered.provision.alerting.rules.settings.groups;
rules = lib.concatMap (g: g.rules) groups;
source = builtins.readFile ../../hive-c0re/src/stats/otel_metrics.rs;
expected = {
@ -994,7 +987,7 @@ let
ok =
let
groups =
grafanaHere.containers.swarm-grafana.config.services.grafana.provision.alerting.rules.settings.groups;
grafanaHere.services.hyperhive.deploy.grafana.rendered.provision.alerting.rules.settings.groups;
rules = lib.concatMap (g: g.rules) groups;
rule = lib.findFirst (r: r.uid == "hh-hive-degraded") null rules;
expr = (lib.head rule.data).model.expr;
@ -1016,7 +1009,7 @@ let
ok =
let
groups =
grafanaHere.containers.swarm-grafana.config.services.grafana.provision.alerting.rules.settings.groups;
grafanaHere.services.hyperhive.deploy.grafana.rendered.provision.alerting.rules.settings.groups;
rules = lib.concatMap (g: g.rules) groups;
source = builtins.readFile ../../swarm-secret-client/src/cert.rs;
threshold =
@ -1052,7 +1045,7 @@ let
ok =
let
groups =
grafanaHere.containers.swarm-grafana.config.services.grafana.provision.alerting.rules.settings.groups;
grafanaHere.services.hyperhive.deploy.grafana.rendered.provision.alerting.rules.settings.groups;
rules = lib.concatMap (g: g.rules) groups;
source = builtins.readFile ../../hive-agent/src/otel_turn_metrics.rs;
rule = lib.findFirst (r: r.uid == "hh-claude-usage-ahead-of-clock") null rules;
@ -1091,7 +1084,7 @@ let
ok =
let
groups =
grafanaHere.containers.swarm-grafana.config.services.grafana.provision.alerting.rules.settings.groups;
grafanaHere.services.hyperhive.deploy.grafana.rendered.provision.alerting.rules.settings.groups;
rules = lib.concatMap (g: g.rules) groups;
source = builtins.readFile ../../hive-c0re/src/stats/otel_metrics.rs;
rule = uid: lib.findFirst (r: r.uid == uid) null rules;
@ -1121,7 +1114,7 @@ let
ok =
let
groups =
grafanaHere.containers.swarm-grafana.config.services.grafana.provision.alerting.rules.settings.groups;
grafanaHere.services.hyperhive.deploy.grafana.rendered.provision.alerting.rules.settings.groups;
rules = lib.concatMap (g: g.rules) groups;
source = builtins.readFile ../../hive-c0re/src/stats/otel_metrics.rs;
rule = uid: lib.findFirst (r: r.uid == uid) null rules;
@ -1151,7 +1144,7 @@ let
ok =
let
groups =
grafanaHere.containers.swarm-grafana.config.services.grafana.provision.alerting.rules.settings.groups;
grafanaHere.services.hyperhive.deploy.grafana.rendered.provision.alerting.rules.settings.groups;
rules = lib.concatMap (g: g.rules) groups;
source = builtins.readFile ../../swarm-controller/src/observability/held_deploys.rs;
rule = uid: lib.findFirst (r: r.uid == uid) null rules;

View file

@ -55,6 +55,7 @@ let
deploy.nats.enable = false;
deploy.victorialogs.enable = false;
deploy.victoriametrics.enable = false;
deploy.grafana.enable = false;
}
);
@ -449,10 +450,10 @@ let
loaded = map toString withNode.services.k3s.images;
rendered = map toString withNode.system.build.k8sImages;
in
lib.length rendered == 17
lib.length rendered == 19
&& lib.all (i: lib.elem i loaded) rendered
&& lib.elem (toString withNode.services.k3s.package.airgap-images) loaded
&& lib.length loaded == 18;
&& lib.length loaded == 20;
}
{
# The all-local host's assertions are the control: they all hold.
@ -766,13 +767,14 @@ let
lib.filter lib.isList (builtins.split "auth/jwt-k8s/role/([a-z-]+)" script)
);
# 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.
# responders and Grafana 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"
"swarm-grafana-oidc"
])
) allRoles;
ttl = "token_ttl=1h \\\n token_max_ttl=1h";
@ -950,10 +952,11 @@ let
&& !(onCluster.systemd.services ? "container@swarm-claude-login")
&& !(onCluster.containers ? swarm-nats)
&& !(onCluster.systemd.services ? "container-nats")
# The log and metrics stores and their responders: no container, no
# unit, no socket directory. The Grafana container beside them, the CA
# The log and metrics stores, Grafana and their responders: no container,
# no unit, no socket directory. The swarm collector's container, the CA
# publisher and a runtime directory of the host's own are the controls
# that the lookups find what a host still runs.
# 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)
@ -966,8 +969,17 @@ let
&& !(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.containers ? swarm-grafana)
&& !(onCluster.systemd.services ? "container@swarm-grafana")
&& !(onCluster.systemd.services ? swarm-bao-grafana-oidc)
&& !(onCluster.systemd.services ? swarm-bao-grafana-admin)
&& !(onCluster.systemd.services ? swarm-bao-grafana-metrics-bearer)
&& !(onCluster.systemd.services ? swarm-bao-grafana-secret-key)
&& !(onCluster.systemd.services ? swarm-bao-grafana-controller-token)
&& !(onCluster.systemd.timers ? swarm-bao-grafana-oidc)
&& !(lib.any (lib.hasPrefix "d /run/swarm-grafana") onCluster.systemd.tmpfiles.rules)
&& onCluster.containers ? swarm-otel
&& onCluster.systemd.services ? "container@swarm-otel"
&& onCluster.systemd.services ? hyperhive-k8s-ca-trust
&& lib.any (lib.hasPrefix "d /run/swarm-") onCluster.systemd.tmpfiles.rules
&&
@ -977,6 +989,7 @@ let
"swarm-ci-metrics"
"swarm-claude-login"
"swarm-controller"
"swarm-grafana"
"swarm-logs-auth"
"swarm-metrics-auth"
"swarm-nats"
@ -1270,6 +1283,7 @@ let
== [
"default-deny-ingress/networkpolicy"
"swarm-controller/networkpolicy"
"swarm-grafana/networkpolicy"
"swarm-logs-auth/networkpolicy"
"swarm-metrics-auth/networkpolicy"
"swarm-nats/networkpolicy"
@ -2168,7 +2182,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 == 11
&& lib.length specs == 12
&& lib.all (s: problems s == [ ]) specs
&& problems withHostPort != [ ]
&& problems withCaps != [ ]
@ -2328,6 +2342,10 @@ let
variable = "$swarm_metrics_store_upstream";
store = "victoriametrics";
};
grafanaLocations = allLocal.services.nginx.virtualHosts.${h.swarm.grafana.domain}.locations;
grafanaSent = lib.filterAttrs (
_: l: hasInfix "/var/lib/swarm-store-auth/grafana.conf" (l.extraConfig or "")
) grafanaLocations;
in
# The log store's four: the operator's `/`, the collector's ingest, the
# query API and the store's own metrics.
@ -2366,20 +2384,24 @@ let
&& lib.all (store: hasInfix "secret/swarm/services/${store}/store-auth" unit.script) [
"victorialogs"
"victoriametrics"
"grafana"
]
&& lib.all (store: hasInfix "/var/lib/swarm-store-auth/${store}.conf" unit.script) [
"victorialogs"
"victoriametrics"
"grafana"
]
&& lib.all (store: hasInfix "swarm-${store}-credentials" unit.script) [
"victorialogs"
"victoriametrics"
"grafana"
]
&& hasInfix "--from-file=store-auth=/var/lib/swarm-store-auth/victoriametrics.password" unit.script
&& unit.environment.BAO_CLIENT_CERT == h.deploy.bao.storeAuthClientCertFile
&& hasInfix "store-auth.pem" h.deploy.bao.storeAuthClientCertFile
&& lib.elem (rule "victorialogs") allLocal.systemd.tmpfiles.rules
&& lib.elem (rule "victoriametrics") allLocal.systemd.tmpfiles.rules
&& lib.elem (rule "grafana") allLocal.systemd.tmpfiles.rules
# nginx runs as `User=nginx`: the directory and the nginx files are its
# group's to read, written that way and fixed when they already hold the
# value, and the passwords stay root's.
@ -2393,9 +2415,19 @@ let
&& !(noNode.systemd.services ? swarm-bao-store-auth)
&& !(lib.elem (rule "victorialogs") noNode.systemd.tmpfiles.rules)
&& !(lib.elem (rule "victoriametrics") noNode.systemd.tmpfiles.rules)
&& !(lib.elem (rule "grafana") noNode.systemd.tmpfiles.rules)
&& !(lib.elem "d /var/lib/swarm-store-auth 0750 root nginx - -" noNode.systemd.tmpfiles.rules)
&& !(noNode.services.nginx.virtualHosts ? ${h.swarm.victorialogs.domain})
&& !(noNode.services.nginx.virtualHosts ? ${h.swarm.victoriametrics.domain});
&& !(noNode.services.nginx.virtualHosts ? ${h.swarm.victoriametrics.domain})
# Grafana's vhost sends it on `/metrics` alone, after the identity
# provider's check; `/` dials the same Service and carries none, which
# is Grafana's own login's. The Service's variable is the control that
# the lookup finds the location.
&& lib.attrNames grafanaSent == [ "= /metrics" ]
&& hasInfix "auth_request /__grafana_metrics_authz;" grafanaLocations."= /metrics".extraConfig
&& hasInfix "$swarm_grafana_upstream" grafanaLocations."/".proxyPass
&& !(hasInfix "swarm-store-auth" grafanaLocations."/".extraConfig)
&& !(noNode.services.nginx.virtualHosts ? ${h.swarm.grafana.domain});
}
{
# The exporter's login has the same three ends as the stores', with the
@ -2773,6 +2805,125 @@ let
&& !(spec ? initContainers)
&& !(lib.any (v: v.name == "tls") spec.volumes);
}
{
# Grafana's pod: the server on a static local volume that is its data
# directory, an init container that fetches its secrets into memory
# before it starts, and a sidecar that keeps the controller's token. The
# server's is the only port, and the pod logs in with its account's token.
# The metrics store's pod, which has neither container, is the control
# for the lookups that name them.
name = "Grafana's pod is the server on a static local volume that is its data directory, with an init container that fetches its secrets into memory and a sidecar that keeps the controller's token";
ok =
let
spec = podSpec "swarm-grafana";
named = n: lib.findFirst (c: c.name == n) null spec.containers;
main = named "swarm-grafana";
side = named "controller-token";
init = lib.head spec.initContainers;
dir = "/var/lib/nixos-containers/swarm-grafana/var/lib/grafana";
pv = objects."swarm-grafana/persistentvolume";
label = "hyperhive.darkest.space/state-grafana";
probe = {
path = "/api/health";
port = "api";
};
envOfC = c: lib.listToAttrs (map (e: lib.nameValuePair e.name e.value) c.env);
mountsOf = c: map (m: m.name) c.volumeMounts;
script = withNode.systemd.services.swarm-bao-k8s-jwt-policy.script;
credentials = lib.findFirst (v: v.name == "credentials") null spec.volumes;
policy = objects."swarm-grafana/networkpolicy".spec;
in
map (c: c.name) spec.containers == [
"swarm-grafana"
"controller-token"
]
&& map (c: c.name) spec.initContainers == [ "fetch-secrets" ]
&& !((podSpec "swarm-victoriametrics") ? initContainers)
&& main.livenessProbe.httpGet == probe
&& main.readinessProbe.httpGet == probe
&& main.startupProbe.httpGet == probe
&& main.startupProbe.failureThreshold == 30
# One port, named once: the init container and the sidecar declare none.
&&
main.ports == [
{
name = "api";
containerPort = 3000;
}
]
&& !(init ? ports)
&& !(side ? ports)
&& objects."swarm-grafana/deployment".spec.strategy.type == "Recreate"
&& pv.spec.local.path == dir
&& !(lib.hasInfix "/private/" pv.spec.local.path)
&& spec.nodeSelector == { ${label} = "true"; }
&& spec.securityContext.fsGroupChangePolicy == "OnRootMismatch"
# The secrets are in memory, and all three containers mount them where
# the settings name them. The `tmp` volume is the control that the
# lookup finds a plain one.
&&
(lib.findFirst (v: v.name == "runtime") null spec.volumes) == {
name = "runtime";
emptyDir.medium = "Memory";
}
&&
(lib.findFirst (v: v.name == "tmp") null spec.volumes) == {
name = "tmp";
emptyDir = { };
}
&& lib.all (c: lib.elem "runtime" (mountsOf c)) [
main
init
side
]
&&
(lib.findFirst (m: m.name == "runtime") null main.volumeMounts).mountPath == "/run/grafana-secrets"
# It logs in with its account's token, under the role its certificate has.
&& spec.serviceAccountName == "swarm-grafana"
&& objects."swarm-grafana/serviceaccount".metadata.name == "swarm-grafana"
&& lib.all (c: (envOfC c).BAO_JWT_ROLE == "swarm-grafana-oidc") [
main
init
side
]
&& !(lib.any (e: lib.hasPrefix "BAO_CLIENT" e.name) (init.env ++ side.env))
&& hasInfix "auth/jwt-k8s/role/swarm-grafana-oidc" script
&& hasInfix "bound_subject=system:serviceaccount:hyperhive:swarm-grafana" script
&& lib.elem "swarm-grafana-oidc" withNode.services.hyperhive.deploy.bao.k8sPrincipals
# The listener credential comes from the Secret the host publishes.
&& lib.any (s: (s.secret.name or null) == "swarm-grafana-credentials") credentials.projected.sources
# The Service and the policy: only the node in, and out to DNS, the
# store and the gateway.
&&
objects."swarm-grafana/service".spec.ports == [
{
name = "api";
protocol = "TCP";
port = 3000;
targetPort = "api";
}
]
&&
policy.policyTypes == [
"Ingress"
"Egress"
]
&& !(policy ? ingress)
&& lib.length policy.egress == 2
&&
(lib.elemAt policy.egress 1).ports == [
{
protocol = "TCP";
port = 8208;
}
{
protocol = "TCP";
port = 443;
}
]
&& lib.elem "d ${dir} - - - -" onCluster.systemd.tmpfiles.rules
&& !(lib.any (lib.hasPrefix "d ${dir} ") noNode.systemd.tmpfiles.rules);
}
{
# 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
@ -2826,6 +2977,7 @@ let
"hyperhive.darkest.space/state-ci-metrics=true"
"hyperhive.darkest.space/state-victorialogs=true"
"hyperhive.darkest.space/state-victoriametrics=true"
"hyperhive.darkest.space/state-grafana=true"
]
)
&&
@ -2836,6 +2988,7 @@ let
"hyperhive.darkest.space/state-ci-metrics" = "true";
"hyperhive.darkest.space/state-victorialogs" = "true";
"hyperhive.darkest.space/state-victoriametrics" = "true";
"hyperhive.darkest.space/state-grafana" = "true";
}
&& noNode.services.k3s.nodeLabel == [ ];
}

View file

@ -176,10 +176,6 @@ let
machine = "swarm-ci-runner";
option = "ci-runner";
}
{
machine = "swarm-grafana";
option = "grafana";
}
{
machine = "hive-forge";
option = "forge";
@ -193,14 +189,6 @@ let
# Those that share the host netns, on the shared services host.
hostNetnsForwarders = [
{
machine = "swarm-grafana";
option = "grafana";
name = "grafana";
cert = "grafana-bao-cert";
key = "grafana-bao-key";
port = 8896;
}
{
machine = "hive-forge";
option = "forge";
@ -712,7 +700,7 @@ let
lib.all ok1 (map (svc: svc.machine) forwarderServices);
}
{
name = "the swarm collector admits the CI runner, Grafana, log store, forge and homeserver forwarders on receivers of their own";
name = "the swarm collector admits the CI runner, forge and homeserver forwarders on receivers of their own";
ok =
let
h = shared.baoWithCollector;
@ -753,11 +741,7 @@ let
&& c.kind == "machine";
browser = find h.services.hyperhive.swarm.grafana.oidc.clientId;
in
lib.all ok1 ownClientForwarders
&&
h.services.hyperhive.swarm.grafana.otel.clientId != h.services.hyperhive.swarm.grafana.oidc.clientId
&& browser != null
&& browser.kind != "machine";
lib.all ok1 ownClientForwarders && browser != null && browser.kind != "machine";
}
{
# The homeserver's container already holds matrix-ctl's identity, bound

View file

@ -217,7 +217,7 @@ let
# The forwarders in the CI runner's, Grafana's, the log store's, the
# forge's, the homeserver's and authelia's containers read their secrets
# from these paths, and the metrics store's host reads its container's.
name = "the mint stores the CI runner, Grafana, log store, cache, forge, homeserver, metrics store and authelia forwarders' secrets";
name = "the mint stores the CI runner, cache, forge, homeserver and authelia forwarders' secrets";
ok =
lib.all
(
@ -228,7 +228,6 @@ let
)
[
"swarm-ci-runner-collector"
"swarm-grafana-collector"
"swarm-forge-collector"
"swarm-matrix-collector"
"swarm-authelia-collector"

View file

@ -201,7 +201,6 @@ let
"swarm-bao"
"swarm-builder"
"swarm-ci-runner"
"swarm-grafana"
"swarm-otel"
];
@ -259,7 +258,6 @@ let
"authelia-swarm.service"
"forgejo.service"
"gitea-runner-swarm.service"
"grafana.service"
"opentelemetry-collector.service"
];
}
@ -749,7 +747,6 @@ let
lib.all (m: c ? ${m}) [
"swarm-authelia"
"swarm-bao"
"swarm-grafana"
"swarm-otel"
"hive-matrix"
"hive-forge"
@ -797,7 +794,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) == 9
lib.length (lib.attrNames serviceContainers) == 8
&& lib.all (
c: c.privateNetwork == (c.config.services.hyperhive.swarmContainer.privateNetwork or null)
) (lib.attrValues serviceContainers);
@ -986,37 +983,6 @@ let
&& bridge.serviceConfig.Group == "gateway-nginx"
&& lib.elem dir unit.serviceConfig.ReadWritePaths;
}
{
# On the bridge by DHCP, listening on a socket (protocol `socket` ignores the
# upstream-default `http_port`, which stays in the settings): the
# gateway's locations (UI, metrics, websocket upgrade) all forward to it.
# `forwarded != []` keeps an empty filter from passing.
name = "grafana runs on a netns of its own and the gateway reaches it over its socket";
ok =
let
host = serviceContainersWithCi;
net = host.services.hyperhive.network;
c = host.containers.swarm-grafana;
dir = "/run/swarm-grafana";
sock = "${dir}/grafana.sock";
locs = host.services.nginx.virtualHosts.${host.services.hyperhive.swarm.grafana.domain}.locations;
forwarded = lib.filter (p: p != null && !(lib.hasPrefix "https://" p)) (
map (l: l.proxyPass) (lib.attrValues locs)
);
server = c.config.services.grafana.settings.server;
in
c.privateNetwork
&& c.hostBridge == net.bridgeName
&& c.hostAddress == null
&& c.localAddress == null
&& c.config.networking.interfaces.eth0.useDHCP
&& c.config.networking.firewall.enable
&& server.protocol == "socket"
&& server.socket == sock
&& forwarded != [ ]
&& lib.all (lib.hasPrefix "http://unix:${sock}:") forwarded
&& c.bindMounts.${dir}.hostPath == dir;
}
{
# atticd listens on the container's own loopback, a relay there forwards
# the socket nginx connects to, and the host unit that drives the
@ -1096,7 +1062,7 @@ let
&& inner.networking.firewall.enable
&& onSocket "h1"
&& onSocket otel.producerName
&& onSocket "swarm-grafana"
&& onSocket "swarm-attic"
&& lib.all (n: (receivers.${n}.protocols.http.transport or null) == "unix") (
lib.attrNames receivers
)
@ -1148,7 +1114,7 @@ let
verdict locs
&& lib.elem "/run/swarm-otel/h1.sock" receiverSocks
&& lib.elem "/run/swarm-otel/${otel.producerName}.sock" receiverSocks
&& lib.elem "/run/swarm-otel/swarm-grafana.sock" receiverSocks
&& lib.elem "/run/swarm-otel/swarm-attic.sock" receiverSocks
&&
lib.length unixReceivers == lib.length (lib.attrNames host.services.hyperhive.swarm.hives)
+ 1
@ -1338,9 +1304,9 @@ let
in
lib.length routes > 2
&& lib.elem "h1" routes
&& lib.elem "swarm-grafana" routes
&& lib.elem "swarm-attic" routes
&& verdict script
&& !verdict (lib.replaceStrings [ " swarm-grafana" ] [ "" ] script)
&& !verdict (lib.replaceStrings [ " swarm-attic" ] [ "" ] script)
&& !verdict (lib.replaceStrings [ "000 | 502 | 504" ] [ "000 | 504" ] script)
&& !verdict (lib.replaceStrings [ "--resolve" ] [ "--noresolve" ] script)
&& !verdict (lib.replaceStrings [ "%header{x-swarm-otel-route}" ] [ "" ] script)
@ -1348,7 +1314,7 @@ let
}
{
name = "a service container that sets no stateVersion of its own is on 26.05";
ok = serviceContainers.swarm-grafana.config.system.stateVersion == "26.05";
ok = serviceContainers.swarm-attic.config.system.stateVersion == "26.05";
}
{
name = "a service container that pins its own stateVersion keeps it";
@ -1473,11 +1439,6 @@ let
readers = [
"swarm-bao-forge-oidc"
"swarm-bao-matrix-oidc"
"swarm-bao-grafana-oidc"
"swarm-bao-grafana-admin"
"swarm-bao-grafana-metrics-bearer"
"swarm-bao-grafana-secret-key"
"swarm-bao-grafana-controller-token"
"swarm-bao-ci-runner"
"swarm-bao-otel-oidc"
];
@ -1501,9 +1462,9 @@ let
!(lib.any (n: lib.elem n (s.after ++ s.wants ++ s.requires)) storeUnits);
in
lib.all waits readers
&& lib.length readersHere == 9
&& lib.elem "swarm-bao-grafana-admin" readersHere
&& lib.length readersElsewhere == 7
&& lib.length readersHere == 4
&& lib.elem "swarm-bao-forge-oidc" readersHere
&& lib.length readersElsewhere == 2
&& lib.all namesNoStoreUnit readersElsewhere
&& !(lib.any (u: storeOnlyNoHive.systemd.services ? ${u}) readers);
}