agent ssh unit on every agent with a store, one ssh_config, controller reads the ca key
This commit is contained in:
parent
910f9a6f50
commit
624d5f24f8
1 changed files with 99 additions and 165 deletions
|
|
@ -7,7 +7,7 @@
|
|||
# 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.
|
||||
# client config, which every agent with a store address gets.
|
||||
{
|
||||
pkgs,
|
||||
lib,
|
||||
|
|
@ -34,30 +34,12 @@ 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 {
|
||||
|
|
@ -65,12 +47,6 @@ 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 { };
|
||||
|
||||
|
|
@ -551,15 +527,18 @@ let
|
|||
&& 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";
|
||||
# ../agent-modules/ssh.nix. There is no agent-config switch: the store
|
||||
# address is the whole gate, and the operator's switch is the UI's.
|
||||
name = "an agent with a store address gets the ssh certificate unit and its timer with no setting, and one without 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);
|
||||
&&
|
||||
lib.hasInfix "IdentityFile /run/hive-agent-ssh-cert/key"
|
||||
agentForgeBao.environment.etc."ssh/ssh_config".text
|
||||
&& !(lib.hasInfix "hive-agent-ssh-cert" agentForgeNoBao.environment.etc."ssh/ssh_config".text);
|
||||
}
|
||||
{
|
||||
# The key and the certificate stay in the unit's own tmpfs directory,
|
||||
|
|
@ -598,39 +577,39 @@ let
|
|||
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";
|
||||
# `ssh user@host` works for any host the target trusts: one `Host *` block
|
||||
# offers the agent's certificate and only it, and keeps its pins in the
|
||||
# state dir. The host key is recorded at first contact: the explainer in
|
||||
# the swarm UI says so.
|
||||
name = "the ssh config offers only the agent's certificate to every host and records host keys in the state dir";
|
||||
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"
|
||||
"Host *"
|
||||
" IdentityFile /run/hive-agent-ssh-cert/key"
|
||||
" CertificateFile /run/hive-agent-ssh-cert/key-cert.pub"
|
||||
" IdentitiesOnly yes"
|
||||
" StrictHostKeyChecking yes"
|
||||
" StrictHostKeyChecking accept-new"
|
||||
" UserKnownHostsFile /agents/a1/state/ssh_known_hosts"
|
||||
];
|
||||
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}");
|
||||
&& !(lib.hasInfix "StrictHostKeyChecking no" conf)
|
||||
&& !(lib.hasInfix "hive-agent-ssh-cert" agentForgeNoBao.environment.etc."ssh/ssh_config".text);
|
||||
}
|
||||
{
|
||||
name = "enabling ssh on an agent with no store address is refused, naming the option";
|
||||
# A refused signing is the switch being off, not a fault: one line and a
|
||||
# clean exit. Any other failure still fails the unit so it retries.
|
||||
name = "the ssh certificate unit ends cleanly on a 403 and fails on any other signing error";
|
||||
ok =
|
||||
let
|
||||
refuses = m: lib.any (a: !a.assertion && lib.hasInfix "agent.bao.addr" a.message) m.assertions;
|
||||
s = (sshUnit agentForgeBao).script;
|
||||
at = lib.splitString "Code: 403" s;
|
||||
in
|
||||
refuses agentSshNoBao && !(refuses agentForgeBao);
|
||||
lib.length at == 2
|
||||
&& lib.hasInfix "exit 0" (lib.head (lib.splitString "exit 1" (lib.elemAt at 1)))
|
||||
&& lib.hasInfix "exit 1" (lib.elemAt at 1);
|
||||
}
|
||||
];
|
||||
in
|
||||
|
|
|
|||
Loading…
Reference in a new issue