interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / infra / test / edge-bundle-limit.sh

253 lines · 16827 bytes Raw Blame History
1#!/usr/bin/env bash
2# The download limit of the forge edge for the bundle of an imported proposal
3# (GET /<owner>/<repo>/patches/<id>.bundle), WITHOUT touching a live nginx. As
4# infra/test/edge-patch-limit.sh does: for each site copy (hub infra/nginx/sites-enabled/interplane.io,
5# Spark infra/spark/nginx/sites-interplane.io) with the shared infra/nginx conf.d and snippets of the
6# working tree, paths rewritten into a temp dir, `nginx -t -c` on the scratch copy, then a throwaway
7# nginx whose only listener is one loopback port (EDGE_BUNDLE_PORT, default 19374; the site's 4444
8# listen is dropped and 4448 becomes that port, ssl + proxy_protocol) and whose upstream is a stand-in
9# on a Unix socket (a test oracle, not forged). The stand-in answers "got <path> base=<X-Forge-Base>
10# ip=<X-Real-IP>"; a request whose query has hold=1 is held until the test removes $T/hold (a slow
11# download), and csp=sandbox makes it send forged's raw sandbox policy.
12# Clients: every request carries a PROXY v1 header (curl --haproxy-protocol --haproxy-clientip ADDR)
13# and the scratch listener has proxy_protocol, so $proxy_protocol_addr, the key of the live zone
14# forge_dl (conf.d/forge.conf), is ADDR: the zones are keyed exactly as live. Each 429 is matched to
15# its zone by nginx's error log line `limiting connections by zone "forge_dl"` (or "forge_dl_all"),
16# so a 429 of limit_req is never taken for the download limit.
17# Checks per copy:
18# 1. client A holds two .bundle downloads; its third .bundle answers 429 (zone forge_dl) and never
19# reaches the upstream; client B's .bundle proceeds meanwhile; A's .patch, .diff and page of the
20# same id, three at once while A still holds two downloads, all answer 200 from @forged
21# (X-Forge-Cache set); after the release every held download is 200 with X-Forge-Base / and the
22# client's address, no X-Forge-Cache, and A's next .bundle is 200 again
23# 2. six downloads held over three addresses: a fourth address gets 429 (zone forge_dl_all)
24# 3. the archive location still limits as before (two held, the third 429); a bundle and an archive
25# share the client's two slots (one of each held: a third of either is 429)
26# 4. CSP: a .bundle without forged's policy, its 429 included, gets the edge's sandbox fallback;
27# with forged's sandbox policy the edge adds none; .diff keeps the page policy
28# 5. paths that are not a bundle stay in @forged: 15 hex digits, upper-case hex, .bundle/x, a third
29# path segment
30# Control (must fail): the tree's site file without the bundle location: A's third .bundle is not
31# refused and answers 200 after the release, so check 1 fails there.
32# Usage: bash infra/test/edge-bundle-limit.sh (exit 0 = every check passed; KEEP=1 keeps the temp dir)
33set -u
34cd "$(dirname "$0")/../.."
35PORT=${EDGE_BUNDLE_PORT:-19374}
36T=$(mktemp -d "${TMPDIR:-/tmp}/edge-bundle.XXXXXX")
37PASS=0; FAIL=0
38ok() { PASS=$((PASS+1)); echo "PASS $*"; }
39bad() { FAIL=$((FAIL+1)); echo "FAIL $*"; }
40UPPID=
41ngstop() { [ -f "$T/run/nginx.pid" ] && kill "$(cat "$T/run/nginx.pid")" 2>/dev/null; rm -f "$T/run/nginx.pid"; sleep 0.3; }
42cleanup() { rm -f "$T/hold"; ngstop; [ -n "$UPPID" ] && kill "$UPPID" 2>/dev/null; wait 2>/dev/null; [ -n "${KEEP:-}" ] || rm -rf "$T"; }
43trap cleanup EXIT
44[ -z "$(ss -ltnH "sport = :$PORT")" ] || { echo "port $PORT is taken - refusing to test"; exit 2; }
45
46mkdir -p "$T/certs" "$T/www" "$T/r"
47openssl req -x509 -newkey rsa:2048 -nodes -days 1 -subj "/CN=interplane.io" -addext "subjectAltName=DNS:interplane.io" \
48 -keyout "$T/certs/key.pem" -out "$T/certs/cert.pem" > /dev/null 2>&1 || { echo "openssl failed"; exit 2; }
49echo STATIC > "$T/www/index.html"
50
51# The stand-in upstream: one HTTP/1.0 request per connection; every arrival is logged as "<ip> <path>".
52cat > "$T/up.py" <<'EOF'
53import os, socketserver, sys, time
54sock, arrivals, hold = sys.argv[1], sys.argv[2], sys.argv[3]
55class H(socketserver.StreamRequestHandler):
56 def handle(self):
57 line = self.rfile.readline().decode('latin-1').split()
58 hdr = {}
59 while True:
60 h = self.rfile.readline()
61 if h in (b'\r\n', b'\n', b''): break
62 k, _, v = h.decode('latin-1').partition(':')
63 hdr[k.strip().lower()] = v.strip()
64 target = line[1] if len(line) > 1 else '?'
65 path, _, query = target.partition('?')
66 with open(arrivals, 'a') as f:
67 f.write('%s %s\n' % (hdr.get('x-real-ip', '-'), target))
68 if 'hold=1' in query.split('&'):
69 end = time.time() + 30
70 while os.path.exists(hold) and time.time() < end:
71 time.sleep(0.05)
72 body = ('got %s base=%s ip=%s\n' % (path, hdr.get('x-forge-base', '-'), hdr.get('x-real-ip', '-'))).encode()
73 csp = b"Content-Security-Policy: sandbox; default-src 'none'\r\n" if 'csp=sandbox' in query.split('&') else b''
74 self.wfile.write(b'HTTP/1.0 200 OK\r\nContent-Type: application/octet-stream\r\n' + csp +
75 b'Content-Length: %d\r\n\r\n' % len(body) + body)
76socketserver.ThreadingUnixStreamServer.daemon_threads = True
77socketserver.ThreadingUnixStreamServer(sock, H).serve_forever()
78EOF
79: > "$T/arrivals"
80python3 "$T/up.py" "$T/up.sock" "$T/arrivals" "$T/hold" & UPPID=$!
81for i in $(seq 50); do [ -S "$T/up.sock" ] && break; sleep 0.1; done
82[ -S "$T/up.sock" ] || { echo "stand-in upstream did not start"; exit 2; }
83
84# setup SITEFILE: the scratch configuration of SITEFILE with this tree's conf.d and snippets
85setup() {
86 rm -rf "$T/c" "$T/run"; mkdir -p "$T/c/snippets" "$T/c/conf.d" "$T/run/cache" "$T/run/tmp"
87 rw() { sed -e "s#/etc/nginx/snippets/#$T/c/snippets/#g" -e "s#/var/cache/nginx/forge#$T/run/cache#g" \
88 -e "s#server 127\.0\.0\.1:18480 #server unix:$T/up.sock #"; }
89 for s in forge-proxy forge-headers forge-archive bazinga-git; do rw < "infra/nginx/snippets/$s.conf" > "$T/c/snippets/$s.conf"; done
90 rw < infra/nginx/conf.d/forge.conf > "$T/c/conf.d/forge.conf"
91 rw < "$1" | sed -e '/listen 127\.0\.0\.1:4444 ssl/d' \
92 -e "s#listen 127\.0\.0\.1:4448 ssl http2 proxy_protocol;#listen 127.0.0.1:$PORT ssl proxy_protocol;#" \
93 -e "s#/etc/nginx/certs/interplane.io/fullchain.pem#$T/certs/cert.pem#" \
94 -e "s#/etc/nginx/certs/interplane.io/key.pem#$T/certs/key.pem#" \
95 -e "s#root /var/www/interplane.io;#root $T/www;#" > "$T/c/site"
96 grep -q "unix:$T/up.sock" "$T/c/conf.d/forge.conf" && grep -q "listen 127.0.0.1:$PORT ssl proxy_protocol;" "$T/c/site" \
97 && ! grep -q 'listen 127.0.0.1:44' "$T/c/site" && ! grep -rqh '^[^#]*/etc/nginx/' "$T/c" || { echo "rewrite of $1 failed"; exit 2; }
98 cat > "$T/c/nginx.conf" <<EOF
99worker_processes 1;
100pid $T/run/nginx.pid;
101error_log $T/run/error.log;
102events { worker_connections 64; }
103http {
104 client_body_temp_path $T/run/tmp/body;
105 proxy_temp_path $T/run/tmp/proxy;
106 fastcgi_temp_path $T/run/tmp/fastcgi;
107 uwsgi_temp_path $T/run/tmp/uwsgi;
108 scgi_temp_path $T/run/tmp/scgi;
109 access_log off;
110 include $T/c/conf.d/forge.conf;
111 include $T/c/site;
112}
113EOF
114}
115# run SITEFILE LABEL: nginx -t, then start and leave running
116run() {
117 setup "$1"
118 if nginx -t -c "$T/c/nginx.conf" -p "$T/run/" -e "$T/run/error.log" > "$T/t.out" 2>&1; then
119 ok "$2: nginx -t -c (scratch copy of $1): $(grep -o 'syntax is ok' "$T/t.out"), $(grep -o 'test is successful' "$T/t.out")"
120 else bad "$2: nginx -t: $(tr '\n' ' ' < "$T/t.out")"; return 1; fi
121 nginx -c "$T/c/nginx.conf" -p "$T/run/" -e "$T/run/error.log" || { bad "$2: nginx did not start: $(tail -3 "$T/run/error.log")"; return 1; }
122 for i in $(seq 50); do [ -n "$(ss -ltnH "sport = :$PORT")" ] && return 0; sleep 0.1; done
123 bad "$2: nginx is not listening on $PORT"; return 1
124}
125
126O=/drakestapleton/fixrepo
127ID=0123456789abcdef
128BUN=$O/patches/$ID.bundle
129ARC=$O/archive/refs/heads/main.zip
130N=0
131# req NAME ADDR PATH: start a GET of PATH from client ADDR in the background (a unique query string,
132# so no cache entry or cache lock joins two requests); answer in $T/r/NAME.{code,h,b}, then NAME.done
133req() {
134 local q; N=$((N+1)); case $3 in *\?*) q="&n=$N";; *) q="?n=$N";; esac
135 rm -f "$T/r/$1".*
136 ( curl -sk --haproxy-protocol --haproxy-clientip "$2" --max-time 60 --resolve "interplane.io:$PORT:127.0.0.1" \
137 -D "$T/r/$1.h" -o "$T/r/$1.b" -w '%{http_code}' "https://interplane.io:$PORT$3$q" > "$T/r/$1.code" 2>/dev/null
138 touch "$T/r/$1.done" ) &
139}
140# fin NAME: wait for it (60 s at most); its status
141fin() { local i; for i in $(seq 600); do [ -e "$T/r/$1.done" ] && break; sleep 0.1; done; cat "$T/r/$1.code" 2>/dev/null; }
142# done1 NAME SECONDS: the status if the request finished within SECONDS, else "held"
143done1() {
144 local i; for i in $(seq $(($2*10))); do [ -e "$T/r/$1.done" ] && { cat "$T/r/$1.code"; return; }; sleep 0.1; done; echo held
145}
146arr() { wc -l < "$T/arrivals"; }
147# waitarr N: the stand-in has seen N requests (10 s at most)
148waitarr() { local i; for i in $(seq 100); do [ "$(arr)" -ge "$1" ] && return 0; sleep 0.1; done; return 1; }
149zlines() { grep -c "limiting connections by zone \"$1\"" "$T/run/error.log"; }
150hdr() { grep -i "^$2:" "$T/r/$1.h" | cut -d' ' -f2- | tr -d '\r'; }
151csps() { grep -ic '^content-security-policy:' "$T/r/$1.h"; }
152# isdl NAME ADDR PATH: a 200 from the new location: the stand-in's answer, X-Forge-Base /, no X-Forge-Cache
153isdl() { fin "$1" > /dev/null; [ "$(cat "$T/r/$1.code")" = 200 ] && [ "$(cat "$T/r/$1.b")" = "got $3 base=/ ip=$2" ] && [ -z "$(hdr "$1" x-forge-cache)" ]; }
154# isfg NAME: a 200 answered through @forged (X-Forge-Cache set)
155isfg() { fin "$1" > /dev/null; [ "$(cat "$T/r/$1.code")" = 200 ] && grep -q '^got ' "$T/r/$1.b" && [ -n "$(hdr "$1" x-forge-cache)" ]; }
156# third NAME ADDR PATH ZONE LABEL: a request that must be refused 429 by ZONE at once, unseen upstream
157third() {
158 local a z c; a=$(arr); z=$(zlines "$4"); req "$1" "$2" "$3"; c=$(done1 "$1" 5); sleep 0.2
159 if [ "$c" = 429 ] && [ "$(arr)" = "$a" ] && [ "$(zlines "$4")" -gt "$z" ]; then ok "$5: GET $3 from $2 -> 429 (zone $4 in the error log), not seen upstream"
160 else bad "$5: GET $3 from $2: want 429 by zone $4, got $c (upstream saw $(( $(arr) - a )) more, zone lines $z -> $(zlines "$4"))"; fi
161}
162release() { rm -f "$T/hold"; }
163
164# suite LABEL SITEFILE: checks 1-5 against a running scratch nginx of SITEFILE
165suite() {
166 local L=$1 a0 n c ok1
167 : > "$T/arrivals"
168 echo "== $L 1. one client's third bundle download"
169 touch "$T/hold"; a0=0
170 req A1 192.0.2.10 "$BUN?hold=1"; req A2 192.0.2.10 "$BUN?hold=1"
171 waitarr 2 && ok "$L: client A's two .bundle downloads reached the upstream and are held" || bad "$L: A's two downloads did not reach the upstream ($(arr))"
172 third A3 192.0.2.10 "$BUN" forge_dl "$L third .bundle from client A"
173 req B1 192.0.2.20 "$BUN?hold=1"
174 waitarr 3 && ok "$L: client B's .bundle reached the upstream while A holds two" || bad "$L: B's .bundle was held back ($(arr) arrivals)"
175 req AP 192.0.2.10 "$O/patches/$ID.patch?hold=1"; req AD 192.0.2.10 "$O/patches/$ID.diff?hold=1"; req AG 192.0.2.10 "$O/patches/$ID?hold=1"
176 waitarr 6 && ok "$L: A's .patch, .diff and page of $ID reached the upstream together while A holds two downloads" \
177 || bad "$L: A's .patch/.diff/page did not all reach the upstream ($(arr) arrivals, want 6)"
178 release
179 ok1=1; for r in A1 A2; do isdl $r 192.0.2.10 "$BUN" || ok1=; done; isdl B1 192.0.2.20 "$BUN" || ok1=
180 [ -n "$ok1" ] && ok "$L: the held downloads (A twice, B) answer 200 with X-Forge-Base / and the client's address, no X-Forge-Cache" \
181 || bad "$L: held downloads: A1 $(cat "$T/r/A1.code") $(cat "$T/r/A1.b"), A2 $(cat "$T/r/A2.code"), B1 $(cat "$T/r/B1.code") $(cat "$T/r/B1.b")"
182 for r in AP AD AG; do fin $r > /dev/null; done
183 isfg AP && isfg AD && isfg AG && ok "$L: A's .patch, .diff and page answer 200 through @forged (X-Forge-Cache $(hdr AD x-forge-cache))" \
184 || bad "$L: A's .patch $(cat "$T/r/AP.code") .diff $(cat "$T/r/AD.code") page $(cat "$T/r/AG.code"), X-Forge-Cache '$(hdr AD x-forge-cache)'"
185 req A4 192.0.2.10 "$BUN"; fin A4 > /dev/null
186 isdl A4 192.0.2.10 "$BUN" && ok "$L: after the release A's next .bundle answers 200 (its slots are free)" || bad "$L: A's next .bundle: $(cat "$T/r/A4.code")"
187
188 echo "== $L 2. six downloads in total"
189 : > "$T/arrivals"; touch "$T/hold"
190 for x in 31 32 33; do req C${x}a 192.0.2.$x "$BUN?hold=1"; req C${x}b 192.0.2.$x "$BUN?hold=1"; done
191 waitarr 6 && ok "$L: six .bundle downloads from three addresses are held at the upstream" || bad "$L: six held downloads: $(arr) arrivals"
192 third D1 192.0.2.40 "$BUN" forge_dl_all "$L seventh download (a fourth address)"
193 release
194 n=0; for x in 31 32 33; do isdl C${x}a 192.0.2.$x "$BUN" && n=$((n+1)); isdl C${x}b 192.0.2.$x "$BUN" && n=$((n+1)); done
195 [ "$n" = 6 ] && ok "$L: the six held downloads answer 200" || bad "$L: $n of the six held downloads answered 200"
196
197 echo "== $L 3. the archive location, and archive + bundle share the client's slots"
198 : > "$T/arrivals"; touch "$T/hold"
199 req G1 192.0.2.50 "$ARC?hold=1"; req G2 192.0.2.50 "$ARC?hold=1"
200 req H1 192.0.2.60 "$ARC?hold=1"; req H2 192.0.2.60 "$BUN?hold=1"
201 waitarr 4 && ok "$L: two archive downloads (client G) and one archive + one bundle (client H) are held" || bad "$L: held archive/bundle: $(arr) arrivals"
202 third G3 192.0.2.50 "$ARC" forge_dl "$L third archive download from client G (as before)"
203 third H3 192.0.2.60 "$BUN" forge_dl "$L .bundle from client H holding an archive and a bundle"
204 third H4 192.0.2.60 "$ARC" forge_dl "$L archive from client H holding an archive and a bundle"
205 release
206 c=; for r in G1 G2 H1; do fin $r > /dev/null; [ "$(cat "$T/r/$r.code")" = 200 ] || c="$c $r"; done
207 isdl H2 192.0.2.60 "$BUN" || c="$c H2"
208 [ -z "$c" ] && ok "$L: the held archive and bundle downloads answer 200" || bad "$L: held downloads not 200:$c"
209
210 echo "== $L 4. Content-Security-Policy"
211 [ "$(csps A1)" = 1 ] && hdr A1 content-security-policy | grep -q "^sandbox; default-src 'none'; img-src 'self'" \
212 && ok "$L: .bundle without forged's policy: one CSP, the edge's sandbox fallback" || bad "$L: .bundle CSP: $(hdr A1 content-security-policy)"
213 [ "$(csps A3)" = 1 ] && hdr A3 content-security-policy | grep -q "^sandbox; default-src 'none'" \
214 && ok "$L: the 429 of a .bundle carries the sandbox fallback" || bad "$L: 429 CSP: $(hdr A3 content-security-policy)"
215 req S1 192.0.2.70 "$BUN?csp=sandbox"; fin S1 > /dev/null
216 [ "$(cat "$T/r/S1.code")" = 200 ] && [ "$(csps S1)" = 1 ] && [ "$(hdr S1 content-security-policy)" = "sandbox; default-src 'none'" ] \
217 && ok "$L: .bundle with forged's sandbox policy: that policy alone, the edge adds none" || bad "$L: .bundle with forged's CSP: $(grep -ic '^content-security-policy' "$T/r/S1.h") headers"
218 [ "$(csps AD)" = 1 ] && hdr AD content-security-policy | grep -q "^default-src 'self'; script-src 'none'" \
219 && ok "$L: .diff without forged's policy keeps the edge page policy" || bad "$L: .diff CSP: $(hdr AD content-security-policy)"
220
221 echo "== $L 5. paths that are not a bundle stay in @forged"
222 for p in "$O/patches/0123456789abcde.bundle" "$O/patches/0123456789ABCDEF.bundle" "$O/patches/$ID.bundle/x" "/a$O/patches/$ID.bundle"; do
223 req NM 192.0.2.80 "$p"; fin NM > /dev/null
224 isfg NM && ok "$L: GET $p -> 200 through @forged" || bad "$L: GET $p: $(cat "$T/r/NM.code"), X-Forge-Cache '$(hdr NM x-forge-cache)'"
225 done
226}
227
228nginx -v 2>&1
229for site in infra/nginx/sites-enabled/interplane.io infra/spark/nginx/sites-interplane.io; do
230 L=${site#infra/}
231 run "$site" "$L" && suite "$L" "$site"
232 release; ngstop
233done
234hub="$T/hub-site"
235spark="$T/spark-site"
236awk 'NF && $0 !~ /include \/etc\/nginx\/snippets\/bazinga-git\.conf;/' infra/nginx/sites-enabled/interplane.io > "$hub"
237awk 'NF' infra/spark/nginx/sites-interplane.io > "$spark"
238cmp -s "$hub" "$spark" && ok "both edges share the same forge routes" || bad "the edge forge routes differ"
239
240echo "== control: the site file without the bundle location (check 1 must fail)"
241awk '/^ # Bundle of an imported proposal/{skip=1} skip&&/^ }$/{skip=0; next} !skip' infra/nginx/sites-enabled/interplane.io > "$T/mutant-site"
242if cmp -s infra/nginx/sites-enabled/interplane.io "$T/mutant-site" || grep -q 'bundle\$"' "$T/mutant-site"; then bad "control: the mutant still has the bundle location"
243elif run "$T/mutant-site" "control (no bundle location)"; then
244 : > "$T/arrivals"; touch "$T/hold"
245 req K1 192.0.2.90 "$BUN?hold=1"; req K2 192.0.2.90 "$BUN?hold=1"; waitarr 2
246 req K3 192.0.2.90 "$BUN?hold=1"; c=$(done1 K3 5); waitarr 3; release
247 [ "$c" = held ] && c=$(fin K3)
248 if [ "$c" = 200 ] && [ "$(arr)" = 3 ]; then ok "control: without the location the third .bundle from one client answers 200 (reached the upstream): check 1 fails there, as it must"
249 else bad "control: without the location the third .bundle answered $c ($(arr) arrivals): the limit does not come from the new location"; fi
250fi
251release; ngstop
252echo "EDGE-BUNDLE: pass=$PASS fail=$FAIL"
253[ "$FAIL" = 0 ]