Watch
0
0
Fork
You've already forked hyperhive
0

add an ssh ca to bao, agent ssh certificates and a target module

This commit is contained in:
atlas 2026-11-16 16:24:37 +01:00 • committed by mara
commit 0afb111862
2 changed files with 1056 additions and 17 deletions

View file

@ -0,0 +1,23 @@
# What the swarm's SSH CA and its agent role fix, spelled once for the two
# files that must agree: ../swarm-bao.nix writes the role, and
# ../../agent-modules/ssh.nix signs through it and renews on a schedule
# derived from the certificate's lifetime.
#
# `mountPath` and `roleName` are also `swarm_secret_client::policy::SSH_MOUNT`
# and `SSH_ROLE`, which the shared agent policy names; the bao-grants module-eval
# suite reads that file and fails when the two disagree.
#
# Pure attrset, not a NixOS module.
{ }:
rec {
mountPath = "ssh-agents";
roleName = "swarm-agent";
# A certificate is valid this long, and the role refuses a longer request.
certTtlMinutes = 60;
certMaxTtlMinutes = 180;
# A third of the lifetime: a run that fails once still leaves the old
# certificate valid for the retry, and a few failed runs still do.
renewEveryMinutes = certTtlMinutes / 3;
}

View file

@ -195,10 +195,13 @@ let
# the services root. The next six set up the agent PKI mount: the mount, its
# root and the `swarm-*` role agent certificates are issued through. No
# `root` delete there: every agent's cert-auth role pins that root by value,
# so replacing it would lock every agent out. The last three configure the
# `oidc` auth method: its config, its `swarm-*` roles, and a read on the one
# client secret that config carries. No `sys/auth`: the auth mounts are
# created with the bootstrap token by `swarm-bao-granter-role`.
# so replacing it would lock every agent out. The four after those set up the
# agent SSH CA mount: the mount, its issuer list, the CA write and its
# `swarm-*` role. No read on the CA and no delete: the CA is generated once.
# The last three configure the `oidc` auth method: its config, its `swarm-*`
# roles, and a read on the one client secret that config carries. No
# `sys/auth`: the auth mounts are created with the bootstrap token by
# `swarm-bao-granter-role`.
#
# Piped as a shell-quoted argument like `controllerPolicyText`, so
# ../module-eval/bao-grants.nix can read it out of the unit script.
@ -271,6 +274,22 @@ let
capabilities = ["create", "update"]
}
path "sys/mounts/${sshAgentsMountPath}" {
capabilities = ["create", "update"]
}
path "${sshAgentsMountPath}/issuers" {
capabilities = ["list"]
}
path "${sshAgentsMountPath}/config/ca" {
capabilities = ["create", "update"]
}
path "${sshAgentsMountPath}/roles/swarm-*" {
capabilities = ["create", "update"]
}
path "auth/oidc/config" {
capabilities = ["create", "update"]
}
@ -1082,6 +1101,43 @@ let
# live agent's leaf once it is past half of it (`agent_renewal.rs`).
agentPkiLeafTtl = "168h";
# The SSH secrets engine mount whose CA signs the agents' user certificates,
# and the one role they sign through, spelled once for this file and
# ../agent-modules/ssh.nix in ./lib/ssh-agent-ca.nix.
sshAgent = import ./lib/ssh-agent-ca.nix { };
sshAgentsMountPath = sshAgent.mountPath;
sshAgentRoleName = sshAgent.roleName;
# What the role pins. `{{identity.entity.name}}` is the name of the entity the
# agent's cert login lands on, which swarm-controller sets to the agent's name
# (`swarm-controller/src/agent_entity.rs`); with `allowed_users_template` the
# engine checks every requested principal against it, and with
# `default_user_template` an unrequested one defaults to it, so a certificate
# names its holder's own agent and no other. `allowed_user_key_lengths` limits
# the keys it signs to ed25519, whatever the length. The only extension is
# `permit-pty`; `allow_host_certificates = false` keeps the CA from signing a
# host key. A certificate is valid for an hour and never longer than three, and
# `ssh-agent-ca.nix` derives the agent's renewal interval from the hour.
sshAgentRoleBody = builtins.toJSON {
key_type = "ca";
allow_user_certificates = true;
allow_host_certificates = false;
allowed_users = "{{identity.entity.name}}";
allowed_users_template = true;
default_user = "{{identity.entity.name}}";
default_user_template = true;
allow_user_key_ids = false;
allowed_user_key_lengths = {
"ssh-ed25519" = 0;
};
allowed_extensions = "permit-pty";
default_extensions = {
"permit-pty" = "";
};
ttl = "${toString sshAgent.certTtlMinutes}m";
max_ttl = "${toString sshAgent.certMaxTtlMinutes}m";
};
# Lifetime of a token from a cert login, on every cert-auth role this file
# writes except the batch ones. The store keeps a lease per token until it
# expires; a login per operation at the mount default (32 days) leaves one
@ -4181,6 +4237,40 @@ in
'') hiveLeafIssuers
);
# The SSH CA agents get their short-lived user certificates from: the
# mount, the CA key the engine generates inside itself, and the one role
# every agent signs through. The same idempotence as the agent PKI unit
# below, without its restart: nothing here changes what the listener trusts.
systemd.services.swarm-bao-agent-ssh-policy = granterPolicyUnit "set up the swarm SSH CA mount and the role agents sign their user certificates through" ''
mounts="$(bao secrets list -format=json)"
case "$mounts" in
*'"${sshAgentsMountPath}/"'*) ;;
*) bao secrets enable -path=${sshAgentsMountPath} ssh ;;
esac
# Generated once, ever: every target machine trusts this CA's public key
# by value, so a second `config/ca` write would replace the default
# issuer and lock every agent out of every target. There is no replace
# branch. The issuer list is the guard, and `{}` is the only answer that
# means "none".
if issuers="$(bao list -format=json ${lib.escapeShellArg "${sshAgentsMountPath}/issuers"})"; then
echo "the ${sshAgentsMountPath} mount already has a CA — leaving it alone"
elif [ "$issuers" = '{}' ]; then
echo "generating the swarm agent SSH CA into the ${sshAgentsMountPath} mount"
bao write -field=public_key ${lib.escapeShellArg "${sshAgentsMountPath}/config/ca"} \
generate_signing_key=true \
key_type=ssh-ed25519 >/dev/null
else
echo "could not list the ${sshAgentsMountPath} issuers — not generating a CA over one that may exist" >&2
exit 1
fi
# Upserted every run, so a change to the role applies on deploy.
printf '%s' ${lib.escapeShellArg sshAgentRoleBody} |
bao write ${lib.escapeShellArg "${sshAgentsMountPath}/roles/${sshAgentRoleName}"} -
'';
# The agent PKI mount: its root, its one role, the host's copy of the
# root's certificate, and the listener's trust in it.
#