interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / infra / test / edge-patch-limit.sh
| 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) |
| 15 | set -u |
| 16 | cd "$(dirname "$0")/../.." |
| 17 | CONTROL=${1:-2dc1deda63fe50cbd7665495b51706f0814e79d4} |
| 18 | PORT=${EDGE_PATCH_PORT:-18488} |
| 19 | T=$(mktemp -d "${TMPDIR:-/tmp}/edge-patch.XXXXXX") |
| 20 | PASS=0; FAIL=0 |
| 21 | ok() { PASS=$((PASS+1)); echo "PASS $*"; } |
| 22 | bad() { FAIL=$((FAIL+1)); echo "FAIL $*"; } |
| 23 | NGPID=; UPPID= |
| 24 | ngstop() { [ -f "$T/run/nginx.pid" ] && kill "$(cat "$T/run/nginx.pid")" 2>/dev/null; rm -f "$T/run/nginx.pid"; sleep 0.3; } |
| 25 | cleanup() { ngstop; [ -n "$UPPID" ] && kill "$UPPID" 2>/dev/null; [ -n "${KEEP:-}" ] || rm -rf "$T"; } |
| 26 | trap cleanup EXIT |
| 27 | [ -z "$(ss -ltnH "sport = :$PORT")" ] || { echo "port $PORT is taken - refusing to test"; exit 2; } |
| 28 | |
| 29 | mkdir -p "$T/certs" "$T/www" |
| 30 | openssl 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; } |
| 32 | echo STATIC > "$T/www/index.html" |
| 33 | for 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>". |
| 36 | cat > "$T/up.py" <<'EOF' |
| 37 | import socketserver, sys |
| 38 | class 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) |
| 50 | socketserver.ThreadingUnixStreamServer(sys.argv[1], H).serve_forever() |
| 51 | EOF |
| 52 | python3 "$T/up.py" "$T/up.sock" & UPPID=$! |
| 53 | for 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) |
| 57 | file() { 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 |
| 59 | setup() { |
| 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 |
| 74 | worker_processes 1; |
| 75 | pid $T/run/nginx.pid; |
| 76 | error_log $T/run/error.log; |
| 77 | events { worker_connections 64; } |
| 78 | http { |
| 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 | } |
| 88 | EOF |
| 89 | } |
| 90 | # post PATH BYTES: the status of a POST of BYTES bytes to PATH (PROXY header from 127.0.0.1) |
| 91 | post() { |
| 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 |
| 96 | expect() { |
| 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 |
| 102 | run() { |
| 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 | |
| 112 | nginx -v 2>&1 |
| 113 | for 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 |
| 124 | done |
| 125 | if 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 |
| 127 | fi |
| 128 | ngstop |
| 129 | echo "EDGE-PATCH: pass=$PASS fail=$FAIL" |
| 130 | [ "$FAIL" = 0 ] |