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

View file

@ -6,6 +6,8 @@
# fetches it, and ../agent-modules/forge.nix's readers find it. The same fixtures
# cover ../agent-modules/egress-proxy.nix, the agent's proxy credential and the
# environment built from it.
# They also cover ../agent-modules/ssh.nix: the agent's SSH certificate unit and
# client config, on the same store address.
{
pkgs,
lib,
@ -32,12 +34,30 @@ let
# A forge and a store: the fetch exists and every reader points at it.
agentForgeBao = agentWith {
services.hyperhive.agent.bao.addr = baoAddr;
services.hyperhive.agent.ssh = {
enable = true;
hosts.testvm = {
hostName = "10.0.0.7";
user = "test";
hostKey = hostKeyA;
};
hosts.altvm = {
hostName = "vm2.t.local";
user = "ops";
port = 2222;
hostKey = hostKeyB;
};
};
services.hyperhive.agent.forge.url = forgeUrl;
# A second `EnvironmentFile=` on the harness, which the proxy file must join.
services.hyperhive.agent.backendEnvironmentFile = "/var/lib/t/backend.env";
services.hyperhive.agent.icon = pkgs.emptyFile;
};
# Outbound SSH on the same agent: two hosts, one on a non-default port.
hostKeyA = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOHfYeWemIBqSc5Ac51ecuhi0G9LTN+57DFRq6mFKKk0";
hostKeyB = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMY1xboa8d2ncPDhTSPKsq5WMyYfWwIqLcCOnkHLO3ad";
# A forge and no store: the absence arm, and what makes the cases above able
# to fail.
agentForgeNoBao = agentWith {
@ -45,6 +65,15 @@ let
services.hyperhive.agent.icon = pkgs.emptyFile;
};
# SSH asked for with no store: the certificate has nowhere to be signed.
agentSshNoBao = agentWith {
services.hyperhive.agent.ssh.enable = true;
services.hyperhive.agent.icon = pkgs.emptyFile;
};
sshUnit = machine: machine.systemd.services.hive-agent-ssh-cert;
sshCa = import ../host-modules/lib/ssh-agent-ca.nix { };
fetchUnit = machine: machine.systemd.services.hive-agent-forge-token;
proxyUnit = machine: machine.systemd.services.hive-agent-proxy-credential;
accountsUnit = machine: machine.systemd.services.hive-agent-forge-accounts;
@ -521,6 +550,88 @@ let
&& lib.hasInfix "NODE_USE_ENV_PROXY=1" s
&& lib.hasInfix "url=\"http://${name}:$secret@$proxy\"" s;
}
{
# ../agent-modules/ssh.nix. Presence arm first, then the agent that
# did not ask for SSH, which gets neither the unit nor a host block.
name = "an agent that enables ssh gets the certificate unit and its timer, and one that does not gets neither";
ok =
agentForgeBao.systemd.services ? hive-agent-ssh-cert
&& agentForgeBao.systemd.timers ? hive-agent-ssh-cert
&& !(agentForgeNoBao.systemd.services ? hive-agent-ssh-cert)
&& !(agentForgeNoBao.systemd.timers ? hive-agent-ssh-cert)
&& !(lib.hasInfix "Host testvm" agentForgeNoBao.environment.etc."ssh/ssh_config".text);
}
{
# The key and the certificate stay in the unit's own tmpfs directory,
# owner-only, and the unit asks for no principal: the role's default is
# the agent's own name, and a `valid_principals` here would be the one
# place the unit could name another.
name = "the ssh certificate unit keeps the key in a 0700 directory under /run as the agent user, asks for no principal, and renames the certificate in";
ok =
let
u = sshUnit agentForgeBao;
s = u.script;
in
u.serviceConfig.User == "a1"
&& u.serviceConfig.RuntimeDirectory == "hive-agent-ssh-cert"
&& u.serviceConfig.RuntimeDirectoryMode == "0700"
&& u.serviceConfig.UMask == "0377"
&& lib.hasInfix "key=/run/hive-agent-ssh-cert/key" s
&& lib.hasInfix "ssh-keygen -q -t ed25519" s
&& lib.hasInfix "bao write -field=signed_key ssh-agents/sign/swarm-agent" s
&& lib.hasInfix "mv -f \"$staged\" \"$cert\"" s
&& !(lib.hasInfix "valid_principals" s)
&& !(lib.hasInfix ".ssh" s)
&& lib.elem "hive-agent-bao-identity.service" u.after
&& u.environment.BAO_CLIENT_CERT == agentForgeBao.services.hyperhive.agent.bao.clientCertFile;
}
{
# The certificate lives an hour and the timer fires every third of
# that, so one missed run still leaves the old certificate valid.
name = "the ssh certificate is renewed at least twice within its lifetime";
ok =
let
every = lib.toInt (
lib.removeSuffix "min" agentForgeBao.systemd.timers.hive-agent-ssh-cert.timerConfig.OnUnitInactiveSec
);
in
every * 2 < sshCa.certTtlMinutes && every > 0;
}
{
# `ssh testvm` has to work with no other flag: the host block names the
# target, the user, the port, the one identity and its certificate, and
# refuses an unknown host key.
name = "each ssh host gets a Host block offering only the agent's certificate, and a pinned known_hosts line";
ok =
let
conf = agentForgeBao.environment.etc."ssh/ssh_config".text;
known = agentForgeBao.environment.etc."ssh/ssh_known_hosts".text;
block = lib.concatStringsSep "\n" [
"Host testvm"
" HostName 10.0.0.7"
" User test"
" Port 22"
" IdentityFile /run/hive-agent-ssh-cert/key"
" CertificateFile /run/hive-agent-ssh-cert/key-cert.pub"
" IdentitiesOnly yes"
" StrictHostKeyChecking yes"
];
in
lib.hasInfix block conf
&& lib.hasInfix "Host altvm\n HostName vm2.t.local\n User ops\n Port 2222\n" conf
&& lib.hasInfix "10.0.0.7 ${hostKeyA}" known
# A port other than 22 is spelled the way known_hosts keys it.
&& lib.hasInfix "[vm2.t.local]:2222 ${hostKeyB}" known
&& !(lib.hasInfix "\nvm2.t.local " "\n${known}");
}
{
name = "enabling ssh on an agent with no store address is refused, naming the option";
ok =
let
refuses = m: lib.any (a: !a.assertion && lib.hasInfix "agent.bao.addr" a.message) m.assertions;
in
refuses agentSshNoBao && !(refuses agentForgeBao);
}
];
in
runGroup "agent-forge-bao" cases