resolve() больше не доверяет кэшу: валидность сессии всегда проверяется в PostgreSQL, поэтому неудавшаяся инвалидация при недоступном Valkey не воскрешает отозванный токен. Valkey остаётся read-through кэшем неизменяемого профиля. Добавлены scripts/check-revocation.py и student-tests/test_session_revocation.py.
80 lines
2.7 KiB
Python
80 lines
2.7 KiB
Python
#!/usr/bin/env python3
|
|
"""Regression check: logout must not be bypassed while Valkey is unavailable.
|
|
|
|
Scenario (the one that used to fail):
|
|
1. login and warm any session cache;
|
|
2. pause Valkey;
|
|
3. logout -> 204 (session row removed from PostgreSQL);
|
|
4. resume Valkey;
|
|
5. after the cache circuit breaker window, the old token must still be 401 on
|
|
every replica, and it must not be able to send a message.
|
|
|
|
Requires the docker CLI and a running lab3 stack.
|
|
"""
|
|
import argparse
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
import uuid
|
|
|
|
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "tests"))
|
|
from client import Client # noqa: E402
|
|
|
|
|
|
def _docker(*args: str) -> None:
|
|
subprocess.run(["docker", *args], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
|
|
|
|
|
def main() -> None:
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--base-url", default=os.environ.get("BASE_URL", "http://localhost:8080"))
|
|
parser.add_argument("--valkey-container", default=os.environ.get("VALKEY_CONTAINER", "messenger-lab3-valkey-1"))
|
|
parser.add_argument("--wait", type=float, default=6.0, help="seconds to wait after resuming Valkey")
|
|
args = parser.parse_args()
|
|
|
|
c = Client(args.base_url)
|
|
alice, bob, eve, chat = c.bootstrap()
|
|
|
|
# Warm the cache on both replicas.
|
|
for _ in range(6):
|
|
c.call("GET", "/api/v1/users/me", actor=alice, schema="User")
|
|
c.message(alice, chat, "warm-up before revocation")
|
|
|
|
print("Pausing Valkey (%s) ..." % args.valkey_container)
|
|
_docker("pause", args.valkey_container)
|
|
try:
|
|
logout = c.call("DELETE", "/api/v1/auth/sessions/current", 204, actor=alice)
|
|
assert logout["status"] == 204, logout["status"]
|
|
print("Logout returned 204 while the cache was unavailable.")
|
|
finally:
|
|
_docker("unpause", args.valkey_container)
|
|
print("Valkey resumed; waiting %.1fs for the cache circuit breaker ..." % args.wait)
|
|
time.sleep(args.wait)
|
|
|
|
seen = set()
|
|
for _ in range(12):
|
|
response = c.call("GET", "/api/v1/users/me", 401, actor=alice, schema="Error")
|
|
seen.add(response["headers"].get("X-Instance-Id"))
|
|
assert len(seen) >= 1, "no replica observed"
|
|
|
|
blocked = c.call(
|
|
"POST",
|
|
"/api/v1/chats/" + chat["id"] + "/messages",
|
|
401,
|
|
actor=alice,
|
|
body={"text": "must not be written"},
|
|
headers={"Idempotency-Key": uuid.uuid4().hex},
|
|
schema="Error",
|
|
)
|
|
assert blocked["status"] == 401, blocked["status"]
|
|
|
|
print(
|
|
"PASS: revoked session stays 401 through %d replica(s) and cannot post after a "
|
|
"failed cache invalidation." % len(seen)
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|