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 485da0c3c7
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, - **Files:** the pod mounts each file the unit names at the same path,
so the environment carries over unchanged: so the environment carries over unchanged:
- credentials at `/run/credentials/<service>.service/<id>`, from the Secret - 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 - trust bundles (any `…/trust-bundle.pem` the environment names), from the
ConfigMap `hyperhive-ca-trust`, key `trust-bundle.pem`; ConfigMap `hyperhive-ca-trust`, key `trust-bundle.pem`;
- `RuntimeDirectory` as an `emptyDir`, and `StateDirectory` on the - `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. 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 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 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, contents. Nothing here creates the Secret.
because the hive CA exists only at runtime. Nothing here creates either
object. 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 ## Pods

View file

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

View file

@ -205,6 +205,32 @@ let
&& &&
(lib.head (podSpec "swarm-controller").volumes).secret.secretName == "swarm-controller-credentials"; (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"; name = "the poller's CronJob runs on its timer's interval and deadline, never overlapping";
ok = ok =