Files
openwaf/scripts/smoke.sh
T
Pavlo Ratushnyi 39d08eca51 A pillar that stopped reading its table kept answering, and nothing on the surface said so
Measured on the shipped image (openwaf:ci, the head tree), one stand with a real
upstream:

  PUT /admin-api/policy {"streakThreshold":2,"trustProxyHeaders":true} -> 200
  404 404 from 203.0.113.79, third request -> 403   (the threshold is in force)
  04:44:43  DROP TABLE policy_settings, from outside the process
  04:45:2x  GET /admin-api/policy -> still "streakThreshold": 2, answered plainly
            GET /admin-api/state  -> "snapshotAgeSeconds":0 — the refresh "succeeded",
                                     because the tables it also reads were still there
            /healthz 200, /readyz 200, and exactly 1 line in the log
  docker restart -> GET /admin-api/policy -> "streakThreshold": 10,
                                          "trustProxyHeaders": false
  four 404s from 203.0.113.81, fifth -> 404: nothing banned

Both pillars — the policy provider and the guard's snapshot store — catch
everything their read throws, keep the last value they had, and log one line.
Keeping the last good value is the right failure posture: a database restart must
not open the gate. Keeping it unmentioned is not: for a minute the operator's page
showed a threshold that no longer existed anywhere in storage, readiness said
ready, and after a restart the defence silently came back at the default N=10.

Now: EdgePolicyProvider and EdgeStateStore count consecutive failed reads and keep
the last error; /admin-api/state answers snapshotStale (age against the TTL the
process refuses to start without), snapshotRefreshFailures,
snapshotLastRefreshError, policyReloadFailures and policyLastReadAt;
/admin-api/policy carries X-Policy-Reload-Failures and X-Policy-Last-Read
alongside the policy it is enforcing, the same way a capped listing carries its
completeness in headers; /readyz answers 503 while either counter is non-zero,
naming the pillar and the age of the snapshot it is running on; and the console's
summary line goes warn and says "older than the ttl", "snapshot refresh failing
xN", "policy not re-read from storage xN".

Teeth first: PillarFailureVisibilityTests (4) run against the unfixed tree — 4
failed, 0 passed, all four on a missing field (KeyNotFoundException), which is the
point: the numbers did not exist. The refresh test drops country_rules rather than
block_entries on purpose — only one of them is what /readyz probes, so before the
fix that test saw the refresh failing and readiness still answering 200.

Gates: compose-smoke now drops policy_settings from the running stand through a
--volumes-from sidecar, waits for the failure to be counted, asserts the counters,
the 503 and the header, puts the table back and asserts the recovery (+6
assertions; the counters are consecutive, not cumulative, so recovery is part of
the claim). smoke.sh asserts the healthy case with numbers rather than silence
(+3). console-check drives the page against that state answer and requires the
summary line to be marked and to name both pillars (+3); the state body is served
through a route because the guard refreshes the snapshot on every request — a live
stand cannot be left to age — while the contract being rendered is the one
compose-smoke measures end to end against the shipped image.

Numbers on this tree. Host: build 0 warnings, unit 128/128, integration 117/117,
smoke 25 passed / 0 failed, compose 53 passed / 0 failed, console 53 assertions /
0 failed, image 261 MB. Linux rehearsal, each stage on its own — source in
mcr.microsoft.com/dotnet/sdk:10.0 (Ubuntu 24.04.5, SDK 10.0.401): 128/128,
117/117, 25/0; compose in docker:cli: 53/0, enforcement lag 12 ms; console in
mcr.microsoft.com/playwright:v1.63.0-noble: 53/0.
2026-10-02 07:07:25 +02:00

155 lines
7.5 KiB
Bash
Executable File

#!/usr/bin/env bash
# End-to-end smoke test against a real edge and a real upstream.
#
# The integration tests prove the decision logic in isolation; this proves the
# deployed shape: the binary starts, proxies to an upstream it knows nothing
# about, bans a source that drew enough refusals, answers the way the design
# says it should, and healthchecks itself the way a shell-less distroless image
# needs.
#
# Sources are simulated with X-Forwarded-For and TrustProxyHeaders=on. That is
# not a shortcut: it is the only way to exercise "this source is banned, that
# one is not" over a loopback connection, and it doubles as a check that the
# trusted-proxy path is wired.
set -uo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
EDGE_PORT=${EDGE_PORT:-18088}
UP_PORT=${UP_PORT:-18900}
DB=/tmp/openwaf-smoke-$$.db
TOKEN=smoketoken
BIN="$ROOT/dotnet/OpenWaf.Edge/bin/Release/net10.0/OpenWaf.Edge.dll"
[ -f "$BIN" ] || BIN="$ROOT/dotnet/OpenWaf.Edge/bin/Debug/net10.0/OpenWaf.Edge.dll"
export HEALTHCHECK_PORT="$EDGE_PORT"
SCANNER=203.0.113.66
OFFICER=198.51.100.7
Bystander=198.51.100.99
# Payloads live in single-quoted variables: embedding them in the command
# substitutions below with escaped quotes is exactly the kind of thing that
# silently truncates a body.
BLOCK_JSON='{"ip":"198.51.100.7","reason":"confirmed scraper","redirectTo":"https://honeypot.example.org/"}'
ALLOW_JSON='{"match":"198.51.100.0/24","note":"office"}'
POLICY_OFF_JSON='{"mode":"Off"}'
cleanup() {
[ -n "${EDGE_PID:-}" ] && kill "$EDGE_PID" 2>/dev/null
[ -n "${UP_PID:-}" ] && kill "$UP_PID" 2>/dev/null
rm -f "$DB" "$DB-shm" "$DB-wal"
}
trap cleanup EXIT
mkdir -p /tmp/openwaf-upstream
python3 -m http.server "$UP_PORT" --bind 127.0.0.1 -d /tmp/openwaf-upstream >/dev/null 2>&1 &
UP_PID=$!
rm -f "$DB"
ASPNETCORE_URLS="http://127.0.0.1:$EDGE_PORT" \
OPENWAF_ADMIN_TOKEN="$TOKEN" \
ConnectionStrings__OpenWaf="Data Source=$DB" \
OpenWaf__Policy__StreakThreshold=3 \
OpenWaf__Policy__Mode=Enforce \
OpenWaf__Policy__BanDuration="00:00:06" \
OpenWaf__Policy__TrustProxyHeaders=true \
ReverseProxy__Clusters__upstream__Destinations__upstream1__Address="http://127.0.0.1:$UP_PORT/" \
dotnet "$BIN" >/tmp/openwaf-smoke.log 2>&1 &
EDGE_PID=$!
for _ in $(seq 1 60); do
curl -fsS "http://127.0.0.1:$EDGE_PORT/healthz" >/dev/null 2>&1 && break
sleep 0.25
done
pass=0; fail=0
check() {
if [ "$2" = "$3" ]; then pass=$((pass+1)); printf ' ok %-56s %s\n' "$1" "$3"
else fail=$((fail+1)); printf ' FAIL %-56s expected %s, got %s\n' "$1" "$2" "$3"; fi
}
code() { curl -s -o /dev/null -w '%{http_code}' "$@"; }
from() { printf 'http://127.0.0.1:%s/%s' "$EDGE_PORT" "$1"; } # usage: from path + -H "X-Forwarded-For: ip"
echo "edge :$EDGE_PORT upstream :$UP_PORT db $DB"
echo '- streak ban'
for i in 1 2 3; do
check "probe $i from the scanner reaches the upstream" 404 \
"$(code -H "X-Forwarded-For: $SCANNER" "$(from wp-admin)")"
done
check '4th request from the scanner is refused by the edge' 403 \
"$(code -H "X-Forwarded-For: $SCANNER" "$(from anything)")"
check 'a bystander is untouched' 404 \
"$(code -H "X-Forwarded-For: $Bystander" "$(from anything)")"
check 'the refusal explains itself' blocked_by_policy \
"$(curl -s -H "X-Forwarded-For: $SCANNER" "$(from anything)" | python3 -c 'import sys,json;print(json.load(sys.stdin)["error"])')"
check 'the ban carries a Retry-After' yes \
"$(curl -s -D- -o /dev/null -H "X-Forwarded-For: $SCANNER" "$(from anything)" | grep -qi '^retry-after:' && echo yes || echo no)"
echo '- operator list'
check 'add an operator block' 204 \
"$(code -X POST -H "X-Admin-Token: $TOKEN" -H 'Content-Type: application/json' \
--data-binary "$BLOCK_JSON" \
"http://127.0.0.1:$EDGE_PORT/admin-api/blocks")"
check 'an operator block answers 301 with Location' https://honeypot.example.org/ \
"$(curl -s -o /dev/null -w '%{redirect_url}' -H "X-Forwarded-For: $OFFICER" "$(from anything)")"
echo '- allowlist outranks everything'
check 'allow the office range' 204 \
"$(code -X POST -H "X-Admin-Token: $TOKEN" -H 'Content-Type: application/json' \
--data-binary "$ALLOW_JSON" \
"http://127.0.0.1:$EDGE_PORT/admin-api/allow")"
check 'the blocked office address gets through' 404 \
"$(code -H "X-Forwarded-For: $OFFICER" "$(from anything)")"
check 'the scanner outside that range stays banned' 403 \
"$(code -H "X-Forwarded-For: $SCANNER" "$(from anything)")"
# The exemption listing used to be the only listing with no ceiling: every paging
# parameter was ignored and the response said nothing about how many rows there
# were. Measured on the shipped image: 1101 rows, 130,386 bytes, no X-Total-Count,
# and ?limit=10 answered all 1101.
check 'the allow listing says how many rows there are' yes \
"$(curl -s -D- -o /dev/null -H "X-Admin-Token: $TOKEN" \
"http://127.0.0.1:$EDGE_PORT/admin-api/allow" | grep -qi '^x-total-count:' && echo yes || echo no)"
check 'the allow listing refuses a limit nobody meant' 400 \
"$(code -H "X-Admin-Token: $TOKEN" "http://127.0.0.1:$EDGE_PORT/admin-api/allow?limit=0")"
check 'a page of one is a page of one' 1 \
"$(curl -s -H "X-Admin-Token: $TOKEN" \
"http://127.0.0.1:$EDGE_PORT/admin-api/allow?limit=1" | python3 -c 'import sys,json;print(len(json.load(sys.stdin)))')"
echo '- expiry and clearing'
sleep 7
check 'the engine ban lifted itself' 404 "$(code -H "X-Forwarded-For: $SCANNER" "$(from wp-admin)")"
check 'clearing engine bans leaves operator rows' '{"deleted":1}' \
"$(curl -s -X DELETE -H "X-Admin-Token: $TOKEN" "http://127.0.0.1:$EDGE_PORT/admin-api/blocks?source=engine" | tr -d ' ')"
check 'an operator row survives it' 1 \
"$(curl -s -H "X-Admin-Token: $TOKEN" "http://127.0.0.1:$EDGE_PORT/admin-api/blocks?source=operator" | python3 -c 'import sys,json;print(len(json.load(sys.stdin)))')"
echo '- admin surface'
check 'no token is refused' 401 "$(code "http://127.0.0.1:$EDGE_PORT/admin-api/blocks")"
check 'the policy is readable' Enforce \
"$(curl -s -H "X-Admin-Token: $TOKEN" "http://127.0.0.1:$EDGE_PORT/admin-api/policy" | python3 -c 'import sys,json;print(json.load(sys.stdin)["mode"])')"
# Each pillar keeps its last good value when its own read fails, which is the
# right failure posture and was, until recently, an unmentioned one. The healthy
# case has to be said with numbers too — silence and "0" look the same from
# outside, and only one of them is a claim.
STATE_JSON="$(curl -s -H "X-Admin-Token: $TOKEN" "http://127.0.0.1:$EDGE_PORT/admin-api/state")"
check 'the state counts failed pillar reads, and there are none' '0 0' \
"$(printf '%s' "$STATE_JSON" | python3 -c 'import sys,json;d=json.load(sys.stdin);print(d["snapshotRefreshFailures"], d["policyReloadFailures"])')"
check 'the state says the snapshot is inside its ttl' False \
"$(printf '%s' "$STATE_JSON" | python3 -c 'import sys,json;print(json.load(sys.stdin)["snapshotStale"])')"
check 'the policy answer carries its provenance header' 0 \
"$(curl -s -D- -o /dev/null -H "X-Admin-Token: $TOKEN" \
"http://127.0.0.1:$EDGE_PORT/admin-api/policy" | tr -d '\r' \
| sed -n 's/^[Xx]-[Pp]olicy-[Rr]eload-[Ff]ailures: //p')"
check 'switching the engine off is accepted' Off \
"$(curl -s -X PUT -H "X-Admin-Token: $TOKEN" -H 'Content-Type: application/json' --data-binary "$POLICY_OFF_JSON" \
"http://127.0.0.1:$EDGE_PORT/admin-api/policy" | python3 -c 'import sys,json;print(json.load(sys.stdin)["mode"])')"
echo '- healthcheck mode (a shell-less image can only ask the binary)'
dotnet "$BIN" --healthcheck; check 'healthcheck exit code' 0 "$?"
echo
echo "passed $pass, failed $fail"
[ "$fail" -eq 0 ]