Every pillar this edge had until now judged the response or the source: a streak of refusals the upstream produced, a country, an operator's list of addresses. The request itself was never looked at. A known-bad request therefore always reached the origin and cost it something, and became a defence only if the origin's answer happened to fall in the refusal range three times in a row. A request rule is a path pattern an operator writes: `GET/POST/DELETE /admin-api/rules`, listed in the order they are enforced, with an action. `observe` counts matches (one alert per rule per 15 minutes, the tally on the listing) and lets the request through, which is how an operator learns what a rule would have blocked before it blocks anything; `block` answers 403 naming the rule id — the id, not the pattern, because a 403 body that quotes the pattern tells the visitor which payload to change. It fires in Observe mode too: the mode switches off what the edge decides by itself, not what a human wrote down. It sits below the allowlist and above the two source-level lists, because "this path is refused" is the more specific claim about this request. The pattern language is a glob — `*` and `?`, anchored, case-insensitive, `*` crossing `/` — and deliberately not a regex: the operator writes the pattern and an anonymous visitor writes the path, and `(a+)+$` against 400 a's is a ReDoS with the edge's CPU. A pattern that can only silently do nothing (no leading `/`, `/*`, a control character, over 256 characters) is refused with the reason, and at most 500 live rules exist because every request is checked against each. Two things the run caught rather than the design assumed: * The rules read shared one statement block with the rest of the snapshot. `request_rules` is the newest table in the schema, and the default schema step is EnsureCreated, which creates a missing database and changes nothing about an existing one — so every database created before this feature has every table except that one, and the first refresh after startup would have thrown, frozen the operator's block list at its old contents, and looked healthy. The rules read is a statement of its own now: a missing table costs the rules pillar and says so (`requestRulesAvailable`, `requestRulesError`, a NOT READ line over the rules table) while every other pillar keeps enforcing. The migration set was stale in the same direction — it never had `source_streaks` either, so the `migrate` path could not create the table OPEN-52 added; `AddRequestRules` adds both. * The console gate banned its own probe address. The seed sets the streak threshold to 3 and the gate probes 404 paths, so the last assertion in the new scene read a `blocked_by_policy` as "the rule is still in force" — and the ban, unlike the rule, outlives `compose down` in the data volume. The scene probes its own address now and the cleanup clears engine bans. Measured on this tree: build 0 warnings / 0 errors, unit 195/195 (44 new: the glob's semantics and rejections, and where the new pillar sits in the order), integration 208/208 in 25 s (14 new, including a fixture that drops the rules table and asserts the operator block is still enforced), smoke 36/0 (7 new against a running edge), console 85 assertions 0 failed (6 new, driving the rule form in a real browser), compose 209/0 against the built image at 261 MB.
199 lines
11 KiB
Bash
Executable File
199 lines
11 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)))')"
|
|
|
|
# A path rule is the one pillar about the request rather than about what the
|
|
# upstream answered, so the smoke run asks the question the feature exists to
|
|
# answer: does a refused path reach the origin or not.
|
|
# Not $Bystander: the allowlist scene above exempted 198.51.100.0/24, and an
|
|
# exempt address is the one answer a path rule must not override.
|
|
RULE_IP=203.0.113.77
|
|
RULE_JSON='{"pattern":"/gate-blocked*","action":"block","note":"smoke: the request itself"}'
|
|
BAD_RULE_JSON='{"pattern":"gate-blocked","action":"block"}'
|
|
echo '- a path rule refuses the request itself'
|
|
check 'add a blocking path rule' 204 \
|
|
"$(code -X POST -H "X-Admin-Token: $TOKEN" -H 'Content-Type: application/json' \
|
|
--data-binary "$RULE_JSON" \
|
|
"http://127.0.0.1:$EDGE_PORT/admin-api/rules")"
|
|
check 'the path is refused rather than proxied' 403 \
|
|
"$(code -H "X-Forwarded-For: $RULE_IP" "$(from gate-blocked)")"
|
|
check 'the refusal names the rule and not the pattern' yes \
|
|
"$(curl -s -H "X-Forwarded-For: $RULE_IP" "$(from gate-blocked)" | grep -q blocked_by_rule && echo yes || echo no)"
|
|
check 'a pattern that could never match is refused' 400 \
|
|
"$(code -X POST -H "X-Admin-Token: $TOKEN" -H 'Content-Type: application/json' \
|
|
--data-binary "$BAD_RULE_JSON" \
|
|
"http://127.0.0.1:$EDGE_PORT/admin-api/rules")"
|
|
check 'the rules listing holds the rule' 1 \
|
|
"$(curl -s -H "X-Admin-Token: $TOKEN" "http://127.0.0.1:$EDGE_PORT/admin-api/rules" | python3 -c 'import sys,json;print(len(json.load(sys.stdin)))')"
|
|
RULE_ID="$(curl -s -H "X-Admin-Token: $TOKEN" "http://127.0.0.1:$EDGE_PORT/admin-api/rules" | python3 -c 'import sys,json;print(json.load(sys.stdin)[0]["id"])')"
|
|
check 'the rule can be removed by id' 204 \
|
|
"$(code -X DELETE -H "X-Admin-Token: $TOKEN" "http://127.0.0.1:$EDGE_PORT/admin-api/rules/$RULE_ID")"
|
|
check 'and the path flows again once it is gone' 404 \
|
|
"$(code -H "X-Forwarded-For: $RULE_IP" "$(from gate-blocked)")"
|
|
|
|
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"])')"
|
|
# Whether a raised alert goes anywhere on its own. Off by default, and the
|
|
# surface has to say which of "no alerts" and "no push" it is answering.
|
|
check 'the state says alerts are not pushed anywhere by default' False \
|
|
"$(printf '%s' "$STATE_JSON" | python3 -c 'import sys,json;print(json.load(sys.stdin)["alertWebhookConfigured"])')"
|
|
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 ]
|