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
1 changed files with 720 additions and 69 deletions
|
|
@ -14,6 +14,12 @@ let
|
|||
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
|
||||
|
|
@ -28,12 +34,15 @@ let
|
|||
classifierApiKeyLeaf
|
||||
controllerForgeTokenLeaf
|
||||
controllerGrafanaTokenLeaf
|
||||
controllerPolicyName
|
||||
createStanza
|
||||
credentialMountPath
|
||||
grafanaAdminLeaf
|
||||
grafanaMetricsBearerLeaf
|
||||
grafanaSecretKeyLeaf
|
||||
granterPolicyUnit
|
||||
granterSetupSteps
|
||||
haveGranter
|
||||
haveServerTls
|
||||
issuerPublicLeaf
|
||||
issuerSeedLeaf
|
||||
|
|
@ -362,6 +371,25 @@ let
|
|||
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
|
||||
|
|
@ -369,28 +397,33 @@ let
|
|||
# `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.
|
||||
#
|
||||
# `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.
|
||||
readerRoleWrite =
|
||||
obj:
|
||||
let
|
||||
ttlArgs =
|
||||
if obj.batchToken or false then
|
||||
" \\\n token_type=batch \\\n token_ttl=5m"
|
||||
else
|
||||
" \\\n token_ttl=${certRoleTokenTtl} \\\n token_max_ttl=${certRoleTokenTtl}";
|
||||
in
|
||||
''
|
||||
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}${ttlArgs}
|
||||
'';
|
||||
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 ++ 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`
|
||||
|
|
@ -405,6 +438,19 @@ let
|
|||
|
||||
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
|
||||
|
|
@ -442,5 +488,78 @@ in
|
|||
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
|
||||
];
|
||||
})
|
||||
]
|
||||
);
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue