From b19ef152b003721c4623a8ae00d4574754619fa5 Mon Sep 17 00:00:00 2001 From: atlas Date: Sun, 22 Nov 2026 02:37:17 +0100 Subject: [PATCH] module-eval: the claude daemons' units, host-mode containers, pods, jwt roles and refusals --- nix/module-eval/k8s.nix | 221 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 210 insertions(+), 11 deletions(-) diff --git a/nix/module-eval/k8s.nix b/nix/module-eval/k8s.nix index 9c05d7aad..e784b0155 100644 --- a/nix/module-eval/k8s.nix +++ b/nix/module-eval/k8s.nix @@ -49,8 +49,9 @@ let }; withNode = hiveWithController nodeSettings; - # The same host with the router, the speech-to-text service and the CI run - # poller in the cluster, so it has neither container nor poller unit. A + # The same host with the router, the speech-to-text service, the review + # classifier, the claude login minter and the CI run poller in the cluster, + # so it has none of their containers, nor the poller's unit or timer. A # second full host, because the claim is about what the configuration drops; # the other cases of this group read `withNode`. onCluster = hiveWithController ( @@ -58,6 +59,8 @@ let deploy.notify-router.runtime = "k8s"; deploy.stt.runtime = "k8s"; deploy.ci-metrics.runtime = "k8s"; + deploy.review-classifier.runtime = "k8s"; + deploy.claude-login.runtime = "k8s"; } ); @@ -66,6 +69,8 @@ let deploy.notify-router.runtime = "k8s"; deploy.stt.runtime = "k8s"; deploy.ci-metrics.runtime = "k8s"; + deploy.review-classifier.runtime = "k8s"; + deploy.claude-login.runtime = "k8s"; }; runtimeRefusals = lib.filter (a: !a.assertion) runtimeRefused.assertions; @@ -96,6 +101,10 @@ let swarm-notify-router = allLocal.containers.swarm-notify-router.config.systemd.services.swarm-notify-router; swarm-stt = allLocal.containers.swarm-stt.config.systemd.services.swarm-stt; + swarm-review-classifier = + allLocal.containers.swarm-review-classifier.config.systemd.services.swarm-review-classifier; + swarm-claude-login = + allLocal.containers.swarm-claude-login.config.systemd.services.swarm-claude-login; swarm-ci-metrics = allLocal.systemd.services.swarm-ci-metrics; }; @@ -152,6 +161,14 @@ let SWARM_STT_HEALTH_ADDR = "0.0.0.0:8080"; STATE_DIRECTORY = "/var/lib/swarm-stt"; }; + swarm-review-classifier = { + OTEL_EXPORTER_OTLP_ENDPOINT = collectorEndpoint; + SWARM_REVIEW_CLASSIFIER_HEALTH_ADDR = "0.0.0.0:8080"; + }; + swarm-claude-login = { + OTEL_EXPORTER_OTLP_ENDPOINT = collectorEndpoint; + SWARM_CLAUDE_LOGIN_HEALTH_ADDR = "0.0.0.0:8080"; + }; # No endpoint: the poller keeps the route its unit names. swarm-ci-metrics = { STATE_DIRECTORY = "/var/lib/swarm-ci-metrics"; @@ -174,7 +191,10 @@ let ++ units.swarm-ci-metrics.serviceConfig.LoadCredential ) ++ map (f: lib.last (lib.splitString ":" f)) ( - allLocal.containers.swarm-notify-router.extraFlags ++ allLocal.containers.swarm-stt.extraFlags + allLocal.containers.swarm-notify-router.extraFlags + ++ allLocal.containers.swarm-stt.extraFlags + ++ allLocal.containers.swarm-review-classifier.extraFlags + ++ allLocal.containers.swarm-claude-login.extraFlags ); credentialIds = lib.concatMap ( u: map (e: lib.head (lib.splitString ":" e)) u.serviceConfig.LoadCredential @@ -190,6 +210,10 @@ let "notify-router-bao-key" "stt-bao-cert" "stt-bao-key" + "review-classifier-bao-cert" + "review-classifier-bao-key" + "claude-login-bao-cert" + "claude-login-bao-key" ]; ciTimer = allLocal.systemd.timers.swarm-ci-metrics.timerConfig; @@ -213,6 +237,8 @@ let routerPrune = withNode.systemd.services.swarm-notify-router-k8s-prune; sttPrune = withNode.systemd.services.swarm-stt-k8s-prune; ciPrune = withNode.systemd.services.swarm-ci-metrics-k8s-prune; + classifierPrune = withNode.systemd.services.swarm-review-classifier-k8s-prune; + loginPrune = withNode.systemd.services.swarm-claude-login-k8s-prune; clusterRouterSpec = (render onCluster)."swarm-notify-router/deployment".spec.template.spec; cases = [ { @@ -301,17 +327,17 @@ let } { # The images are the ones the manifests run, one per rendered service: - # the four workloads are the control that the list is not empty. + # the six workloads 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 == 4 + lib.length rendered == 6 && lib.all (i: lib.elem i loaded) rendered && lib.elem (toString withNode.services.k3s.package.airgap-images) loaded - && lib.length loaded == 5; + && lib.length loaded == 7; } { # Nothing the host already runs differs but the store's side of the pods' @@ -321,7 +347,7 @@ let # granter's role unit (which enables the login's auth mount) starts the # same command, and the manifests are the same derivation as without the # node. - name = "the node adds only k3s, the jwt unit and the prune units of the router, the speech-to-text service and the CI run poller to the all-local host, and changes no container, timer or manifest"; + name = "the node adds only k3s, the jwt unit and the prune units of the four daemons and the CI run poller to the all-local host, and changes no container, timer or manifest"; ok = let names = a: lib.attrNames a; @@ -338,11 +364,15 @@ let || n == "swarm-notify-router-k8s-prune" || n == "swarm-stt-k8s-prune" || n == "swarm-ci-metrics-k8s-prune" + || n == "swarm-review-classifier-k8s-prune" + || n == "swarm-claude-login-k8s-prune" ) added && lib.elem "swarm-bao-k8s-jwt-policy" added && lib.elem "swarm-notify-router-k8s-prune" added && lib.elem "swarm-stt-k8s-prune" added && lib.elem "swarm-ci-metrics-k8s-prune" added + && lib.elem "swarm-review-classifier-k8s-prune" added + && lib.elem "swarm-claude-login-k8s-prune" added && lib.all ( @@ -428,7 +458,7 @@ let hiveTier = lib.filter (n: n != "swarm-ci-metrics") (lib.attrNames units); ownRoute = "https://${allLocal.services.hyperhive.swarm.otel.domain}/swarm-ci-metrics"; in - lib.length hiveTier == 3 + lib.length hiveTier == 5 && lib.all ( name: (envOf name).OTEL_EXPORTER_OTLP_ENDPOINT == collectorEndpoint @@ -572,6 +602,8 @@ let certScript = { swarm-notify-router = s.swarm-bao-notify-router-policy.script; swarm-stt = s.swarm-bao-stt-policy.script; + swarm-review-classifier = s.swarm-bao-review-classifier-policy.script; + swarm-claude-login = s.swarm-bao-claude-login-policy.script; swarm-ci-metrics = s.swarm-bao-ci-metrics-policy.script; swarm-controller = s.swarm-bao-controller-policy.script; }; @@ -587,7 +619,7 @@ let ${ttl} ''; in - lib.length roles == 4 + lib.length roles == 6 && lib.sort lib.lessThan roles == lib.sort lib.lessThan (lib.attrNames units) && lib.sort lib.lessThan withNode.services.hyperhive.deploy.bao.k8sPrincipals @@ -724,7 +756,7 @@ let && !(allLocal.systemd.services ? hyperhive-k8s-ca-trust); } { - name = "a router, a speech-to-text service or a poller in the cluster leaves the host no container, service, timer or prune unit, and applies the namespace and its objects"; + name = "a daemon or the poller in the cluster leaves the host no container, service, timer or prune unit, and applies the namespace and its objects"; ok = !(onCluster.containers ? swarm-notify-router) && !(onCluster.systemd.services ? "container@swarm-notify-router") @@ -736,12 +768,20 @@ let && !(onCluster.systemd.timers ? swarm-ci-metrics) && !(onCluster.systemd.services ? swarm-ci-metrics-k8s-prune) && !(onCluster.systemd.services ? swarm-ci-metrics-ca-bundle) + && !(onCluster.containers ? swarm-review-classifier) + && !(onCluster.systemd.services ? "container@swarm-review-classifier") + && !(onCluster.systemd.services ? swarm-review-classifier-k8s-prune) + && !(onCluster.containers ? swarm-claude-login) + && !(onCluster.systemd.services ? "container@swarm-claude-login") + && !(onCluster.systemd.services ? swarm-claude-login-k8s-prune) && manifestNames onCluster == [ "coredns-custom" "hyperhive-namespace" "swarm-ci-metrics" + "swarm-claude-login" "swarm-notify-router" + "swarm-review-classifier" "swarm-stt" ] && onCluster.services.k3s.manifests.hyperhive-namespace.content.kind == "Namespace" @@ -1010,10 +1050,169 @@ let && spec.securityContext.seccompProfile.type == "RuntimeDefault"; } { - name = "the router's, the speech-to-text service's and the poller's runtime \"k8s\" needs the node"; + # The unit is the container's whole unit, so the stand-alone evaluation + # cannot drift from what the host runs. The claude binary is the control + # that the unfree package is in the pod's unit: the stand-alone + # evaluation allows `claude-code` by its own predicate and nothing else. + name = "the review classifier's and the claude login minter's units, and so their manifests, are the same whether the container or the stand-alone evaluation supplies them"; + ok = + let + classifier = c: c.services.hyperhive.deploy.review-classifier.unit; + login = c: c.services.hyperhive.deploy.claude-login.unit; + in + lib.hasSuffix "/bin/claude" (classifier onCluster).environment.SWARM_REVIEW_CLASSIFIER_CLAUDE + && lib.hasInfix "claude-code" (classifier onCluster).environment.SWARM_REVIEW_CLASSIFIER_CLAUDE + && lib.hasSuffix "/bin/opencode" (classifier onCluster).environment.SWARM_REVIEW_CLASSIFIER_OPENCODE + && lib.hasInfix "claude-code" (login onCluster).environment.SWARM_CLAUDE_LOGIN_CLAUDE + && (classifier onCluster).serviceConfig == (classifier withNode).serviceConfig + && (classifier onCluster).environment == (classifier withNode).environment + && (login onCluster).serviceConfig == (login withNode).serviceConfig + && (login onCluster).environment == (login withNode).environment + && render onCluster == render withNode; + } + { + # The host-mode containers are pinned by value: both modules moved into + # let bindings, and nothing about the containers they define may change. + name = "with the review classifier and the claude login minter on the host, their containers, credential flags and units are as the modules define them"; + ok = + let + check = + { + name, + id, + prune, + }: + let + c = withNode.containers.${name}; + flags = c.extraFlags; + in + c.autoStart + && !c.ephemeral + && c.privateNetwork + && c.hostBridge == withNode.services.hyperhive.network.bridgeName + && lib.all (f: lib.hasPrefix "--load-credential=${id}-bao-" f) flags + && lib.any (lib.hasPrefix "--load-credential=${id}-bao-cert:") flags + && lib.any (lib.hasPrefix "--load-credential=${id}-bao-key:") flags + && withNode.systemd.services ? "container@${name}" + && prune.before == [ "container@${name}.service" ] + && prune.requiredBy == [ "container@${name}.service" ] + && lib.hasInfix "${name}.yaml" prune.script; + sc = name: units.${name}.serviceConfig; + in + check { + name = "swarm-review-classifier"; + id = "review-classifier"; + prune = classifierPrune; + } + && check { + name = "swarm-claude-login"; + id = "claude-login"; + prune = loginPrune; + } + && (sc "swarm-review-classifier").DynamicUser + && (sc "swarm-review-classifier").PrivateTmp + && (sc "swarm-claude-login").User == "swarm-claude-login" + && (sc "swarm-claude-login").PrivateTmp + && (sc "swarm-claude-login").UMask == "0077" + # The control: a host with them in the cluster has no such container. + && !(onCluster.containers ? swarm-review-classifier) + && !(onCluster.containers ? swarm-claude-login); + } + { + # Neither unit sets a limit, so the pods carry none. The stt pod, whose + # unit does, is the control that the lookup finds a limit where there is + # one. Neither keeps state, so their objects are a Deployment and an + # account, and the pod's `/tmp` volume is where the scratch directories + # and the `HOME` the daemons give `claude` land. + name = "the review classifier's and the claude login minter's pods carry no limits, keep no volume of state, probe their health listener and keep their scratch in the pod's /tmp volume"; + ok = + let + check = + { name, prefix }: + let + c = container name; + spec = podSpec name; + cred = lib.findFirst (v: v.name == "credentials") null spec.volumes; + kinds = lib.sort lib.lessThan (map (o: o.kind) onCluster.services.k3s.manifests.${name}.content); + in + !(c ? resources) + && + kinds == [ + "Deployment" + "ServiceAccount" + ] + && (lib.head c.ports).containerPort == 8080 + && c.readinessProbe.httpGet.path == "/readyz" + && c.livenessProbe.httpGet.path == "/healthz" + && (envOf name)."${prefix}_HEALTH_ADDR" == "0.0.0.0:8080" + && lib.any (m: m.name == "tmp" && m.mountPath == "/tmp") c.volumeMounts + && (lib.findFirst (v: v.name == "tmp") null spec.volumes).emptyDir == { } + && !(lib.any (v: v ? persistentVolumeClaim) spec.volumes) + && !((envOf name) ? TMPDIR) + && !((envOf name) ? HOME) + && c.securityContext.readOnlyRootFilesystem + && c.securityContext.runAsNonRoot + && c.securityContext.capabilities.drop == [ "ALL" ] + && spec.securityContext.seccompProfile.type == "RuntimeDefault" + && (envOf name).BAO_AUTH == "jwt" + && (envOf name).BAO_JWT_ROLE == name + && !((envOf name) ? BAO_CLIENT_CERT) + && !(cred ? secret) + && + map (s: s.configMap.items) cred.projected.sources == [ + [ + { + key = "bao-server-ca.pem"; + path = lib.removePrefix "swarm-" name + "-bao-server-ca"; + } + ] + ]; + in + check { + name = "swarm-review-classifier"; + prefix = "SWARM_REVIEW_CLASSIFIER"; + } + && check { + name = "swarm-claude-login"; + prefix = "SWARM_CLAUDE_LOGIN"; + } + && (container "swarm-stt") ? resources; + } + { + # The minter's jwt login must be able to write what its certificate + # writes: the cert role's policy is the jwt role's, and it grants create + # and update on every agent's credentials. The classifier's policy is the + # control that the search for them finds only what is there. + name = "the claude login minter's jwt role has the policy that writes agents' credentials, and the classifier's reads its keys"; + ok = + let + s = withNode.systemd.services; + login = s.swarm-bao-claude-login-policy.script; + classifier = s.swarm-bao-review-classifier-policy.script; + jwt = s.swarm-bao-k8s-jwt-policy.script; + writes = '' + path "secret/data/swarm/agents/+/claude-credentials" { + capabilities = ["create", "update"] + } + ''; + in + lib.hasInfix (lib.removeSuffix "\n" writes) login + && !(lib.hasInfix "claude-credentials" classifier) + && lib.hasInfix "anthropic-api-key" classifier + && lib.hasInfix "token_policies=swarm-claude-login " jwt + && lib.hasInfix "bound_subject=system:serviceaccount:hyperhive:swarm-claude-login" jwt + && lib.hasInfix "bound_subject=system:serviceaccount:hyperhive:swarm-review-classifier" jwt + && lib.hasInfix "token_policies=swarm-claude-login " login; + } + { + name = "the four daemons' and the poller's runtime \"k8s\" needs the node"; ok = lib.any (a: lib.hasInfix "notify-router.runtime = \"k8s\" needs the" a.message) runtimeRefusals && lib.any (a: lib.hasInfix "stt.runtime = \"k8s\" needs the" a.message) runtimeRefusals + && lib.any ( + a: lib.hasInfix "review-classifier.runtime = \"k8s\" needs the" a.message + ) runtimeRefusals + && lib.any (a: lib.hasInfix "claude-login.runtime = \"k8s\" needs the" a.message) runtimeRefusals && lib.any (a: lib.hasInfix "ci-metrics.runtime = \"k8s\" needs the" a.message) runtimeRefusals && lib.all (a: a.assertion) onCluster.assertions; }