191 lines
7.5 KiB
Nix
191 lines
7.5 KiB
Nix
# Outbound SSH for this agent: a certificate for a key it generates itself,
|
|
# signed by the swarm's SSH CA, and a client config that offers it.
|
|
#
|
|
# 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.
|
|
{
|
|
pkgs,
|
|
lib,
|
|
config,
|
|
...
|
|
}:
|
|
let
|
|
baoCfg = config.services.hyperhive.agent.bao;
|
|
|
|
# The unix user is named for the agent, so this is also the principal the
|
|
# role puts on the certificate (the identity entity swarm-controller names
|
|
# after the agent).
|
|
agentName = config.services.hyperhive.agent.user.name;
|
|
|
|
ca = import ../host-modules/lib/ssh-agent-ca.nix { };
|
|
|
|
# The server CA id ./bao.nix loads.
|
|
serverCaCredential = "hive-agent-bao-server-ca";
|
|
|
|
unitName = "hive-agent-ssh-cert";
|
|
runtimeDir = unitName;
|
|
keyFile = "/run/${runtimeDir}/key";
|
|
pubFile = "${keyFile}.pub";
|
|
# `ssh` reads `<IdentityFile>-cert.pub` by itself, but the config names it so
|
|
# that the file a Host block uses is visible in the config.
|
|
certFile = "${keyFile}-cert.pub";
|
|
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 { };
|
|
|
|
in
|
|
{
|
|
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}
|
|
'';
|
|
|
|
systemd.services.${unitName} = {
|
|
description = "sign this agent's SSH key with the swarm's SSH CA";
|
|
after = [
|
|
"network.target"
|
|
# Ordering only: that unit reports a store this agent cannot reach as
|
|
# itself, and should get to say so first.
|
|
"hive-agent-bao-identity.service"
|
|
];
|
|
wantedBy = [ "multi-user.target" ];
|
|
path = [
|
|
pkgs.openbao
|
|
pkgs.openssh
|
|
pkgs.coreutils
|
|
];
|
|
# ../host-modules/lib/store-retry.nix.
|
|
inherit (storeRetry) startLimitBurst startLimitIntervalSec;
|
|
serviceConfig = storeRetry.serviceConfig // {
|
|
Type = "oneshot";
|
|
# Not `RemainAfterExit`: the timer has to be able to start this unit
|
|
# again. `RuntimeDirectoryPreserve` keeps the key between runs.
|
|
RemainAfterExit = false;
|
|
TimeoutStartSec = 30;
|
|
User = agentName;
|
|
Group = agentName;
|
|
RuntimeDirectory = runtimeDir;
|
|
RuntimeDirectoryMode = "0700";
|
|
RuntimeDirectoryPreserve = "yes";
|
|
UMask = "0377";
|
|
LoadCredential = [ serverCaCredential ];
|
|
};
|
|
environment = {
|
|
BAO_ADDR = toString baoCfg.addr;
|
|
BAO_CLIENT_CERT = baoCfg.clientCertFile;
|
|
BAO_CLIENT_KEY = baoCfg.clientKeyFile;
|
|
};
|
|
script = ''
|
|
set -euo pipefail
|
|
|
|
# No identity delivered: ./bao.nix's check reports that; saying it
|
|
# twice adds nothing.
|
|
for f in "$BAO_CLIENT_CERT" "$BAO_CLIENT_KEY"; do
|
|
if [ ! -s "$f" ]; then
|
|
echo "this agent has no store identity, so it cannot get an SSH certificate." >&2
|
|
exit 0
|
|
fi
|
|
if [ ! -r "$f" ]; then
|
|
echo "cannot read $f, so this agent cannot present its store identity." >&2
|
|
exit 1
|
|
fi
|
|
done
|
|
|
|
if [ -s "$CREDENTIALS_DIRECTORY/${serverCaCredential}" ]; then
|
|
export BAO_CACERT="$CREDENTIALS_DIRECTORY/${serverCaCredential}"
|
|
fi
|
|
|
|
# `UMask=0377` makes every file this script creates `0400`, so each one
|
|
# is removed before the command that creates it.
|
|
key=${lib.escapeShellArg keyFile}
|
|
pub=${lib.escapeShellArg pubFile}
|
|
cert=${lib.escapeShellArg certFile}
|
|
staged=${lib.escapeShellArg stagingFile}
|
|
err=${lib.escapeShellArg errFile}
|
|
trap 'rm -f "$err" "$staged"' EXIT
|
|
|
|
# The key lives as long as /run does, which is as long as this
|
|
# container: a restart is a new key. Only a pair with both halves is
|
|
# kept.
|
|
if [ ! -s "$key" ] || [ ! -s "$pub" ]; then
|
|
rm -f "$key" "$pub" "$cert"
|
|
ssh-keygen -q -t ed25519 -N "" -C ${lib.escapeShellArg "hive-agent-${agentName}"} -f "$key"
|
|
chmod 0400 "$key"
|
|
fi
|
|
|
|
rm -f "$err"
|
|
if ! BAO_TOKEN="$(bao login -method=cert -token-only 2>"$err")"; then
|
|
echo "the swarm secret store at $BAO_ADDR refused or could not answer this agent's certificate login:" >&2
|
|
if [ -s "$err" ]; then cat "$err" >&2; fi
|
|
exit 1
|
|
fi
|
|
export BAO_TOKEN
|
|
|
|
# No principal is requested: the role's default is this agent's own
|
|
# 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
|
|
fi
|
|
|
|
# A reply that is not a certificate must not replace the one in use.
|
|
if ! ssh-keygen -L -f "$staged" >/dev/null 2>&1; then
|
|
echo "what the store returned at ${signPath} is not an SSH certificate; keeping the current one." >&2
|
|
exit 1
|
|
fi
|
|
|
|
# One rename: ssh never reads a half-written certificate.
|
|
mv -f "$staged" "$cert"
|
|
echo "signed this agent's SSH key as ${agentName}, valid for ${toString ca.certTtlMinutes} minutes."
|
|
'';
|
|
};
|
|
|
|
# Every third of the certificate's lifetime, so a store that is down for
|
|
# a run or two does not outlast it.
|
|
systemd.timers.${unitName} = {
|
|
description = "sign this agent's SSH key again before its certificate expires";
|
|
wantedBy = [ "timers.target" ];
|
|
timerConfig = {
|
|
OnUnitInactiveSec = "${toString ca.renewEveryMinutes}min";
|
|
RandomizedDelaySec = "1min";
|
|
};
|
|
};
|
|
};
|
|
}
|