Watch
0
0
Fork
You've already forked hyperhive
0

agent ssh unit on every agent with a store, one ssh_config, controller reads the ca key

This commit is contained in:
atlas 2026-11-16 18:33:03 +01:00 • committed by mara
commit 624d5f24f8

View file

@ -1,14 +1,20 @@
# Outbound SSH for this agent: a certificate for a key it generates itself,
# signed by the swarm's SSH CA, and a client config for the hosts it may reach.
# signed by the swarm's SSH CA, and a client config that offers it.
#
# Nothing is configured unless `services.hyperhive.agent.ssh.enable` is set. The
# agent has no standing SSH credential: `${unitName}.service` generates an
# ed25519 key in its own `/run` directory (tmpfs, never on disk), logs in to the
# swarm secret store with the identity ./bao.nix keeps current, and has the
# public half signed by the store's SSH engine
# (`swarm_secret_client::policy::SSH_MOUNT`). The role it signs through issues a
# user certificate with this agent's name as its only principal, valid for an
# hour; a timer signs again well before that runs out.
# On every agent that has a store address (the same switch as ./bao.nix: with no
# store there is nothing to sign with). The agent has no standing SSH credential:
# `${unitName}.service` generates an ed25519 key in its own `/run` directory
# (tmpfs, never on disk), logs in to the swarm secret store with the identity
# ./bao.nix keeps current, and has the public half signed by the store's SSH
# engine (`swarm_secret_client::policy::SSH_MOUNT`). The role it signs through
# issues a user certificate with this agent's name as its only principal, valid
# for an hour; a timer signs again well before that runs out.
#
# Whether the store signs is the operator's per-agent switch in the swarm UI
# (the `ssh` permission, `swarm-controller`'s `agent_ssh`). With it off the store
# refuses, the unit logs one line and ends successfully, and the next tick asks
# again; with it on the agent has a certificate within one tick. The unit
# needs no change to be switched.
#
# A target machine trusts the CA, not this key: ../target-modules/agent-ssh.nix.
{
@ -18,7 +24,6 @@
...
}:
let
cfg = config.services.hyperhive.agent.ssh;
baoCfg = config.services.hyperhive.agent.bao;
# The unix user is named for the agent, so this is also the principal the
@ -41,113 +46,28 @@ let
stagingFile = "/run/${runtimeDir}/cert.new";
errFile = "/run/${runtimeDir}/bao.err";
# In the state dir, which is bound from the host, so a pin survives a restart.
knownHostsFile = "/agents/${agentName}/state/ssh_known_hosts";
signPath = "${ca.mountPath}/sign/${ca.roleName}";
storeRetry = import ../host-modules/lib/store-retry.nix { };
# `[host]:port` is how a known_hosts line names a host on a non-default port.
knownHostName = h: if h.port == 22 then h.hostName else "[${h.hostName}]:${toString h.port}";
hostBlock = name: h: ''
Host ${name}
HostName ${h.hostName}
User ${h.user}
Port ${toString h.port}
IdentityFile ${keyFile}
CertificateFile ${certFile}
IdentitiesOnly yes
StrictHostKeyChecking yes
'';
hostType = lib.types.submodule {
options = {
hostName = lib.mkOption {
type = lib.types.strMatching "[A-Za-z0-9._:-]+";
example = "vm1.pc2.example.com";
description = ''
Name or address the agent connects to. The host key below is pinned to
exactly this spelling.
'';
};
user = lib.mkOption {
type = lib.types.strMatching "[A-Za-z0-9._-]+";
example = "test";
description = ''
Account to log in as on the target. The target's
{option}`principals` for that account must list this agent.
'';
};
port = lib.mkOption {
type = lib.types.port;
default = 22;
description = "TCP port of the target's sshd.";
};
hostKey = lib.mkOption {
type = lib.types.strMatching "(ssh-ed25519|ssh-rsa|ecdsa-sha2-nistp(256|384|521)) [A-Za-z0-9+/]+=*( [A-Za-z0-9@._-]+)?";
example = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAA…";
description = ''
The target's SSH host public key, one line as in
{file}`/etc/ssh/ssh_host_ed25519_key.pub` on the target. It is pinned:
the agent refuses a target that presents any other key, and there is
no trust on first use.
'';
};
};
};
in
{
options.services.hyperhive.agent.ssh = {
enable = lib.mkEnableOption ''
outbound SSH for this agent with short-lived certificates from the swarm
secret store's SSH CA. Needs {option}`services.hyperhive.agent.bao.addr`
config = lib.mkIf (baoCfg.addr != null) {
# Every host: the certificate, and the pins in the state dir, which outlives
# the container root. `accept-new` records a host's key at first contact and
# refuses a changed one after that.
programs.ssh.extraConfig = ''
Host *
IdentityFile ${keyFile}
CertificateFile ${certFile}
IdentitiesOnly yes
StrictHostKeyChecking accept-new
UserKnownHostsFile ${knownHostsFile}
'';
hosts = lib.mkOption {
type = lib.types.attrsOf hostType;
default = { };
example = lib.literalExpression ''
{
testvm = {
hostName = "10.0.0.7";
user = "test";
hostKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAA…";
};
}
'';
description = ''
Hosts this agent may SSH to, by the name it types: `ssh testvm`. Each
entry becomes a `Host` block in the system {file}`ssh_config` that
offers this agent's certificate and nothing else, and a pinned line in
the system {file}`ssh_known_hosts`.
This does not open the network path: with the hive's egress set to
reject, direct TCP out of the container is refused whatever is
configured here.
'';
};
};
config = lib.mkIf cfg.enable {
assertions = [
{
assertion = baoCfg.addr != null;
message = "services.hyperhive.agent.ssh.enable needs services.hyperhive.agent.bao.addr: the certificate is signed by the swarm secret store, and this agent has no store address";
}
{
assertion = lib.all (n: builtins.match "[A-Za-z0-9._-]+" n != null) (lib.attrNames cfg.hosts);
message = "services.hyperhive.agent.ssh.hosts names are written into ssh_config and may hold only letters, digits, `.`, `_` and `-`";
}
];
programs.ssh.extraConfig = lib.concatStrings (lib.mapAttrsToList hostBlock cfg.hosts);
programs.ssh.knownHosts = lib.mapAttrs' (
name: h:
lib.nameValuePair "hive-agent-ssh-${name}" {
hostNames = [ (knownHostName h) ];
publicKey = h.hostKey;
}
) cfg.hosts;
systemd.services.${unitName} = {
description = "sign this agent's SSH key with the swarm's SSH CA";
after = [
@ -233,6 +153,13 @@ in
# name, and it refuses any other.
rm -f "$err" "$staged"
if ! bao write -field=signed_key ${lib.escapeShellArg signPath} public_key=@"$pub" >"$staged" 2>"$err"; then
# A 403 is the operator's switch being off for this agent: not a fault,
# so no failed unit. Anything else (a sealed or unreachable store) is
# one, and retried.
if [[ "$(<"$err")" == *"Code: 403"* ]]; then
echo "SSH certificates are not enabled for this agent (the swarm store refused to sign); asking again next tick."
exit 0
fi
echo "the swarm secret store would not sign this agent's SSH key at ${signPath}:" >&2
if [ -s "$err" ]; then cat "$err" >&2; fi
exit 1