Skip to content

Commit ca4f596

Browse files
acrogenesisryanrhughes
authored andcommitted
Harden existing key-based SSH setups
1 parent 981274b commit ca4f596

2 files changed

Lines changed: 198 additions & 0 deletions

File tree

‎migrations/1788124236.sh‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
echo "Disable SSH password authentication on existing key-based setups"
2+
3+
config=/etc/ssh/sshd_config.d/10-omarchy-hardening.conf
4+
authorized_keys="$HOME/.ssh/authorized_keys"
5+
6+
as_root() {
7+
if (( EUID == 0 )); then
8+
"$@"
9+
else
10+
sudo "$@"
11+
fi
12+
}
13+
14+
# The fixed setup command writes this file itself. Its presence is also the
15+
# machine-wide completion state, so migrations run by another account no-op.
16+
if [[ -e $config || -L $config ]]; then
17+
exit 0
18+
fi
19+
20+
# Earlier versions enabled sshd before importing the key, but did not leave a
21+
# marker saying that Omarchy configured it. Limit the repair to a daemon that is
22+
# enabled or currently exposed and a user who already has a usable authorized
23+
# key. A machine that never set SSH up exits without prompting for privileges.
24+
if ! systemctl is-enabled --quiet sshd.service 2>/dev/null &&
25+
! systemctl is-active --quiet sshd.service 2>/dev/null; then
26+
exit 0
27+
fi
28+
29+
if [[ ! -f $authorized_keys || -L $authorized_keys || ! -s $authorized_keys || ! -r $authorized_keys ]] ||
30+
! ssh-keygen -lf "$authorized_keys" >/dev/null 2>&1; then
31+
echo "Leaving SSH password authentication unchanged because $authorized_keys has no usable public key."
32+
exit 0
33+
fi
34+
35+
echo "Disabling SSH password authentication on the existing key-based SSH setup..."
36+
if ! as_root install -Dm644 /dev/stdin "$config" <<'CONF'
37+
# Written by Omarchy once an SSH key was already authorized.
38+
# Delete this file and reload sshd to allow password logins again.
39+
PasswordAuthentication no
40+
KbdInteractiveAuthentication no
41+
CONF
42+
then
43+
echo "Administrator privileges are required to harden the existing SSH setup. Run omarchy-migrate again from a terminal." >&2
44+
exit 1
45+
fi
46+
47+
# Syntax alone is insufficient because sshd uses the first value it reads. If
48+
# another administrator rule wins, remove our ineffective file and keep the
49+
# migration pending rather than claiming the machine is protected.
50+
if ! as_root sshd -t; then
51+
as_root rm -f -- "$config" || true
52+
echo "sshd rejected the hardening config. Fix the SSH configuration and run omarchy-migrate again." >&2
53+
exit 1
54+
fi
55+
56+
effective_config=$(as_root sshd -T) || {
57+
as_root rm -f -- "$config" || true
58+
echo "Could not inspect sshd's effective configuration. Run omarchy-migrate again after fixing SSH." >&2
59+
exit 1
60+
}
61+
62+
if ! grep -qixF "passwordauthentication no" <<<"$effective_config" ||
63+
! grep -qixF "kbdinteractiveauthentication no" <<<"$effective_config"; then
64+
as_root rm -f -- "$config" || true
65+
echo "Another SSH rule keeps password authentication enabled. Fix its ordering and run omarchy-migrate again." >&2
66+
exit 1
67+
fi
68+
69+
# An enabled but deliberately stopped daemon picks the file up on its next
70+
# start. Reload only a daemon that is currently serving connections so existing
71+
# sessions survive while new ones get the hardened policy.
72+
if systemctl is-active --quiet sshd.service 2>/dev/null; then
73+
if ! as_root systemctl reload sshd.service; then
74+
echo "The hardening config is valid but sshd could not reload it. Run omarchy-migrate again after fixing the service." >&2
75+
exit 1
76+
fi
77+
fi
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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

Comments
 (0)