181 lines
5.3 KiB
Shell
181 lines
5.3 KiB
Shell
# 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
|