Keep WARP window open across firewall reload
Some checks are pending
md-naming / markdown naming convention (push) Waiting to run
Pre-commit checks / pre-commit (${{ matrix.group }}) (mypy, mypy) (push) Waiting to run
Pre-commit checks / pre-commit (${{ matrix.group }}) (pylint, pylint) (push) Waiting to run
Pre-commit checks / pre-commit (${{ matrix.group }}) (rest, ) (push) Waiting to run
Pre-commit checks / pre-commit (${{ matrix.group }}) (shellcheck, shellcheck) (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
Shell tests / shell-tests (push) Waiting to run
Shell tests / lib-tests (push) Waiting to run
Shell tests / arch-tests (push) Waiting to run

Each deploy ran allow-web, which reloaded inet filter and emptied
warp_open; the work laptop then read "closed" and dropped WARP.
A reload now restores an open window with the time it had left, an
identical live ruleset is not reloaded, and nftables is enabled
without --now so the file is not loaded twice.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWVoDCxaTn3Vnoj8yRb
This commit is contained in:
Krzysztof Rudnicki 2026-11-20 11:41:05 +01:00
parent fe29823291
commit 77a5552626
3 changed files with 166 additions and 1 deletions

View File

@ -0,0 +1,41 @@
{
"intent": "A firewall reload on the PC (e.g. a deploy running allow-web) no longer closes the home WARP window, so the work laptop keeps WARP until 17:00.",
"scope": [
"linux_configuration/features/lib/wg_firewall.sh",
"linux_configuration/features/lib/tests/test_wg_firewall_apply.sh",
"Non-goal: the laptop side (warp_home.sh) and the window rules are unchanged"
],
"changes": [
"verify_nftables_then_apply restores an open warp_open element right after nft -f, with the timeout it had left (never extends, never opens a closed one)",
"An identical ruleset that is already live is not reloaded; nftables is enabled without --now so it is not loaded a second time"
],
"verification": [
{
"command": "bash linux_configuration/features/lib/tests/test_wg_firewall_apply.sh",
"result": "pass",
"evidence": "RED before fix: 3 passed, 4 failed; after: 7 passed, 0 failed"
},
{
"command": "unshare -rn /tmp/warpe2e/run.sh live.conf (real nft, private netns)",
"result": "pass",
"evidence": "before 2h59m59s998ms, after changed reload 2h59m59s975ms, unchanged skipped, closed stays []"
},
{
"command": "sudo features/setup_wireguard_ssh.sh allow-web (live PC)",
"result": "pass",
"evidence": "nftables ruleset unchanged; not reloading. No nftables.service start logged."
},
{
"command": "test_wg_firewall.sh, test_warp_window.sh, test_warp_home.sh, test_dns_gateway.sh",
"result": "pass",
"evidence": "37, 35, 35, 15 passed, 0 failed"
}
],
"risks": [
"A live ruleset that drifted while /etc/nftables.conf stayed identical is no longer re-applied by allow-* (nftables-drift verify still reports it)"
],
"rollback": [
"git revert the commit",
"After rollback, a reload empties warp_open again: check sudo warp-window status"
]
}

View File

@ -0,0 +1,96 @@
#!/usr/bin/env bash
# Tests for verify_nftables_then_apply -- a firewall reload and the WARP window.
#
# A reload replaces 'inet filter' and with it the warp_open set that
# warp_window.sh filled. Each todoImplementer deploy ran 'allow-web', the set
# came back empty, and the work laptop's warp-home tick read "closed" and
# dropped WARP mid-workday. The window must survive a reload with the time it
# had left, and a reload that changes nothing must not happen at all.
#
# nft and systemctl are fakes that model just enough state: one file stands
# for the open window element, and anything that loads the ruleset empties it.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=./features_harness.sh
. "$SCRIPT_DIR/features_harness.sh"
_t_setup_env
trap _t_teardown EXIT
NFT_CONF="$TEST_TMPDIR/nftables.conf"
WINDOW="$TEST_TMPDIR/warp_open.elem"
log_ok() { log "$1"; }
log_warn() { log "$1"; }
die() {
log "$1"
exit 1
}
is_service_active() { return 0; }
sleep() { :; }
# The real one (lib/common_services.sh) is 'enable --now', i.e. a second load.
enable_service() { systemctl enable --now "$1"; }
# shellcheck source=../wg_firewall.sh
. "$FEATURES_LIB_DIR/wg_firewall.sh"
# After the source line, or the real one replaces the stub.
restore_docker_rules() { :; }
cat >"$TEST_TMPDIR/bin/nft" <<EOF
#!/usr/bin/env bash
printf 'nft %s\n' "\$*" >>"$TEST_TMPDIR/calls.log"
case "\$*" in
"-c -f "*) exit 0 ;;
"-f "*) rm -f "$WINDOW" ;;
"list table inet filter") exit 0 ;;
"list set inet filter warp_open")
printf 'set warp_open {\n'
[[ -f "$WINDOW" ]] && printf '\telements = { %s }\n' "\$(<"$WINDOW")"
printf '}\n' ;;
"add element inet filter warp_open "*)
el="\${*#add element inet filter warp_open }"
el="\${el#\{ }"
printf '%s expires 5h31m31s119ms\n' "\${el% \}}" >"$WINDOW" ;;
esac
EOF
cat >"$TEST_TMPDIR/bin/systemctl" <<EOF
#!/usr/bin/env bash
printf 'systemctl %s\n' "\$*" >>"$TEST_TMPDIR/calls.log"
[[ "\$*" == *--now* || "\$*" == start* || "\$*" == restart* ]] && rm -f "$WINDOW"
exit 0
EOF
chmod +x "$TEST_TMPDIR/bin/nft" "$TEST_TMPDIR/bin/systemctl"
window() { cat "$WINDOW" 2>/dev/null || printf 'closed'; }
printf '\n-- a reload keeps an open WARP window --\n'
printf 'old ruleset\n' >"$NFT_CONF"
printf 'new ruleset\n' >"${NFT_CONF}.new"
printf '0.0.0.0/0 timeout 5h32m21s expires 5h31m31s119ms\n' >"$WINDOW"
: >"$TEST_TMPDIR/calls.log"
(verify_nftables_then_apply) >/dev/null 2>&1
_t_called 'nft -f' "a changed ruleset is loaded"
_t_has "$(window)" '0.0.0.0/0 timeout 5h31m31s119ms' \
"the window is back with the time it had left, not more"
printf '\n-- a reload never opens a closed window --\n'
rm -f "$WINDOW"
printf 'newer ruleset\n' >"${NFT_CONF}.new"
(verify_nftables_then_apply) >/dev/null 2>&1
_t_eq 'closed' "$(window)" "a closed window stays closed"
printf '\n-- an unchanged ruleset is not reloaded --\n'
cp "$NFT_CONF" "${NFT_CONF}.new"
printf '0.0.0.0/0 timeout 1h expires 59m\n' >"$WINDOW"
: >"$TEST_TMPDIR/calls.log"
out="$(verify_nftables_then_apply 2>&1)"
if grep -qE '^nft -f|--now|^systemctl (re)?start' "$TEST_TMPDIR/calls.log"; then
_t_fail "no load and no service start when nothing changed"
else
_t_pass "no load and no service start when nothing changed"
fi
_t_has "$(window)" 'expires 59m' "the window is untouched"
_t_has "$out" 'unchanged' "the skip is reported"
_t_not "the scratch ruleset is cleaned up" test -e "${NFT_CONF}.new"
_t_report "test_wg_firewall_apply.sh"

View File

@ -163,11 +163,36 @@ restore_docker_rules() {
log_ok "docker restarted; its firewall rules are back."
}
# Loading the ruleset replaces 'inet filter', and with it the warp_open set
# warp_window.sh fills. Empty, the work laptop's warp-home tick reads "closed"
# and drops WARP, so an open window is put back with the time it had left:
# a reload neither closes nor extends it. Prints nothing when it is closed.
warp_window_left() {
nft list set inet filter warp_open 2>/dev/null |
grep -o 'expires [0-9hms]*' | head -n1 | cut -d' ' -f2 || true
}
warp_window_restore() {
[[ -n $1 ]] || return 0
nft add element inet filter warp_open "{ 0.0.0.0/0 timeout $1 }"
log_ok "WARP window kept open (${1} left)."
}
verify_nftables_then_apply() {
nft -c -f "${NFT_CONF}.new" || die "nftables ruleset failed syntax check -- not applying."
# Deploys re-run allow-web on every push; an identical ruleset that is
# already live needs no reload, and every reload is a window for drops.
if cmp -s "${NFT_CONF}.new" "$NFT_CONF" && nft list table inet filter >/dev/null 2>&1; then
rm -f "${NFT_CONF}.new"
log_ok "nftables ruleset unchanged; not reloading."
return 0
fi
local warp_left
warp_left="$(warp_window_left)"
mv "${NFT_CONF}.new" "$NFT_CONF"
log_warn "Applying a default-drop firewall now."
nft -f "$NFT_CONF"
warp_window_restore "$warp_left"
sleep 2
if ! is_service_active sshd; then
nft delete table inet filter
@ -176,7 +201,10 @@ verify_nftables_then_apply() {
log_ok "nftables applied; sshd is still active."
restore_docker_rules
log_warn "Before closing this terminal, open a SECOND ssh session now and confirm it connects."
enable_service nftables
# Enable only: nftables.service is a oneshot without RemainAfterExit, so
# 'enable --now' would load the same file a second time and empty
# warp_open again right after it was restored.
systemctl enable nftables
}
allow_dot() {