sign ssh certificates on demand from ssh itself, warn before setup, say what to do when a host key changes
This commit is contained in:
parent
fc29fc89c0
commit
3d7541c0af
1 changed files with 521 additions and 67 deletions
|
|
@ -3,18 +3,26 @@
|
|||
#
|
||||
# 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.
|
||||
# the helper below generates an ed25519 key in `/run/${unitName}` (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.
|
||||
#
|
||||
# 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.
|
||||
# (the `ssh` permission, `swarm-controller`'s `agent_ssh`). Every signing makes a
|
||||
# fresh login, so a token never outlives the policies the role had when it was
|
||||
# issued by more than the helper's own run. Two things run the helper:
|
||||
#
|
||||
# - `${unitName}.service`, from a timer, renews well before the certificate
|
||||
# runs out. With the switch off the store refuses: the unit logs one line, ends
|
||||
# successfully and asks again at the next tick.
|
||||
# - `ssh` itself, through a `Match exec` line in the system config: when the
|
||||
# certificate is missing or close to its end, the helper signs before the
|
||||
# connection starts. That is what makes the first `ssh` after the operator
|
||||
# switches SSH on work at once. It is silent and its exit status is whether a
|
||||
# usable certificate exists; nothing in the config depends on the answer.
|
||||
#
|
||||
# A target machine trusts the CA, not this key: ../target-modules/agent-ssh.nix.
|
||||
{
|
||||
|
|
@ -38,28 +46,57 @@ let
|
|||
|
||||
unitName = "hive-agent-ssh-cert";
|
||||
runtimeDir = unitName;
|
||||
keyFile = "/run/${runtimeDir}/key";
|
||||
dir = "/run/${runtimeDir}";
|
||||
keyFile = "${dir}/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";
|
||||
stagingFile = "${dir}/cert.new";
|
||||
errFile = "${dir}/bao.err";
|
||||
# The store's CA, copied out of the unit's credentials so that the helper can
|
||||
# reach the store from an `ssh` run, which has no credentials directory.
|
||||
serverCaCopy = "${dir}/server-ca.pem";
|
||||
# Written when the store refuses, so that a loop of `ssh` calls does not log in
|
||||
# to the store each time the switch is off.
|
||||
refusedFile = "${dir}/refused";
|
||||
|
||||
# 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}";
|
||||
|
||||
# A certificate with less than this left is signed again by `ssh`. The timer
|
||||
# fires every `ca.renewEveryMinutes`, so a healthy agent never gets here.
|
||||
ensureMarginSec = 600;
|
||||
# How long a refusal is remembered. The delay the operator sees after
|
||||
# switching on is at most this, then one login and one signing.
|
||||
refusedForSec = 10;
|
||||
|
||||
storeRetry = import ../host-modules/lib/store-retry.nix { };
|
||||
|
||||
binPath = lib.makeBinPath [
|
||||
pkgs.openbao
|
||||
pkgs.openssh
|
||||
pkgs.coreutils
|
||||
pkgs.gnused
|
||||
pkgs.util-linux
|
||||
];
|
||||
|
||||
# The helper, and the unit's script: one text. `HIVE_SSH_CERT_ENSURE=1` is the
|
||||
# `ssh` entry; without it this is the timer's. The tools go after the caller's
|
||||
# `PATH`, so a caller that has its own wins.
|
||||
helper = pkgs.writeShellScript unitName config.systemd.services.${unitName}.script;
|
||||
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.
|
||||
# refuses a changed one after that. The `Match` line has no settings: ssh
|
||||
# runs its command while it reads the config, before it loads any identity,
|
||||
# and the answer is not used.
|
||||
programs.ssh.extraConfig = ''
|
||||
Match exec "HIVE_SSH_CERT_ENSURE=1 ${helper} </dev/null"
|
||||
Host *
|
||||
IdentityFile ${keyFile}
|
||||
CertificateFile ${certFile}
|
||||
|
|
@ -77,11 +114,6 @@ in
|
|||
"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 // {
|
||||
|
|
@ -98,19 +130,77 @@ in
|
|||
UMask = "0377";
|
||||
LoadCredential = [ serverCaCredential ];
|
||||
};
|
||||
environment = {
|
||||
BAO_ADDR = toString baoCfg.addr;
|
||||
BAO_CLIENT_CERT = baoCfg.clientCertFile;
|
||||
BAO_CLIENT_KEY = baoCfg.clientKeyFile;
|
||||
};
|
||||
script = ''
|
||||
set -euo pipefail
|
||||
# `UMask=0377` in the unit makes every file this script creates `0400`,
|
||||
# so each one is removed before the command that creates it. `ssh` runs
|
||||
# the same text, which therefore sets it too.
|
||||
umask 0377
|
||||
export PATH="$PATH:${binPath}"
|
||||
export BAO_ADDR=${lib.escapeShellArg (toString baoCfg.addr)}
|
||||
export BAO_CLIENT_CERT=${lib.escapeShellArg baoCfg.clientCertFile}
|
||||
export BAO_CLIENT_KEY=${lib.escapeShellArg baoCfg.clientKeyFile}
|
||||
|
||||
dir=${lib.escapeShellArg dir}
|
||||
key=${lib.escapeShellArg keyFile}
|
||||
pub=${lib.escapeShellArg pubFile}
|
||||
cert=${lib.escapeShellArg certFile}
|
||||
staged=${lib.escapeShellArg stagingFile}
|
||||
err=${lib.escapeShellArg errFile}
|
||||
server_ca=${lib.escapeShellArg serverCaCopy}
|
||||
refused=${lib.escapeShellArg refusedFile}
|
||||
|
||||
ensure=
|
||||
if [ -n "''${HIVE_SSH_CERT_ENSURE-}" ]; then ensure=1; fi
|
||||
|
||||
# The `ssh` entry is silent and says yes or no by its exit status; the
|
||||
# unit's output is its log.
|
||||
if [ -n "$ensure" ]; then exec >/dev/null 2>&1; fi
|
||||
|
||||
# `ssh` can run before the unit's first start has made the directory.
|
||||
if [ ! -d "$dir" ]; then mkdir -m 0700 "$dir"; fi
|
||||
|
||||
# One run at a time, the unit's or an `ssh`'s: two would race on the key
|
||||
# and the staged file.
|
||||
exec {lock}<"$dir"
|
||||
if ! flock -w 20 "$lock"; then
|
||||
echo "another run holds $dir and did not finish in 20 seconds." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Epoch second the certificate ends, 0 when there is none.
|
||||
cert_end() {
|
||||
local end
|
||||
end=$(ssh-keygen -L -f "$cert" 2>/dev/null | sed -n 's/^ *Valid: from .* to //p') || end=
|
||||
if [ -n "$end" ]; then date -d "$end" +%s 2>/dev/null || echo 0; else echo 0; fi
|
||||
}
|
||||
|
||||
if [ -n "$ensure" ]; then
|
||||
now=$(date +%s)
|
||||
# Enough left: nothing to do, and nothing asked of the store.
|
||||
if [ "$(cert_end)" -gt $((now + ${toString ensureMarginSec})) ]; then exit 0; fi
|
||||
# The store refused a moment ago: the switch is off.
|
||||
if [ -e "$refused" ] && [ $((now - $(stat -c %Y "$refused"))) -lt ${toString refusedForSec} ]; then
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# The unit has the credentials directory and keeps the store's CA where
|
||||
# an `ssh` run can find it.
|
||||
if [ -n "''${CREDENTIALS_DIRECTORY-}" ] && [ -s "$CREDENTIALS_DIRECTORY/${serverCaCredential}" ]; then
|
||||
rm -f "$server_ca"
|
||||
cp "$CREDENTIALS_DIRECTORY/${serverCaCredential}" "$server_ca"
|
||||
fi
|
||||
if [ -s "$server_ca" ]; then export BAO_CACERT="$server_ca"; fi
|
||||
|
||||
trap 'rm -f "$err" "$staged"' EXIT
|
||||
|
||||
# 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
|
||||
if [ -n "$ensure" ]; then exit 1; fi
|
||||
exit 0
|
||||
fi
|
||||
if [ ! -r "$f" ]; then
|
||||
|
|
@ -119,19 +209,6 @@ in
|
|||
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.
|
||||
|
|
@ -141,6 +218,8 @@ in
|
|||
chmod 0400 "$key"
|
||||
fi
|
||||
|
||||
# A fresh login every time: a token carries the policies the role had
|
||||
# when it was issued, so only a new one sees the operator's switch.
|
||||
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
|
||||
|
|
@ -157,7 +236,10 @@ in
|
|||
# so no failed unit. Anything else (a sealed or unreachable store) is
|
||||
# one, and retried.
|
||||
if [[ "$(<"$err")" == *"Code: 403"* ]]; then
|
||||
rm -f "$refused"
|
||||
: >"$refused"
|
||||
echo "SSH certificates are not enabled for this agent (the swarm store refused to sign); asking again next tick."
|
||||
if [ -n "$ensure" ]; then exit 1; fi
|
||||
exit 0
|
||||
fi
|
||||
echo "the swarm secret store would not sign this agent's SSH key at ${signPath}:" >&2
|
||||
|
|
@ -173,6 +255,7 @@ in
|
|||
|
||||
# One rename: ssh never reads a half-written certificate.
|
||||
mv -f "$staged" "$cert"
|
||||
rm -f "$refused"
|
||||
echo "signed this agent's SSH key as ${agentName}, valid for ${toString ca.certTtlMinutes} minutes."
|
||||
'';
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in a new issue