# The store's per-principal grants for the services that each read or write a # few leaves with a leaf of their own: one policy and one cert-auth role per # principal, written by the granter. Split out of ./swarm-bao.nix; the unit # shape, the leaves and the stanzas it shares with the other grants are in # ./lib/swarm-bao-shared.nix. { config, lib, pkgs, ... }: let cfg = config.services.hyperhive.swarm.bao; hyperhiveCfg = config.services.hyperhive; deployCfg = hyperhiveCfg.deploy; baoDeploy = deployCfg.bao; shared = import ./lib/swarm-bao-shared.nix { inherit config lib pkgs; }; inherit (shared) atticFactLeaves atticJwtLeaf atticPublicKeyLeaf atticPushTokenLeaf builderForgeTokenLeaf certRoleTokenTtl ciMetricsForgeTokenLeaf ciRunnerTokenLeaf classifierAcpKeyLeaf classifierApiKeyLeaf controllerForgeTokenLeaf controllerGrafanaTokenLeaf createStanza credentialMountPath grafanaAdminLeaf grafanaMetricsBearerLeaf grafanaSecretKeyLeaf granterPolicyUnit haveServerTls issuerPublicLeaf issuerSeedLeaf listStanza readStanza tlsDir userPublicLeaf userSeedLeaf xkeyPublicLeaf xkeySeedLeaf ; # ── the principals that each hold a leaf of their own ────────────────────── # # 🩸 Each of these reads exactly ONE path in the store, and does it holding # its own leaf rather than `deploy.bao.clientCertFile` — the hive's own leaf, # whose policy (`swarm-secret-client`'s `policy::render_shared_hives`) grants read on # `swarm/agents/*` AND `swarm/hives//*`, and nothing under # `swarm/services/`. # Principals presenting one certificate are ONE principal to bao, so sharing # the hive's leaf would give each of them everything that leaf reads: a unit # that fetches a service's OIDC client secret could fetch every agent # credential in the swarm, and could not fetch its own secret at all, as # that path is not the hive's to read. # # Each gets its own leaf, its own cert-auth role and a policy holding the # one `secret/data/…` path that principal's script actually names — spelled # to the LEAF rather than to a prefix, for the reason `matrixCtlPolicyText` # above gives for doing the same. createReadStanza = path: '' path "${path}" { capabilities = ["create", "read"] } ''; # Singletons, so a list of one rather than a second shape: the granting unit # below takes a list either way and the units stay identical apart from # the objects they write. # # Grafana's leaf also creates its admin password (`swarm-bao-grafana-admin` # in ./swarm-grafana.nix) and the bearer its metrics datasource presents # (`swarm-bao-grafana-metrics-bearer`) and its datasource-encryption key # (`swarm-bao-grafana-secret-key`). `create` without `update`, so a stored # value is never replaced. # # The controller's Grafana token is the one it may replace: # `swarm-bao-grafana-controller-token` mints a new one when Grafana refuses # the stored one, as it does after its database is recreated. grafanaOidcReaders = [ { name = "swarm-grafana-oidc"; cn = baoDeploy.grafanaOidcCommonName; policyText = readStanza "${credentialMountPath}/data/swarm/services/${hyperhiveCfg.swarm.grafana.oidc.clientId}/oidc/client" # The journal forwarder in its container, under this same leaf. + readStanza "${credentialMountPath}/data/swarm/services/${hyperhiveCfg.swarm.grafana.otel.clientId}/oidc/client" + createStanza "${credentialMountPath}/data/${grafanaAdminLeaf}" + createStanza "${credentialMountPath}/data/${grafanaMetricsBearerLeaf}" + createStanza "${credentialMountPath}/data/${grafanaSecretKeyLeaf}" + '' path "${credentialMountPath}/data/${controllerGrafanaTokenLeaf}" { capabilities = ["create", "read", "update"] } ''; } ]; otelOidcReaders = [ { name = "swarm-otel-oidc"; cn = baoDeploy.otelOidcCommonName; policyText = readStanza "${credentialMountPath}/data/swarm/services/${hyperhiveCfg.swarm.otel.clientId}/oidc/client"; } ]; # The store's own forwarder. Its path is `forwarderStoreSecretPath` below with # KV v2's `data/` inserted. forwarderOidcReaders = [ { name = "swarm-forwarder-oidc"; cn = baoDeploy.forwarderOidcCommonName; policyText = readStanza "${credentialMountPath}/data/swarm/services/${cfg.otel.clientId}/oidc/client"; } ]; # The queue's auth-callout responder, which checks the secret an agent # presents against the one stored for it. One leaf under every agent: `+` is # one path segment, where `*` globs only at the end and would reach every # other credential an agent holds. Plus the responder's own OIDC client # secret, which it reads as it starts and again when the IdP refuses it. # # And the two leaves of each callout key, which `ensure-callout-keys` # creates when the store holds no seed. `create` without `update`, so a # stored pair is never replaced; `read` because creating is # read-then-create, and the responder loads the seeds as it starts. natsAuthReaders = [ { name = "swarm-nats-auth"; cn = baoDeploy.natsAuthCommonName; batchToken = true; policyText = readStanza "${credentialMountPath}/data/swarm/agents/+/queue" + readStanza "${credentialMountPath}/data/swarm/services/${hyperhiveCfg.swarm.nats.clientId}/oidc/client" + lib.concatMapStrings (leaf: createStanza "${credentialMountPath}/data/${leaf}") [ xkeySeedLeaf xkeyPublicLeaf issuerSeedLeaf issuerPublicLeaf userSeedLeaf userPublicLeaf ]; } ]; # The log store's auth responders: the agent one checks the secret an agent # presents against the one stored for it (the same `+` leaf as the queue # responder's) and reads the agent's mirrored `logs`/`metrics` grant, the other checks the bearer Grafana presents against # Grafana's metrics bearer; and the store container's journal forwarder # reads its own client secret under the same leaf. Nothing else. logsAuthReaders = [ { name = "swarm-logs-auth"; cn = baoDeploy.logsAuthCommonName; batchToken = true; policyText = readStanza "${credentialMountPath}/data/swarm/agents/+/queue" + readStanza "${credentialMountPath}/data/swarm/agents/+/observability" + readStanza "${credentialMountPath}/data/${grafanaMetricsBearerLeaf}" # The journal forwarder in the store's container, under this same leaf. + readStanza "${credentialMountPath}/data/swarm/services/${hyperhiveCfg.swarm.victorialogs.otel.clientId}/oidc/client"; } ]; # The metrics store's responders under one leaf: the agent one checks the # secret an agent presents against the one stored for it (the same `+` leaf # as the log store's) and reads the agent's mirrored grant, the other checks the bearer Grafana presents; and the # host unit that fetches the store container's journal forwarder secret. # Nothing else. metricsAuthReaders = [ { name = "swarm-metrics-auth"; cn = baoDeploy.metricsAuthCommonName; batchToken = true; policyText = readStanza "${credentialMountPath}/data/swarm/agents/+/queue" + readStanza "${credentialMountPath}/data/swarm/agents/+/observability" + readStanza "${credentialMountPath}/data/${grafanaMetricsBearerLeaf}" # The metrics store container's journal forwarder, fetched on its host. + readStanza "${credentialMountPath}/data/swarm/services/${hyperhiveCfg.swarm.victoriametrics.otel.clientId}/oidc/client"; } ]; # The CI runner's token fetch. The one reader of the path, which the # controller writes and cannot read back. ciRunnerReaders = [ { name = "swarm-ci-runner"; cn = baoDeploy.ciRunnerCommonName; policyText = readStanza "${credentialMountPath}/data/${ciRunnerTokenLeaf}" # The journal forwarder in the runner's container, under this same leaf. + readStanza "${credentialMountPath}/data/swarm/services/${hyperhiveCfg.swarm.ci-runner.otel.clientId}/oidc/client"; } ]; # The review classifier: the API key and the ACP provider key the operator # writes, the one its run's runtime needs read once per run, and its own # queue client secret, read at start. All by the daemon inside its # container. reviewClassifierReaders = [ { name = "swarm-review-classifier"; cn = baoDeploy.reviewClassifierCommonName; policyText = readStanza "${credentialMountPath}/data/${classifierApiKeyLeaf}" + readStanza "${credentialMountPath}/data/${classifierAcpKeyLeaf}" + readStanza "${credentialMountPath}/data/swarm/services/${hyperhiveCfg.swarm.review-classifier.clientId}/oidc/client"; } ]; # The swarm builder: its forge token, read once per build, the cache push token, # read once per push, and its own queue client secret, read at start. All by the # daemon inside its container. builderReaders = [ { name = "swarm-builder"; cn = baoDeploy.builderCommonName; policyText = readStanza "${credentialMountPath}/data/${builderForgeTokenLeaf}" + readStanza "${credentialMountPath}/data/${atticPushTokenLeaf}" + readStanza "${credentialMountPath}/data/swarm/services/${hyperhiveCfg.swarm.builder.clientId}/oidc/client"; } ]; # The in-swarm cache's six secrets: create and read, so a rewrite of a signing # key or a token an operator holds is refused. The public key also gets # `update`: the cache's host unit replaces it when a re-created cache signs # with another. The cache container's journal forwarder reads its client # secret under the same leaf. atticWriters = [ { name = "swarm-attic"; cn = baoDeploy.atticCommonName; policyText = lib.concatMapStrings (leaf: '' path "${credentialMountPath}/data/${leaf}" { capabilities = ["create", "read"] } '') ( [ atticJwtLeaf atticPushTokenLeaf ] ++ lib.remove atticPublicKeyLeaf atticFactLeaves ) + '' path "${credentialMountPath}/data/${atticPublicKeyLeaf}" { capabilities = ["create", "read", "update"] } '' + readStanza "${credentialMountPath}/data/swarm/services/${hyperhiveCfg.swarm.attic.otel.clientId}/oidc/client"; } ]; # The backup archive: the users file is rendered from one leaf per agent, and # the prune reads each agent's restic password. Read and list only; the # controller writes both. It also creates the marker that tells the # controller an archive exists. With an external S3 backend it reads the # provider key, which the operator writes. archiveReaders = [ { name = "swarm-archive"; cn = baoDeploy.archiveCommonName; policyText = readStanza "${credentialMountPath}/data/swarm/services/archive/users/*" + readStanza "${credentialMountPath}/data/swarm/services/archive/restic/*" + readStanza "${credentialMountPath}/data/swarm/services/archive/s3/*" + listStanza "${credentialMountPath}/metadata/swarm/services/archive/users/" + createReadStanza "${credentialMountPath}/data/swarm/services/archive/ready"; } ]; # The CI run poller: its forge token and its own OIDC client secret, both # read once per pass by the host unit. ciMetricsReaders = [ { name = "swarm-ci-metrics"; cn = baoDeploy.ciMetricsCommonName; policyText = readStanza "${credentialMountPath}/data/${ciMetricsForgeTokenLeaf}" + readStanza "${credentialMountPath}/data/swarm/services/${hyperhiveCfg.swarm.ci-metrics.clientId}/oidc/client"; } ]; # The claude login minter: every agent's `claude-credentials` leaf, # create and update so a relogin replaces the last, and nothing else under # an agent; the leaf's metadata, to say when it was written (`read`) and to # delete every version of it (`delete`; the `data/` path's delete would leave # the older versions, each a live session), never the credential itself; and # its own queue client secret, read at start. claudeLoginWriters = [ { name = "swarm-claude-login"; cn = baoDeploy.claudeLoginCommonName; policyText = '' path "${credentialMountPath}/data/swarm/agents/+/claude-credentials" { capabilities = ["create", "update"] } path "${credentialMountPath}/metadata/swarm/agents/+/claude-credentials" { capabilities = ["read", "delete"] } '' + readStanza "${credentialMountPath}/data/swarm/services/${hyperhiveCfg.swarm.claude-login.clientId}/oidc/client"; } ]; # The notification router: its own queue client secret, read at start by the # daemon inside its container, and nothing else. notifyRouterReaders = [ { name = "swarm-notify-router"; cn = baoDeploy.notifyRouterCommonName; policyText = readStanza "${credentialMountPath}/data/swarm/services/${hyperhiveCfg.swarm.notify-router.clientId}/oidc/client"; } ]; # The speech-to-text service: its own queue client secret, read at start by # the daemon inside its container, and nothing else. sttReaders = [ { name = "swarm-stt"; cn = baoDeploy.sttCommonName; policyText = readStanza "${credentialMountPath}/data/swarm/services/${hyperhiveCfg.swarm.stt.clientId}/oidc/client"; } ]; # The forge host's one store identity. The forge's # `forgejo-swarm-controller-account`, in its container, writes the # controller's forge token under it, the one writer of that path and # write-only: it puts the token it mints and never reads one back. # `swarm-bao-forge-oidc` reads the forge's own OIDC client secret, and the # journal forwarder in its container its own, those two leaves and no other # service's. forgePublisherWriters = [ { name = "swarm-forge-publisher"; cn = baoDeploy.forgePublisherCommonName; policyText = '' path "${credentialMountPath}/data/${controllerForgeTokenLeaf}" { capabilities = ["create", "update"] } '' + readStanza "${credentialMountPath}/data/swarm/services/${hyperhiveCfg.swarm.forge.sso.clientId}/oidc/client" # The journal forwarder in the forge's container, under this same leaf. + readStanza "${credentialMountPath}/data/swarm/services/${hyperhiveCfg.swarm.forge.otel.clientId}/oidc/client"; } ]; # The role name IS the policy name, as for the three service principals # above: the role attaches the policy by spelling it identically, and one # string for both objects removes the way they drift apart. readerPolicyWrite = obj: '' printf '%s' ${lib.escapeShellArg obj.policyText} | bao policy write ${lib.escapeShellArg obj.name} - ''; # ⚠️ Names are `swarm-[-]`, outside the `hive-*` namespace the # controller may rewrite, for the reason the three service principals above # state. A per-hive CN cannot collide with a hive's own leaf either — a hive's # CN is its bare name and ../reserved-hive-fragments.nix forbids the substring # `swarm` in one — and ./swarm.nix reserves the concrete per-hive spellings # anyway, because the prefixes are operator-settable and an operator may spell # one without that substring. # # `batchToken = true` on an object makes its logins batch tokens, which the # store does not persist and cannot renew or revoke. Only for a principal # that logs in, reads and drops the token within one call: the five # minutes outlast that read, and a leaked token dies as fast. A principal # that holds a handle longer must not set it. readerRoleWrite = obj: let ttlArgs = if obj.batchToken or false then " \\\n token_type=batch \\\n token_ttl=5m" else " \\\n token_ttl=${certRoleTokenTtl} \\\n token_max_ttl=${certRoleTokenTtl}"; in '' bao write auth/cert/certs/${lib.escapeShellArg obj.name} \ certificate=@${tlsDir}/client-ca.pem \ allowed_common_names=${lib.escapeShellArg obj.cn} \ token_policies=${lib.escapeShellArg obj.name} \ display_name=${lib.escapeShellArg obj.cn}${ttlArgs} ''; # One unit per PRINCIPAL, not one unit for all of them: these names are # operator-facing strings, the same reason `swarm-bao-secret-publisher-policy` # below gives for not being more script inside the controller's unit. A # reader whose secret never arrives is diagnosed by the state of the unit # named after it. readerPolicyUnit = description: objects: granterPolicyUnit description ( lib.concatMapStrings readerPolicyWrite objects + "\n" + lib.concatMapStrings readerRoleWrite objects ); in { config = lib.mkIf (deployCfg.bao.enable && haveServerTls) { # 🩸 The principals that each log in with a leaf of their own, not as the # hive. See the lists in this file for what each grant is; the # units are identical apart from the objects they write, so the shape # lives in `readerPolicyUnit` and the difference is visible here in one # line each. systemd.services.swarm-bao-grafana-oidc-policy = readerPolicyUnit "write Grafana's OIDC-secret-reader bao policy and cert-auth role" grafanaOidcReaders; systemd.services.swarm-bao-otel-oidc-policy = readerPolicyUnit "write the collector's OIDC-secret-reader bao policy and cert-auth role" otelOidcReaders; systemd.services.swarm-bao-forwarder-oidc-policy = readerPolicyUnit "write the store forwarder's OIDC-secret-reader bao policy and cert-auth role" forwarderOidcReaders; systemd.services.swarm-bao-nats-auth-policy = readerPolicyUnit "write the queue responder's agent-credential-reader bao policy and cert-auth role" natsAuthReaders; systemd.services.swarm-bao-logs-auth-policy = readerPolicyUnit "write the log store responder's agent-credential-reader bao policy and cert-auth role" logsAuthReaders; systemd.services.swarm-bao-metrics-auth-policy = readerPolicyUnit "write the metrics store responder's Grafana-bearer-reader bao policy and cert-auth role" metricsAuthReaders; systemd.services.swarm-bao-ci-runner-policy = readerPolicyUnit "write the CI runner's registration-token-reader bao policy and cert-auth role" ciRunnerReaders; systemd.services.swarm-bao-review-classifier-policy = readerPolicyUnit "write the review classifier's API-key-reader bao policy and cert-auth role" reviewClassifierReaders; systemd.services.swarm-bao-builder-policy = readerPolicyUnit "write the swarm builder's forge-token-reader bao policy and cert-auth role" builderReaders; systemd.services.swarm-bao-ci-metrics-policy = readerPolicyUnit "write the CI run poller's forge-token-reader bao policy and cert-auth role" ciMetricsReaders; systemd.services.swarm-bao-claude-login-policy = readerPolicyUnit "write the claude login minter's credentials-writer bao policy and cert-auth role" claudeLoginWriters; systemd.services.swarm-bao-attic-policy = readerPolicyUnit "write the in-swarm cache's secret-creator bao policy and cert-auth role" atticWriters; systemd.services.swarm-bao-archive-policy = readerPolicyUnit "write the backup archive's user and password reader bao policy and cert-auth role" archiveReaders; systemd.services.swarm-bao-notify-router-policy = readerPolicyUnit "write the notification router's client-secret-reader bao policy and cert-auth role" notifyRouterReaders; systemd.services.swarm-bao-stt-policy = readerPolicyUnit "write the speech-to-text service's client-secret-reader bao policy and cert-auth role" sttReaders; systemd.services.swarm-bao-forge-publisher-policy = readerPolicyUnit "write the forge's controller-token-publisher bao policy and cert-auth role" forgePublisherWriters; }; }