Files
mtusi2026lab7/student-tests/traffic.py
T
zemiles 9cc80d2529 ЛР 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

141 lines
5.8 KiB
Python
Raw 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.
"""Lab6: фоновая смешанная нагрузка для экспериментов с инцидентами (стандартная библиотека).
Профиль (closed loop, `threads` потоков): каждый поток в цикле читает историю чата, отправляет
сообщение с Idempotency-Key и раз в `upload_every` итераций загружает маленький PNG (64x48,
уникальный цвет). Пауза `think` между итерациями. Ошибки не прерывают нагрузку: каждый ответ
учитывается по секундам (коды, latency), чтобы сопоставить с метриками/alerts.
Токены живут только в памяти, в результат не попадают.
"""
import base64
import json
import threading
import time
import urllib.error
import urllib.request
import uuid
from lab5lib import png
BASE = "https://localhost:8443"
class Traffic:
def __init__(self, client, actor, chat, threads=4, think=0.2, upload_every=5, timeout=15, base=BASE):
self.c = client
self.actor = actor
self.chat = chat
self.threads = threads
self.think = think
self.upload_every = upload_every
self.timeout = timeout
self.base = client.base if hasattr(client, "base") else base
self.stop_ev = threading.Event()
self.lock = threading.Lock()
self.samples = [] # (t, kind, status, latency_s)
self.uploads = [] # (t, attachment_id)
self._threads = []
self.started = None
def _req(self, method, path, body=None, raw=None, headers=None):
h = {"Authorization": "Bearer " + self.actor["token"], "Accept": "application/json"}
if body is not None:
raw = json.dumps(body).encode()
h["Content-Type"] = "application/json"
h.update(headers or {})
req = urllib.request.Request(self.base + path, data=raw, headers=h, method=method)
t = time.monotonic()
try:
# Lab7: HTTPS — opener публичного клиента (проверка CA_FILE и hostname).
opener = getattr(self.c, "opener", None)
with (opener.open(req, timeout=self.timeout) if opener else urllib.request.urlopen(req, timeout=self.timeout)) as resp:
data = resp.read()
status = resp.status
except urllib.error.HTTPError as exc:
data, status = exc.read(), exc.code
except Exception:
data, status = b"", 0 # timeout / connection error
return status, time.monotonic() - t, data
def _record(self, kind, status, latency):
with self.lock:
self.samples.append((time.monotonic() - self.started, kind, status, latency))
def _loop(self, idx):
i = 0
cid = self.chat["id"]
while not self.stop_ev.is_set():
i += 1
st, lat, _ = self._req("GET", "/api/v1/chats/%s/messages?limit=20" % cid)
self._record("list", st, lat)
st, lat, _ = self._req("POST", "/api/v1/chats/%s/messages" % cid, body={"text": "load %d-%d" % (idx, i)},
headers={"Idempotency-Key": uuid.uuid4().hex})
self._record("send", st, lat)
if self.upload_every and i % self.upload_every == 0:
img = png(64, 48, color=((idx * 40 + i) % 256, (i * 7) % 256, 90))
st, lat, data = self._req("POST", "/api/v1/chats/%s/attachments?filename=load.png" % cid, raw=img,
headers={"Content-Type": "image/png"})
self._record("upload", st, lat)
if st == 202:
try:
with self.lock:
self.uploads.append((time.monotonic() - self.started, json.loads(data)["id"]))
except Exception:
pass
self.stop_ev.wait(self.think)
def start(self):
self.started = time.monotonic()
for i in range(self.threads):
t = threading.Thread(target=self._loop, args=(i,), daemon=True)
t.start()
self._threads.append(t)
return self
def stop(self):
self.stop_ev.set()
for t in self._threads:
t.join(timeout=self.timeout + 5)
def window(self, t_from, t_to):
with self.lock:
rows = [s for s in self.samples if t_from <= s[0] < t_to]
return summarize(rows)
def summary_by(self, step=10):
with self.lock:
rows = list(self.samples)
if not rows:
return []
end = max(r[0] for r in rows)
out = []
t = 0.0
while t <= end:
part = [r for r in rows if t <= r[0] < t + step]
s = summarize(part)
s["t"] = round(t, 1)
out.append(s)
t += step
return out
def summarize(rows):
if not rows:
return {"requests": 0}
lat = sorted(r[3] for r in rows if 200 <= r[2] < 300)
codes = {}
for r in rows:
codes[str(r[2])] = codes.get(str(r[2]), 0) + 1
def q(p):
if not lat:
return None
return round(lat[min(len(lat) - 1, int(p * len(lat)))] * 1000, 1)
errors_5xx = sum(v for k, v in codes.items() if k.startswith("5") or k == "0")
span = (max(r[0] for r in rows) - min(r[0] for r in rows)) or 1
return {"requests": len(rows), "ok": len(lat), "codes": codes, "error_ratio_5xx": round(errors_5xx / len(rows), 4),
"ok_rps": round(len(lat) / span, 1), "p50_ms": q(0.50), "p95_ms": q(0.95), "p99_ms": q(0.99)}
def basic(username, password):
return "Basic " + base64.b64encode(("%s:%s" % (username, password)).encode()).decode()