diff --git a/scripts/compose-smoke.sh b/scripts/compose-smoke.sh index 6d14463..c283798 100755 --- a/scripts/compose-smoke.sh +++ b/scripts/compose-smoke.sh @@ -902,6 +902,167 @@ check 'and the ban the edge decided hours of polling ago is now in storage' 'yes check 'the pending queue is empty again' '0' "$(admin "$EDGE/admin-api/state" | jval bansPendingRetry)" check 'and the source that was never banned during the break is banned on the wire' '403' \ "$(code -H "$(xff "$RETRY_SRC")" "$EDGE/retry-6")" + +echo '- a listing says which part of itself it is' +# OPEN-24, measured on the shipped image before the fix: with 1103 recorded +# sources GET /admin-api/observations?limit=100000 answered 200 with 1000 rows, +# no X-Total-Count and no X-Truncated — 103 sources were unreachable and the +# answer still read as a complete one; ?limit=0 and ?limit=-1 each answered 200 +# with a single row. /streaks carried the opposite defect: the flag was counted +# from the size of the streak map, so two standing streaks plus one reset source +# answered X-Truncated: true for a listing that had cut nothing. +# +# The in-process suites have covered the fixed behaviour since; what was missing +# is the same sentence measured over a socket on the image that ships it. Until +# this scene, the image-level gate asserted the paging headers for /allow only. +TMPH=$(mktemp); TMPB=$(mktemp) +# The response's own arithmetic: does X-Truncated agree with X-Total-Count, the +# offset that was asked for, and the rows that actually came back? Written once +# so every listing in this scene is held to the same rule, and so a header that +# is simply absent can never be read as "not truncated". +truth() { + python3 - "$TMPH" "$TMPB" "$1" <<'PY' +import json, sys +hdrs = {} +for line in open(sys.argv[1]): + if ':' in line: + k, v = line.split(':', 1) + hdrs[k.strip().lower()] = v.strip() +try: + d = json.load(open(sys.argv[2])) +except Exception: + print('unparseable-body') + raise SystemExit(0) +rows = d.get('streaks', []) if isinstance(d, dict) else d +total = hdrs.get('x-total-count') +if total is None and isinstance(d, dict): + total = d.get('standingStreaks') +trunc = hdrs.get('x-truncated') +if total is None or trunc is None: + print('no-header') +else: + want = str(bool(int(sys.argv[3]) + len(rows) < int(total))).lower() + print('yes' if trunc == want else 'no') +PY +} +admin -X PUT -H 'Content-Type: application/json' \ + -d '{"mode":"Enforce","streakThreshold":5,"trustProxyHeaders":true}' "$EDGE/admin-api/policy" >/dev/null +admin -X DELETE "$EDGE/admin-api/blocks?source=operator" >/dev/null +# Five sources, one second apart, so the listing's own ordering (newest first) +# is reproducible: five rows sharing one timestamp can be returned in any order +# and the pagination assertions would then be comparing noise. +for n in 1 2 3 4 5; do + code -H "$(xff "203.0.113.18$n")" "$EDGE/page-$n" >/dev/null + sleep 1 +done +SEEN5=0 +for _ in $(seq 1 12); do + SEEN5=$(admin "$EDGE/admin-api/observations?limit=5" | python3 -c "import json,sys +d=json.load(sys.stdin) +print(sum(1 for o in d if o['sourceKey'].startswith('203.0.113.18')))") + [ "${SEEN5:-0}" = "5" ] && break + sleep 3 +done +printf ' info this run seeded five sources 1 s apart; the newest five rows hold %s of them\n' "$SEEN5" +check 'the five sources this scene just created are all listable' '5' "${SEEN5:-0}" +PAGE_KEYS=''; PAGE_TOTALS='' +# Accumulated space-separated and split per line below: the count of unique +# keys is the assertion, so the leading empty field has to be dropped rather +# than counted as a sixth source. +for off in 0 1 2 3 4; do + admin -D "$TMPH" -o "$TMPB" "$EDGE/admin-api/observations?limit=1&offset=$off" + PAGE_KEYS="$PAGE_KEYS $(jget "True" "sourceKey" < "$TMPB")" + PAGE_TOTALS="$PAGE_TOTALS $(grep -i '^x-total-count:' "$TMPH" | tr -cd '0-9')" +done +printf ' info the five single-row pages showed:%s (totals:%s)\n' "$PAGE_KEYS" "$PAGE_TOTALS" +check 'five single-row pages show five different sources' '5' \ + "$(tr ' ' '\n' <<<"$PAGE_KEYS" | grep . | sort -u | grep -c .)" +check 'and those five are exactly the first five rows of the whole listing' 'yes' \ + "$([ "$(tr ' ' '\n' <<<"$PAGE_KEYS" | grep . | sort)" = "$(admin "$EDGE/admin-api/observations?limit=5" | python3 -c "import json,sys +print('\n'.join(sorted(o['sourceKey'] for o in json.load(sys.stdin))))")" ] && echo yes || echo no)" +check 'every page names the same total, not the size of its own page' '1' \ + "$(tr ' ' '\n' <<<"$PAGE_TOTALS" | grep . | sort -u | grep -c .)" +check 'a one-row page says it cut the list' 'X-Truncated: true' \ + "$(hdr -H "X-Admin-Token: $OPENWAF_ADMIN_TOKEN" "$EDGE/admin-api/observations?limit=1" | grep -i '^x-truncated:' | tr -d '\r')" +check 'and the flag agrees with the total and the row count it shipped' 'yes' \ + "$(admin -D "$TMPH" -o "$TMPB" "$EDGE/admin-api/observations?limit=1&offset=1"; truth 1)" +check 'a page that reaches the end says nothing was cut' 'X-Truncated: false' \ + "$(hdr -H "X-Admin-Token: $OPENWAF_ADMIN_TOKEN" "$EDGE/admin-api/observations?limit=1000" | grep -i '^x-truncated:' | tr -d '\r')" +check 'the alert listing carries both headers' 'yes' \ + "$(admin -D "$TMPH" -o "$TMPB" "$EDGE/admin-api/alerts?limit=1"; grep -qi '^x-total-count:' "$TMPH" && grep -qi '^x-truncated:' "$TMPH" && echo yes || echo no)" +check 'and its flag agrees with its own arithmetic' 'yes' \ + "$(admin -D "$TMPH" -o "$TMPB" "$EDGE/admin-api/alerts?limit=1&offset=1"; truth 1)" +# The block listing gets exact numbers, because this scene owns the whole +# filtered set: two operator rows and nothing else under source=operator. +admin -X POST -H 'Content-Type: application/json' -d '{"ip":"203.0.113.181","note":"paging-1"}' "$EDGE/admin-api/blocks" >/dev/null +admin -X POST -H 'Content-Type: application/json' -d '{"ip":"203.0.113.182","note":"paging-2"}' "$EDGE/admin-api/blocks" >/dev/null +check 'the operator listing counts exactly what this scene wrote' 'X-Total-Count: 2' \ + "$(hdr -H "X-Admin-Token: $OPENWAF_ADMIN_TOKEN" "$EDGE/admin-api/blocks?source=operator" | grep -i '^x-total-count:' | tr -d '\r')" +check 'and says it cut the list at one row' 'X-Truncated: true' \ + "$(hdr -H "X-Admin-Token: $OPENWAF_ADMIN_TOKEN" "$EDGE/admin-api/blocks?source=operator&limit=1" | grep -i '^x-truncated:' | tr -d '\r')" +check 'the second page is the other block, not the first again' 'yes' \ + "$([ "$(admin "$EDGE/admin-api/blocks?source=operator&limit=1" | jget "True" "sourceKey")" != "$(admin "$EDGE/admin-api/blocks?source=operator&limit=1&offset=1" | jget "True" "sourceKey")" ] && echo yes || echo no)" +check 'a page past the end returns no rows' '0' \ + "$(admin "$EDGE/admin-api/blocks?source=operator&limit=1&offset=2" | jcount "True")" +check 'and does not claim a cut that did not happen' 'X-Truncated: false' \ + "$(hdr -H "X-Admin-Token: $OPENWAF_ADMIN_TOKEN" "$EDGE/admin-api/blocks?source=operator&limit=1&offset=2" | grep -i '^x-truncated:' | tr -d '\r')" +# Bounds, on the wire: a limit nobody meant used to be answered with one row. +check 'a limit of zero is refused, not served as one row' '400' \ + "$(code -H "X-Admin-Token: $OPENWAF_ADMIN_TOKEN" "$EDGE/admin-api/observations?limit=0")" +check 'and the refusal names the range and the value' 'limit must be 1..1000; got 0' \ + "$(admin "$EDGE/admin-api/observations?limit=0" | jval error)" +check 'a limit above the ceiling is refused too' '400' \ + "$(code -H "X-Admin-Token: $OPENWAF_ADMIN_TOKEN" "$EDGE/admin-api/observations?limit=100000")" +check 'naming the ceiling it hit' 'limit must be 1..1000; got 100000' \ + "$(admin "$EDGE/admin-api/observations?limit=100000" | jval error)" +check 'the alert listing refuses both ends the same way' '400 400' \ + "$(code -H "X-Admin-Token: $OPENWAF_ADMIN_TOKEN" "$EDGE/admin-api/alerts?limit=0") $(code -H "X-Admin-Token: $OPENWAF_ADMIN_TOKEN" "$EDGE/admin-api/alerts?limit=100000")" +check 'the streak map refuses them the same way' '400 400' \ + "$(code -H "X-Admin-Token: $OPENWAF_ADMIN_TOKEN" "$EDGE/admin-api/streaks?limit=0") $(code -H "X-Admin-Token: $OPENWAF_ADMIN_TOKEN" "$EDGE/admin-api/streaks?limit=100000")" +check 'the block listing quotes its own, wider, ceiling' 'limit must be 1..2000; got 0' \ + "$(admin "$EDGE/admin-api/blocks?limit=0" | jval error)" +check 'a negative offset is refused rather than read as zero' '400' \ + "$(code -H "X-Admin-Token: $OPENWAF_ADMIN_TOKEN" "$EDGE/admin-api/observations?offset=-1")" +check 'and says which parameter it means' 'offset must be 0 or more; got -1' \ + "$(admin "$EDGE/admin-api/observations?offset=-1" | jval error)" +check 'the same negative offset on the block listing' 'offset must be 0 or more; got -1' \ + "$(admin "$EDGE/admin-api/blocks?offset=-1" | jval error)" +check 'and on the alert listing' 'offset must be 0 or more; got -1' \ + "$(admin "$EDGE/admin-api/alerts?offset=-1" | jval error)" +# The streak map, with the two counts deliberately different: two sources with +# something standing, one whose run ended on a clean answer and therefore stays +# in the map while the listing leaves it out. That gap is what makes the flag +# testable — with the two numbers equal, counting the map instead of the listing +# answers correctly by accident. +code -H "$(xff 203.0.113.186)" "$EDGE/streak-a" >/dev/null +code -H "$(xff 203.0.113.186)" "$EDGE/streak-a2" >/dev/null +code -H "$(xff 203.0.113.187)" "$EDGE/streak-b" >/dev/null +code -H "$(xff 203.0.113.188)" "$EDGE/streak-c" >/dev/null +code -H "$(xff 203.0.113.188)" "$EDGE/streak-c2" >/dev/null +CLEAN_CODE=$(code -H "$(xff 203.0.113.188)" "$EDGE/") +printf ' info the clean answer that ends a streak, from the upstream root: %s\n' "$CLEAN_CODE" +check 'a clean answer is available to end a streak with' '200' "$CLEAN_CODE" +streaks_fetch; streaks_info +check 'the map holds more sources than it has streaks to list' 'yes' \ + "$(printf '%s' "$STREAKS_JSON" | python3 -c "import json,sys +d=json.load(sys.stdin); print('yes' if d['trackedSources']>d['standingStreaks'] and d['standingStreaks']>=2 else 'no')")" +check 'a listing that returned every standing streak says so' 'X-Truncated: false' \ + "$(hdr -H "X-Admin-Token: $OPENWAF_ADMIN_TOKEN" "$EDGE/admin-api/streaks?limit=1000" | grep -i '^x-truncated:' | tr -d '\r')" +check 'the body names both counts side by side' 'yes' \ + "$(printf '%s' "$STREAKS_JSON" | python3 -c "import json,sys +d=json.load(sys.stdin) +print('yes' if d['standingStreaks']==len(d['streaks']) and d['trackedSources']>d['standingStreaks'] else 'no')")" +check 'a one-row streak page says it cut the list' 'X-Truncated: true' \ + "$(hdr -H "X-Admin-Token: $OPENWAF_ADMIN_TOKEN" "$EDGE/admin-api/streaks?limit=1" | grep -i '^x-truncated:' | tr -d '\r')" +check 'the second streak page is a different source' 'yes' \ + "$([ "$(printf '%s' "$STREAKS_JSON" | python3 -c "import json,sys; print(json.load(sys.stdin)['streaks'][0]['sourceKey'])")" != "$(admin "$EDGE/admin-api/streaks?limit=1&offset=1" | python3 -c "import json,sys; print(json.load(sys.stdin)['streaks'][0]['sourceKey'])")" ] && echo yes || echo no)" +check 'a filtered streak answer is not reported as a cut' 'X-Truncated: false' \ + "$(hdr -H "X-Admin-Token: $OPENWAF_ADMIN_TOKEN" "$EDGE/admin-api/streaks?limit=1000&source=203.0.113.186" | grep -i '^x-truncated:' | tr -d '\r')" +check 'and its flag is computed from the filtered count' 'yes' \ + "$(admin -D "$TMPH" -o "$TMPB" "$EDGE/admin-api/streaks?limit=1000&source=203.0.113.186"; truth 0)" +admin -X DELETE "$EDGE/admin-api/blocks?source=operator" >/dev/null +rm -f "$TMPH" "$TMPB" + echo printf 'compose smoke: %d passed, %d failed\n' "$PASS" "$FAIL" [ "$FAIL" -eq 0 ]