Watch
0
0
Fork
You've already forked hyperhive
0

module-eval: the queue's pod, jwt roles, host units, exposure rules and store guard

This commit is contained in:
atlas 2026-11-22 17:31:51 +01:00 • committed by maia
commit 580fdaaacb

View file

@ -68,6 +68,8 @@ let
deploy.review-classifier.runtime = "k8s"; deploy.review-classifier.runtime = "k8s";
deploy.claude-login.runtime = "k8s"; deploy.claude-login.runtime = "k8s";
deploy.swarm-controller.runtime = "k8s"; deploy.swarm-controller.runtime = "k8s";
deploy.nats.runtime = "k8s";
deploy.nats.k8s.freshStore = true;
} }
); );
@ -82,6 +84,7 @@ let
deploy.swarm-controller.enable = true; deploy.swarm-controller.enable = true;
deploy.swarm-controller.runtime = "k8s"; deploy.swarm-controller.runtime = "k8s";
deploy.swarm-controller.tcp.listenAddress = "10.0.0.5"; deploy.swarm-controller.tcp.listenAddress = "10.0.0.5";
deploy.nats.runtime = "k8s";
}; };
runtimeRefusals = lib.filter (a: !a.assertion) runtimeRefused.assertions; runtimeRefusals = lib.filter (a: !a.assertion) runtimeRefused.assertions;
@ -106,6 +109,9 @@ let
inherit lib pkgs config; inherit lib pkgs config;
}).objects; }).objects;
objects = render allLocal; objects = render allLocal;
# The queue's pod reads the settings of the system it runs from, which differ
# in the path of the server's key: what a switch must leave alone is the rest.
notQueue = lib.filterAttrs (n: _: !(lib.hasPrefix "swarm-nats/" n));
units = { units = {
swarm-controller = allLocal.systemd.services.swarm-controller; swarm-controller = allLocal.systemd.services.swarm-controller;
@ -347,19 +353,19 @@ let
&& !(lib.hasInfix "--disable=local-storage" start); && !(lib.hasInfix "--disable=local-storage" start);
} }
{ {
# The images are the ones the manifests run, one per rendered service and # The images are the ones the manifests run, one per rendered service, the
# the controller's listener leaf: the seven are the control that the list # controller's listener leaf and the queue's three more (server, exporter,
# is not empty. # init): the eleven 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"; name = "the node loads the image of every service the manifests run, and k3s's own";
ok = ok =
let let
loaded = map toString withNode.services.k3s.images; loaded = map toString withNode.services.k3s.images;
rendered = map toString withNode.system.build.k8sImages; rendered = map toString withNode.system.build.k8sImages;
in in
lib.length rendered == 7 lib.length rendered == 11
&& lib.all (i: lib.elem i loaded) rendered && lib.all (i: lib.elem i loaded) rendered
&& lib.elem (toString withNode.services.k3s.package.airgap-images) loaded && lib.elem (toString withNode.services.k3s.package.airgap-images) loaded
&& lib.length loaded == 8; && lib.length loaded == 12;
} }
{ {
# Nothing the host already runs differs but the store's side of the pods' # Nothing the host already runs differs but the store's side of the pods'
@ -394,6 +400,7 @@ let
|| n == "swarm-review-classifier-k8s-prune" || n == "swarm-review-classifier-k8s-prune"
|| n == "swarm-claude-login-k8s-prune" || n == "swarm-claude-login-k8s-prune"
|| n == "swarm-controller-k8s-prune" || n == "swarm-controller-k8s-prune"
|| n == "swarm-nats-k8s-prune"
) added ) added
&& lib.elem "swarm-bao-k8s-jwt-policy" added && lib.elem "swarm-bao-k8s-jwt-policy" added
&& lib.elem "swarm-notify-router-k8s-prune" added && lib.elem "swarm-notify-router-k8s-prune" added
@ -402,6 +409,7 @@ let
&& lib.elem "swarm-review-classifier-k8s-prune" added && lib.elem "swarm-review-classifier-k8s-prune" added
&& lib.elem "swarm-claude-login-k8s-prune" added && lib.elem "swarm-claude-login-k8s-prune" added
&& lib.elem "swarm-controller-k8s-prune" added && lib.elem "swarm-controller-k8s-prune" added
&& lib.elem "swarm-nats-k8s-prune" added
&& &&
lib.all lib.all
( (
@ -655,7 +663,11 @@ let
swarm-ci-metrics = s.swarm-bao-ci-metrics-policy.script; swarm-ci-metrics = s.swarm-bao-ci-metrics-policy.script;
swarm-controller = s.swarm-bao-controller-policy.script; swarm-controller = s.swarm-bao-controller-policy.script;
}; };
roles = map lib.head (lib.filter lib.isList (builtins.split "auth/jwt-k8s/role/([a-z-]+)" script)); 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: a case of their own.
roles = lib.filter (r: !(lib.hasPrefix "swarm-nats" r)) allRoles;
ttl = "token_ttl=1h \\\n token_max_ttl=1h"; ttl = "token_ttl=1h \\\n token_max_ttl=1h";
roleText = name: '' roleText = name: ''
bao write auth/jwt-k8s/role/${name} \ bao write auth/jwt-k8s/role/${name} \
@ -671,7 +683,7 @@ let
&& lib.sort lib.lessThan roles == lib.sort lib.lessThan (lib.attrNames units) && lib.sort lib.lessThan roles == lib.sort lib.lessThan (lib.attrNames units)
&& &&
lib.sort lib.lessThan withNode.services.hyperhive.deploy.bao.k8sPrincipals lib.sort lib.lessThan withNode.services.hyperhive.deploy.bao.k8sPrincipals
== lib.sort lib.lessThan roles == lib.sort lib.lessThan allRoles
&& lib.all ( && lib.all (
name: name:
lib.hasInfix (lib.removeSuffix "\n" (roleText name)) script lib.hasInfix (lib.removeSuffix "\n" (roleText name)) script
@ -682,6 +694,35 @@ let
) roles ) roles
&& lib.hasInfix "bao write auth/jwt-k8s/config -" script; && lib.hasInfix "bao write auth/jwt-k8s/config -" script;
} }
{
# The two roles are the leaf's and the responder's, each with its cert
# role's policy, and both bind the one account the pod runs under. The
# responder's lifetimes are its cert role's batch token.
name = "the store writes the queue's two jwt roles, each with its cert role's policy and both bound to the pod's one account";
ok =
let
s = withNode.systemd.services;
script = s.swarm-bao-k8s-jwt-policy.script;
role = name: ''
bao write auth/jwt-k8s/role/${name} \
role_type=jwt \
user_claim=sub \
bound_subject=system:serviceaccount:hyperhive:swarm-nats \
bound_audiences=bao \
token_policies=${name} \
'';
in
lib.hasInfix (role "swarm-nats") script
&& lib.hasInfix (role "swarm-nats-auth") script
&& lib.hasInfix "token_type=batch" s.swarm-bao-nats-auth-policy.script
&& lib.hasInfix "auth/cert/certs/swarm-nats " s.swarm-bao-nats-tls-policy.script
&& lib.hasInfix "auth/cert/certs/swarm-nats-auth" s.swarm-bao-nats-auth-policy.script
&& lib.elem "swarm-nats" withNode.services.hyperhive.deploy.bao.k8sPrincipals
&& lib.elem "swarm-nats-auth" withNode.services.hyperhive.deploy.bao.k8sPrincipals
# The account the pod renders under is the one both roles bind.
&& (podSpec "swarm-nats").serviceAccountName == "swarm-nats"
&& objects."swarm-nats/serviceaccount".metadata.name == "swarm-nats";
}
{ {
# The keys come from the node's own files, and the issuer is the one the # The keys come from the node's own files, and the issuer is the one the
# node's flags give its API server. # node's flags give its API server.
@ -822,6 +863,9 @@ let
&& !(onCluster.containers ? swarm-claude-login) && !(onCluster.containers ? swarm-claude-login)
&& !(onCluster.systemd.services ? "container@swarm-claude-login") && !(onCluster.systemd.services ? "container@swarm-claude-login")
&& !(onCluster.systemd.services ? swarm-claude-login-k8s-prune) && !(onCluster.systemd.services ? swarm-claude-login-k8s-prune)
&& !(onCluster.containers ? swarm-nats)
&& !(onCluster.systemd.services ? "container-nats")
&& !(onCluster.systemd.services ? swarm-nats-k8s-prune)
&& &&
manifestNames onCluster == [ manifestNames onCluster == [
"coredns-custom" "coredns-custom"
@ -829,6 +873,7 @@ let
"swarm-ci-metrics" "swarm-ci-metrics"
"swarm-claude-login" "swarm-claude-login"
"swarm-controller" "swarm-controller"
"swarm-nats"
"swarm-notify-router" "swarm-notify-router"
"swarm-review-classifier" "swarm-review-classifier"
"swarm-stt" "swarm-stt"
@ -863,7 +908,7 @@ let
&& &&
(routerUnit onCluster).serviceConfig.LoadCredential == (routerUnit withNode) (routerUnit onCluster).serviceConfig.LoadCredential == (routerUnit withNode)
.serviceConfig.LoadCredential .serviceConfig.LoadCredential
&& render onCluster == render withNode; && notQueue (render onCluster) == notQueue (render withNode);
} }
{ {
# The controller's pod is the control for the Secret lookup: its # The controller's pod is the control for the Secret lookup: its
@ -924,7 +969,7 @@ let
] ]
&& (unitOf onCluster).serviceConfig == (unitOf withNode).serviceConfig && (unitOf onCluster).serviceConfig == (unitOf withNode).serviceConfig
&& (unitOf onCluster).environment == (unitOf withNode).environment && (unitOf onCluster).environment == (unitOf withNode).environment
&& render onCluster == render withNode; && notQueue (render onCluster) == notQueue (render withNode);
} }
{ {
# The host-mode container is pinned by value: the module moved into a # The host-mode container is pinned by value: the module moved into a
@ -1120,7 +1165,7 @@ let
&& (classifier onCluster).environment == (classifier withNode).environment && (classifier onCluster).environment == (classifier withNode).environment
&& (login onCluster).serviceConfig == (login withNode).serviceConfig && (login onCluster).serviceConfig == (login withNode).serviceConfig
&& (login onCluster).environment == (login withNode).environment && (login onCluster).environment == (login withNode).environment
&& render onCluster == render withNode; && notQueue (render onCluster) == notQueue (render withNode);
} }
{ {
# The host-mode containers are pinned by value: both modules moved into # The host-mode containers are pinned by value: both modules moved into
@ -1269,6 +1314,7 @@ let
&& lib.any ( && lib.any (
a: lib.hasInfix "swarm-controller.runtime = \"k8s\" needs the" a.message a: lib.hasInfix "swarm-controller.runtime = \"k8s\" needs the" a.message
) runtimeRefusals ) runtimeRefusals
&& lib.any (a: lib.hasInfix "nats.runtime = \"k8s\" needs the cluster" a.message) runtimeRefusals
&& lib.any ( && lib.any (
a: lib.hasInfix "controller.tcp.enable: the host cannot reach" a.message a: lib.hasInfix "controller.tcp.enable: the host cannot reach" a.message
) runtimeRefusals ) runtimeRefusals
@ -1383,7 +1429,7 @@ let
&& !(c.timers ? swarm-controller-tls) && !(c.timers ? swarm-controller-tls)
&& !(c.services ? swarm-controller-k8s-prune) && !(c.services ? swarm-controller-k8s-prune)
&& !(allLocal.systemd.services ? swarm-controller-k8s-prune) && !(allLocal.systemd.services ? swarm-controller-k8s-prune)
&& render onCluster == render withNode && notQueue (render onCluster) == notQueue (render withNode)
&& &&
lib.sort lib.lessThan (map (o: o.kind) onCluster.services.k3s.manifests.swarm-controller.content) lib.sort lib.lessThan (map (o: o.kind) onCluster.services.k3s.manifests.swarm-controller.content)
== [ == [
@ -1527,6 +1573,218 @@ let
&& lib.any (e: e.name == "SWARM_CONTROLLER_TCP_CLIENT_CA" && e.value == servicesRoot) main.env; && lib.any (e: e.name == "SWARM_CONTROLLER_TCP_CLIENT_CA" && e.value == servicesRoot) main.env;
} }
{
# Everything of the pod is read off the manifest the module renders. The
# `tmp` volume is the control that the lookup of a volume's medium finds
# a plain one.
name = "the queue's pod is one replaced copy of the responder, the server and the exporter, started after the store guard, the leaf and the callout config";
ok =
let
o = objects."swarm-nats/deployment";
spec = o.spec.template.spec;
named = n: lib.findFirst (c: c.name == n) { } spec.containers;
volume = n: lib.findFirst (v: v.name == n) null spec.volumes;
pvc = objects."swarm-nats/persistentvolumeclaim";
env = envOf "swarm-nats";
in
o.spec.replicas == 1
&& o.spec.strategy.type == "Recreate"
&&
map (c: c.name) spec.containers == [
"swarm-nats"
"nats"
"tls-renew"
"exporter"
]
# The renewal signals the server, so the two share their processes, and
# it writes the volume the server only reads.
&& spec.shareProcessNamespace
&& (named "tls-renew").command == [ "/bin/swarm-nats-tls-renew" ]
&& lib.any (m: m.name == "nats-tls" && !(m.readOnly or false)) (named "tls-renew").volumeMounts
&& lib.all (m: m.readOnly or false) (
lib.filter (m: m.name == "nats-tls") (named "nats").volumeMounts
)
&&
map (c: c.name) spec.initContainers == [
"store-guard"
"tls-issue"
"callout-config"
]
# The responder and the server dial the queue's name on the pod's own
# loopback, where the leaf's name is checked.
&&
spec.hostAliases == [
{
ip = "127.0.0.1";
hostnames = [ "nats.t.local" ];
}
]
&& lib.elem "tls://nats.t.local:4222" (named "swarm-nats").args
&&
(named "nats").args == [
"-c"
"/var/lib/nats-callout/nats.conf"
]
# The client port is published on every address of the node, and the
# exporter's on the bridge's.
&&
(named "nats").ports == [
{
name = "client";
containerPort = 4222;
hostPort = 4222;
hostIP = "0.0.0.0";
}
]
&&
(named "exporter").ports == [
{
name = "metrics";
containerPort = 7777;
hostPort = 7777;
hostIP = "10.42.0.1";
}
]
&& lib.elem "0.0.0.0" (named "exporter").args
&&
(named "nats").readinessProbe.exec.command == [
"/bin/wget"
"-q"
"-O"
"/dev/null"
"http://127.0.0.1:8222/healthz?js-enabled-only=true"
]
# The responder's own probes, on an address of the pod's own.
&& env.SWARM_NATS_AUTH_HEALTH_ADDR == "0.0.0.0:8080"
&& (named "swarm-nats").readinessProbe.httpGet.path == "/readyz"
&& env.BAO_JWT_ROLE == "swarm-nats-auth"
&& !(env ? BAO_CLIENT_CERT)
# The per-agent subjects keep their `$`: a pod's arguments are not
# unescaped by systemd.
&& lib.elem "$SWARM.term.{agent}" (named "swarm-nats").args
&& !(lib.any (a: lib.hasInfix "$$" a) (named "swarm-nats").args)
# The leaf's key is in memory; the store is a claim; nothing is capped.
&& (volume "nats-tls").emptyDir.medium == "Memory"
&& !((volume "tmp").emptyDir ? medium)
&& (volume "nats-data").persistentVolumeClaim.claimName == pvc.metadata.name
&& pvc.spec.accessModes == [ "ReadWriteOnce" ]
&& !(lib.any (c: c ? resources) spec.containers)
&& !(lib.any (v: v ? secret) spec.volumes);
}
{
# The default holds the server back on an empty store; a fresh install
# opts in. Each arm's value is the control for the other.
name = "the store guard reads the volume the server writes, and lets an empty store through only where the install is fresh";
ok =
let
guard = c: lib.findFirst (i: i.name == "store-guard") { } (podSpec "swarm-nats").initContainers;
fresh = render onCluster;
freshGuard = lib.findFirst (i: i.name == "store-guard") { } (
fresh."swarm-nats/deployment".spec.template.spec.initContainers
);
freshEnv = g: (lib.findFirst (e: e.name == "SWARM_NATS_FRESH_STORE") { } g.env).value;
in
freshEnv (guard null) == "false"
&& freshEnv freshGuard == "true"
&&
(guard null).volumeMounts == [
{
name = "tmp";
mountPath = "/tmp";
}
{
name = "nats-data";
mountPath = "/var/lib/nats";
readOnly = true;
}
]
# The server mounts the same volume, writable, at the same path.
&&
lib.any (m: m.name == "nats-data" && m.mountPath == "/var/lib/nats" && !(m.readOnly or false))
(lib.findFirst (c: c.name == "nats") { } (podSpec "swarm-nats").containers).volumeMounts;
}
{
# The responder's unit is the container's on the host and the
# stand-alone evaluation's in the cluster; its arguments and its
# environment are one text.
name = "the responder's unit is the same whether the container or the stand-alone evaluation supplies it";
ok =
let
u = c: c.services.hyperhive.deploy.nats.unit;
in
(u withNode).serviceConfig.ExecStart != null
&& lib.hasInfix "--nats-url tls://nats.t.local:4222" (u withNode).serviceConfig.ExecStart
&& (u onCluster).serviceConfig.ExecStart == (u withNode).serviceConfig.ExecStart
&& (u onCluster).environment == (u withNode).environment
&& (u onCluster).serviceConfig.LoadCredential == (u withNode).serviceConfig.LoadCredential;
}
{
# The host with the queue on it is the control for each absence: it has
# the container, both delivery units and no rule in the raw table.
name = "the queue in the cluster leaves the host no container and no delivery unit, keeps the leaf for the gateway's vhost, and points the vhost at the exporter's published port";
ok =
let
vhost = c: c.services.nginx.virtualHosts."nats.t.local".locations."= /metrics".proxyPass;
tls = onCluster.systemd.services.swarm-bao-nats-tls;
in
withNode.containers ? swarm-nats
&& withNode.systemd.services ? "container@swarm-nats"
&& withNode.systemd.services.swarm-nats-auth-secrets.enable
&& withNode.systemd.services.swarm-bao-nats-callout-xkey.enable
&& withNode.systemd.services.swarm-nats-k8s-prune.before == [ "container@swarm-nats.service" ]
&& withNode.systemd.services.swarm-nats-k8s-prune.requiredBy == [ "container@swarm-nats.service" ]
&& lib.hasInfix "swarm-nats.yaml" withNode.systemd.services.swarm-nats-k8s-prune.script
&& lib.elem "container@swarm-nats.service" withNode.systemd.services.swarm-bao-nats-tls.before
&& !(lib.hasInfix "nixos-fw-nats" withNode.networking.firewall.extraCommands)
&& vhost withNode == "http://127.0.0.1:7777/metrics"
# In the cluster: nothing to start the server or deliver to it, a leaf
# that restarts nothing, the exporter on the bridge, the bridge up.
&& !(onCluster.containers ? swarm-nats)
&& !(onCluster.systemd.services ? "container@swarm-nats")
&& !onCluster.systemd.services.swarm-nats-auth-secrets.enable
&& !onCluster.systemd.services.swarm-bao-nats-callout-xkey.enable
&& !(lib.elem "container@swarm-nats.service" tls.before)
&& !(lib.elem "container@swarm-nats.service" tls.wantedBy)
&& onCluster.systemd.services ? swarm-nats-gateway-cert
&& vhost onCluster == "http://10.42.0.1:7777/metrics"
&& onCluster.services.hyperhive.network.enable;
}
{
# Both arms of `openFirewall`, from the function the module calls, and
# the module's own text for the default.
name = "the pod's raw-table rules close the client port as the container's INPUT rules do, and the exporter's port to all but the host's loopback";
ok =
let
rules = import ../host-modules/lib/swarm-nats-raw-rules.nix { inherit lib; };
args = {
chain = "c";
clientPort = 4222;
metricsPort = 7777;
trusted = [
"lo"
"br0"
"cni0"
];
};
closed = rules (args // { openClient = false; });
open = rules (args // { openClient = true; });
has = lib.hasInfix;
in
has "-p tcp --dport 4222 -i br0 -j RETURN" closed
&& has "-p tcp --dport 4222 -i cni0 -j RETURN" closed
&& has "-p tcp --dport 4222 -m addrtype --dst-type LOCAL -j DROP" closed
# Open, the client port has no rule; the exporter's still has.
&& !(has "--dport 4222" open)
&& has "-p tcp --dport 7777 -i lo -j RETURN" open
&& has "-p tcp --dport 7777 -m addrtype --dst-type LOCAL -j DROP" open
# The exporter's port is not returned for the bridge or the pods.
&& !(has "--dport 7777 -i br0" closed)
&& !(has "--dport 7777 -i cni0" closed)
&& has "-t raw -I PREROUTING -j c" closed
&& has "-t raw -A nixos-fw-nats -p tcp --dport 4222 -i cni0 -j RETURN" onCluster.networking.firewall.extraCommands
&& has "-t raw -A nixos-fw-nats -p tcp --dport 4222 -m addrtype --dst-type LOCAL -j DROP" onCluster.networking.firewall.extraCommands
&& has "-t raw -F nixos-fw-nats" onCluster.networking.firewall.extraStopCommands;
}
]; ];
in in
runGroup "k8s" cases runGroup "k8s" cases