From 1c8798a3cad1d7cfb882563cf3f56c1c4e357218 Mon Sep 17 00:00:00 2001 From: atlas Date: Sun, 22 Nov 2026 17:31:51 +0100 Subject: [PATCH] bao: the queue pod's two jwt roles share one ServiceAccount --- nix/host-modules/lib/swarm-bao-shared.nix | 4 +++ nix/host-modules/swarm-bao-readers.nix | 30 +++++++++++++++++------ nix/host-modules/swarm-bao.nix | 2 +- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/nix/host-modules/lib/swarm-bao-shared.nix b/nix/host-modules/lib/swarm-bao-shared.nix index 74ec9e724..e7101fb39 100644 --- a/nix/host-modules/lib/swarm-bao-shared.nix +++ b/nix/host-modules/lib/swarm-bao-shared.nix @@ -470,6 +470,9 @@ let # by spelling it the same way, and is itself named after it. controllerPolicyName = "swarm-controller"; + # The queue's principal: its policy and its cert-auth role carry this name. + natsPolicyName = "swarm-nats"; + in { inherit @@ -525,6 +528,7 @@ in listStanza openbaoStartSec openbaoStopSec + natsPolicyName readStanza refreshConsumer servicesPkiRootLabel diff --git a/nix/host-modules/swarm-bao-readers.nix b/nix/host-modules/swarm-bao-readers.nix index 5d9b7ff97..97370543c 100644 --- a/nix/host-modules/swarm-bao-readers.nix +++ b/nix/host-modules/swarm-bao-readers.nix @@ -35,6 +35,7 @@ let controllerForgeTokenLeaf controllerGrafanaTokenLeaf controllerPolicyName + natsPolicyName createStanza credentialMountPath grafanaAdminLeaf @@ -417,16 +418,27 @@ let ++ reviewClassifierReaders ++ claudeLoginWriters ++ ciMetricsReaders - ++ [ { name = controllerPolicyName; } ]; + ++ [ { name = controllerPolicyName; } ] + # The queue's pod holds both of its roles under one ServiceAccount: the + # server's leaf is issued with `swarm-nats`, and the responder reads its + # keys and credentials with `swarm-nats-auth`. + ++ map (obj: obj // { serviceAccount = natsPodAccount; }) ( + [ { name = natsPolicyName; } ] ++ natsAuthReaders + ); - # The jwt role of one principal: the token of exactly its ServiceAccount, for - # this store's audience. Both the subject and the audience are bound, so a + # The ServiceAccount the queue's pod runs under, as ../k8s/render.nix names + # it: the pod is the service `swarm-nats`. + natsPodAccount = "swarm-nats"; + + # The jwt role of one principal: the token of exactly its ServiceAccount (the + # one of its name, unless the record names another), for this store's + # audience. Both the subject and the audience are bound, so a # token of another account, or one minted for the Kubernetes API, is refused. k8sJwtRoleWrite = obj: '' bao write auth/${podAuth.mount}/role/${lib.escapeShellArg obj.name} \ role_type=jwt \ user_claim=sub \ - bound_subject=${lib.escapeShellArg "system:serviceaccount:${hyperhiveCfg.k8s.namespace}:${obj.name}"} \ + bound_subject=${lib.escapeShellArg "system:serviceaccount:${hyperhiveCfg.k8s.namespace}:${obj.serviceAccount or obj.name}"} \ bound_audiences=${lib.escapeShellArg podAuth.audience} \ token_policies=${lib.escapeShellArg obj.name}${roleTokenArgs obj} ''; @@ -450,10 +462,12 @@ in internal = true; default = map (p: p.name) k8sPrincipals; description = '' - The in-cluster services that log in to the store with a ServiceAccount - token. Each is one principal: its cert-auth role and its jwt role carry - the same name, policy and token lifetimes, and its pod runs under a - ServiceAccount of that name. + The principals of the in-cluster services that log in to the store with + a ServiceAccount token. Each is one principal: its cert-auth role and + its jwt role carry the same name, policy and token lifetimes, and its + pod runs under a ServiceAccount of that name. The queue's pod is the + exception: it holds two principals, `swarm-nats` and + `swarm-nats-auth`, under the one ServiceAccount `swarm-nats`. ''; }; diff --git a/nix/host-modules/swarm-bao.nix b/nix/host-modules/swarm-bao.nix index d40fc897d..f07ae0780 100644 --- a/nix/host-modules/swarm-bao.nix +++ b/nix/host-modules/swarm-bao.nix @@ -74,6 +74,7 @@ let controllerGrafanaTokenLeaf controllerPolicyName controllerWebhookSecretLeaf + natsPolicyName createStanza credentialMountPath grafanaAdminLeaf @@ -899,7 +900,6 @@ let # The queue's principal: one `update` on its own role, the shape of the # services issuer's grant above, narrowed to one name by the role itself. - natsPolicyName = "swarm-nats"; natsCn = baoDeploy.natsCommonName; natsPkiRoleName = baoDeploy.natsPkiRoleName; natsPolicyText = ''