forked from mtusi2026/mtusi2026lab3
539 lines
28 KiB
Python
539 lines
28 KiB
Python
#!/usr/bin/env python3
|
|
"""Lab2 auth acceptance tests. Uses the unchanged teacher HTTP client.
|
|
|
|
No server startup, automatic Docker actions, retries of writes or secret output.
|
|
Infrastructure hooks run only in explicitly selected suites.
|
|
"""
|
|
import argparse
|
|
import base64
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
import datetime as dt
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
import sys
|
|
import threading
|
|
import time
|
|
import unittest
|
|
import uuid
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / "tests"))
|
|
from client import Client, Failure, LAB, check, timestamp # noqa: E402
|
|
|
|
LOGIN = "/api/v1/auth/sessions"
|
|
LOGOUT = LOGIN + "/current"
|
|
ME = "/api/v1/users/me"
|
|
ARGS = None
|
|
|
|
|
|
def basic(username, password):
|
|
value = base64.b64encode((username + ":" + password).encode("utf-8")).decode("ascii")
|
|
return {"Authorization": "Basic " + value}
|
|
|
|
|
|
def registration(password=None):
|
|
return {
|
|
"username": "auth_" + uuid.uuid4().hex[:20],
|
|
"display_name": "Проверка auth",
|
|
"password": password if password is not None else "Aa:" + uuid.uuid4().hex,
|
|
}
|
|
|
|
|
|
def new_client(timeout=None):
|
|
return Client(ARGS.base_url, ARGS.ca_file, timeout=timeout or ARGS.timeout)
|
|
|
|
|
|
def error(c, method, path, status, code, **kwargs):
|
|
if LAB >= 3 and method == "POST" and path.endswith("/messages"):
|
|
kwargs["headers"] = {**kwargs.get("headers", {}), "Idempotency-Key": uuid.uuid4().hex}
|
|
response = c.call(method, path, status, **kwargs)
|
|
check(response["json"]["error"]["code"] == code, "incorrect contract error code")
|
|
if status == 401:
|
|
challenge = ('Basic realm="messenger", charset="UTF-8"'
|
|
if path == LOGIN else 'Bearer realm="messenger"')
|
|
check(response["headers"].get("WWW-Authenticate") == challenge,
|
|
"incorrect authentication challenge")
|
|
return response
|
|
|
|
|
|
def create_actor(c, password=None):
|
|
body = registration(password)
|
|
user = c.call("POST", "/api/v1/users", 201, body=body, schema="User")["json"]
|
|
session = c.call("POST", LOGIN, 201,
|
|
headers=basic(body["username"], body["password"]), schema="Session")["json"]
|
|
check(session["user"] == user, "login returned a different user")
|
|
check(timestamp(session["expires_at"]) > dt.datetime.now(dt.timezone.utc),
|
|
"new session is already expired")
|
|
return {"id": user["id"], "user": user, "registration": body,
|
|
"token": session["token"], "expires_at": session["expires_at"]}
|
|
|
|
|
|
def login(c, actor):
|
|
return c.call("POST", LOGIN, 201, schema="Session",
|
|
headers=basic(actor["registration"]["username"],
|
|
actor["registration"]["password"]))["json"]
|
|
|
|
|
|
def chat(c, owner, members=None):
|
|
members = members if members is not None else [owner["id"]]
|
|
return c.call("POST", "/api/v1/chats", 201, actor=owner,
|
|
body={"title": "Auth regression", "member_ids": members}, schema="Chat")["json"]
|
|
|
|
|
|
class AuthContract(unittest.TestCase):
|
|
"""Independent cases: a logout or rejection never poisons another test."""
|
|
|
|
def setUp(self):
|
|
self.c = new_client()
|
|
|
|
def test_public_health_and_registration(self):
|
|
for path in ("/health/live", "/health/ready"):
|
|
with self.subTest(path=path):
|
|
self.c.call("GET", path, schema="Health")
|
|
body = registration()
|
|
# Registration is public even when a stale credential is supplied.
|
|
user = self.c.call("POST", "/api/v1/users", 201, body=body,
|
|
headers={"Authorization": "Bearer invalid"}, schema="User")["json"]
|
|
check(user["username"] == body["username"], "username changed")
|
|
check(user["display_name"] == body["display_name"], "display_name changed")
|
|
|
|
def test_password_unicode_boundaries_and_colon(self):
|
|
for label, password in (
|
|
("ascii_min", "a" * 12), ("ascii_max", "b" * 128),
|
|
("unicode_min", "😀" * 12), ("unicode_max", "😀" * 128),
|
|
("colon", "начало:пароль:конец"),
|
|
("spaces_are_significant", " significant-password "),
|
|
):
|
|
with self.subTest(case=label):
|
|
actor = create_actor(self.c, password)
|
|
me = self.c.call("GET", ME, actor=actor, schema="User")["json"]
|
|
check(me == actor["user"], "current user differs from registration")
|
|
altered = password.strip() if password != password.strip() else password + "x"
|
|
error(self.c, "POST", LOGIN, 401, "unauthorized",
|
|
headers=basic(actor["registration"]["username"], altered))
|
|
|
|
def test_invalid_password_length_and_blank(self):
|
|
for label, value in (
|
|
("empty", ""), ("ascii_short", "a" * 11), ("ascii_long", "a" * 129),
|
|
("unicode_short", "😀" * 11), ("unicode_long", "😀" * 129),
|
|
("blank_ascii", " " * 12), ("blank_unicode", "\u2003" * 12),
|
|
):
|
|
with self.subTest(case=label):
|
|
body = registration(value)
|
|
error(self.c, "POST", "/api/v1/users", 400, "invalid_request", body=body)
|
|
|
|
def test_registration_required_fields_types_and_names(self):
|
|
for field in ("username", "display_name", "password"):
|
|
for label, value in (("missing", None), ("null", None), ("integer", 1),
|
|
("boolean", True), ("array", []), ("object", {})):
|
|
with self.subTest(field=field, case=label):
|
|
body = registration()
|
|
if label == "missing":
|
|
body.pop(field)
|
|
else:
|
|
body[field] = value
|
|
error(self.c, "POST", "/api/v1/users", 400, "invalid_request", body=body)
|
|
for alias in (field.upper(), field[0].upper() + field[1:]):
|
|
for extra in (False, True):
|
|
with self.subTest(field=field, alias=alias, extra=extra):
|
|
body = registration()
|
|
body[alias] = body[field] if extra else body.pop(field)
|
|
error(self.c, "POST", "/api/v1/users", 400, "invalid_request", body=body)
|
|
for root in (None, True, 1, "string", []):
|
|
with self.subTest(root_type=type(root).__name__):
|
|
error(self.c, "POST", "/api/v1/users", 400, "invalid_request",
|
|
raw=json.dumps(root).encode(), headers={"Content-Type": "application/json"})
|
|
body = registration()
|
|
body["sender_id"] = str(uuid.uuid4())
|
|
error(self.c, "POST", "/api/v1/users", 400, "invalid_request", body=body)
|
|
|
|
def test_registration_bytes_json_and_content_type(self):
|
|
for label, raw in (
|
|
("empty", b""), ("malformed", b'{"username":'),
|
|
("second_json", json.dumps(registration()).encode() + b"{}"),
|
|
("trailing_garbage", json.dumps(registration()).encode() + b" garbage"),
|
|
):
|
|
with self.subTest(case=label):
|
|
error(self.c, "POST", "/api/v1/users", 400, "invalid_request",
|
|
raw=raw, headers={"Content-Type": "application/json"})
|
|
for field in ("username", "display_name", "password"):
|
|
for label, bad in (("ff", b"\xff"), ("overlong", b"\xc0\xaf"),
|
|
("truncated", b"\xe2\x82"), ("surrogate", b"\xed\xa0\x80")):
|
|
with self.subTest(field=field, encoding=label):
|
|
body = registration()
|
|
body[field] = "BYTE_MARKER"
|
|
raw = json.dumps(body).encode().replace(b"BYTE_MARKER", b"prefix" + bad + b"suffix")
|
|
error(self.c, "POST", "/api/v1/users", 400, "invalid_request",
|
|
raw=raw, headers={"Content-Type": "application/json"})
|
|
for media in ("text/plain", "application/octet-stream"):
|
|
with self.subTest(media=media):
|
|
error(self.c, "POST", "/api/v1/users", 415, "unsupported_media_type",
|
|
raw=json.dumps(registration()).encode(), headers={"Content-Type": media})
|
|
|
|
def test_username_and_display_name_constraints(self):
|
|
for username in ("ab", "a" * 33, "ABC", "имя", "bad-name", "bad name", " bad"):
|
|
with self.subTest(case="username", length=len(username)):
|
|
body = registration()
|
|
body["username"] = username
|
|
error(self.c, "POST", "/api/v1/users", 400, "invalid_request", body=body)
|
|
for name in ("", " \t\n", "\u00a0\u2003", "😀" * 101):
|
|
with self.subTest(case="display_name", length=len(name)):
|
|
body = registration()
|
|
body["display_name"] = name
|
|
error(self.c, "POST", "/api/v1/users", 400, "invalid_request", body=body)
|
|
for length in (1, 100):
|
|
with self.subTest(case="valid_display_name", length=length):
|
|
body = registration()
|
|
body["display_name"] = "😀" * length
|
|
user = self.c.call("POST", "/api/v1/users", 201, body=body, schema="User")["json"]
|
|
check(user["display_name"] == body["display_name"], "Unicode display name changed")
|
|
body = registration()
|
|
body["username"] = uuid.uuid4().hex
|
|
user = self.c.call("POST", "/api/v1/users", 201, body=body, schema="User")["json"]
|
|
check(user["username"] == body["username"], "maximum-length username changed")
|
|
|
|
def test_wrong_credentials_have_same_public_error(self):
|
|
actor = create_actor(self.c)
|
|
responses = []
|
|
for label, username, password in (
|
|
("wrong_password", actor["registration"]["username"], "wrong-password"),
|
|
("unknown_user", registration()["username"], actor["registration"]["password"]),
|
|
):
|
|
with self.subTest(case=label):
|
|
responses.append(error(self.c, "POST", LOGIN, 401, "unauthorized",
|
|
headers=basic(username, password)))
|
|
if len(responses) == 2:
|
|
check(responses[0]["json"]["error"]["message"] ==
|
|
responses[1]["json"]["error"]["message"],
|
|
"credentials errors reveal whether username exists")
|
|
self.c.call("GET", ME, actor=actor, schema="User")
|
|
|
|
def test_login_rejects_missing_or_malformed_basic(self):
|
|
actor = create_actor(self.c)
|
|
encoded_no_colon = base64.b64encode(b"username-only").decode()
|
|
for label, headers in (
|
|
("absent", {}), ("legacy_id", {"X-User-Id": actor["id"]}),
|
|
("bearer", {"Authorization": "Bearer " + actor["token"]}),
|
|
("empty_basic", {"Authorization": "Basic"}),
|
|
("invalid_base64", {"Authorization": "Basic !!!"}),
|
|
("missing_separator", {"Authorization": "Basic " + encoded_no_colon}),
|
|
("invalid_username_utf8", {"Authorization": "Basic " + base64.b64encode(b"\xffuser:synthetic-password").decode("ascii")}),
|
|
("username_contains_nul", {"Authorization": "Basic " + base64.b64encode(b"bad\x00user:synthetic-password").decode("ascii")}),
|
|
("wrong_scheme", {"Authorization": "Digest anything"}),
|
|
("empty_password", basic(actor["registration"]["username"], "")),
|
|
):
|
|
with self.subTest(case=label):
|
|
error(self.c, "POST", LOGIN, 401, "unauthorized", headers=headers)
|
|
|
|
def test_login_does_not_accept_json_credentials(self):
|
|
actor = create_actor(self.c)
|
|
body = {"username": actor["registration"]["username"],
|
|
"password": actor["registration"]["password"]}
|
|
# Authentication precedes payload validation.
|
|
error(self.c, "POST", LOGIN, 401, "unauthorized", body=body)
|
|
error(self.c, "POST", LOGIN, 400, "invalid_request", body=body,
|
|
headers=basic(body["username"], body["password"]))
|
|
|
|
def test_protected_routes_require_bearer_before_validation(self):
|
|
actor = create_actor(self.c)
|
|
basic_headers = basic(actor["registration"]["username"], actor["registration"]["password"])
|
|
unknown = str(uuid.uuid4())
|
|
routes = (
|
|
("GET", ME, None), ("GET", "/api/v1/chats", None),
|
|
("POST", "/api/v1/chats", {"title": "", "member_ids": []}),
|
|
("GET", "/api/v1/chats/" + unknown, None),
|
|
("GET", "/api/v1/chats/not-a-uuid/messages?limit=0", None),
|
|
("POST", "/api/v1/chats/" + unknown + "/messages", {"text": ""}),
|
|
("DELETE", LOGOUT, None),
|
|
)
|
|
for method, path, body in routes:
|
|
for label, headers in (
|
|
("absent", {}), ("basic", basic_headers), ("legacy_id", {"X-User-Id": actor["id"]}),
|
|
("empty_bearer", {"Authorization": "Bearer"}),
|
|
("unknown_token", {"Authorization": "Bearer " + uuid.uuid4().hex * 2}),
|
|
("space_in_token", {"Authorization": "Bearer invalid token"}),
|
|
):
|
|
with self.subTest(method=method, path=path, auth=label):
|
|
error(self.c, method, path, 401, "unauthorized", body=body, headers=headers)
|
|
|
|
def test_identity_acl_and_sender_ignore_legacy_header(self):
|
|
alice, bob, eve = (create_actor(self.c) for _ in range(3))
|
|
room = chat(self.c, alice, [alice["id"], bob["id"]])
|
|
path = "/api/v1/chats/" + room["id"]
|
|
me = self.c.call("GET", ME, actor=alice, headers={"X-User-Id": eve["id"]},
|
|
schema="User")["json"]
|
|
check(me == alice["user"], "legacy ID overrode session identity")
|
|
result = self.c.call("POST", path + "/messages", 201, actor=alice,
|
|
headers={"X-User-Id": bob["id"],
|
|
**({"Idempotency-Key": uuid.uuid4().hex} if LAB >= 3 else {})},
|
|
body={"text": "Auth sender"}, schema="Message")["json"]
|
|
check(result["sender_id"] == alice["id"], "sender not derived from session")
|
|
for suffix in ("", "/messages"):
|
|
error(self.c, "GET", path + suffix, 404, "not_found", actor=eve,
|
|
headers={"X-User-Id": alice["id"]})
|
|
error(self.c, "POST", path + "/messages", 404, "not_found", actor=eve,
|
|
body={"text": "Forbidden"}, headers={"X-User-Id": alice["id"]})
|
|
hidden = self.c.call("GET", "/api/v1/chats?limit=100", actor=eve,
|
|
schema="ChatPage")["json"]
|
|
check(all(item["id"] != room["id"] for item in hidden["items"]), "foreign chat leaked")
|
|
page = self.c.call("GET", path + "/messages", actor=bob, schema="MessagePage")["json"]
|
|
check(page["items"] == [result], "confirmed message is not immediately visible")
|
|
error(self.c, "POST", path + "/messages", 400, "invalid_request", actor=alice,
|
|
body={"text": "Forged sender", "sender_id": bob["id"]})
|
|
|
|
def test_invalid_chat_does_not_leave_visible_partial_chat(self):
|
|
actor = create_actor(self.c)
|
|
before = self.c.call("GET", "/api/v1/chats?limit=100", actor=actor, schema="ChatPage")["json"]
|
|
for members in ([actor["id"], str(uuid.uuid4())],
|
|
[actor["id"], actor["id"]],
|
|
[actor["id"], actor["id"].upper()], [str(uuid.uuid4())]):
|
|
with self.subTest(member_count=len(members)):
|
|
error(self.c, "POST", "/api/v1/chats", 400, "invalid_request", actor=actor,
|
|
body={"title": "Must not exist", "member_ids": members})
|
|
after = self.c.call("GET", "/api/v1/chats?limit=100", actor=actor, schema="ChatPage")["json"]
|
|
check(after == before, "rejected chat request left visible partial data")
|
|
|
|
def test_logout_revokes_only_current_session(self):
|
|
actor = create_actor(self.c)
|
|
other = login(self.c, actor)
|
|
check(other["token"] != actor["token"], "separate logins reused a token")
|
|
self.c.call("DELETE", LOGOUT, 204, actor=actor)
|
|
error(self.c, "GET", ME, 401, "unauthorized", actor=actor)
|
|
error(self.c, "DELETE", LOGOUT, 401, "unauthorized", actor=actor)
|
|
second = dict(actor, token=other["token"])
|
|
me = self.c.call("GET", ME, actor=second, schema="User")["json"]
|
|
check(me == actor["user"], "logout revoked another session")
|
|
fresh = login(self.c, actor)
|
|
check(fresh["token"] not in (actor["token"], other["token"]), "login reused an earlier token")
|
|
self.c.call("GET", ME, actor=dict(actor, token=fresh["token"]), schema="User")
|
|
|
|
def test_session_ttl_matches_configuration(self):
|
|
actor = create_actor(self.c)
|
|
remaining = (timestamp(actor["expires_at"]) - dt.datetime.now(dt.timezone.utc)).total_seconds()
|
|
check(ARGS.ttl - 5 <= remaining <= ARGS.ttl + 5, "SESSION_TTL_SECONDS not respected")
|
|
for _ in range(3):
|
|
self.c.call("GET", ME, actor=actor, schema="User")
|
|
# Exact non-sliding deadline is checked by the storage and short-TTL suites.
|
|
|
|
def test_tokens_distinct_opaque_and_tamper_rejected(self):
|
|
actor = create_actor(self.c)
|
|
issued = [actor["token"]] + [login(self.c, actor)["token"] for _ in range(3)]
|
|
check(len(set(issued)) == len(issued), "independent logins reused tokens")
|
|
for token in issued:
|
|
check(token != actor["id"] and token != actor["registration"]["username"],
|
|
"token contains identity instead of an opaque credential")
|
|
check(token != actor["registration"]["password"], "password used as session token")
|
|
token = actor["token"]
|
|
changed = token[:-1] + ("A" if token[-1] != "A" else "B")
|
|
error(self.c, "GET", ME, 401, "unauthorized", actor=dict(actor, token=changed))
|
|
self.c.call("GET", ME, actor=actor, schema="User")
|
|
# Length/uniqueness is not proof of entropy. RNG is reviewed separately.
|
|
|
|
def test_duplicate_username_concurrent_registration(self):
|
|
body = registration()
|
|
barrier = threading.Barrier(4)
|
|
|
|
def send(index):
|
|
c = new_client()
|
|
candidate = dict(body, display_name="Race " + str(index), password=body["password"] + str(index))
|
|
barrier.wait(timeout=ARGS.timeout)
|
|
result = c.call("POST", "/api/v1/users", (201, 409), body=candidate, schema="User")
|
|
if result["status"] == 409:
|
|
check(result["json"]["error"]["code"] == "conflict", "race loser did not receive conflict")
|
|
return candidate, result
|
|
|
|
with ThreadPoolExecutor(max_workers=4) as pool:
|
|
results = list(pool.map(send, range(4)))
|
|
winners = [(body, result) for body, result in results if result["status"] == 201]
|
|
check(len(winners) == 1, "username race must have exactly one winner")
|
|
winner_body, result = winners[0]
|
|
session = self.c.call("POST", LOGIN, 201, headers=basic(winner_body["username"],
|
|
winner_body["password"]), schema="Session")["json"]
|
|
check(session["user"] == result["json"], "race winner was overwritten")
|
|
for candidate, response in results:
|
|
if response["status"] == 409:
|
|
error(self.c, "POST", LOGIN, 401, "unauthorized",
|
|
headers=basic(candidate["username"], candidate["password"]))
|
|
|
|
|
|
def hook(path):
|
|
script = Path(path).expanduser().resolve()
|
|
check(script.is_file() and os.access(script, os.X_OK), "hook must exist and be executable")
|
|
check(".example." not in script.name, "implement a real hook first")
|
|
try:
|
|
result = subprocess.run([str(script)], stdout=subprocess.DEVNULL,
|
|
stderr=subprocess.DEVNULL, timeout=ARGS.hook_timeout)
|
|
except (OSError, subprocess.TimeoutExpired):
|
|
raise Failure("infrastructure hook failed or timed out; inspect it locally") from None
|
|
check(result.returncode == 0, "infrastructure hook returned an error")
|
|
|
|
|
|
def wait_ready(c):
|
|
deadline = time.monotonic() + ARGS.hook_timeout
|
|
while True:
|
|
try:
|
|
c.call("GET", "/health/ready", schema="Health")
|
|
return
|
|
except Failure:
|
|
check(time.monotonic() < deadline, "API readiness did not recover")
|
|
time.sleep(0.25)
|
|
|
|
|
|
def expiry():
|
|
c = new_client()
|
|
actor = create_actor(c)
|
|
start = time.monotonic()
|
|
remaining = (timestamp(actor["expires_at"]) - dt.datetime.now(dt.timezone.utc)).total_seconds()
|
|
check(0 < remaining <= ARGS.ttl + 5, "short TTL not applied")
|
|
c.call("GET", ME, actor=actor, schema="User")
|
|
# Reading halfway must not renew the original deadline.
|
|
time.sleep(max(0, remaining / 2))
|
|
c.call("GET", ME, actor=actor, schema="User")
|
|
time.sleep(max(0, remaining + 0.25 - (time.monotonic() - start)))
|
|
error(c, "GET", ME, 401, "unauthorized", actor=actor)
|
|
error(c, "DELETE", LOGOUT, 401, "unauthorized", actor=actor)
|
|
fresh = login(c, actor)
|
|
check(fresh["token"] != actor["token"], "expired token reused")
|
|
c.call("GET", ME, actor=dict(actor, token=fresh["token"]), schema="User")
|
|
|
|
|
|
def persistence():
|
|
c = new_client(timeout=2)
|
|
actor = create_actor(c)
|
|
revoked = dict(actor, token=login(c, actor)["token"])
|
|
c.call("DELETE", LOGOUT, 204, actor=revoked)
|
|
room = chat(c, actor)
|
|
original = c.message(actor, room, "Auth persistence")["json"]
|
|
remaining = (timestamp(actor["expires_at"]) - dt.datetime.now(dt.timezone.utc)).total_seconds()
|
|
check(remaining > 2 * ARGS.hook_timeout + 10, "TTL too short for restart bounds")
|
|
hook(ARGS.restart_hook)
|
|
wait_ready(c)
|
|
# No re-login before checking the original token.
|
|
check(c.call("GET", ME, actor=actor, schema="User")["json"] == actor["user"],
|
|
"original session/user did not survive restart")
|
|
check(c.call("GET", "/api/v1/chats/" + room["id"], actor=actor, schema="Chat")["json"] == room,
|
|
"chat/membership changed after restart")
|
|
page = c.call("GET", "/api/v1/chats/" + room["id"] + "/messages",
|
|
actor=actor, schema="MessagePage")["json"]
|
|
check(page["items"] == [original], "confirmed message changed after restart")
|
|
error(c, "GET", ME, 401, "unauthorized", actor=revoked)
|
|
error(c, "DELETE", LOGOUT, 401, "unauthorized", actor=revoked)
|
|
c.call("DELETE", LOGOUT, 204, actor=actor)
|
|
error(c, "GET", ME, 401, "unauthorized", actor=actor)
|
|
check(login(c, actor)["user"] == actor["user"], "password/user not persistent")
|
|
# HTTP exposes expiry only at login. Storage test verifies deadline stability.
|
|
|
|
|
|
def dependency():
|
|
c = new_client()
|
|
actor = create_actor(c)
|
|
failures = []
|
|
fallback_session = None
|
|
revoked_during_outage = False
|
|
sql_with_redis = ARGS.dependency == "postgres" and getattr(ARGS, "session_store", "redis") == "redis"
|
|
if sql_with_redis:
|
|
fallback_session = dict(actor, token=login(c, actor)["token"])
|
|
|
|
def observe(name, operation):
|
|
try:
|
|
operation()
|
|
print("PASS:", name)
|
|
return True
|
|
except Failure as exc:
|
|
failures.append(name + ": " + str(exc))
|
|
print("FAIL:", name + ": " + str(exc))
|
|
return False
|
|
|
|
try:
|
|
hook(ARGS.failure_hook)
|
|
observe("liveness during outage", lambda: c.call("GET", "/health/live", schema="Health"))
|
|
critical = not (ARGS.dependency == "redis" and ARGS.cache_policy == "fallback")
|
|
if critical:
|
|
observe("readiness during outage", lambda: error(c, "GET", "/health/ready", 503, "unavailable"))
|
|
observe("valid session during outage", lambda: error(c, "GET", ME, 503, "unavailable", actor=actor))
|
|
else:
|
|
observe("fallback readiness", lambda: c.call("GET", "/health/ready", schema="Health"))
|
|
observe("fallback identity", lambda: c.call("GET", ME, actor=actor, schema="User"))
|
|
for name, headers in (("missing auth", {}), ("legacy identity", {"X-User-Id": actor["id"]})):
|
|
observe(name, lambda headers=headers: error(c, "GET", ME, 401, "unauthorized", headers=headers))
|
|
# A syntactically valid Bearer cannot be declared unknown while the
|
|
# sole session store is unavailable. The lookup failed, not identity.
|
|
unknown_status = 503 if ARGS.dependency == "redis" and critical else 401
|
|
observe("unknown bearer during outage", lambda: error(c, "GET", ME, unknown_status,
|
|
"unavailable" if unknown_status == 503 else "unauthorized",
|
|
headers={"Authorization": "Bearer " + uuid.uuid4().hex * 2}))
|
|
if critical:
|
|
observe("login during outage", lambda: error(c, "POST", LOGIN, 503, "unavailable",
|
|
headers=basic(actor["registration"]["username"], actor["registration"]["password"])))
|
|
if not critical:
|
|
fallback_session = dict(actor, token=login(c, actor)["token"])
|
|
if sql_with_redis or not critical:
|
|
revoked_during_outage = observe("Redis logout independent of SQL", lambda: c.call("DELETE", LOGOUT, 204, actor=actor))
|
|
else:
|
|
observe("logout during outage", lambda: error(c, "DELETE", LOGOUT, 503, "unavailable", actor=actor))
|
|
if ARGS.dependency == "postgres":
|
|
body = registration()
|
|
observe("registration during SQL outage", lambda: error(c, "POST", "/api/v1/users", 503, "unavailable", body=body))
|
|
finally:
|
|
hook(ARGS.recovery_hook)
|
|
observe("readiness recovers", lambda: wait_ready(c))
|
|
observe("liveness recovers", lambda: c.call("GET", "/health/live", schema="Health"))
|
|
survivor = fallback_session or actor
|
|
observe("independent session after recovery", lambda: c.call("GET", ME, actor=survivor, schema="User"))
|
|
if revoked_during_outage:
|
|
observe("logout remains revoked after recovery", lambda: error(c, "GET", ME, 401, "unauthorized", actor=actor))
|
|
if ARGS.dependency == "postgres":
|
|
observe("failed registration left no user", lambda: c.call("POST", "/api/v1/users", 201, body=body, schema="User"))
|
|
check(not failures, "; ".join(failures))
|
|
|
|
|
|
def main():
|
|
global ARGS
|
|
p = argparse.ArgumentParser(description=__doc__)
|
|
p.add_argument("--base-url", default=None)
|
|
p.add_argument("--ca-file", default=None)
|
|
p.add_argument("--timeout", type=float, default=15)
|
|
p.add_argument("--ttl", type=int, default=3600, help="actual server SESSION_TTL_SECONDS")
|
|
p.add_argument("--suite", choices=("normal", "expiry", "persistence", "dependency"), default="normal")
|
|
p.add_argument("--restart-hook")
|
|
p.add_argument("--failure-hook")
|
|
p.add_argument("--recovery-hook")
|
|
p.add_argument("--dependency", choices=("postgres", "redis"))
|
|
p.add_argument("--cache-policy", choices=("critical", "fallback"))
|
|
p.add_argument("--session-store", choices=("postgres", "redis"), default="redis",
|
|
help="declared session backend; controls logout expectation during a SQL outage")
|
|
p.add_argument("--hook-timeout", type=float, default=90)
|
|
ARGS = p.parse_args()
|
|
if LAB not in (2, 3):
|
|
p.error("this suite targets the lab2 auth contract retained in lab3")
|
|
if ARGS.timeout <= 0 or ARGS.hook_timeout <= 0:
|
|
p.error("timeouts must be positive")
|
|
if ARGS.suite == "expiry":
|
|
if not 2 <= ARGS.ttl <= 10:
|
|
p.error("expiry needs a separately configured server with TTL 2..10")
|
|
elif ARGS.ttl < 300:
|
|
p.error("normal, restart and dependency suites require TTL >= 300")
|
|
if ARGS.suite == "persistence" and not ARGS.restart_hook:
|
|
p.error("persistence requires an explicit --restart-hook")
|
|
if ARGS.suite == "dependency":
|
|
if not (ARGS.failure_hook and ARGS.recovery_hook and ARGS.dependency):
|
|
p.error("dependency suite needs failure/recovery hooks and the dependency name")
|
|
if ARGS.dependency == "redis" and not ARGS.cache_policy:
|
|
p.error("declare whether Redis is critical or has a safe fallback")
|
|
if ARGS.suite == "normal":
|
|
result = unittest.TextTestRunner(verbosity=2).run(
|
|
unittest.defaultTestLoader.loadTestsFromTestCase(AuthContract))
|
|
raise SystemExit(0 if result.wasSuccessful() else 1)
|
|
try:
|
|
{"expiry": expiry, "persistence": persistence, "dependency": dependency}[ARGS.suite]()
|
|
except (Failure, ValueError, OSError) as exc:
|
|
print("FAIL:", str(exc))
|
|
raise SystemExit(1)
|
|
print("PASS:", ARGS.suite, "(hook scope and runtime settings need separate evidence)")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|