interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

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

130 lines · 7230 bytes Raw Blame History
1#!/usr/bin/env bash
2# The per-path body limit of the forge edge for the git patch upload, WITHOUT touching a live
3# nginx: for each site copy (hub infra/nginx/sites-enabled/interplane.io, Spark
4# infra/spark/nginx/sites-interplane.io) with the shared infra/nginx conf.d and snippets, paths
5# rewritten into a temp dir, it runs `nginx -t -c` on the scratch copy, then starts a throwaway nginx
6# whose only listener is one loopback port (EDGE_PATCH_PORT, default 18488: the site's 4444 listen
7# is dropped and 4448 becomes that port, ssl + proxy_protocol) and whose upstream is a stand-in on a
8# Unix socket that answers 200 and the body length it received (a test oracle, not forged).
9# Checks per copy: POST /<owner>/<repo>/patches/new passes 3145728 bytes and is refused 413 at
10# 3145729; POST to every other path tried (/login, /<owner>/<repo>/issues/new,
11# /<owner>/<repo>/patches/new2, /<a>/<b>/<c>/patches/new) passes 65536 bytes and is refused 413 at
12# 65537. Control: the files of CONTROL (default 2dc1deda, before this limit) refuse the
13# 3145728-byte upload with 413.
14# Usage: bash infra/test/edge-patch-limit.sh [CONTROL] (exit 0 = every check passed)
15set -u
16cd "$(dirname "$0")/../.."
17CONTROL=${1:-2dc1deda63fe50cbd7665495b51706f0814e79d4}
18PORT=${EDGE_PATCH_PORT:-18488}
19T=$(mktemp -d "${TMPDIR:-/tmp}/edge-patch.XXXXXX")
20PASS=0; FAIL=0
21ok() { PASS=$((PASS+1)); echo "PASS $*"; }
22bad() { FAIL=$((FAIL+1)); echo "FAIL $*"; }
23NGPID=; UPPID=
24ngstop() { [ -f "$T/run/nginx.pid" ] && kill "$(cat "$T/run/nginx.pid")" 2>/dev/null; rm -f "$T/run/nginx.pid"; sleep 0.3; }
25cleanup() { ngstop; [ -n "$UPPID" ] && kill "$UPPID" 2>/dev/null; [ -n "${KEEP:-}" ] || rm -rf "$T"; }
26trap cleanup EXIT
27[ -z "$(ss -ltnH "sport = :$PORT")" ] || { echo "port $PORT is taken - refusing to test"; exit 2; }
28
29mkdir -p "$T/certs" "$T/www"
30openssl req -x509 -newkey rsa:2048 -nodes -days 1 -subj "/CN=interplane.io" -addext "subjectAltName=DNS:interplane.io" \
31 -keyout "$T/certs/key.pem" -out "$T/certs/cert.pem" > /dev/null 2>&1 || { echo "openssl failed"; exit 2; }
32echo STATIC > "$T/www/index.html"
33for n in 65536 65537 3145728 3145729; do head -c "$n" /dev/zero | tr '\0' a > "$T/body.$n"; done
34
35# The stand-in upstream: one HTTP/1.0 request per connection, answers "got <body bytes> <path>".
36cat > "$T/up.py" <<'EOF'
37import socketserver, sys
38class H(socketserver.StreamRequestHandler):
39 def handle(self):
40 line = self.rfile.readline().decode('latin-1').split()
41 n = 0
42 while True:
43 h = self.rfile.readline()
44 if h in (b'\r\n', b'\n', b''): break
45 k, _, v = h.decode('latin-1').partition(':')
46 if k.strip().lower() == 'content-length': n = int(v.strip())
47 got = len(self.rfile.read(n)) if n else 0
48 body = ('got %d %s\n' % (got, line[1] if len(line) > 1 else '?')).encode()
49 self.wfile.write(b'HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\nContent-Length: %d\r\n\r\n' % len(body) + body)
50socketserver.ThreadingUnixStreamServer(sys.argv[1], H).serve_forever()
51EOF
52python3 "$T/up.py" "$T/up.sock" & UPPID=$!
53for i in $(seq 50); do [ -S "$T/up.sock" ] && break; sleep 0.1; done
54[ -S "$T/up.sock" ] || { echo "stand-in upstream did not start"; exit 2; }
55
56# file REV PATH: PATH at REV ("tree" = the working tree)
57file() { if [ "$1" = tree ]; then cat "$2"; else git show "$1:$2"; fi; }
58# setup REV SITE: the scratch configuration of SITE at REV in $T/c
59setup() {
60 rm -rf "$T/c" "$T/run"; mkdir -p "$T/c/snippets" "$T/c/conf.d" "$T/run/cache" "$T/run/tmp"
61 rw() { sed -e "s#/etc/nginx/snippets/#$T/c/snippets/#g" -e "s#/var/cache/nginx/forge#$T/run/cache#g" \
62 -e "s#server 127\.0\.0\.1:18480 #server unix:$T/up.sock #"; }
63 for s in forge-proxy forge-headers forge-archive; do file "$1" "infra/nginx/snippets/$s.conf" | rw > "$T/c/snippets/$s.conf"; done
64 file "$1" infra/nginx/snippets/bazinga-git.conf 2>/dev/null | rw > "$T/c/snippets/bazinga-git.conf"
65 file "$1" infra/nginx/conf.d/forge.conf | rw > "$T/c/conf.d/forge.conf"
66 file "$1" "$2" | rw | sed -e '/listen 127\.0\.0\.1:4444 ssl/d' \
67 -e "s#listen 127\.0\.0\.1:4448 ssl http2 proxy_protocol;#listen 127.0.0.1:$PORT ssl proxy_protocol;#" \
68 -e "s#/etc/nginx/certs/interplane.io/fullchain.pem#$T/certs/cert.pem#" \
69 -e "s#/etc/nginx/certs/interplane.io/key.pem#$T/certs/key.pem#" \
70 -e "s#root /var/www/interplane.io;#root $T/www;#" > "$T/c/site"
71 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" \
72 && ! grep -q 'listen 127.0.0.1:44' "$T/c/site" && ! grep -rqh '^[^#]*/etc/nginx/' "$T/c" || { echo "rewrite of $2 at $1 failed"; exit 2; }
73 cat > "$T/c/nginx.conf" <<EOF
74worker_processes 1;
75pid $T/run/nginx.pid;
76error_log $T/run/error.log;
77events { worker_connections 64; }
78http {
79 client_body_temp_path $T/run/tmp/body;
80 proxy_temp_path $T/run/tmp/proxy;
81 fastcgi_temp_path $T/run/tmp/fastcgi;
82 uwsgi_temp_path $T/run/tmp/uwsgi;
83 scgi_temp_path $T/run/tmp/scgi;
84 access_log off;
85 include $T/c/conf.d/forge.conf;
86 include $T/c/site;
87}
88EOF
89}
90# post PATH BYTES: the status of a POST of BYTES bytes to PATH (PROXY header from 127.0.0.1)
91post() {
92 curl -sk --haproxy-protocol --max-time 30 --resolve "interplane.io:$PORT:127.0.0.1" -o "$T/resp" -w '%{http_code}' \
93 --data-binary "@$T/body.$2" "https://interplane.io:$PORT$1"
94}
95# expect LABEL PATH BYTES STATUS: 200 must be the stand-in's answer with every byte received
96expect() {
97 local c; c=$(post "$2" "$3")
98 if [ "$c" = "$4" ] && { [ "$4" != 200 ] || [ "$(cat "$T/resp")" = "got $3 $2" ]; }; then ok "$1: POST $2 with $3 bytes -> $c"
99 else bad "$1: POST $2 with $3 bytes: want $4, got $c $(head -c 120 "$T/resp" | tr '\n' ' ')"; fi
100}
101# run REV SITE LABEL: nginx -t, then start and leave running
102run() {
103 setup "$1" "$2"
104 if nginx -t -c "$T/c/nginx.conf" -p "$T/run/" -e "$T/run/error.log" > "$T/t.out" 2>&1; then
105 ok "$3: nginx -t -c (scratch copy of $2 at $1): $(grep -o 'syntax is ok' "$T/t.out"), $(grep -o 'test is successful' "$T/t.out")"
106 else bad "$3: nginx -t: $(tr '\n' ' ' < "$T/t.out")"; return 1; fi
107 nginx -c "$T/c/nginx.conf" -p "$T/run/" -e "$T/run/error.log" || { bad "$3: nginx did not start: $(tail -3 "$T/run/error.log")"; return 1; }
108 for i in $(seq 50); do [ -n "$(ss -ltnH "sport = :$PORT")" ] && return 0; sleep 0.1; done
109 bad "$3: nginx is not listening on $PORT"; return 1
110}
111
112nginx -v 2>&1
113for site in infra/nginx/sites-enabled/interplane.io infra/spark/nginx/sites-interplane.io; do
114 L=${site#infra/}
115 if run tree "$site" "$L"; then
116 expect "$L patch upload at the limit" /drakestapleton/fixrepo/patches/new 3145728 200
117 expect "$L patch upload over the limit" /drakestapleton/fixrepo/patches/new 3145729 413
118 for p in /login /drakestapleton/fixrepo/issues/new /drakestapleton/fixrepo/patches/new2 /a/b/c/patches/new; do
119 expect "$L other path at 64k" "$p" 65536 200
120 expect "$L other path over 64k" "$p" 65537 413
121 done
122 fi
123 ngstop
124done
125if run "$CONTROL" infra/nginx/sites-enabled/interplane.io "control nginx/sites-enabled/interplane.io"; then
126 expect "control $CONTROL (64k everywhere)" /drakestapleton/fixrepo/patches/new 3145728 413
127fi
128ngstop
129echo "EDGE-PATCH: pass=$PASS fail=$FAIL"
130[ "$FAIL" = 0 ]