Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/interplane-forge/blob/main/infra/test/http80-netns.sh # interplane-forge INTERPLANE Forge Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers 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 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. 15 set -u 16 [ "$(id -u)" = 0 ] || { echo "run as root (sudo)" >&2; exit 2; } 17 SCR=$(cd "${1:?usage: http80-netns.sh }" && pwd) || { echo "scratch dir not usable" >&2; exit 2; } 18 INFRA=$(cd "$(dirname "$0")/.." && pwd) 19 T=$SCR/http80-netns.$$ 20 SRV=ipl80-srv CLI=ipl80-cli 21 pass=0; fail=0 22 ok() { pass=$((pass+1)); echo "PASS $*"; } 23 bad() { fail=$((fail+1)); echo "FAIL $*"; } 24 own_srv=0 25 own_cli=0 26 own_scratch=0 27 # Refuse before installing the EXIT trap. A second run must not kill the first run's processes. 28 ip netns list | grep -qE "^($SRV|$CLI)( |$)" && { echo "netns $SRV/$CLI already exist; refusing" >&2; exit 2; } 29 cleanup() { 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 } 39 trap cleanup EXIT 40 mkdir -p "$T/etc-nginx/sites-enabled" "$T/etc-atlas" "$T/log" "$T/lib" || { echo "cannot create $T" >&2; exit 2; } 41 own_scratch=1 42 install -m 600 /etc/atlas/ingress.nft "$T/etc-atlas/ingress.nft" 43 cp -p "$T/etc-atlas/ingress.nft" "$T/ingress.orig" 44 cat > "$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) 46 worker_processes 1; 47 pid /var/lib/nginx/nginx.pid; 48 error_log /var/log/nginx/error.log; 49 events { worker_connections 64; } 50 http { 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 } 60 EOF 61 62 ip netns add "$SRV" && own_srv=1 || { echo "cannot create netns $SRV" >&2; exit 2; } 63 ip netns add "$CLI" && own_cli=1 || { echo "cannot create netns $CLI" >&2; exit 2; } 64 ip link add enP7s7 netns "$SRV" type veth peer name cli0 netns "$CLI" 65 ip -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 66 ip -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 69 insrv() { 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 } 75 insrv nft -f /etc/atlas/ingress.nft || { echo "copy of live firewall does not load in the netns" >&2; exit 2; } 76 insrv nginx || { echo "test nginx does not start" >&2; exit 2; } 77 sleep 0.5 78 cli() { ip netns exec "$CLI" "$@"; } 79 80 # N1: negative control, live firewall copy: public http is dropped (today's outside timeout) 81 cli 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" 83 insrv 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 85 CHECKS="EDGE_LAN_NETNS=$CLI EDGE_SKIP_HTTPS=1 bash $INFRA/spark/edge-http-checks.sh" 86 RELOAD="nginx -s reload" 87 # apply 88 out=$(insrv env EDGE_RELOAD="$RELOAD" EDGE_CHECKS="$CHECKS" bash "$INFRA/spark/edge-http-apply.sh" "$INFRA" 2>&1); rc=$? 89 echo "$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 92 out=$(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"; } 94 n=$(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 97 sed 's/tcp dport 4480 ct original proto-dst 80 accept/tcp dport 4480 accept/' "$T/etc-atlas/ingress.nft" > "$T/mutant.nft" 98 insrv nft -f "$T/mutant.nft" 99 out=$(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)" 101 insrv nft -f /etc/atlas/ingress.nft 102 # N2: a second apply is refused and changes nothing 103 cp -p "$T/etc-atlas/ingress.nft" "$T/ingress.applied" 104 out=$(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 107 bk=$(ls -1d "$T/etc-nginx/forge-backups"/http80-* | tail -n 1) 108 out=$(insrv env EDGE_RELOAD="$RELOAD" bash "$INFRA/spark/edge-http-rollback.sh" "/etc/nginx/forge-backups/$(basename "$bk")" 2>&1); rc=$? 109 echo "$out" | sed 's/^/ rollback| /' 110 [ "$rc" = 0 ] && ok "rollback rc 0" || bad "rollback rc $rc" 111 # P2: back to the original 112 cmp -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" 114 sleep 0.5 115 insrv ss -ltnH 'sport = :4480' | grep -q . && bad "P2 4480 still listening" || ok "P2 4480 no longer listening" 116 cli 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" 118 insrv 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. 122 out=$(insrv env EDGE_RELOAD=false bash "$INFRA/spark/edge-http-apply.sh" "$INFRA" 2>&1); rc=$? 123 echo "$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" 125 cmp -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" 127 insrv 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. 130 cat > "$T/reload-once.sh" << 'EOF' 131 #!/bin/sh 132 flag=$1 133 if [ -f "$flag" ]; then 134 exec nginx -s reload 135 fi 136 touch "$flag" 137 exit 1 138 EOF 139 chmod 755 "$T/reload-once.sh" 140 rm -f "$T/reload-once.flag" 141 out=$(insrv env EDGE_RELOAD="$T/reload-once.sh $T/reload-once.flag" bash "$INFRA/spark/edge-http-apply.sh" "$INFRA" 2>&1); rc=$? 142 echo "$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" 144 cmp -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" 146 insrv ss -ltnH 'sport = :4480' | grep -q . && bad "N4 4480 still listening" || ok "N4 4480 not listening" 147 148 echo "http80-netns: $pass passed, $fail failed" 149 [ "$fail" = 0 ]