571 lines
25 KiB
Nix
571 lines
25 KiB
Nix
# 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;
|
|
|
|
# The cluster node on this host, and what its pods and this store agree on
|
|
# about the pods' login.
|
|
k8sNode = deployCfg.k8s;
|
|
podAuth = import ./lib/bao-pod-auth.nix;
|
|
|
|
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
|
|
controllerPolicyName
|
|
createStanza
|
|
credentialMountPath
|
|
grafanaAdminLeaf
|
|
grafanaMetricsBearerLeaf
|
|
grafanaSecretKeyLeaf
|
|
granterPolicyUnit
|
|
granterSetupSteps
|
|
haveGranter
|
|
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/<hive>/*`, 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} -
|
|
'';
|
|
|
|
# The token lifetime arguments of a principal's role, as `bao write`
|
|
# arguments continuing the line before them. The cert role and the jwt role of
|
|
# one principal take the same ones.
|
|
#
|
|
# `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.
|
|
roleTokenArgs =
|
|
obj:
|
|
let
|
|
ttl = certRoleTokenTtl;
|
|
in
|
|
if obj.batchToken or false then
|
|
" \\\n token_type=batch \\\n token_ttl=5m"
|
|
else
|
|
" \\\n token_ttl=${ttl} \\\n token_max_ttl=${ttl}";
|
|
|
|
# ⚠️ Names are `swarm-<principal>[-<hive>]`, 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.
|
|
readerRoleWrite = obj: ''
|
|
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}${roleTokenArgs obj}
|
|
'';
|
|
|
|
# The principals whose service runs in the cluster, and so logs in with its
|
|
# pod's ServiceAccount token as well as with its certificate: the records the
|
|
# cert roles above are written from, plus the controller's, whose cert role
|
|
# is written by `swarm-bao-controller-policy`. Each one's jwt role takes the
|
|
# principal's name, policy and token lifetimes, and its ServiceAccount is
|
|
# named after it (../k8s/render.nix renders the pod under that account).
|
|
k8sPrincipals =
|
|
notifyRouterReaders
|
|
++ sttReaders
|
|
++ reviewClassifierReaders
|
|
++ claudeLoginWriters
|
|
++ ciMetricsReaders
|
|
++ [ { name = controllerPolicyName; } ];
|
|
|
|
# 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
|
|
# 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_audiences=${lib.escapeShellArg podAuth.audience} \
|
|
token_policies=${lib.escapeShellArg obj.name}${roleTokenArgs obj}
|
|
'';
|
|
|
|
# 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
|
|
{
|
|
options.services.hyperhive.deploy.bao.k8sPrincipals = lib.mkOption {
|
|
type = lib.types.listOf lib.types.str;
|
|
readOnly = true;
|
|
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.
|
|
'';
|
|
};
|
|
|
|
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;
|
|
|
|
# The pods' login: the `jwt` auth mount's config and one role per
|
|
# in-cluster service. The mount exists once `swarm-bao-granter-role` has
|
|
# run with a bootstrap token that carries `sys/auth/jwt-k8s`;
|
|
# until then this stops with exit 0, as the OIDC unit does, and
|
|
# `swarm-bao-granter-role` restarts it each time it succeeds.
|
|
#
|
|
# The tokens are checked against the public halves of the keys k3s signs
|
|
# them with, derived here from the key files, so no key is copied by hand.
|
|
# A rotation of those keys needs a restart of this unit.
|
|
systemd.services.swarm-bao-k8s-jwt-policy = lib.mkIf k8sNode.enable (
|
|
lib.mkMerge [
|
|
(granterPolicyUnit "write the pods' ServiceAccount-token login: the jwt auth mount's config and one role per in-cluster service" ''
|
|
|
|
caps="$(bao token capabilities auth/${podAuth.mount}/config)"
|
|
case "$caps" in
|
|
*update*) ;;
|
|
*)
|
|
echo "the granter may not configure auth/${podAuth.mount} yet (capabilities: $caps), so the pods' login stays off." >&2
|
|
echo "one-time step, as root on this host (docs/1-getting-started/setup.md):" >&2
|
|
${lib.concatMapStringsSep "\n" (l: "echo ${lib.escapeShellArg " ${l}"} >&2") granterSetupSteps}
|
|
echo "swarm-bao-granter-role re-runs this unit once it succeeds." >&2
|
|
exit 0
|
|
;;
|
|
esac
|
|
|
|
# One public key per private key in the files, which may each hold
|
|
# several. A file k3s has not written is skipped, and no key at all
|
|
# fails the unit, which retries.
|
|
work="$(mktemp -d)"
|
|
trap 'rm -rf "$work" "$err"' EXIT
|
|
: > "$work/public.pem"
|
|
for key_file in ${lib.escapeShellArgs k8sNode.serviceAccountKeyFiles}; do
|
|
[ -s "$key_file" ] || continue
|
|
rm -f "$work"/block-*
|
|
awk -v dir="$work" '
|
|
/-----BEGIN [A-Z ]*PRIVATE KEY-----/ { n++; out = dir "/block-" n }
|
|
out != "" { print > out }
|
|
/-----END [A-Z ]*PRIVATE KEY-----/ { if (out != "") close(out); out = "" }
|
|
' "$key_file"
|
|
for block in "$work"/block-*; do
|
|
[ -e "$block" ] || continue
|
|
openssl pkey -in "$block" -pubout >> "$work/public.pem"
|
|
done
|
|
done
|
|
if [ ! -s "$work/public.pem" ]; then
|
|
echo "no ServiceAccount signing key in ${lib.escapeShellArgs k8sNode.serviceAccountKeyFiles}: k3s has not written them yet." >&2
|
|
exit 1
|
|
fi
|
|
|
|
# A JSON body on stdin, because `jwt_validation_pubkeys` is a list.
|
|
# The tokens' signature algorithm follows the type of k3s's key.
|
|
keys="$(jq -Rs '[scan("-----BEGIN PUBLIC KEY-----[^-]+-----END PUBLIC KEY-----")] | unique' "$work/public.pem")"
|
|
jq -n --argjson keys "$keys" --arg issuer ${lib.escapeShellArg k8sNode.serviceAccountIssuer} '{
|
|
jwt_validation_pubkeys: $keys,
|
|
bound_issuer: $issuer,
|
|
jwt_supported_algs: ["RS256", "ES256"]
|
|
}' |
|
|
bao write auth/${podAuth.mount}/config -
|
|
|
|
${lib.concatMapStrings k8sJwtRoleWrite k8sPrincipals}
|
|
'')
|
|
(lib.mkIf haveGranter {
|
|
after = [ "k3s.service" ];
|
|
wants = [ "k3s.service" ];
|
|
path = [
|
|
pkgs.gawk
|
|
pkgs.jq
|
|
pkgs.openssl
|
|
];
|
|
})
|
|
]
|
|
);
|
|
};
|
|
}
|