Files
mtusi2026lab7/student-tests/lab5lib.py
T
zemiles 9cc80d2529
Application / app (pull_request) Canceled after 0s
Skeleton contracts / contracts (pull_request) Canceled after 0s
ЛР 7 — БПИ2303 — Лазарев — на 3
Перенесена реализация lab6; бизнес-API без изменений, транспорт — HTTPS.

- Внешний HTTPS (public-ca) на 127.0.0.1:8443; HSTS, TLS >= 1.2.
- Обязательное mTLS LB -> каждая API-реплика: API проверяет цепочку
  internal-ca и SAN клиента (allowlist: lb); health только на 127.0.0.1.
- TLS с проверкой сервера ко всем зависимостям: PostgreSQL verify-full
  (pg_hba только hostssl), HTTPS S3, AMQPS, OTLP/HTTPS с client cert,
  mTLS Collector -> Prometheus/Loki/Tempo, Grafana -> datasources, scrape.
- OpenBao 2.7.2 не в dev-mode: Raft на volume, init/unseal оператором,
  auth/cert с ролью и policy на сервис, TTL 15 мин + renew/re-login.
- Отдельные роли PostgreSQL с грантами по таблицам; разные ключи S3 и
  пользователи брокера выдаются из OpenBao.
- Argon2id (m=19 MiB, t=2, p=1, замер на стенде) и rehash при входе.
- Случайный X-Instance-Id; hardening контейнеров (non-root, cap_drop ALL,
  no-new-privileges, read-only, лимиты); TCP_NODELAY на сокете API.
- Модель угроз, trust matrix, отрицательные тесты TLS/mTLS, API, OpenBao,
  hardening; evidence, в т.ч. прогон из чистого clone.
2026-11-18 23:59:35 +03:00

176 lines
7.1 KiB
Python

"""Общие помощники проверок lab5 (запускаются на хосте, управляют стендом через docker compose).
Токены сессий живут только в памяти процесса и не печатаются. В evidence пишутся ID,
статусы, времена и события заданий — без секретов и содержимого файлов.
"""
import datetime as dt
import json
import os
import random
import struct
import subprocess
import sys
import time
import zlib
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "tests"))
from client import Client, Failure, check # noqa: E402,F401
SAMPLE_PNG = (ROOT / "tests/fixtures/sample.png").read_bytes()
def now():
return dt.datetime.now(dt.timezone.utc).strftime("%H:%M:%S.%f")[:-3]
class Log:
def __init__(self, name):
self.name = name
self.steps = []
self.started = time.monotonic()
def step(self, text, **data):
item = {"t": round(time.monotonic() - self.started, 2), "utc": now(), "step": text}
item.update(data)
self.steps.append(item)
print("[%7.2fs] %s %s" % (item["t"], text, json.dumps(data, ensure_ascii=False) if data else ""), flush=True)
def save(self, path, **extra):
if not path:
return
out = {"scenario": self.name, "date_utc": dt.datetime.now(dt.timezone.utc).isoformat(timespec="seconds"),
"steps": self.steps}
out.update(extra)
Path(path).parent.mkdir(parents=True, exist_ok=True)
Path(path).write_text(json.dumps(out, ensure_ascii=False, indent=1) + "\n", encoding="utf-8")
def compose(*args, env=None, timeout=300, ok=True):
full_env = dict(os.environ)
full_env.update(env or {})
proc = subprocess.run(["docker", "compose", *args], cwd=ROOT, capture_output=True, text=True,
env=full_env, timeout=timeout)
if ok and proc.returncode != 0:
raise Failure("docker compose %s failed: %s" % (" ".join(args[:3]), proc.stderr.strip()[-400:]))
return proc
def recreate(service, env=None):
"""Пересоздать сервис с переопределёнными переменными (shell env важнее .env)."""
compose("up", "-d", "--no-deps", "--force-recreate", "--wait", service, env=env, timeout=400)
def sql(query):
proc = subprocess.run(
["docker", "compose", "exec", "-T", "db", "sh", "-c",
'su-exec postgres psql -U postgres -d "$POSTGRES_DB" -tA -F "|" -v ON_ERROR_STOP=1'],
cwd=ROOT, input=query, capture_output=True, text=True, timeout=60)
if proc.returncode != 0:
raise Failure("psql failed: " + proc.stderr.strip()[-300:])
return [line.split("|") for line in proc.stdout.strip().splitlines() if line]
def ops(*args, ok=True):
proc = compose("--profile", "tools", "run", "--rm", "-T", "ops", *args, ok=False, timeout=120)
if ok and proc.returncode not in (0, 3):
raise Failure("ops %s failed: %s" % (args[0], proc.stderr.strip()[-400:]))
try:
return json.loads(proc.stdout)
except ValueError:
raise Failure("ops output is not JSON: " + proc.stdout[-300:] + proc.stderr[-300:])
def queues():
proc = compose("exec", "-T", "broker", "rabbitmqctl", "-q", "-p", "messenger", "list_queues", "name",
"messages", "messages_ready", "messages_unacknowledged", "consumers", "--formatter", "json")
return {q["name"]: {k: q[k] for k in ("messages", "messages_ready", "messages_unacknowledged", "consumers")}
for q in json.loads(proc.stdout)}
def container_ids(service, running_only=True):
args = ["ps", "-q"] + (["--status", "running"] if running_only else []) + [service]
return [x for x in compose(*args).stdout.split() if x]
def upload(c, actor, chat, data, filename="photo.png", ctype="image/png", expected=202):
return c.call("POST", "/api/v1/chats/%s/attachments?filename=%s" % (chat["id"], filename), expected,
actor=actor, raw=data, headers={"Content-Type": ctype},
schema="Attachment" if expected == 202 else None)["json"]
def get_att(c, actor, att_id):
return c.call("GET", "/api/v1/attachments/" + att_id, actor=actor, schema="Attachment")["json"]
def wait_status(c, actor, att_id, statuses, timeout=90, interval=0.3):
deadline = time.monotonic() + timeout
while time.monotonic() < deadline:
att = get_att(c, actor, att_id)
if att["status"] in statuses:
return att
time.sleep(interval)
raise Failure("attachment %s did not reach %s in %ss (last %s)" % (att_id, statuses, timeout, att["status"]))
def wait_until(fn, timeout, interval=0.5, what="condition"):
deadline = time.monotonic() + timeout
last = None
while time.monotonic() < deadline:
last = fn()
if last:
return last
time.sleep(interval)
raise Failure("timeout waiting for " + what)
def events(info):
return [e["event"] for e in info["events"]]
def invariants(info):
"""Ключ эффекта: ровно 3 варианта и 4 конечных объекта на (attachment, v1), staging убран."""
names = sorted(v["name"] for v in info["variants"])
finals = [o[0] for o in info["s3_final_objects"]]
return {
"variants": names,
"variants_ok": names == ["crop", "optimized", "thumbnail"],
"final_objects": len(finals),
"final_objects_ok": len(finals) == 4,
"staging_left": info["attachment"]["staging_key"] is not None,
"job_status": info["jobs"][0]["status"],
"attempt": info["jobs"][0]["attempt"],
}
def png(width, height, rgba=False, noise=False, color=(40, 120, 200), seed=1):
"""PNG стандартной библиотекой: шум (несжимаемый) или заливка."""
channels = 4 if rgba else 3
rng = random.Random(seed)
rows = []
if noise:
for _ in range(height):
rows.append(b"\x00" + bytes(rng.getrandbits(8) for _ in range(width * channels)))
else:
px = bytes(color[:3]) + (b"\x80" if rgba else b"")
row = b"\x00" + px * width
rows = [row] * height
raw = b"".join(rows)
def chunk(kind, data):
return struct.pack(">I", len(data)) + kind + data + struct.pack(">I", zlib.crc32(kind + data) & 0xFFFFFFFF)
ihdr = struct.pack(">IIBBBBB", width, height, 8, 6 if rgba else 2, 0, 0, 0)
return (b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", ihdr) + chunk(b"IDAT", zlib.compress(raw, 6))
+ chunk(b"IEND", b""))
def noise_png_fast(width, height, seed=1):
"""Несжимаемый PNG через os.urandom (быстро для больших размеров)."""
raw = b"".join(b"\x00" + os.urandom(width * 3) for _ in range(height))
def chunk(kind, data):
return struct.pack(">I", len(data)) + kind + data + struct.pack(">I", zlib.crc32(kind + data) & 0xFFFFFFFF)
ihdr = struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0)
return b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", ihdr) + chunk(b"IDAT", zlib.compress(raw, 1)) + chunk(b"IEND", b"")