#!/bin/sh set -eu project_root=$(CDPATH='' cd -- "$(dirname -- "$0")/.." && pwd) cd "$project_root" admin_psql() { docker compose exec -T postgres sh -ec ' export PGPASSWORD=$(cat /run/secrets/postgres_admin_password) exec psql -v ON_ERROR_STOP=1 -U lab7_admin -d messenger -At ' } admin_psql <<'SQL' INSERT INTO login_rate_limits (identifier_hash, window_started_at, attempts) VALUES (decode(repeat('42', 32), 'hex'), CURRENT_TIMESTAMP - INTERVAL '1 day', 1) ON CONFLICT (identifier_hash) DO UPDATE SET window_started_at = EXCLUDED.window_started_at, attempts = EXCLUDED.attempts; INSERT INTO login_rate_limits (identifier_hash, window_started_at, attempts) VALUES (decode(repeat('43', 32), 'hex'), CURRENT_TIMESTAMP, 4) ON CONFLICT (identifier_hash) DO UPDATE SET window_started_at = EXCLUDED.window_started_at, attempts = EXCLUDED.attempts; SQL docker compose up -d --no-deps --no-build --force-recreate api ingress >/dev/null attempts=0 until curl --silent --show-error --fail --cacert secrets/pki/ca.crt \ https://127.0.0.1:${HTTPS_PORT:-8443}/health/live >/dev/null 2>&1; do attempts=$((attempts + 1)) [ "$attempts" -lt 40 ] || { printf '%s\n' "API did not become ready for pruning test" >&2 exit 1 } sleep 1 done status=$(curl --silent --show-error --cacert secrets/pki/ca.crt \ --user pruning-probe:wrong-password --output /dev/null --write-out '%{http_code}' \ --request POST https://127.0.0.1:${HTTPS_PORT:-8443}/api/v1/auth/sessions) [ "$status" = 401 ] || { printf '%s\n' "Pruning probe returned HTTP $status, expected 401" >&2 exit 1 } remaining=$(admin_psql <<'SQL' SELECT count(*) FROM login_rate_limits WHERE identifier_hash = decode(repeat('42', 32), 'hex'); SQL ) [ "$remaining" = 0 ] || { printf '%s\n' "Expired login limiter row was not pruned" >&2 exit 1 } active_attempts=$(admin_psql <<'SQL' SELECT attempts FROM login_rate_limits WHERE identifier_hash = decode(repeat('43', 32), 'hex'); SQL ) [ "$active_attempts" = 4 ] || { printf '%s\n' "Active login limiter row was pruned or changed" >&2 exit 1 } admin_psql <<'SQL' DELETE FROM login_rate_limits WHERE identifier_hash = decode(repeat('43', 32), 'hex'); SQL printf '%s\n' "PASS: bounded pruning removes expired state and preserves active limiter windows"