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:
parent
a70710dc7d
commit
2a6b0ee00a
2 changed files with 720 additions and 69 deletions
|
|
@ -41,6 +41,11 @@ let
|
|||
builtins.bitAnd pa.base keep == builtins.bitAnd pb.base keep;
|
||||
|
||||
cidrType = lib.types.strMatching "([0-9]{1,3}\\.){3}[0-9]{1,3}/[0-9]{1,2}";
|
||||
|
||||
# k3s's own defaults, named so the flags below and the ServiceAccount
|
||||
# issuer and key paths read from one place.
|
||||
dataDir = "/var/lib/rancher/k3s";
|
||||
clusterDomain = "cluster.local";
|
||||
in
|
||||
{
|
||||
options.services.hyperhive.deploy.k8s = {
|
||||
|
|
@ -86,6 +91,45 @@ in
|
|||
constraints as {option}`services.hyperhive.deploy.k8s.podCidr` apply.
|
||||
'';
|
||||
};
|
||||
|
||||
cniInterfaces = lib.mkOption {
|
||||
type = lib.types.listOf lib.types.str;
|
||||
default = [
|
||||
"cni0"
|
||||
"flannel.1"
|
||||
];
|
||||
readOnly = true;
|
||||
internal = true;
|
||||
description = ''
|
||||
The pod network's interfaces. The firewall trusts them, and a
|
||||
listener meant for pods only is opened on these and no other.
|
||||
'';
|
||||
};
|
||||
|
||||
serviceAccountIssuer = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
readOnly = true;
|
||||
internal = true;
|
||||
default = "https://kubernetes.default.svc.${clusterDomain}";
|
||||
description = ''
|
||||
The `iss` claim of the ServiceAccount tokens the cluster issues, the
|
||||
issuer k3s gives its API server for the cluster domain it runs with.
|
||||
'';
|
||||
};
|
||||
|
||||
serviceAccountKeyFiles = lib.mkOption {
|
||||
type = lib.types.listOf lib.types.str;
|
||||
readOnly = true;
|
||||
internal = true;
|
||||
default = [
|
||||
"${dataDir}/server/tls/service.key"
|
||||
"${dataDir}/server/tls/service.current.key"
|
||||
];
|
||||
description = ''
|
||||
The files k3s keeps its ServiceAccount keys in: the key that verifies
|
||||
tokens and the one that signs them, which differ after a rotation.
|
||||
'';
|
||||
};
|
||||
};
|
||||
|
||||
config = lib.mkIf cfg.enable {
|
||||
|
|
@ -136,6 +180,8 @@ in
|
|||
"--secrets-encryption"
|
||||
"--cluster-cidr=${cfg.podCidr}"
|
||||
"--service-cidr=${cfg.serviceCidr}"
|
||||
"--data-dir=${dataDir}"
|
||||
"--cluster-domain=${clusterDomain}"
|
||||
];
|
||||
# k3s's own images come first: with no registry the node pulls nothing,
|
||||
# not even CoreDNS.
|
||||
|
|
@ -145,9 +191,6 @@ in
|
|||
++ (config.system.build.k8sImages or [ ]);
|
||||
};
|
||||
|
||||
networking.firewall.trustedInterfaces = [
|
||||
"cni0"
|
||||
"flannel.1"
|
||||
];
|
||||
networking.firewall.trustedInterfaces = cfg.cniInterfaces;
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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";
|
||||
|
|
|
|||
Loading…
Reference in a new issue