84 lines
3.5 KiB
Bash
Executable File
84 lines
3.5 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# D5 - the alert dedup is per process, and two edges is where that shows.
|
|
#
|
|
# The compose gate measures one edge: 12 server errors at N=3 cost 3 alert rows
|
|
# (crossing at 3, doublings at 6 and 12) after the edge-trigger fix, 10 before
|
|
# it. Behind a load balancer the streak counter lives in each process, so each
|
|
# process runs its own crossing and its own doublings. The question this answers
|
|
# with a number, not an opinion: for one source and one upstream outage, how
|
|
# many alert rows does the shared alerts table get when two edges see it?
|
|
#
|
|
# Method: same stack as run3.sh/run4.sh (two edges, one Postgres, one upstream),
|
|
# with the upstream answering 502 on /boom (compose.502.yml mounts
|
|
# upstream-502.conf). Phase 1 sends 10 broken responses through edge-a only, so
|
|
# the expected row set is the single-process answer. Phase 2 sends 10 through
|
|
# edge-a and 10 through edge-b for a second source: same logical episode, twice
|
|
# the processes. Different source keys per phase, so neither phase can satisfy
|
|
# the other's assertion from leftover rows.
|
|
|
|
set -euo pipefail
|
|
cd "$(dirname "$0")"
|
|
|
|
export OPENWAF_ADMIN_TOKEN="${OPENWAF_ADMIN_TOKEN:-multi-instance-throwaway}"
|
|
A=http://127.0.0.1:18081
|
|
B=http://127.0.0.1:18082
|
|
CF=(-f compose.yml -f compose.502.yml)
|
|
S1=203.0.113.151
|
|
S2=203.0.113.152
|
|
TTL=${TTL:-5}
|
|
N=${N:-10}
|
|
|
|
admin() { curl -s -m 10 -H "X-Admin-Token: $OPENWAF_ADMIN_TOKEN" "$@"; }
|
|
code() { curl -s -m 10 -o /dev/null -w '%{http_code}' "$@"; }
|
|
xff() { printf 'X-Forwarded-For: %s' "$1"; }
|
|
|
|
# The counts, not just the number of rows: [3,6] and [3,3,6,6] are the same
|
|
# length as [3,6,12] and mean completely different things.
|
|
alert_counts() {
|
|
admin "$A/admin-api/alerts?limit=1000" | python3 -c "
|
|
import json, sys
|
|
rows = [r for r in json.load(sys.stdin) if r['kind'] == '$1' and r['sourceKey'] == '$2']
|
|
print('%d rows, counts=%s' % (len(rows), sorted(r['count'] for r in rows)))"
|
|
}
|
|
|
|
trap 'if [ "${KEEP_UP:-0}" != 1 ]; then docker compose "${CF[@]}" down >/dev/null 2>&1; fi' EXIT
|
|
|
|
docker build -q -t openwaf:multi -f ../../Dockerfile ../.. >/dev/null
|
|
TTL=$TTL docker compose "${CF[@]}" up -d --force-recreate >/dev/null
|
|
for n in edge-a edge-b; do
|
|
for _ in $(seq 1 120); do
|
|
[ "$(docker compose "${CF[@]}" ps -q "$n" | xargs docker inspect --format '{{.State.Health.Status}}')" = healthy ] && break
|
|
sleep 0.5
|
|
done
|
|
done
|
|
printf ' ttl edge-a=%s edge-b=%s\n' \
|
|
"$(curl -s "$A/healthz" | sed -n 's/.*"snapshotTtlSeconds":\([0-9.]*\).*/\1/p')" \
|
|
"$(curl -s "$B/healthz" | sed -n 's/.*"snapshotTtlSeconds":\([0-9.]*\).*/\1/p')"
|
|
|
|
admin -X PUT -H 'Content-Type: application/json' \
|
|
-d '{"mode":"Enforce","geo":"Off","streakThreshold":3,"trustProxyHeaders":true}' "$A/admin-api/policy" >/dev/null
|
|
sleep $(( TTL + 3 ))
|
|
|
|
echo "== phase 1: $N server errors through edge-a only, source $S1 =="
|
|
last=''
|
|
for _ in $(seq 1 "$N"); do last=$(code -H "$(xff "$S1")" "$A/boom"); done
|
|
printf ' every response: %s\n' "$last"
|
|
sleep 3
|
|
printf ' %s\n' "$(alert_counts server_error_streak "$S1")"
|
|
|
|
echo "== phase 2: $N through edge-a and $N through edge-b, same source $S2 =="
|
|
for _ in $(seq 1 "$N"); do
|
|
code -H "$(xff "$S2")" "$A/boom" >/dev/null
|
|
code -H "$(xff "$S2")" "$B/boom" >/dev/null
|
|
done
|
|
sleep 3
|
|
printf ' %s\n' "$(alert_counts server_error_streak "$S2")"
|
|
|
|
# The safety property still holds with two processes: a 5xx never bans, and it
|
|
# must not ban from either edge.
|
|
printf ' engine bans written for these sources: %s\n' \
|
|
"$(admin "$A/admin-api/blocks" | python3 -c "
|
|
import json, sys
|
|
rows = json.load(sys.stdin)
|
|
print(sum(1 for r in rows if r.get('sourceKey') in ('$S1', '$S2')))")"
|