Watch
0
0
Fork
You've already forked hyperhive
0

k8s: pods log in to the secret store with their ServiceAccount token

The store gets a listener for the pods that asks for no client certificate,
open on the pod network's interfaces only, and a jwt auth mount with one
role per in-cluster service. The rendered pods run under a ServiceAccount
with a projected token in place of their client certificate.
This commit is contained in:
atlas 2026-11-21 14:03:28 +01:00 • committed by maia
commit 2a6b0ee00a

View file

@ -6,7 +6,10 @@
# The pod takes the unit's environment, credential ids, state and runtime
# directories, and timer. Every file the unit names is mounted at the path the
# unit names it by, so the environment carries over as it is and a change to a
# unit shows up in its manifest.
# unit shows up in its manifest. The one change is the store login: a service
# the store has a jwt role for (`deploy.bao.k8sPrincipals`) runs under a
# ServiceAccount of its name, with a projected token for the store's audience
# in place of its client certificate and key.
#
# Secrets are referenced by name and credential id only. The output lands in
# the world-readable nix store, so it never holds a credential's host path or
@ -20,6 +23,8 @@ let
hyperhiveCfg = config.services.hyperhive;
deployCfg = hyperhiveCfg.deploy;
k8sCfg = hyperhiveCfg.k8s;
baoDeployCfg = deployCfg.bao;
podAuth = import ../lib/bao-pod-auth.nix;
# `serviceUser` is the user every image runs its daemon as. Mounted files are
# group-owned by its group through `fsGroup`.
@ -125,7 +130,7 @@ let
credDir = "/run/credentials/${name}.service";
# `LoadCredential` entries are `id:source` on the host and a bare `id`
# inside a container. Only the id is rendered.
credIds = map (e: lib.head (lib.splitString ":" e)) (lib.toList (sc.LoadCredential or [ ]));
unitCredIds = map (e: lib.head (lib.splitString ":" e)) (lib.toList (sc.LoadCredential or [ ]));
runtimeDir = if sc ? RuntimeDirectory then "/run/${sc.RuntimeDirectory}" else null;
stateDir = if sc ? StateDirectory then "/var/lib/${sc.StateDirectory}" else null;
@ -137,6 +142,36 @@ let
lib.filterAttrs (n: v: v != null && n != "PATH") unit.environment
);
# A service the store knows by a jwt role as well as by its certificate
# logs in with its pod's ServiceAccount token: the pod holds no client
# certificate. It reaches the store on the listener that asks for none
# (`deploy.bao.podListenerPort`), by the name every reader dials.
baoLogin = lib.elem name baoDeployCfg.k8sPrincipals && unitEnv ? BAO_CLIENT_CERT;
baoClientCredIds =
map
(
var:
let
v = unitEnv.${var} or "";
in
if under credDir v then
lib.removePrefix "${credDir}/" v
else
fail name "${var} is not a credential of the unit, which the pod's login drops"
)
[
"BAO_CLIENT_CERT"
"BAO_CLIENT_KEY"
];
credIds = if baoLogin then lib.subtractLists baoClientCredIds unitCredIds else unitCredIds;
baoEnv = {
BAO_ADDR = "https://${hyperhiveCfg.swarm.bao.domain}:${toString baoDeployCfg.podListenerPort}";
BAO_AUTH = "jwt";
BAO_JWT_FILE = podAuth.tokenFile;
BAO_JWT_ROLE = name;
BAO_JWT_MOUNT = podAuth.mount;
};
# Trust bundles (../lib/hive-ca-trust.nix, ../hive-tls.nix): system roots
# plus the hive CA, assembled at runtime, so the cluster holds the one
# bundle as a ConfigMap and each path the unit names mounts it.
@ -145,7 +180,16 @@ let
);
env =
unitEnv
(
if baoLogin then
removeAttrs unitEnv [
"BAO_CLIENT_CERT"
"BAO_CLIENT_KEY"
]
// baoEnv
else
unitEnv
)
// podOtlpEnv unitEnv
// lib.optionalAttrs (runtimeDir != null) { RUNTIME_DIRECTORY = runtimeDir; }
// lib.optionalAttrs (stateDir != null) { STATE_DIRECTORY = stateDir; }
@ -158,6 +202,7 @@ let
"/nix/store"
]
++ caFiles
++ lib.optional baoLogin podAuth.tokenDir
++ lib.optional (runtimeDir != null) runtimeDir
++ lib.optional (stateDir != null) stateDir
++ map (f: "/etc/${f}") svc.etcFiles;
@ -195,6 +240,24 @@ let
}) credIds;
};
}
++ lib.optional baoLogin {
name = "bao-token";
projected = {
# 0440, as the credentials: the pod's group reads the token.
defaultMode = 288;
sources = [
{
serviceAccountToken = {
audience = podAuth.audience;
# The shortest the API server grants. The kubelet refreshes
# the file before it expires, and a login reads it afresh.
expirationSeconds = 600;
path = "token";
};
}
];
};
}
++ lib.optional (caFiles != [ ]) {
name = "ca-trust";
configMap.name = "hyperhive-ca-trust";
@ -226,6 +289,11 @@ let
mountPath = credDir;
readOnly = true;
}
++ lib.optional baoLogin {
name = "bao-token";
mountPath = podAuth.tokenDir;
readOnly = true;
}
++ map (f: {
name = "ca-trust";
mountPath = f;
@ -296,6 +364,8 @@ let
podSpec = {
inherit terminationGracePeriodSeconds volumes;
containers = [ container ];
# The API token is never mounted; a pod that logs in to the store gets
# a token of its own, projected for the store's audience.
automountServiceAccountToken = false;
enableServiceLinks = false;
securityContext = {
@ -304,7 +374,8 @@ let
fsGroup = serviceUser.gid;
seccompProfile.type = "RuntimeDefault";
};
};
}
// lib.optionalAttrs baoLogin { serviceAccountName = name; };
podTemplate = restartPolicy: {
metadata.labels = labelsFor name;
@ -353,6 +424,15 @@ let
objects = {
"${name}/${lib.toLower svc.kind}" = main;
}
// lib.optionalAttrs baoLogin {
# Named after the service: the store's jwt role binds this account.
"${name}/serviceaccount" = {
apiVersion = "v1";
kind = "ServiceAccount";
metadata = metaFor name;
automountServiceAccountToken = false;
};
}
// lib.optionalAttrs (stateDir != null) {
"${name}/persistentvolumeclaim" = {
apiVersion = "v1";