diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 0044bc0..3352fd8 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -96,7 +96,7 @@ Recorded because they are the places a reader will otherwise step in again. green. `compose-smoke.sh` now asserts pin == loaded, and that is the assertion which catches it. 3. Multi-instance edge → measured on `research/2026-10-01-multi-instance/`, - not guessed. Two findings, each reproduced twice: + not guessed. Three findings, the first two reproduced twice: * The streak is the problem, and it is not lag. Round-robin over two edges multiplies the effective streak threshold by the instance count: four refusals split 2+2 at threshold 3 produced no ban anywhere (D1). @@ -106,6 +106,13 @@ Recorded because they are the places a reader will otherwise step in again. with no measurable change on the request path (p50 1.75 to 1.93 ms, p95 3.15 to 2.78 ms at n=200; the two move in opposite directions, so the honest reading is "inside noise", not "cheaper"). + * Alerting is per process as well. One upstream outage seen by one edge wrote + 2 alert rows (counts 3 and 6); the same outage seen by both edges wrote 4 + (3, 3, 6, 6) → the single-process answer, twice, with identical + counts per process. Before the edge-trigger fix the same two phases wrote 8 + and 16 (D5). So the per-request growth is gone and the per-instance + duplication is not, because both are decided by a counter that lives in the + process. The block list is already shared through the database; the streak map is not. Move the streak map when the threshold has to be exact behind a load balancer — it costs a round trip on a path whose measured p95 is ~3 ms. diff --git a/research/2026-10-01-multi-instance/RESULTS.md b/research/2026-10-01-multi-instance/RESULTS.md index fb9c4cd..b0523ec 100644 --- a/research/2026-10-01-multi-instance/RESULTS.md +++ b/research/2026-10-01-multi-instance/RESULTS.md @@ -7,7 +7,7 @@ the SQLite default the tests run on. Two `edge` containers, one shared database nginx upstream, `TrustProxyHeaders=true` so both edges derive the same source key, `Mode=Enforce`, `StreakThreshold=3`. -Reproduce: `./run.sh` (M1–M5), `./run2.sh` (D1, D1b), `./run3.sh` (D3), `./run4.sh` (D4, two TTLs; its raw output is kept in `run4-output.txt`). Each +Reproduce: `./run.sh` (M1–M5), `./run2.sh` (D1, D1b), `./run3.sh` (D3), `./run4.sh` (D4, two TTLs; its raw output is kept in `run4-output.txt`), `./run5.sh` (D5, both sides of the alert-edge change; raw output in `run5-output.txt`). Each builds the image, starts the stack, measures and tears it down; `run.sh` writes `results.json` next to itself. @@ -24,6 +24,7 @@ builds the image, starts the stack, measures and tears it down; `run.sh` writes | M4c | Propagation of an operator block at that size, edge-b idle | 1.7 s. | | M5 | Observations written by both edges | one row per source key; the shared table does not double-count a source both edges saw. | | D4 | Propagation of an operator block written on edge-a, edge-b under a steady 200-only stream (~28 req/s), at two snapshot TTLs | **23.1 s at the default 30 s; 3.4 s at `OpenWaf:State:RefreshTtlSeconds=5`** — a 6.9x cut. Request path through edge-b at the same time: p50 1.75 ms / p95 3.15 ms at TTL 30, p50 1.93 ms / p95 2.78 ms at TTL 5 (n=200 each) — the two move in opposite directions, so the honest reading is "inside noise", not "cheaper". | +| D5 | Alert rows in the shared table for one upstream outage, measured on both sides of the edge-trigger fix | **After: 2 rows [3, 6] through one edge, 4 rows [3, 3, 6, 6] through two. Before: 8 rows [3..10] and 16 rows.** The dedup is per process, so two edges produce exactly twice the rows of one — and the row counts are identical per process, which is what proves it is the same rule running twice rather than a shared counter. 0 engine bans in both runs: a 5xx still never bans. ## What the numbers say @@ -57,6 +58,16 @@ builds the image, starts the stack, measures and tears it down; `run.sh` writes same way: the `PUT /admin-api/policy` in run2.sh was visible on the other edge within one TTL, so the environment block in compose is a fallback, not a per-instance setting. +5. **The alert dedup is per process too, and that is visible in the shared + table.** D5 sent the same broken upstream through one edge and then through + two. One edge: 2 rows, counts [3, 6]. Two edges, same source, same outage: + 4 rows, counts [3, 3, 6, 6] — the single-process answer, twice. Before the + fix the same two phases wrote 8 and 16. So the fix removes the per-request + growth (which was attacker-shaped) but not the per-instance duplication, and + the duplication is the same root cause as D1: the counter that decides + whether to alert lives in the process. Option (c) below would collapse both + at once; option (a) makes the operator's alert list read correctly by + construction. ## The decision this feeds (OPEN-12) @@ -85,6 +96,10 @@ builds the image, starts the stack, measures and tears it down; `run.sh` writes version, with the looper hitting a path the upstream answers with 200. - D4 is two runs, one source, one upstream, ~28 req/s — the same shape as D3, not a distribution. The 6.9x is a ratio of two single samples. +- D5 is one run per side, N=10 per phase, one source per phase. The row counts + are exact (they are counts, not timings), so the 8 -> 2 and 16 -> 4 comparison + does not depend on a distribution; what has not been measured is behaviour at + more than two instances, where the expectation is N rows per escalation. - The request-path comparison in D4 is n=200 per phase and the two percentiles moved in opposite directions. It supports "shortening the TTL did not make the request path measurably slower"; it does not support "it is free". \ No newline at end of file diff --git a/research/2026-10-01-multi-instance/compose.502.yml b/research/2026-10-01-multi-instance/compose.502.yml new file mode 100644 index 0000000..c702d17 --- /dev/null +++ b/research/2026-10-01-multi-instance/compose.502.yml @@ -0,0 +1,5 @@ +# Override for run5.sh only: give the shared upstream a 502 to answer with. +services: + upstream: + volumes: + - ./upstream-502.conf:/etc/nginx/conf.d/default.conf:ro diff --git a/research/2026-10-01-multi-instance/run5-output.txt b/research/2026-10-01-multi-instance/run5-output.txt new file mode 100644 index 0000000..af30787 --- /dev/null +++ b/research/2026-10-01-multi-instance/run5-output.txt @@ -0,0 +1,23 @@ +D5 raw output, both sides of the alert-edge change, same script and same stack. + +== AFTER: image built from 9615352 ("Both alerts were level-triggered...") == + ttl edge-a=5 edge-b=5 +== phase 1: 10 server errors through edge-a only, source 203.0.113.151 == + every response: 502 + 2 rows, counts=[3, 6] +== phase 2: 10 through edge-a and 10 through edge-b, same source 203.0.113.152 == + 4 rows, counts=[3, 3, 6, 6] + engine bans written for these sources: 0 + +== BEFORE: image built from f7ced94 (the commit before the fix), via a throwaway + git worktree at that commit, same run5.sh copied in == + ttl edge-a=5 edge-b=5 +== phase 1: 10 server errors through edge-a only, source 203.0.113.151 == + every response: 502 + 8 rows, counts=[3, 4, 5, 6, 7, 8, 9, 10] +== phase 2: 10 through edge-a and 10 through edge-b, same source 203.0.113.152 == + 16 rows, counts=[3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10] + engine bans written for these sources: 0 + +Both runs: StreakThreshold=3, Mode=Enforce, TrustProxyHeaders=true, +OpenWaf:State:RefreshTtlSeconds=5, PostgreSQL 18, nginx answering 502 on /boom. diff --git a/research/2026-10-01-multi-instance/run5.sh b/research/2026-10-01-multi-instance/run5.sh new file mode 100755 index 0000000..62c9295 --- /dev/null +++ b/research/2026-10-01-multi-instance/run5.sh @@ -0,0 +1,83 @@ +#!/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')))")" diff --git a/research/2026-10-01-multi-instance/upstream-502.conf b/research/2026-10-01-multi-instance/upstream-502.conf new file mode 100644 index 0000000..abafc95 --- /dev/null +++ b/research/2026-10-01-multi-instance/upstream-502.conf @@ -0,0 +1,9 @@ +# The upstream for D5. nginx:alpine's own default.conf is replaced by this file: +# /boom answers 502 so the alerting pillar can be exercised end to end, and +# everything else answers 200 so a probe never builds a refusal streak by +# accident (run2.sh's mistake, recorded in RESULTS.md). +server { + listen 80; + location = /boom { return 502; } + location / { return 200 "ok\n"; } +}