Watch
0
0
Fork
You've already forked hyperhive
0

k8s: a pod's public CA files come from a ConfigMap, not a Secret

This commit is contained in:
atlas 2026-11-21 15:15:13 +01:00 • committed by maia
commit 40519f9123
3 changed files with 104 additions and 22 deletions

View file

@ -43,7 +43,9 @@ The renderer reads the service's evaluated unit, not a copy of its settings:
- **Files:** the pod mounts each file the unit names at the same path,
so the environment carries over unchanged:
- credentials at `/run/credentials/<service>.service/<id>`, from the Secret
`<service>-credentials`, one key per `LoadCredential` id;
`<service>-credentials`, one key per `LoadCredential` id, except the ones
that are public CA files, which come from the ConfigMap
`hyperhive-ca-trust` [below](#secrets);
- trust bundles (any `…/trust-bundle.pem` the environment names), from the
ConfigMap `hyperhive-ca-trust`, key `trust-bundle.pem`;
- `RuntimeDirectory` as an `emptyDir`, and `StateDirectory` on the
@ -69,9 +71,15 @@ commit.
The YAML lands in the world-readable nix store, so it holds no credential.
It names the Secret `<service>-credentials` and the credential ids as keys.
It never names the host path the unit loads a credential from, and never its
contents. The `hyperhive-ca-trust` ConfigMap is only referenced as well,
because the hive CA exists only at runtime. Nothing here creates either
object.
contents. Nothing here creates the Secret.
A credential that holds a public CA file isn't a secret. `swarm-notify-router`'s
`notify-router-bao-server-ca` comes from the ConfigMap `hyperhive-ca-trust`,
key `bao-server-ca.pem`, and its trust bundles from the key `trust-bundle.pem`.
The manifests only name the ConfigMap, because the hive CA exists only at
runtime, and nothing here creates it. A service with both kinds of credential
gets one projected volume that merges the Secret's keys and the ConfigMap's into
the credential directory.
## Pods

View file

@ -13,7 +13,8 @@
#
# 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
# contents.
# contents. A credential that is a public CA file comes from the CA ConfigMap,
# not a Secret.
{
lib,
pkgs,
@ -26,6 +27,12 @@ let
baoDeployCfg = deployCfg.bao;
podAuth = import ../lib/bao-pod-auth.nix;
# The ConfigMap that holds the public CA files the pods trust, and its keys.
# A CA certificate is public, so it travels as a ConfigMap, never a Secret.
caConfigMap = "hyperhive-ca-trust";
trustBundleKey = "trust-bundle.pem";
baoServerCaKey = "bao-server-ca.pem";
# `serviceUser` is the user every image runs its daemon as. Mounted files are
# group-owned by its group through `fsGroup`.
inherit (import ../../images { inherit pkgs lib; }) mkServiceImage serviceUser;
@ -52,7 +59,8 @@ let
# systemd unit the host runs today; nothing reads it unless `enable` holds.
# `healthEnv` names the variable that binds the daemon's /healthz and
# /readyz listener; the controller serves /readyz on its unix socket only,
# which the kubelet cannot probe.
# which the kubelet cannot probe. `publicCredentials` maps the ids of the
# credentials that hold a public CA file to their key in the CA ConfigMap.
services = {
swarm-controller = {
inherit (deployCfg.swarm-controller) enable package;
@ -60,6 +68,7 @@ let
kind = "Deployment";
healthEnv = null;
etcFiles = [ "hyperhive/swarm-controller.json" ];
publicCredentials = { };
};
swarm-notify-router = {
inherit (deployCfg.notify-router) enable package;
@ -67,6 +76,7 @@ let
kind = "Deployment";
healthEnv = "SWARM_NOTIFY_ROUTER_HEALTH_ADDR";
etcFiles = [ ];
publicCredentials.notify-router-bao-server-ca = baoServerCaKey;
};
swarm-ci-metrics = {
inherit (deployCfg.ci-metrics) enable package;
@ -75,6 +85,7 @@ let
kind = "CronJob";
healthEnv = null;
etcFiles = [ ];
publicCredentials = { };
};
};
@ -176,7 +187,7 @@ let
# 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.
caFiles = lib.unique (
lib.filter (v: lib.hasPrefix "/" v && baseNameOf v == "trust-bundle.pem") (lib.attrValues unitEnv)
lib.filter (v: lib.hasPrefix "/" v && baseNameOf v == trustBundleKey) (lib.attrValues unitEnv)
);
env =
@ -226,20 +237,57 @@ let
execWords = lib.filter (w: w != "") (lib.splitString " " (toString sc.ExecStart));
entrypoint = "${svc.package}/bin/${name}";
volumes =
lib.optional (credIds != [ ]) {
name = "credentials";
secret = {
secretName = "${name}-credentials";
# 0440: the pod's group reads them, as `LoadCredential` lets the
# unit's user read them.
defaultMode = 288;
items = map (id: {
key = id;
path = id;
}) credIds;
# The credentials that are public CA files come from the CA ConfigMap, the
# rest from the service's Secret. A volume holding both is a projected
# one, which merges them into the one directory the unit's credentials
# are in.
publicIds = lib.filter (id: svc.publicCredentials ? ${id}) credIds;
secretIds = lib.subtractLists publicIds credIds;
# 0440: the pod's group reads them, as `LoadCredential` lets the unit's
# user read them.
credentialsMode = 288;
credentialItems = map (id: {
key = id;
path = id;
});
credentialsVolume =
if publicIds == [ ] then
{
name = "credentials";
secret = {
secretName = "${name}-credentials";
defaultMode = credentialsMode;
items = credentialItems credIds;
};
}
else
{
name = "credentials";
projected = {
defaultMode = credentialsMode;
sources =
lib.optional (secretIds != [ ]) {
secret = {
name = "${name}-credentials";
items = credentialItems secretIds;
};
}
++ [
{
configMap = {
name = caConfigMap;
items = map (id: {
key = svc.publicCredentials.${id};
path = id;
}) publicIds;
};
}
];
};
};
}
volumes =
lib.optional (credIds != [ ]) credentialsVolume
++ lib.optional baoLogin {
name = "bao-token";
projected = {
@ -260,7 +308,7 @@ let
}
++ lib.optional (caFiles != [ ]) {
name = "ca-trust";
configMap.name = "hyperhive-ca-trust";
configMap.name = caConfigMap;
}
++ lib.optional (runtimeDir != null) {
name = "runtime";
@ -297,7 +345,7 @@ let
++ map (f: {
name = "ca-trust";
mountPath = f;
subPath = "trust-bundle.pem";
subPath = trustBundleKey;
readOnly = true;
}) caFiles
++ lib.optional (runtimeDir != null) {

View file

@ -205,6 +205,32 @@ let
&&
(lib.head (podSpec "swarm-controller").volumes).secret.secretName == "swarm-controller-credentials";
}
{
# The controller's pod is the control for the Secret lookup: its
# credentials still come from one.
name = "the router's CA file comes from the CA ConfigMap and its pod mounts no Secret";
ok =
let
volumes = (podSpec "swarm-notify-router").volumes;
credentials = lib.findFirst (v: v.name == "credentials") null volumes;
sources = credentials.projected.sources;
in
credentials != null
&& lib.length sources == 1
&& (lib.head sources).configMap.name == "hyperhive-ca-trust"
&&
map (i: [
i.key
i.path
]) (lib.head sources).configMap.items == [
[
"bao-server-ca.pem"
"notify-router-bao-server-ca"
]
]
&& !(lib.any (v: v ? secret) volumes)
&& lib.any (v: v ? secret) (podSpec "swarm-controller").volumes;
}
{
name = "the poller's CronJob runs on its timer's interval and deadline, never overlapping";
ok =