#!/usr/bin/env bash # ============================================================================= # edge_test.sh — what the public edge does with a fixed set of requests. # # WHAT IT DOES # Sends each request below to the edge and prints one line per request # (status and Location, or one response header). Two kinds of line: # ok / FAIL the privacy boundary and the gates, which have a fixed right # answer: the /notes/ denials, /py/internal/, every gated # prefix. Any FAIL makes the exit status 1. # show / hdr everything else (redirects, headers, carve-outs, uploads), # whose answer depends on the upstream's data. Compare two runs # instead (below). # # USAGE # caddy/edge_test.sh [curl options...] # base-url the www origin, e.g. https://www.example.com # curl options added to every request: --resolve to reach a given # container, -k for dev's internal CA. # Environment, all optional: # EDGE_APEX the apex origin (production only): adds the apex cases. # EDGE_HTTP the plain-http apex origin (production only), e.g. # http://example.com: adds the http -> https case. # EDGE_COOKIE an admin session's Cookie header value: adds the # signed-in cases (each gated prefix must then NOT redirect # to the login). # EDGE_UPLOAD the name of a file under /uploads/ that exists. # # On stpvm, pin the hostname to this machine, or curl goes out over the # internet and back (and hangs): # caddy/edge_test.sh https://www.$DOMAIN --resolve www.$DOMAIN:443:127.0.0.1 # Dev stack: # caddy/edge_test.sh https://localhost -k # # COMPARING TWO EDGES (the nginx -> Caddy cutover) # Run it against each and diff the output: # caddy/edge_test.sh https://www.$DOMAIN --resolve www.$DOMAIN:443:127.0.0.1 >nginx.txt # caddy/edge_test.sh https://www.$DOMAIN:8443 --resolve www.$DOMAIN:8443:127.0.0.1 >caddy.txt # diff nginx.txt caddy.txt # Every remaining difference should be one that was intended. # # NOTHING HERE WRITES. Every request is a GET, apart from one POST to # /py/internal/, which never gets past the edge. # ============================================================================= set -u if [ $# -lt 1 ]; then echo "usage: $0 [curl options...]" >&2 exit 2 fi BASE=${1%/} shift CURL_OPTS=("$@") FAILED=0 # The www host, without scheme or port. BASE_HOST=$(printf '%s' "$BASE" | sed -E 's#^[a-z]+://([^/:]+).*#\1#') # " " for one request. Arguments after the URL are extra # curl options (method, headers, --path-as-is). A redirect to the www host is # shown as its path alone: nginx sends an absolute Location built from its # own port, Caddy a relative one, and either way the browser lands on the # same page. probe() { local url=$1 out shift out=$(curl -sS -o /dev/null --max-time 15 -w '%{http_code} %{redirect_url}' \ "${CURL_OPTS[@]}" "$@" "$url" 2>/dev/null) || out=ERR printf '%s' "$out" | sed -E "s#^([0-9]+) [a-z]+://$BASE_HOST(:[0-9]+)?/#\\1 /#" } # The first value of one response header, or "-". header_of() { local name=$1 url=$2 value shift 2 value=$(curl -sS -o /dev/null -D - --max-time 15 "${CURL_OPTS[@]}" "$@" "$url" 2>/dev/null | tr -d '\r' | grep -i "^$name:" | head -n 1 | cut -d: -f2- | sed 's/^ //') printf '%s' "${value:--}" } # must