|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +source "$(dirname "$0")/base-test.sh" |
| 6 | + |
| 7 | +test_dir=$(mktemp -d) |
| 8 | +trap 'rm -rf "$test_dir"' EXIT |
| 9 | + |
| 10 | +migration="$ROOT/migrations/1788124236.sh" |
| 11 | +stub_bin="$test_dir/bin" |
| 12 | +mkdir -p "$stub_bin" |
| 13 | + |
| 14 | +cat >"$stub_bin/systemctl" <<'STUB' |
| 15 | +#!/bin/bash |
| 16 | +printf 'systemctl %s\n' "$*" >>"${CALL_LOG:?}" |
| 17 | +case "$1 $2" in |
| 18 | +"is-enabled --quiet") [[ ${SSHD_ENABLED:-0} == 1 ]] ;; |
| 19 | +"is-active --quiet") [[ ${SSHD_ACTIVE:-0} == 1 ]] ;; |
| 20 | +"reload sshd.service") [[ ${SSHD_RELOAD_VALID:-1} == 1 ]] ;; |
| 21 | +*) exit 2 ;; |
| 22 | +esac |
| 23 | +STUB |
| 24 | + |
| 25 | +cat >"$stub_bin/sshd" <<'STUB' |
| 26 | +#!/bin/bash |
| 27 | +printf 'sshd %s\n' "$*" >>"${CALL_LOG:?}" |
| 28 | +case $1 in |
| 29 | +-t) [[ ${SSHD_SYNTAX_VALID:-1} == 1 ]] ;; |
| 30 | +-T) |
| 31 | + printf 'PasswordAuthentication %s\n' "${SSHD_PASSWORD_AUTH:-no}" |
| 32 | + printf 'KbdInteractiveAuthentication %s\n' "${SSHD_KBD_AUTH:-no}" |
| 33 | + ;; |
| 34 | +*) exit 2 ;; |
| 35 | +esac |
| 36 | +STUB |
| 37 | + |
| 38 | +cat >"$stub_bin/sudo" <<'STUB' |
| 39 | +#!/bin/bash |
| 40 | +printf 'sudo %s\n' "$*" >>"${CALL_LOG:?}" |
| 41 | +exec "$@" |
| 42 | +STUB |
| 43 | + |
| 44 | +chmod +x "$stub_bin"/* |
| 45 | + |
| 46 | +ssh-keygen -q -t ed25519 -N "" -f "$test_dir/key" |
| 47 | +public_key=$(<"$test_dir/key.pub") |
| 48 | + |
| 49 | +run_migration() { |
| 50 | + local scenario=$1 |
| 51 | + local home="$test_dir/$scenario/home" |
| 52 | + local root="$test_dir/$scenario/root" |
| 53 | + local config="$root/etc/ssh/sshd_config.d/10-omarchy-hardening.conf" |
| 54 | + |
| 55 | + mkdir -p "$home/.ssh" "${config%/*}" |
| 56 | + : >"$test_dir/$scenario.calls" |
| 57 | + if [[ ${AUTHORIZED_KEY_STATE:-valid} == "valid" ]]; then |
| 58 | + printf '%s\n' "$public_key" >"$home/.ssh/authorized_keys" |
| 59 | + elif [[ $AUTHORIZED_KEY_STATE == "invalid" ]]; then |
| 60 | + printf 'not a public key\n' >"$home/.ssh/authorized_keys" |
| 61 | + fi |
| 62 | + |
| 63 | + # Keep the privileged production destination fixed in the shipped migration. |
| 64 | + # For this isolated test only, rewrite that one assignment in the input fed to |
| 65 | + # bash so no scenario can touch the host's /etc. |
| 66 | + sed "s|^config=/etc/ssh/sshd_config.d/10-omarchy-hardening.conf$|config=$config|" "$migration" | |
| 67 | + HOME="$home" CALL_LOG="$test_dir/$scenario.calls" PATH="$stub_bin:$PATH" \ |
| 68 | + SSHD_ENABLED="${SSHD_ENABLED:-0}" SSHD_ACTIVE="${SSHD_ACTIVE:-0}" \ |
| 69 | + SSHD_SYNTAX_VALID="${SSHD_SYNTAX_VALID:-1}" \ |
| 70 | + SSHD_PASSWORD_AUTH="${SSHD_PASSWORD_AUTH:-no}" \ |
| 71 | + SSHD_KBD_AUTH="${SSHD_KBD_AUTH:-no}" \ |
| 72 | + SSHD_RELOAD_VALID="${SSHD_RELOAD_VALID:-1}" \ |
| 73 | + bash -euo pipefail |
| 74 | +} |
| 75 | + |
| 76 | +SSHD_ENABLED=0 SSHD_ACTIVE=0 run_migration disabled |
| 77 | +[[ ! -e $test_dir/disabled/root/etc/ssh/sshd_config.d/10-omarchy-hardening.conf ]] || |
| 78 | + fail "SSH migration leaves a disabled daemon alone" |
| 79 | +! grep -q '^sudo ' "$test_dir/disabled.calls" || fail "disabled SSH does not prompt for privileges" |
| 80 | +pass "SSH migration no-ops when sshd is not enabled or active" |
| 81 | + |
| 82 | +AUTHORIZED_KEY_STATE=missing SSHD_ENABLED=1 run_migration no-key >/dev/null |
| 83 | +[[ ! -e $test_dir/no-key/root/etc/ssh/sshd_config.d/10-omarchy-hardening.conf ]] || |
| 84 | + fail "SSH migration must not disable passwords without an authorized key" |
| 85 | +! grep -q '^sudo ' "$test_dir/no-key.calls" || fail "missing SSH key does not prompt for privileges" |
| 86 | + |
| 87 | +AUTHORIZED_KEY_STATE=invalid SSHD_ENABLED=1 run_migration invalid-key >/dev/null |
| 88 | +[[ ! -e $test_dir/invalid-key/root/etc/ssh/sshd_config.d/10-omarchy-hardening.conf ]] || |
| 89 | + fail "SSH migration must not trust a malformed authorized_keys file" |
| 90 | +pass "SSH migration requires a usable authorized key before disabling passwords" |
| 91 | + |
| 92 | +SSHD_ENABLED=1 SSHD_ACTIVE=1 run_migration active >/dev/null |
| 93 | +config="$test_dir/active/root/etc/ssh/sshd_config.d/10-omarchy-hardening.conf" |
| 94 | +grep -qxF "PasswordAuthentication no" "$config" || fail "SSH migration disables password authentication" |
| 95 | +grep -qxF "KbdInteractiveAuthentication no" "$config" || fail "SSH migration disables keyboard-interactive authentication" |
| 96 | +grep -qxF "sudo sshd -t" "$test_dir/active.calls" || fail "SSH migration validates sshd syntax" |
| 97 | +grep -qxF "sudo sshd -T" "$test_dir/active.calls" || fail "SSH migration validates effective sshd settings" |
| 98 | +grep -qxF "sudo systemctl reload sshd.service" "$test_dir/active.calls" || fail "SSH migration reloads an active daemon" |
| 99 | +pass "SSH migration hardens and reloads an existing key-based SSH setup" |
| 100 | + |
| 101 | +SSHD_ENABLED=1 SSHD_ACTIVE=0 run_migration stopped >/dev/null |
| 102 | +[[ -e $test_dir/stopped/root/etc/ssh/sshd_config.d/10-omarchy-hardening.conf ]] || |
| 103 | + fail "SSH migration hardens an enabled but stopped daemon" |
| 104 | +! grep -qF 'reload sshd.service' "$test_dir/stopped.calls" || fail "SSH migration must not start or reload a stopped daemon" |
| 105 | +pass "SSH migration hardens an enabled daemon without starting it" |
| 106 | + |
| 107 | +if SSHD_ENABLED=1 SSHD_ACTIVE=1 SSHD_PASSWORD_AUTH=yes run_migration ineffective >"$test_dir/ineffective.output" 2>&1; then |
| 108 | + fail "SSH migration must fail when password authentication remains effective" |
| 109 | +fi |
| 110 | +[[ ! -e $test_dir/ineffective/root/etc/ssh/sshd_config.d/10-omarchy-hardening.conf ]] || |
| 111 | + fail "SSH migration removes an ineffective config" |
| 112 | +! grep -qF 'reload sshd.service' "$test_dir/ineffective.calls" || fail "SSH migration must not reload ineffective hardening" |
| 113 | +pass "SSH migration stays pending when another rule keeps password authentication enabled" |
| 114 | + |
| 115 | +if SSHD_ENABLED=1 SSHD_ACTIVE=1 SSHD_SYNTAX_VALID=0 run_migration invalid-config >"$test_dir/invalid-config.output" 2>&1; then |
| 116 | + fail "SSH migration must fail when sshd rejects its config" |
| 117 | +fi |
| 118 | +[[ ! -e $test_dir/invalid-config/root/etc/ssh/sshd_config.d/10-omarchy-hardening.conf ]] || |
| 119 | + fail "SSH migration removes a rejected config" |
| 120 | +! grep -qF 'reload sshd.service' "$test_dir/invalid-config.calls" || fail "SSH migration must not reload rejected hardening" |
| 121 | +pass "SSH migration fails safely when sshd rejects the config" |
0 commit comments