Files
mtusi2026lab2/scripts/collect_evidence.py
2026-10-16 05:27:13 +03:00

291 lines
13 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""Собирает текстовые доказательства уровней 3–4 в evidence/*.log.
python3 scripts/collect_evidence.py # всё (~3 мин)
python3 scripts/collect_evidence.py checks level3 migrations
Секции:
checks make check, make validate, make test, student-tests
persistence docker compose ps до/после + make test-persistence
level3 Basic/Bearer/X-User-Id/logout, KDF, атомарность чата, таблицы
level4 tests/level4.py целиком (включая отказы db/cache)
migrations миграции с нуля, повторный старт, down -v — во временном
compose-проекте lab2fresh (свои volumes, api на :18090),
основной стенд и его данные не затрагиваются
Токены и пароли в логи не попадают.
"""
import argparse
import base64
import json
import statistics
import subprocess
import sys
import time
import urllib.error
import urllib.request
import uuid
from pathlib import Path
from track2_common import ROOT, Pg
BASE = "http://127.0.0.1:8080"
FRESH_PROJECT = "lab2fresh"
FRESH_PORT = 18090
PASSWORD = "Evidence-password-123"
FRESH_OVERRIDE = f"""
services:
db:
ports: !reset []
cache:
ports: !reset []
api:
ports: !override
- "{FRESH_PORT}:8080"
"""
class Log:
def __init__(self, name):
self.path = ROOT / "evidence" / name
commit = sh(["git", "rev-parse", "--short", "HEAD"]).strip()
dirty = " (+ незакоммиченные изменения)" if sh(["git", "status", "--porcelain"]).strip() else ""
self.lines = [f"# {time.strftime('%Y-%m-%d %H:%M:%S %z')}, commit {commit}{dirty}", ""]
def cmd(self, title, args, check=True):
self.lines += [f"$ {title}"]
r = subprocess.run(args, cwd=ROOT, capture_output=True, text=True)
out = (r.stdout + r.stderr).rstrip()
self.lines += [out, f"[exit {r.returncode}]", ""]
print(f" {title}: exit {r.returncode}")
if check and r.returncode != 0:
self.save()
raise SystemExit(f"FAIL: {title}\n{out[-2000:]}")
return out
def note(self, text):
self.lines.append(text)
print(" " + text)
def save(self):
self.path.write_text("\n".join(self.lines) + "\n", encoding="utf-8")
print(f"записан {self.path.relative_to(ROOT)}")
def sh(args):
return subprocess.run(args, cwd=ROOT, capture_output=True, text=True).stdout
def http(method, path, body=None, token=None, headers=None, base=BASE):
h = {"Accept": "application/json"}
data = None
if body is not None:
data = json.dumps(body).encode()
h["Content-Type"] = "application/json"
if token:
h["Authorization"] = "Bearer " + token
h.update(headers or {})
req = urllib.request.Request(base + path, data=data, headers=h, method=method)
try:
with urllib.request.urlopen(req, timeout=15) as resp:
return resp.code, dict(resp.headers), resp.read()
except urllib.error.HTTPError as e:
return e.code, dict(e.headers), e.read()
def basic(username, password):
return {"Authorization": "Basic " + base64.b64encode(f"{username}:{password}".encode()).decode()}
def masked(raw):
try:
j = json.loads(raw)
except (ValueError, TypeError):
return raw.decode(errors="replace") if isinstance(raw, bytes) else str(raw)
if isinstance(j, dict) and "token" in j:
j["token"] = f"<{len(j['token'])} hex-символов, скрыт>"
return json.dumps(j, ensure_ascii=False)
def expect(log, title, status, expected, raw=b"", headers=None):
ok = status == expected
extra = ""
if headers and status == 401:
extra = f" WWW-Authenticate: {headers.get('WWW-Authenticate')}"
log.note(f"{'OK ' if ok else 'FAIL'} {title}: HTTP {status} (ожидалось {expected}){extra}")
if raw:
log.note(f" тело: {masked(raw)[:300]}")
if not ok:
log.save()
raise SystemExit(f"FAIL: {title}")
# ---------- секции ----------
def section_checks():
log = Log("checks.log")
log.cmd("make check", ["make", "check"])
log.cmd("make validate PYTHON=.venv/bin/python", ["make", "validate", "PYTHON=.venv/bin/python"])
log.cmd("make test", ["make", "test"])
log.cmd("python3 student-tests/test_api.py", [sys.executable, "student-tests/test_api.py"])
log.save()
def section_persistence():
log = Log("persistence.log")
log.cmd("docker compose ps (до)", ["docker", "compose", "--env-file", ".env", "ps",
"--format", "table {{.Service}}\t{{.Status}}"])
log.cmd("make test-persistence ACTION_SCRIPT=scripts/restart.sh",
["make", "test-persistence", "ACTION_SCRIPT=scripts/restart.sh"])
log.cmd("docker compose ps (после: все три контейнера перезапущены)",
["docker", "compose", "--env-file", ".env", "ps",
"--format", "table {{.Service}}\t{{.Status}}"])
log.save()
def section_level3():
log = Log("level3.log")
db = Pg.compose()
log.note("## Таблицы PostgreSQL и применённые миграции")
log.lines.append(db.psql("\\dt", tuples=False).strip())
log.note("schema_migrations: " + db.scalar(
"SELECT string_agg(version::text, ',' ORDER BY version) FROM schema_migrations;"))
log.note("")
log.note("## Регистрация и password KDF")
times, user = [], None
for _ in range(5):
t0 = time.perf_counter()
status, _, raw = http("POST", "/api/v1/users", {
"username": "ev_" + uuid.uuid4().hex[:10], "display_name": "Evidence",
"password": PASSWORD})
times.append((time.perf_counter() - t0) * 1000)
expect(log, "POST /users", status, 201)
user = json.loads(raw)
log.note(f"время POST /users (5 запросов): медиана {statistics.median(times):.0f} ms, "
f"min {min(times):.0f}, max {max(times):.0f}")
log.note("в ответе нет password/password_hash: "
f"{'password' not in user and 'password_hash' not in user}")
log.note("параметры hash в БД: " + db.scalar(
"SELECT array_to_string((string_to_array(password_hash, '$'))[2:4], '$') "
f"FROM users WHERE id = '{user['id']}';") + " (соль и hash не выводятся)")
log.note("")
log.note("## Basic только на POST /auth/sessions, Bearer на остальных")
status, h, raw = http("POST", "/api/v1/auth/sessions", headers=basic(user["username"], PASSWORD))
expect(log, "Basic → POST /auth/sessions", status, 201, raw)
token = json.loads(raw)["token"]
status, h, raw = http("POST", "/api/v1/auth/sessions", headers=basic(user["username"], "wrong-password-1"))
expect(log, "неверный пароль → POST /auth/sessions", status, 401, headers=h)
status, h, raw = http("GET", "/api/v1/users/me", headers=basic(user["username"], PASSWORD))
expect(log, "Basic → GET /users/me", status, 401, headers=h)
status, h, raw = http("GET", "/api/v1/users/me", headers={"X-User-Id": user["id"]})
expect(log, "X-User-Id без Bearer → GET /users/me", status, 401, headers=h)
status, h, raw = http("GET", "/api/v1/users/me", token=token)
expect(log, "Bearer → GET /users/me", status, 200, raw)
log.note("")
log.note("## Атомарное создание чата")
title = "atomic-" + uuid.uuid4().hex[:8]
ghost = str(uuid.uuid4())
status, h, raw = http("POST", "/api/v1/chats", {"title": title, "member_ids": [user["id"], ghost]},
token=token)
expect(log, "POST /chats с несуществующим участником", status, 400, raw)
log.note(f"чатов с title={title}: " + db.scalar(f"SELECT count(*) FROM chats WHERE title = '{title}';")
+ f", строк chat_members с этим участником: "
+ db.scalar(f"SELECT count(*) FROM chat_members WHERE user_id = '{ghost}';"))
log.note("")
log.note("## Logout")
status, h, raw = http("DELETE", "/api/v1/auth/sessions/current", token=token)
expect(log, "DELETE /auth/sessions/current", status, 204)
status, h, raw = http("GET", "/api/v1/users/me", token=token)
expect(log, "тот же токен → GET /users/me", status, 401, headers=h)
status, h, raw = http("DELETE", "/api/v1/auth/sessions/current", token=token)
expect(log, "повторный logout", status, 401, headers=h)
log.save()
def section_level4():
log = Log("level4.log")
log.cmd("python3 tests/level4.py", [sys.executable, "tests/level4.py"])
log.save()
def section_migrations():
log = Log("migrations.log")
# Не /tmp: docker compose из snap видит только свой приватный /tmp.
(ROOT / "backups").mkdir(exist_ok=True)
override = str(ROOT / "backups" / f"{FRESH_PROJECT}.override.yaml")
Path(override).write_text(FRESH_OVERRIDE, encoding="utf-8")
compose = ["docker", "compose", "-p", FRESH_PROJECT, "--env-file", ".env",
"-f", "compose.yaml", "-f", override]
fdb = Pg(compose + ["exec", "-T", "db"], FRESH_PROJECT)
fresh = f"http://127.0.0.1:{FRESH_PORT}"
def count_users():
return fdb.scalar("SELECT count(*) FROM users;")
try:
log.note(f"Временный проект `{FRESH_PROJECT}`: отдельные volumes "
f"{FRESH_PROJECT}_pgdata/{FRESH_PROJECT}_valkeydata, api на :{FRESH_PORT}, "
"БД и Valkey без проброса портов. Основной стенд не затрагивается.")
log.cmd("down -v (очистка остатков прошлого запуска)", compose + ["down", "-v"], check=False)
log.cmd("up -d --wait (пустые volumes)", compose + ["up", "-d", "--no-build", "--wait"])
log.cmd("логи api: миграции на пустой БД", compose + ["logs", "api", "--no-log-prefix"])
log.note("schema_migrations: " + fdb.scalar(
"SELECT string_agg(version::text, ',' ORDER BY version) FROM schema_migrations;"))
status, _, _ = http("POST", "/api/v1/users", {
"username": "fresh_" + uuid.uuid4().hex[:8], "display_name": "F", "password": PASSWORD},
base=fresh)
log.note(f"POST /users на новом стенде: HTTP {status}; пользователей в БД: {count_users()}")
log.note("")
time.sleep(1)
restarted_at = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())
log.cmd("restart api (повторный запуск миграций)", compose + ["restart", "api"])
log.cmd("up -d --wait", compose + ["up", "-d", "--no-build", "--wait"])
out = log.cmd(f"логи api после рестарта (--since {restarted_at})",
compose + ["logs", "api", "--no-log-prefix", "--since", restarted_at])
log.note(f"строк 'applied migration' после рестарта: {out.count('applied migration')}; "
f"schema_migrations: {fdb.scalar('SELECT count(*) FROM schema_migrations;')} строки; "
f"пользователей: {count_users()} (данные не дублируются и не теряются)")
log.note("")
log.cmd("down -v (НАМЕРЕННОЕ удаление volumes временного проекта)", compose + ["down", "-v"])
log.cmd("up -d --wait", compose + ["up", "-d", "--no-build", "--wait"])
log.note(f"после down -v пользователей: {count_users()} — все данные и сессии потеряны, "
"миграции применены заново")
finally:
subprocess.run(compose + ["down", "-v"], cwd=ROOT, capture_output=True)
Path(override).unlink(missing_ok=True)
log.note(f"временный проект {FRESH_PROJECT} удалён вместе с volumes")
log.save()
SECTIONS = {
"checks": section_checks,
"persistence": section_persistence,
"level3": section_level3,
"level4": section_level4,
"migrations": section_migrations,
}
def main():
p = argparse.ArgumentParser()
p.add_argument("sections", nargs="*", help=", ".join(SECTIONS) + " (по умолчанию все)")
args = p.parse_args()
unknown = [s for s in args.sections if s not in SECTIONS]
if unknown:
p.error(f"неизвестные секции: {', '.join(unknown)}")
for name in args.sections or SECTIONS:
print(f"== {name} ==")
SECTIONS[name]()
print("PASS: evidence collected")
if __name__ == "__main__":
main()