diff --git a/scripts/compose-smoke.sh b/scripts/compose-smoke.sh index 07671a6..4c0d1a3 100755 --- a/scripts/compose-smoke.sh +++ b/scripts/compose-smoke.sh @@ -40,6 +40,32 @@ jget() { python3 -c "import json,sys d=json.load(sys.stdin) for o in d: if $1: print(o.get('$2') or ''); break"; } +# /admin-api/streaks answers with an OBJECT that wraps the list, so a predicate +# written for jcount walks the object's keys instead and compares strings to +# dicts. streaks_where runs the predicate over the wrapped list and answers -1 +# for a body it cannot parse, so a broken fetch shows up as a wrong number +# rather than as a python traceback in the middle of the gate. +streaks_where() { python3 -c "import json,sys +try: + d=json.load(sys.stdin); print(sum(1 for b in d['streaks'] if $1)) +except Exception: + print(-1)"; } +# Fetched with polling, not sampled once: the first attempt right after a burst +# has been measured answering a completely empty body while the next answered +# complete, and an assertion on one instant is not a contract. +streaks_fetch() { + STREAKS_JSON='' + for _ in $(seq 1 6); do + STREAKS_JSON=$(admin "$EDGE/admin-api/streaks?limit=1000") + [ -n "$STREAKS_JSON" ] && break + sleep 2 + done +} +streaks_info() { + printf ' info the live streak map: %s tracked, %s standing\n' \ + "$(printf '%s' "$STREAKS_JSON" | jval trackedSources)" \ + "$(printf '%s' "$STREAKS_JSON" | jval standingStreaks)" +} code() { curl -s -m 10 -o /dev/null -w '%{http_code}' "$@"; } hdr() { curl -s -m 10 -D - -o /dev/null "$@" | tr -d '\r'; } xff() { printf 'X-Forwarded-For: %s' "$1"; } @@ -361,6 +387,16 @@ echo '- a pillar that cannot write says so' # "AlertWriter: could not persist…". A trigger, not a dropped table, on purpose: # a missing table breaks the listing too, and a broken listing is honest about # being broken. The lie this asserts is the 200 that stays complete-looking. +# State the policy this scene depends on, and prove the source keys it claims +# are real. The read scene above drops policy_settings and recreates it empty, +# which erases the operator's overrides: once the reload succeeds against the +# empty table the policy reverts to the configured defaults, and with +# trustProxyHeaders back to false every "new source" below is the same docker +# bridge address and the scene passes for the wrong reason. Which policy is in +# force after that restore is not deterministic across runs — the first Linux +# rehearsal of the ban scene failed exactly there. +admin -X PUT -H 'Content-Type: application/json' \ + -d '{"streakThreshold":3,"trustProxyHeaders":true}' "$EDGE/admin-api/policy" >/dev/null TOTAL_BEFORE=$(hdr -H "X-Admin-Token: $OPENWAF_ADMIN_TOKEN" "$EDGE/admin-api/observations?limit=1" \ | grep -i '^x-total-count:' | tr -d '\r' | awk '{print $2}') sidecar <<'PYSQL' @@ -393,6 +429,12 @@ TOTAL_AFTER=$(hdr -H "X-Admin-Token: $OPENWAF_ADMIN_TOKEN" "$EDGE/admin-api/obse | grep -i '^x-total-count:' | tr -d '\r' | awk '{print $2}') printf ' info X-Total-Count across the break: %s -> %s (three new sources saw, none stored)\n' "$TOTAL_BEFORE" "$TOTAL_AFTER" check 'the listing still answers with the count it had before the break' "$TOTAL_BEFORE" "$TOTAL_AFTER" +# Not vacuous: the three sources this scene says were seen are in the live +# streak map, so they really were three distinct sources and not one bridge +# address wearing a header nobody trusted. +streaks_fetch; streaks_info +check 'the sources this scene lost were really three distinct sources' '3' \ + "$(printf '%s' "$STREAKS_JSON" | streaks_where "b['sourceKey'] in ('203.0.113.201','203.0.113.202','203.0.113.203')")" check 'the failure it reports is the real reason, not an EF wrapper' 'yes' \ "$(admin "$EDGE/admin-api/state" | jval observationLastWriteError | grep -qi 'durable write blocked' && echo yes || echo no)" # Reporting-path failures are stated, not gated. The first version of this gate @@ -444,6 +486,8 @@ for op in ("INSERT", "UPDATE"): "BEGIN SELECT RAISE(ABORT, 'gate: ban write blocked'); END") print("triggers:", [r[0] for r in c.execute("SELECT name FROM sqlite_master WHERE type='trigger'")]) PYSQL +admin -X PUT -H 'Content-Type: application/json' \ + -d '{"streakThreshold":3,"trustProxyHeaders":true}' "$EDGE/admin-api/policy" >/dev/null BAN_SRC=203.0.113.241 # Counted before and after, not in total: an earlier scene in this same run may # legitimately have produced a race line, and a total would blame this scene for @@ -453,6 +497,9 @@ BAN_CODES="" for i in $(seq 1 5); do BAN_CODES="$BAN_CODES $(code -H "$(xff "$BAN_SRC")" "$EDGE/unbanned-$i")"; done printf ' info answers from a scanner at threshold 3 while ban writes are blocked:%s\n' "$BAN_CODES" check 'the scanner is never banned while the ban write fails' '404 404 404 404 404' "${BAN_CODES# }" +streaks_fetch; streaks_info +check 'the scanner really was one source the edge tracked' 'yes' \ + "$(printf '%s' "$STREAKS_JSON" | streaks_where "b['sourceKey']=='$BAN_SRC'" | awk '{exit ($1+0>=1)?0:1}' && echo yes || echo no)" check 'and no engine ban for it reached storage' 'yes' \ "$(admin "$EDGE/admin-api/blocks?source=engine" | jcount "b['sourceKey']=='$BAN_SRC'" | awk '{exit ($1+0==0)?0:1}' && echo yes || echo no)" check 'the state counts the ban write that failed' 'yes' \