#!/usr/bin/env python3 """Проверки контракта и уровня 4, которым не нужно останавливать Redis/PostgreSQL.""" import json import time import urllib.error import urllib.request import uuid from concurrent.futures import ThreadPoolExecutor from pathlib import Path from urllib.parse import quote from httpapi import basic, bearer, call from sql import compose, psql ROOT = Path(__file__).resolve().parents[1] EVIDENCE = ROOT / "evidence" / "student_checks.txt" LINES = [] def log(text): print(text) LINES.append(text) def check(cond, message): if not cond: log("FAIL: " + message) raise SystemExit(1) def register(username, display_name, password): status, _, payload = call( "POST", "/api/v1/users", {"username": username, "display_name": display_name, "password": password}, ) check(status == 201, "register {} -> {}".format(username, status)) check("password" not in payload and "password_hash" not in payload, "password leaked") return payload def login(username, password): status, headers, payload = call("POST", "/api/v1/auth/sessions", headers=basic(username, password)) check(status == 201, "login -> {}".format(status)) check(headers.get("X-Request-Id"), "no request id") check(payload["token_type"] == "Bearer", "token type") check(len(payload["token"]) >= 43, "token entropy/length") check(username not in payload["token"], "token contains username") return payload def main(): suffix = uuid.uuid4().hex[:8] req = urllib.request.Request( "http://127.0.0.1:8080/api/v1/users", data=b"{not-json", headers={"Content-Type": "application/json", "Accept": "application/json"}, method="POST", ) try: urllib.request.urlopen(req, timeout=30) check(False, "bad json accepted") except urllib.error.HTTPError as error: body = json.loads(error.read().decode("utf-8")) check(error.code == 400 and body["error"]["code"] == "invalid_request", "bad json") check(body["error"]["request_id"] == error.headers["X-Request-Id"], "request id") status, _, _ = call( "POST", "/api/v1/users", {"username": "abc", "display_name": "Имя", "password": "short-pass", "extra": 1}, ) check(status == 400, "extra field {}".format(status)) status, _, _ = call( "POST", "/api/v1/users", {"username": "ab", "display_name": "Имя", "password": "long-enough-pw"}, ) check(status == 400, "short username") status, _, _ = call( "POST", "/api/v1/users", {"username": "ok_" + suffix[:4], "display_name": "Имя", "password": "short"}, ) check(status == 400, "short password") password = "pw-" + suffix + "-ok" alice = register("alice_" + suffix, "Алиса", password) again, _, _ = call( "POST", "/api/v1/users", {"username": alice["username"], "display_name": "Другая", "password": password + "-x"}, ) check(again == 409, "duplicate username {}".format(again)) session = login(alice["username"], password) token = session["token"] check(session["user"] == alice, "session user differs") status, headers, payload = call( "POST", "/api/v1/auth/sessions", headers=basic(alice["username"], "wrong-password-1"), ) check(status == 401 and payload["error"]["code"] == "unauthorized", "wrong password") check(headers.get("WWW-Authenticate", "").startswith("Basic"), "basic challenge") status, _, payload_unknown = call( "POST", "/api/v1/auth/sessions", headers=basic("missing_" + suffix, "wrong-password-1"), ) check(status == 401 and payload_unknown["error"]["code"] == payload["error"]["code"], "user enum") status, headers, _ = call("GET", "/api/v1/users/me", headers=basic(alice["username"], password)) check(status == 401 and headers.get("WWW-Authenticate", "").startswith("Bearer"), "basic on business") status, _, _ = call("GET", "/api/v1/users/me", headers={"X-User-Id": alice["id"]}) check(status == 401, "x-user-id alone") status, _, me = call( "GET", "/api/v1/users/me", headers={**bearer(token), "X-User-Id": str(uuid.uuid4())}, ) check(status == 200 and me["id"] == alice["id"], "x-user-id overrode bearer") colon_pw = "secret:part-" + suffix colon_user = register("colon_" + suffix, "Двоеточие", colon_pw) colon_session = login(colon_user["username"], colon_pw) check(colon_session["user"]["id"] == colon_user["id"], "colon password") uni_pw = "пароль-тест-" + suffix uni_user = register("uni_" + suffix, "Юникод", uni_pw) login(uni_user["username"], uni_pw) bob = register("bob_" + suffix, "Боб", password) eve = register("eve_" + suffix, "Ева", password) bob_token = login(bob["username"], password)["token"] eve_token = login(eve["username"], password)["token"] status, _, chat = call( "POST", "/api/v1/chats", {"title": "Проверка", "member_ids": [alice["id"], bob["id"]]}, bearer(token), ) check(status == 201, "chat {}".format(status)) check(set(chat["member_ids"]) == {alice["id"], bob["id"]}, "members") status, _, _ = call( "POST", "/api/v1/chats", {"title": "Без себя", "member_ids": [bob["id"]]}, bearer(token), ) check(status == 400, "caller not in chat") missing = str(uuid.uuid4()) before = int(psql("SELECT count(*) FROM chats;")) status, _, _ = call( "POST", "/api/v1/chats", {"title": "Битый участник", "member_ids": [alice["id"], missing]}, bearer(token), ) after = int(psql("SELECT count(*) FROM chats;")) check(status == 400 and before == after, "chat rollback left a row {} {}".format(before, after)) psql("DELETE FROM chats WHERE title = 'rollback-probe';") broken = compose( "exec", "-T", "postgres", "psql", "-U", "messenger", "-d", "messenger", "-v", "ON_ERROR_STOP=1", "-q", input_bytes=b""" BEGIN; INSERT INTO chats (id, title, created_at) VALUES ('aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaa1', 'rollback-probe', now()); INSERT INTO chat_members (chat_id, user_id, ordinal) VALUES ('aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaa1', 'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaa2', 0); COMMIT; """, check=False, ) check(broken.returncode != 0, "fk insert unexpectedly committed") left = int(psql("SELECT count(*) FROM chats WHERE title = 'rollback-probe';")) check(left == 0, "sql rollback probe left a chat") log("rollback: api count {} -> {}, sql probe rows {}".format(before, after, left)) status, _, _ = call("GET", "/api/v1/chats/" + chat["id"], headers=bearer(eve_token)) check(status == 404, "foreign chat") status, _, _ = call( "POST", "/api/v1/chats/" + chat["id"] + "/messages", {"text": "чужое"}, bearer(eve_token), ) check(status == 404, "foreign message") status, _, listed = call("GET", "/api/v1/chats?limit=100", headers=bearer(eve_token)) check(status == 200 and all(item["id"] != chat["id"] for item in listed["items"]), "chat leak") sent = [] for index in range(5): actor = token if index % 2 == 0 else bob_token status, _, message = call( "POST", "/api/v1/chats/" + chat["id"] + "/messages", {"text": "сообщение {}".format(index)}, bearer(actor), ) check(status == 201, "message {}".format(status)) sent.append(message["id"]) status, _, _ = call( "POST", "/api/v1/chats/" + chat["id"] + "/messages", {"text": " "}, bearer(token), ) check(status == 400, "blank message") collected = [] cursor = None pages = 0 while True: path = "/api/v1/chats/" + chat["id"] + "/messages?limit=2" if cursor: path += "&cursor=" + quote(cursor, safe="") status, _, page = call("GET", path, headers=bearer(bob_token)) check(status == 200, "page") collected.extend(item["id"] for item in page["items"]) pages += 1 if page["next_cursor"] is None: break cursor = page["next_cursor"] check(pages < 10, "cursor loop") check(pages >= 3 and collected == sent, "pagination {} {}".format(pages, collected)) other_status, _, other = call( "POST", "/api/v1/chats", {"title": "Второй", "member_ids": [alice["id"], bob["id"]]}, bearer(token), ) check(other_status == 201, "second chat") call( "POST", "/api/v1/chats/" + chat["id"] + "/messages", {"text": "для курсора"}, bearer(token), ) call( "POST", "/api/v1/chats/" + chat["id"] + "/messages", {"text": "ещё"}, bearer(bob_token), ) _, _, first_page = call( "GET", "/api/v1/chats/" + chat["id"] + "/messages?limit=1", headers=bearer(token), ) stolen, _, _ = call( "GET", "/api/v1/chats/" + other["id"] + "/messages?limit=1&cursor=" + quote(first_page["next_cursor"], safe=""), headers=bearer(token), ) check(stolen == 400, "foreign cursor {}".format(stolen)) race_name = "race_" + suffix race_password = "race-password-" + suffix def once(_n): return call( "POST", "/api/v1/users", {"username": race_name, "display_name": "Гонка", "password": race_password}, )[0] with ThreadPoolExecutor(max_workers=12) as pool: codes = list(pool.map(once, range(12))) winners = codes.count(201) conflicts = codes.count(409) rows = int(psql("SELECT count(*) FROM users WHERE username = '{}';".format(race_name))) check(winners == 1 and conflicts == 11 and rows == 1, "race {} rows {}".format(codes, rows)) log("username race: one 201, eleven 409, one row") status, _, _ = call("DELETE", "/api/v1/auth/sessions/current", headers=bearer(token)) check(status == 204, "logout {}".format(status)) status, _, _ = call("GET", "/api/v1/users/me", headers=bearer(token)) check(status == 401, "revoked token") status, _, _ = call("DELETE", "/api/v1/auth/sessions/current", headers=bearer(token)) check(status == 401, "second logout") users_before = psql("SELECT count(*) FROM users;") versions_before = psql("SELECT version FROM schema_migrations ORDER BY version;") compose("restart", "api") deadline = time.time() + 60 while True: try: ready, _, _ = call("GET", "/health/ready", timeout=3) except Exception: ready = 0 if ready == 200: break if time.time() > deadline: raise SystemExit("FAIL: api did not come back after migration restart") time.sleep(0.5) users_after = psql("SELECT count(*) FROM users;") versions_after = psql("SELECT version FROM schema_migrations ORDER BY version;") check(users_before == users_after and versions_before == versions_after, "migration duplicated data") log("migrations reapplied, users {} -> {}, versions {}".format(users_before, users_after, versions_after)) redis_info = compose("exec", "-T", "redis", "redis-cli", "INFO", "persistence").stdout.decode("utf-8", "replace") interesting = [ line.strip() for line in redis_info.splitlines() if line.startswith(("aof_enabled", "aof_last_write_status", "aof_current_size", "rdb_last_bgsave_status")) ] log("redis persistence: " + ", ".join(interesting)) check(any(line == "aof_enabled:1" for line in interesting), "aof is off") log("PASS student checks") EVIDENCE.write_text("\n".join(LINES) + "\n", encoding="utf-8") if __name__ == "__main__": try: main() except SystemExit: if LINES: EVIDENCE.write_text("\n".join(LINES) + "\n", encoding="utf-8") raise except Exception as exc: log("FAIL: {}: {}".format(type(exc).__name__, exc)) EVIDENCE.write_text("\n".join(LINES) + "\n", encoding="utf-8") raise SystemExit(1)