Files
mtusi2026lab4/scripts/revocation-pause.sh
T
2026-11-04 22:57:50 +03:00

45 lines
3.0 KiB
Bash
Executable File

#!/usr/bin/env bash
# Reproduces the review scenario on the running stack, everything through the balancer:
# session + message, warm the session on both replicas, CLIENT PAUSE <ms> WRITE on Redis (reads keep working),
# logout, then read and repeat the original message with the old token and key.
# Expected: logout is 204 only once the tombstone is in Redis; afterwards every replica answers 401.
# PAUSE_MS=5000 (default): the logout waits for the pause and answers 204.
# PAUSE_MS=12000: longer than the logout deadline (8 s): the logout answers 503 and every replica still rejects the token.
set -euo pipefail
cd "$(dirname "$0")/.."
PAUSE_MS=${PAUSE_MS:-5000}
B=${BASE_URL:-http://localhost:8080}/api/v1
U=pause_$RANDOM$RANDOM
PW=pause-test-password
KEY=pause-$RANDOM$RANDOM
redis() { docker compose exec -T redis redis-cli --no-auth-warning -a "$(cat secrets/redis_password)" "$@"; }
now() { python3 -c 'import time; print(time.time())'; }
status() { curl -s -o /dev/null -w '%{http_code} %header{x-instance-id}' "$@"; }
curl -s -o /dev/null -X POST "$B/users" -H 'Content-Type: application/json' \
-d "{\"username\":\"$U\",\"display_name\":\"pause\",\"password\":\"$PW\"}"
T=$(curl -s -X POST "$B/auth/sessions" -u "$U:$PW" | python3 -c 'import sys,json;print(json.load(sys.stdin)["token"])')
ME=$(curl -s "$B/users/me" -H "Authorization: Bearer $T" | python3 -c 'import sys,json;print(json.load(sys.stdin)["id"])')
CHAT=$(curl -s -X POST "$B/chats" -H "Authorization: Bearer $T" -H 'Content-Type: application/json' \
-d "{\"title\":\"pause\",\"member_ids\":[\"$ME\"]}" | python3 -c 'import sys,json;print(json.load(sys.stdin)["id"])')
send() { status -X POST "$B/chats/$CHAT/messages" -H "Authorization: Bearer $T" -H 'Content-Type: application/json' \
-H "Idempotency-Key: $KEY" -d '{"text":"original message"}'; }
echo "send message with Idempotency-Key: $(send)"
echo "warm the session on both replicas:"
for i in 1 2 3 4; do echo " GET /users/me -> $(status "$B/users/me" -H "Authorization: Bearer $T")"; done
echo "redis: CLIENT PAUSE $PAUSE_MS WRITE -> $(redis CLIENT PAUSE "$PAUSE_MS" WRITE)"
start=$(now)
echo "DELETE /auth/sessions/current -> $(status -X DELETE "$B/auth/sessions/current" -H "Authorization: Bearer $T") after $(python3 -c "print(round($(now) - $start, 2))") s"
sleep 1.5 # replicas check pending revocations once a second; only matters for the unconfirmed (503) case
echo "old token after logout (redis writes still paused: $(python3 -c "print('yes' if $(now) - $start < $PAUSE_MS / 1000 else 'no')")):"
for i in 1 2 3 4; do echo " GET /users/me -> $(status "$B/users/me" -H "Authorization: Bearer $T")"; done
for i in 1 2; do echo " POST message, same key -> $(send)"; done
python3 -c "import time; time.sleep(max(0, $start + $PAUSE_MS / 1000 + 2 - time.time()))"
echo "after the pause: session key in redis = $(redis GET "session:$(printf %s "$T" | shasum -a 256 | cut -d' ' -f1)")"
for i in 1 2; do echo " GET /users/me -> $(status "$B/users/me" -H "Authorization: Bearer $T")"; done