Files
openwaf/scripts/smoke.sh
T
Pavlo Ratushnyi 3f24c5d31c A ban that could not be written was logged as a concurrency race, and nothing was in force
Measured on the shipped image with writes to block_entries aborted by SQLite
triggers while reads kept working: five refusals from one source at a threshold
of 3 answered 404 five times, no ban reached storage or /state, /readyz answered
200, and the log said "BlockStore: concurrent ban on 203.0.113.12; keeping the
winner" four times over an inner "SQLite Error 19: 'gate: ban write blocked'".
Enforcement reads the snapshot and the snapshot is built from storage, so a ban
that cannot be written is a ban that exists nowhere - and the only sentence the
log offered said the opposite.

BlockStore now asks storage which case it is: re-read the row. A live engine ban
for that source means somebody won the unique-index race and nothing is wrong;
no row, or a read that cannot run either, means the write never happened. The
second case is counted (DurabilityHealth.Bans), logged with its real reason and
its consequence, and gates /readyz - the enforcement path is the path that
gates.

That also moved the boundary drawn last cycle. Observations, alerts and retention
are reporting pillars: counted, shown on /state and on the console, and no longer
gating readiness. The measurement that forced it: with the gate in place, one
lost alert left alertWriteFailures at 3 and /readyz at 503; the triggers were
removed and the stand left idle for 120 s, and it answered 503 the whole time
with the write path healthy. An alert pillar writes only when an alert happens to
be raised, so "not yet proven healthy" had become "unready forever".

ObservationWriter now retries every tracked source while it has an outstanding
failure: the write-amplification bound protects against load, not against
retries, and without this the lost rows waited for each source to be due again a
full interval later.

Teeth: BanWriteFailureTests (3) - 3 failed / 0 passed before the fix; two
WriteFailureVisibilityTests assertions re-drawn for the new boundary. Gates:
compose-smoke 63 -> 72 (the ban scene asserts 404x5, no row in storage, 503
naming bans, race lines 0/0 and the honest line present, then the ban landing on
a later refusal), smoke.sh counters now include bans, console-check 57 -> 58.
2026-10-02 09:24:43 +02:00

166 lines
8.6 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')"
# The write side of the same contract. A failed write is worse than a failed
# read — the row never existed and no later request brings it back — so the
# counters exist even in a clean run, where they read 0 0 0. A surface that
# answers "0" for a field it never publishes is indistinguishable from one that
# publishes it and means it, which is why the keys are asserted too.
check 'the state counts writes it could not persist, and there are none' '0 0 0 0' \
"$(printf '%s' "$STATE_JSON" | python3 -c 'import sys,json;d=json.load(sys.stdin);print(d["banWriteFailures"], d["observationWriteFailures"], d["alertWriteFailures"], d["retentionPruneFailures"])')"
check 'and it carries what was lost and when a write last landed' True \
"$(printf '%s' "$STATE_JSON" | python3 -c 'import sys,json;d=json.load(sys.stdin);print(all(k in d for k in ("bansNotWritten","banLastWriteAt","observationsLost","alertsLost","observationLastWriteAt","alertLastWriteAt","retentionLastPruneAt")))')"
check 'readiness is ready while the write path is healthy' 200 \
"$(code "http://127.0.0.1:$EDGE_PORT/readyz")"
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 ]