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
2 changed files with 521 additions and 67 deletions
92
nix/script-tests/agent-ssh-cert.nix
Normal file
92
nix/script-tests/agent-ssh-cert.nix
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
# `checks.script-test-agent-ssh-cert` — runs ../agent-modules/ssh.nix's helper,
|
||||
# the text of the unit `hive-agent-ssh-cert`, against a stub `bao` and a real
|
||||
# `ssh-keygen`, in both entries: the timer's and the `ssh` one. The cases are in
|
||||
# ./agent-ssh-cert.sh.
|
||||
#
|
||||
# The one edit to the text is its `/run/` prefix, moved under the build
|
||||
# directory because the sandbox has no writable `/run`. The helper adds its own
|
||||
# tools after the caller's `PATH`, so the stub in front wins.
|
||||
{
|
||||
pkgs,
|
||||
lib,
|
||||
self,
|
||||
nixosSystem,
|
||||
}:
|
||||
let
|
||||
inherit
|
||||
(import ../module-eval/lib.nix {
|
||||
inherit
|
||||
pkgs
|
||||
lib
|
||||
self
|
||||
nixosSystem
|
||||
;
|
||||
})
|
||||
agentWith
|
||||
;
|
||||
|
||||
machine = agentWith { services.hyperhive.agent.bao.addr = "https://bao.t.local:8200"; };
|
||||
|
||||
# `login` prints a token; `write` signs `public_key=@<file>` with the test CA
|
||||
# for `FAKE_SIGN_VALID` (an `ssh-keygen -V` interval), or answers 403 when
|
||||
# `FAKE_SIGN_MODE` is `refuse`, or fails otherwise when it is `fail`. A call
|
||||
# without the token the last login printed fails, so a script that signs
|
||||
# without a login of its own cannot pass.
|
||||
fakeBao = pkgs.writeShellScriptBin "bao" ''
|
||||
umask 022
|
||||
echo "$1" >> "$FAKE_BAO_LOG"
|
||||
case "$1" in
|
||||
login)
|
||||
printf 'tok-%s' "$(wc -l < "$FAKE_BAO_LOG")"
|
||||
;;
|
||||
write)
|
||||
if [ "''${BAO_TOKEN-}" != "tok-$(( $(wc -l < "$FAKE_BAO_LOG") - 1 ))" ]; then
|
||||
echo "fake bao: write without the token of the login just before it" >&2
|
||||
exit 97
|
||||
fi
|
||||
case "$FAKE_SIGN_MODE" in
|
||||
refuse)
|
||||
printf 'Error writing data: Code: 403. Errors:\n* permission denied\n' >&2
|
||||
exit 2
|
||||
;;
|
||||
fail)
|
||||
printf 'Error writing data: Code: 503. Errors:\n* sealed\n' >&2
|
||||
exit 2
|
||||
;;
|
||||
esac
|
||||
pub=
|
||||
for a in "$@"; do
|
||||
case "$a" in public_key=@*) pub=''${a#public_key=@} ;; esac
|
||||
done
|
||||
tmp=$(mktemp -d)
|
||||
cp "$pub" "$tmp/k.pub"
|
||||
${pkgs.openssh}/bin/ssh-keygen -q -s "$FAKE_CA_KEY" -I fake -n a1 -V "$FAKE_SIGN_VALID" "$tmp/k.pub"
|
||||
cat "$tmp/k-cert.pub"
|
||||
;;
|
||||
*)
|
||||
echo "fake bao: unexpected call: $*" >&2
|
||||
exit 98
|
||||
;;
|
||||
esac
|
||||
'';
|
||||
|
||||
script = pkgs.writeText "agent-ssh-cert-script" machine.systemd.services.hive-agent-ssh-cert.script;
|
||||
in
|
||||
pkgs.runCommand "hyperhive-script-test-agent-ssh-cert"
|
||||
{
|
||||
nativeBuildInputs = [
|
||||
pkgs.openssh
|
||||
pkgs.coreutils
|
||||
pkgs.util-linux
|
||||
];
|
||||
SSH_CERT_SCRIPT = script;
|
||||
FAKE_BAO_BIN = "${fakeBao}/bin";
|
||||
SSH_CERT_PATH = lib.makeBinPath [
|
||||
pkgs.coreutils
|
||||
pkgs.bash
|
||||
];
|
||||
}
|
||||
''
|
||||
${pkgs.bash}/bin/bash ${./agent-ssh-cert.sh}
|
||||
touch "$out"
|
||||
''
|
||||
181
nix/script-tests/agent-ssh-cert.sh
Normal file
181
nix/script-tests/agent-ssh-cert.sh
Normal file
|
|
@ -0,0 +1,181 @@
|
|||
# Cases for ./agent-ssh-cert.nix. Each case gets a fresh runtime directory and
|
||||
# runs the helper in one entry (the timer's, or `ssh`'s with
|
||||
# HIVE_SSH_CERT_ENSURE=1) with a clean environment, and asserts on the exit
|
||||
# code, the output, the files left behind and the `bao` calls made.
|
||||
set -uo pipefail
|
||||
|
||||
failures=0
|
||||
count=0
|
||||
case=
|
||||
|
||||
fail() {
|
||||
echo "FAILED: $case: $*" >&2
|
||||
failures=$((failures + 1))
|
||||
}
|
||||
|
||||
top=$(mktemp -d)
|
||||
ssh-keygen -q -t ed25519 -N "" -f "$top/ca"
|
||||
|
||||
# setup <description>
|
||||
setup() {
|
||||
case="$1"
|
||||
dir=$(mktemp -d)
|
||||
rt=$dir/run/hive-agent-ssh-cert
|
||||
mkdir -p "$dir/run/hive-agent-bao-identity/current" "$dir/creds"
|
||||
mkdir -m 0700 "$rt"
|
||||
printf cert >"$dir/run/hive-agent-bao-identity/current/cert"
|
||||
printf key >"$dir/run/hive-agent-bao-identity/current/key"
|
||||
printf ca >"$dir/creds/hive-agent-bao-server-ca"
|
||||
log=$dir/bao.log
|
||||
: >"$log"
|
||||
sed -e "s|/run/|$dir/run/|g" "$SSH_CERT_SCRIPT" >"$dir/script"
|
||||
chmod +x "$dir/script"
|
||||
if grep -q ' /run/\|"/run/\|=/run/' "$dir/script"; then fail "the script still names /run after the rewrite"; fi
|
||||
sign_mode=ok
|
||||
valid=+1h
|
||||
creds=$dir/creds
|
||||
}
|
||||
|
||||
# go [ensure]
|
||||
go() {
|
||||
count=$((count + 1))
|
||||
local ensure=
|
||||
if [ "${1-}" = ensure ]; then ensure=1; fi
|
||||
(
|
||||
cd "$dir" || exit 99
|
||||
exec env -i \
|
||||
PATH="$FAKE_BAO_BIN:$SSH_CERT_PATH" \
|
||||
HOME="$dir" \
|
||||
CREDENTIALS_DIRECTORY="$creds" \
|
||||
HIVE_SSH_CERT_ENSURE="$ensure" \
|
||||
FAKE_BAO_LOG="$log" \
|
||||
FAKE_CA_KEY="$top/ca" \
|
||||
FAKE_SIGN_MODE="$sign_mode" \
|
||||
FAKE_SIGN_VALID="$valid" \
|
||||
"$dir/script"
|
||||
) >"$dir/out" 2>"$dir/err"
|
||||
rc=$?
|
||||
}
|
||||
|
||||
expect_rc() {
|
||||
if [ "$rc" != "$1" ]; then fail "exit $rc, expected $1; stderr: $(<"$dir/err")"; fi
|
||||
}
|
||||
|
||||
expect_silent() {
|
||||
if [ -s "$dir/out" ] || [ -s "$dir/err" ]; then fail "output where none was wanted: $(<"$dir/out") $(<"$dir/err")"; fi
|
||||
}
|
||||
|
||||
expect_calls() {
|
||||
local want
|
||||
want=$(printf '%s\n' "$@")
|
||||
if [ "$(<"$log")" != "${want%$'\n'}" ]; then fail "bao calls were '$(<"$log" tr '\n' ' ')', expected '$*'"; fi
|
||||
}
|
||||
|
||||
expect_cert() {
|
||||
if ! ssh-keygen -L -f "$rt/key-cert.pub" >/dev/null 2>&1; then fail "no readable certificate at $rt/key-cert.pub"; fi
|
||||
}
|
||||
|
||||
# The seconds the certificate at $rt has left; 0 when there is none.
|
||||
left() {
|
||||
local end
|
||||
end=$(ssh-keygen -L -f "$rt/key-cert.pub" 2>/dev/null | sed -n 's/^ *Valid: from .* to //p')
|
||||
if [ -z "$end" ]; then echo 0; else echo $(($(date -d "$end" +%s) - $(date +%s))); fi
|
||||
}
|
||||
|
||||
# A certificate for the key the helper will keep, valid for `$1`, put in place
|
||||
# as a first run would have left it.
|
||||
seed_cert() {
|
||||
mkdir -p "$rt"
|
||||
rm -f "$rt/key" "$rt/key.pub" "$rt/key-cert.pub"
|
||||
(umask 0377 && ssh-keygen -q -t ed25519 -N "" -f "$rt/key" && chmod 0400 "$rt/key")
|
||||
ssh-keygen -q -s "$top/ca" -I seed -n a1 -V "$1" "$rt/key.pub"
|
||||
}
|
||||
|
||||
setup "ssh finds no certificate: it logs in, signs, and is silent and true"
|
||||
go ensure
|
||||
expect_rc 0
|
||||
expect_silent
|
||||
expect_calls login write
|
||||
expect_cert
|
||||
if [ "$(left)" -lt 3000 ]; then fail "the new certificate has only $(left)s left"; fi
|
||||
|
||||
setup "ssh finds a certificate with time left: it asks the store nothing"
|
||||
seed_cert +1h
|
||||
go ensure
|
||||
expect_rc 0
|
||||
expect_silent
|
||||
expect_calls
|
||||
|
||||
setup "ssh finds a certificate close to its end: it signs again with a fresh login"
|
||||
seed_cert +5m
|
||||
before=$(left)
|
||||
go ensure
|
||||
expect_rc 0
|
||||
expect_silent
|
||||
expect_calls login write
|
||||
if [ "$(left)" -le "$before" ]; then fail "the certificate was not replaced ($before then $(left))"; fi
|
||||
|
||||
setup "ssh finds an expired certificate: it signs again"
|
||||
seed_cert -2h:-1h
|
||||
go ensure
|
||||
expect_rc 0
|
||||
expect_calls login write
|
||||
if [ "$(left)" -lt 3000 ]; then fail "an expired certificate was kept ($(left)s)"; fi
|
||||
|
||||
setup "the store refuses (switch off): ssh gets false, quietly, and no certificate"
|
||||
sign_mode=refuse
|
||||
go ensure
|
||||
expect_rc 1
|
||||
expect_silent
|
||||
expect_calls login write
|
||||
if [ -e "$rt/key-cert.pub" ]; then fail "a certificate appeared although the store refused"; fi
|
||||
|
||||
setup "right after a refusal ssh does not ask again, and does once the memory has aged"
|
||||
sign_mode=refuse
|
||||
go ensure
|
||||
go ensure
|
||||
expect_rc 1
|
||||
expect_calls login write
|
||||
touch -d '1 minute ago' "$rt/refused"
|
||||
sign_mode=ok
|
||||
go ensure
|
||||
expect_rc 0
|
||||
expect_calls login write login write
|
||||
expect_cert
|
||||
|
||||
setup "the timer's entry signs even with time left, and a refusal is one line and a clean exit"
|
||||
seed_cert +1h
|
||||
go
|
||||
expect_rc 0
|
||||
expect_calls login write
|
||||
sign_mode=refuse
|
||||
go
|
||||
expect_rc 0
|
||||
if ! grep -q 'not enabled for this agent' "$dir/out"; then fail "no line saying signing is off: $(<"$dir/out")"; fi
|
||||
if [ -s "$dir/err" ]; then fail "stderr on a refusal: $(<"$dir/err")"; fi
|
||||
|
||||
setup "any other signing failure fails both entries, and ssh stays quiet"
|
||||
sign_mode=fail
|
||||
go
|
||||
expect_rc 1
|
||||
go ensure
|
||||
expect_rc 1
|
||||
expect_silent
|
||||
|
||||
setup "a store identity that is missing is true for the timer and false for ssh"
|
||||
rm "$dir/run/hive-agent-bao-identity/current/cert"
|
||||
go
|
||||
expect_rc 0
|
||||
go ensure
|
||||
expect_rc 1
|
||||
expect_calls
|
||||
|
||||
setup "the unit keeps the store's CA where an ssh run finds it, and the key stays unreadable to others"
|
||||
go
|
||||
expect_rc 0
|
||||
if [ "$(<"$rt/server-ca.pem")" != ca ]; then fail "the store's CA was not copied"; fi
|
||||
if [ "$(stat -c %a "$rt/key")" != 400 ]; then fail "the key is mode $(stat -c %a "$rt/key")"; fi
|
||||
if [ "$(stat -c %a "$rt/key-cert.pub")" != 400 ]; then fail "the certificate is mode $(stat -c %a "$rt/key-cert.pub")"; fi
|
||||
|
||||
echo "$count runs, $failures failures"
|
||||
if [ "$failures" -ne 0 ]; then exit 1; fi
|
||||
Loading…
Reference in a new issue