interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / infra / test / http80-netns.sh

149 lines · 10030 bytes Raw Blame History
1#!/usr/bin/env bash
2# Rehearsal of spark/edge-http-apply.sh and spark/edge-http-rollback.sh in throwaway network and
3# mount namespaces, so the live firewall, nginx and apache2 are never touched.
4# sudo bash infra/test/http80-netns.sh <scratch dir>
5# Shape: netns ipl80-srv plays the Spark (a veth named enP7s7 holding 192.168.1.108/24, a COPY of the
6# live /etc/atlas/ingress.nft, a test nginx whose *:80 server stands in for apache2) and netns
7# ipl80-cli plays a house-LAN machine (192.168.1.50/24). In ipl80-srv, /etc/nginx, /etc/atlas,
8# /var/log/nginx and /var/lib/nginx are bind mounts of scratch copies inside a private mount namespace.
9# Steps: N1 negative control on the live firewall (port 80 dropped = today's timeout); apply; P1 every
10# edge-http-checks.sh check passes; M1 mutant (4480 accepted without the "came from :80" match) is
11# caught; N2 re-apply refused; rollback; P2 firewall file byte-identical to the original and port 80
12# dropped again; N3 a reload command that always fails must roll back and must not claim success;
13# N4 a reload that fails once must roll back, and that rollback's reload must succeed. Exit 0 only
14# if every step behaves as stated.
15set -u
16[ "$(id -u)" = 0 ] || { echo "run as root (sudo)" >&2; exit 2; }
17SCR=$(cd "${1:?usage: http80-netns.sh <scratch dir>}" && pwd) || { echo "scratch dir not usable" >&2; exit 2; }
18INFRA=$(cd "$(dirname "$0")/.." && pwd)
19T=$SCR/http80-netns.$$
20SRV=ipl80-srv CLI=ipl80-cli
21pass=0; fail=0
22ok() { pass=$((pass+1)); echo "PASS $*"; }
23bad() { fail=$((fail+1)); echo "FAIL $*"; }
24own_srv=0
25own_cli=0
26own_scratch=0
27# Refuse before installing the EXIT trap. A second run must not kill the first run's processes.
28ip netns list | grep -qE "^($SRV|$CLI)( |$)" && { echo "netns $SRV/$CLI already exist; refusing" >&2; exit 2; }
29cleanup() {
30 # Only namespaces and scratch this run created. Partial setup (one netns added, the next
31 # add refused) must not delete a namespace this run does not own.
32 if [ "$own_srv" = 1 ]; then ip netns pids "$SRV" 2>/dev/null | xargs -r kill 2>/dev/null; fi
33 if [ "$own_cli" = 1 ]; then ip netns pids "$CLI" 2>/dev/null | xargs -r kill 2>/dev/null; fi
34 if [ "$own_srv" = 1 ] || [ "$own_cli" = 1 ]; then sleep 0.3; fi
35 [ "$own_srv" = 1 ] && ip netns del "$SRV" 2>/dev/null
36 [ "$own_cli" = 1 ] && ip netns del "$CLI" 2>/dev/null
37 [ "$own_scratch" = 1 ] && rm -rf -- "$T"
38}
39trap cleanup EXIT
40mkdir -p "$T/etc-nginx/sites-enabled" "$T/etc-atlas" "$T/log" "$T/lib" || { echo "cannot create $T" >&2; exit 2; }
41own_scratch=1
42install -m 600 /etc/atlas/ingress.nft "$T/etc-atlas/ingress.nft"
43cp -p "$T/etc-atlas/ingress.nft" "$T/ingress.orig"
44cat > "$T/etc-nginx/nginx.conf" <<'EOF'
45# test nginx for http80-netns.sh (stand-in for the Spark's; only what edge-http-apply.sh touches)
46worker_processes 1;
47pid /var/lib/nginx/nginx.pid;
48error_log /var/log/nginx/error.log;
49events { worker_connections 64; }
50http {
51 access_log off;
52 client_body_temp_path /var/lib/nginx/body;
53 proxy_temp_path /var/lib/nginx/proxy;
54 fastcgi_temp_path /var/lib/nginx/fastcgi;
55 uwsgi_temp_path /var/lib/nginx/uwsgi;
56 scgi_temp_path /var/lib/nginx/scgi;
57 server { listen 80; default_type text/plain; return 200 "apache2 stand-in\n"; } # holds *:80 like apache2
58 include /etc/nginx/sites-enabled/*;
59}
60EOF
61
62ip netns add "$SRV" && own_srv=1 || { echo "cannot create netns $SRV" >&2; exit 2; }
63ip netns add "$CLI" && own_cli=1 || { echo "cannot create netns $CLI" >&2; exit 2; }
64ip link add enP7s7 netns "$SRV" type veth peer name cli0 netns "$CLI"
65ip -n "$SRV" addr add 192.168.1.108/24 dev enP7s7; ip -n "$SRV" link set enP7s7 up; ip -n "$SRV" link set lo up
66ip -n "$CLI" addr add 192.168.1.50/24 dev cli0; ip -n "$CLI" link set cli0 up; ip -n "$CLI" link set lo up
67
68# everything "on the Spark" runs through this: private mounts + netns ipl80-srv
69insrv() {
70 ip netns exec "$SRV" unshare -m --propagation private sh -c '
71 T=$1; shift
72 mount --bind "$T/etc-nginx" /etc/nginx && mount --bind "$T/etc-atlas" /etc/atlas &&
73 mount --bind "$T/log" /var/log/nginx && mount --bind "$T/lib" /var/lib/nginx && exec "$@"' _ "$T" "$@"
74}
75insrv nft -f /etc/atlas/ingress.nft || { echo "copy of live firewall does not load in the netns" >&2; exit 2; }
76insrv nginx || { echo "test nginx does not start" >&2; exit 2; }
77sleep 0.5
78cli() { ip netns exec "$CLI" "$@"; }
79
80# N1: negative control, live firewall copy: public http is dropped (today's outside timeout)
81cli curl -sS -o /dev/null -m 4 -H 'Host: interplane.io' http://192.168.1.108/ 2>/dev/null; rc=$?
82[ "$rc" = 28 ] && ok "N1 live firewall: http://192.168.1.108/ times out (rc 28), as outside visitors saw" || bad "N1 want timeout rc 28, got rc $rc"
83insrv curl -sS -m 3 http://127.0.0.1/ 2>/dev/null | grep -q 'apache2 stand-in' && ok "N1 stand-in holds *:80 on loopback" || bad "N1 stand-in not answering on loopback"
84
85CHECKS="EDGE_LAN_NETNS=$CLI EDGE_SKIP_HTTPS=1 bash $INFRA/spark/edge-http-checks.sh"
86RELOAD="nginx -s reload"
87# apply
88out=$(insrv env EDGE_RELOAD="$RELOAD" EDGE_CHECKS="$CHECKS" bash "$INFRA/spark/edge-http-apply.sh" "$INFRA" 2>&1); rc=$?
89echo "$out" | sed 's/^/ apply| /'
90[ "$rc" = 0 ] && echo "$out" | grep -q 'APPLIED' && ok "apply rc 0, APPLIED" || bad "apply rc $rc"
91# P1: checks again on their own
92out=$(insrv env EDGE_LAN_NETNS=$CLI EDGE_SKIP_HTTPS=1 bash "$INFRA/spark/edge-http-checks.sh" 2>&1); rc=$?
93[ "$rc" = 0 ] && ok "P1 $(echo "$out" | tail -n 1)" || { echo "$out"; bad "P1 checks rc $rc"; }
94n=$(diff "$T/ingress.orig" "$T/etc-atlas/ingress.nft" | grep -c '^>'); m=$(diff "$T/ingress.orig" "$T/etc-atlas/ingress.nft" | grep -c '^<')
95[ "$n:$m" = "5:0" ] && ok "P1 firewall file = original +5 lines, -0" || bad "P1 firewall diff +$n -$m"
96# M1: mutant, 4480 accepted without "ct original proto-dst 80": the direct-:4480 check must catch it
97sed 's/tcp dport 4480 ct original proto-dst 80 accept/tcp dport 4480 accept/' "$T/etc-atlas/ingress.nft" > "$T/mutant.nft"
98insrv nft -f "$T/mutant.nft"
99out=$(insrv env EDGE_LAN_NETNS=$CLI EDGE_SKIP_HTTPS=1 bash "$INFRA/spark/edge-http-checks.sh" 2>&1); rc=$?
100[ "$rc" != 0 ] && echo "$out" | grep -q 'FAIL \[http80\] direct :4480' && ok "M1 mutant caught: $(echo "$out" | grep 'FAIL' | head -n 1)" || bad "M1 mutant not caught (rc $rc)"
101insrv nft -f /etc/atlas/ingress.nft
102# N2: a second apply is refused and changes nothing
103cp -p "$T/etc-atlas/ingress.nft" "$T/ingress.applied"
104out=$(insrv env EDGE_RELOAD="$RELOAD" EDGE_CHECKS="$CHECKS" bash "$INFRA/spark/edge-http-apply.sh" "$INFRA" 2>&1); rc=$?
105[ "$rc" != 0 ] && cmp -s "$T/ingress.applied" "$T/etc-atlas/ingress.nft" && ok "N2 re-apply refused (rc $rc: $(echo "$out" | tail -n 1)), nothing changed" || bad "N2 re-apply rc $rc"
106# rollback
107bk=$(ls -1d "$T/etc-nginx/forge-backups"/http80-* | tail -n 1)
108out=$(insrv env EDGE_RELOAD="$RELOAD" bash "$INFRA/spark/edge-http-rollback.sh" "/etc/nginx/forge-backups/$(basename "$bk")" 2>&1); rc=$?
109echo "$out" | sed 's/^/ rollback| /'
110[ "$rc" = 0 ] && ok "rollback rc 0" || bad "rollback rc $rc"
111# P2: back to the original
112cmp -s "$T/ingress.orig" "$T/etc-atlas/ingress.nft" && ok "P2 firewall file byte-identical to the original" || bad "P2 firewall file differs"
113[ ! -e "$T/etc-nginx/sites-enabled/http-redirect" ] && ok "P2 redirect site removed" || bad "P2 redirect site still there"
114sleep 0.5
115insrv ss -ltnH 'sport = :4480' | grep -q . && bad "P2 4480 still listening" || ok "P2 4480 no longer listening"
116cli curl -sS -o /dev/null -m 4 -H 'Host: interplane.io' http://192.168.1.108/ 2>/dev/null; rc=$?
117[ "$rc" = 28 ] && ok "P2 http://192.168.1.108/ times out again (rc 28)" || bad "P2 want rc 28, got $rc"
118insrv curl -sS -m 3 http://127.0.0.1/ 2>/dev/null | grep -q 'apache2 stand-in' && ok "P2 stand-in still holds *:80" || bad "P2 stand-in gone"
119
120# N3: the reload command itself fails. Apply must roll back, and that rollback must say the
121# second reload failed instead of claiming nginx was reloaded.
122out=$(insrv env EDGE_RELOAD=false bash "$INFRA/spark/edge-http-apply.sh" "$INFRA" 2>&1); rc=$?
123echo "$out" | sed 's/^/ n3| /'
124[ "$rc" != 0 ] && printf '%s\n' "$out" | grep -q 'rolling back' && printf '%s\n' "$out" | grep -q 'ROLLBACK FAILED' && printf '%s\n' "$out" | grep -q 'nginx reload failed after the redirect site was removed' && ok "N3 reload failure is reported, not claimed as success (rc $rc)" || bad "N3 reload failure rc $rc, rollback not reported"
125cmp -s "$T/ingress.orig" "$T/etc-atlas/ingress.nft" && ok "N3 firewall file still the original" || bad "N3 firewall file changed"
126[ ! -e "$T/etc-nginx/sites-enabled/http-redirect" ] && ok "N3 redirect site not left installed" || bad "N3 redirect site left installed"
127insrv curl -sS -m 3 http://127.0.0.1/ 2>/dev/null | grep -q 'apache2 stand-in' && ok "N3 stand-in still holds *:80" || bad "N3 stand-in gone"
128# N4: the reload command fails once, then succeeds. Apply must roll back, the rollback reload
129# must succeed, and the redirect site must not stay installed.
130cat > "$T/reload-once.sh" << 'EOF'
131#!/bin/sh
132flag=$1
133if [ -f "$flag" ]; then
134 exec nginx -s reload
135fi
136touch "$flag"
137exit 1
138EOF
139chmod 755 "$T/reload-once.sh"
140rm -f "$T/reload-once.flag"
141out=$(insrv env EDGE_RELOAD="$T/reload-once.sh $T/reload-once.flag" bash "$INFRA/spark/edge-http-apply.sh" "$INFRA" 2>&1); rc=$?
142echo "$out" | sed 's/^/ n4| /'
143[ "$rc" != 0 ] && printf '%s\n' "$out" | grep -q 'rolling back' && printf '%s\n' "$out" | grep -q 'nginx reload failed' && ! printf '%s\n' "$out" | grep -q 'ROLLBACK FAILED' && ok "N4 failed reload rolled back, rollback reload succeeded (rc $rc)" || bad "N4 failed reload rc $rc"
144cmp -s "$T/ingress.orig" "$T/etc-atlas/ingress.nft" && ok "N4 firewall file still the original" || bad "N4 firewall file changed"
145[ ! -e "$T/etc-nginx/sites-enabled/http-redirect" ] && ok "N4 redirect site not left installed" || bad "N4 redirect site left installed"
146insrv ss -ltnH 'sport = :4480' | grep -q . && bad "N4 4480 still listening" || ok "N4 4480 not listening"
147
148echo "http80-netns: $pass passed, $fail failed"
149[ "$fail" = 0 ]