Перенесена реализация lab5 (API x2 за HAProxy, PostgreSQL, SeaweedFS S3, RabbitMQ, relay outbox, worker x2, janitor) без изменения бизнес-API. Наблюдаемость: - OpenTelemetry SDK в API, relay, worker, janitor: traces, metrics, logs по OTLP/HTTP; структурные JSON-логи с trace_id/span_id - OTel Collector (memory_limiter, batch, ограниченные очереди и retry), Prometheus (метрики, alert rules), Loki (логи), Tempo (трассы), Grafana с provisioning datasources и dashboard - W3C trace context API -> outbox -> RabbitMQ -> worker (parent-child, включая retry); метрики очереди, пула БД, загрузки worker, CPU/RAM - SLI/SLO, 10 alert rules с unit-тестами promtool и runbooks - инциденты: остановка worker, блокировка БД, отказ telemetry backend; по наблюдениям исправлен простой worker в цикле AMQP (разбор x2.4)
162 lines
8.7 KiB
Python
162 lines
8.7 KiB
Python
#!/usr/bin/env python3
|
||
"""Регрессии по внешнему аудиту (HTTP-уровень): F02, F05, F08, F09, F17.
|
||
|
||
python3 student-tests/api_negative_lab5.py --output evidence/api-negative-lab5.json
|
||
|
||
F08 непарный суррогат (JSON "\\ud800") в display_name / password / title / text -> 400, не 500;
|
||
то же в filename (%ED%A0%80 — невалидный UTF-8) -> 400
|
||
F17 HEAD / OPTIONS / неизвестный метод -> 405 JSON с X-Request-Id и X-Instance-Id (у HEAD без тела)
|
||
F05 PNG с байтом фильтра 5 и 255, interlaced PNG с плохим фильтром -> 415, строка не создана
|
||
F09 объект ready-вложения испорчен в S3 (та же длина) -> content 503, а не 200 с чужими байтами
|
||
F02 4 progressive 4:4:4 + 1 progressive 4:2:0 + 1 baseline JPEG ~40 Мпикс одновременно -> все 202
|
||
и ready; RestartCount/OOMKilled реплик API не изменились
|
||
"""
|
||
import argparse
|
||
import http.client
|
||
import json
|
||
import struct
|
||
import subprocess
|
||
import uuid
|
||
import zlib
|
||
from urllib.parse import urlsplit
|
||
|
||
from lab5lib import (Client, Log, SAMPLE_PNG, check, compose, container_ids, ops, sql, upload, wait_status)
|
||
|
||
|
||
def api_state():
|
||
out = {}
|
||
for cid in container_ids("api"):
|
||
r = subprocess.run(["docker", "inspect", "-f", "{{.Name}} {{.RestartCount}} {{.State.OOMKilled}}", cid],
|
||
capture_output=True, text=True).stdout.split()
|
||
out[r[0].lstrip("/")] = {"restarts": int(r[1]), "oom_killed": r[2] == "true"}
|
||
return out
|
||
|
||
|
||
def raw(base, method, path, body=None, headers=None):
|
||
u = urlsplit(base)
|
||
conn = http.client.HTTPConnection(u.hostname, u.port or 80, timeout=15)
|
||
conn.request(method, path, body=body, headers=headers or {})
|
||
r = conn.getresponse()
|
||
data = r.read()
|
||
conn.close()
|
||
return r.status, dict((k.lower(), v) for k, v in r.getheaders()), data
|
||
|
||
|
||
def png(w, h, raw_rows, interlace=0):
|
||
def chunk(k, d):
|
||
return struct.pack(">I", len(d)) + k + d + struct.pack(">I", zlib.crc32(k + d) & 0xFFFFFFFF)
|
||
return (b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", struct.pack(">IIBBBBB", w, h, 8, 2, 0, 0, interlace))
|
||
+ chunk(b"IDAT", zlib.compress(raw_rows)) + chunk(b"IEND", b""))
|
||
|
||
|
||
def main():
|
||
p = argparse.ArgumentParser()
|
||
p.add_argument("--base-url", default="http://localhost:8080")
|
||
p.add_argument("--output")
|
||
args = p.parse_args()
|
||
log = Log("audit regressions: unicode, protocol errors, PNG filters, download integrity, progressive JPEG")
|
||
c = Client(args.base_url, timeout=60)
|
||
alice, bob, _eve, chat = c.bootstrap()
|
||
res = {}
|
||
|
||
# --- F08 -------------------------------------------------------------------------
|
||
bad = '"\\ud800"'
|
||
cases = {
|
||
"display_name": ("/api/v1/users", None,
|
||
'{"username":"u_%s","display_name":%s,"password":"Passw0rd-long-1"}' % (uuid.uuid4().hex[:10], bad)),
|
||
"password": ("/api/v1/users", None,
|
||
'{"username":"u_%s","display_name":"x","password":%s}' % (uuid.uuid4().hex[:10], '"Passw0rd-long-\\ud800"')),
|
||
"chat title": ("/api/v1/chats", alice, '{"title":%s,"member_ids":["%s"]}' % (bad, alice["id"])),
|
||
"message text": ("/api/v1/chats/%s/messages" % chat["id"], alice, '{"text":%s}' % bad),
|
||
}
|
||
f08 = {}
|
||
for name, (path, actor, body) in cases.items():
|
||
h = {"Content-Type": "application/json", "Idempotency-Key": uuid.uuid4().hex}
|
||
if actor:
|
||
h["Authorization"] = "Bearer " + actor["token"]
|
||
st, hd, data = raw(args.base_url, "POST", path, body.encode(), h)
|
||
code = json.loads(data)["error"]["code"] if data.startswith(b"{") else None
|
||
f08[name] = {"status": st, "code": code}
|
||
check(st == 400 and code == "invalid_request", "F08 %s: %s %s" % (name, st, code))
|
||
r = c.call("POST", "/api/v1/chats/%s/attachments?filename=%%ED%%A0%%80.png" % chat["id"], 400, actor=alice,
|
||
raw=SAMPLE_PNG, headers={"Content-Type": "image/png"})
|
||
f08["filename %ED%A0%80"] = {"status": r["status"]}
|
||
res["F08"] = f08
|
||
log.step("F08 lone surrogates -> 400", **f08)
|
||
|
||
# --- F17 -------------------------------------------------------------------------
|
||
f17 = {}
|
||
for method, path in (("HEAD", "/health/live"), ("OPTIONS", "/api/v1/chats"), ("BREW", "/health/live"),
|
||
("TRACE", "/health/live")):
|
||
st, hd, data = raw(args.base_url, method, path)
|
||
item = {"status": st, "x_request_id": bool(hd.get("x-request-id")), "x_instance_id": bool(hd.get("x-instance-id")),
|
||
"content_type": hd.get("content-type"), "body_bytes": len(data)}
|
||
if method != "HEAD":
|
||
item["code"] = json.loads(data)["error"]["code"]
|
||
item["request_id_matches"] = json.loads(data)["error"]["request_id"] == hd.get("x-request-id")
|
||
check(item["request_id_matches"], "F17 request_id")
|
||
check(st == 405 and item["x_request_id"] and item["x_instance_id"], "F17 %s: %s" % (method, item))
|
||
check(method != "HEAD" or len(data) == 0, "HEAD must have no body")
|
||
f17[method] = item
|
||
res["F17"] = f17
|
||
log.step("F17 unsupported methods -> 405 JSON", **f17)
|
||
|
||
# --- F05 -------------------------------------------------------------------------
|
||
f05 = {}
|
||
import sys
|
||
sys.path.insert(0, str(__import__("lab5lib").ROOT / "src"))
|
||
import images
|
||
w, h = 13, 11
|
||
passes = images._png_passes(w, h, 24, 1)
|
||
adam = bytearray(b"".join((b"\x00" + bytes(rb - 1)) * rows for rows, rb in passes))
|
||
adam[sum(rr * bb for rr, bb in passes[:4]) + passes[4][1] * 2] = 9
|
||
samples = {"filter 5 (1x1)": png(1, 1, b"\x05\x01\x02\x03"),
|
||
"filter 255 in row 57 (10x100)": png(10, 100, b"".join((b"\xff" if i == 57 else b"\x00") + bytes(30)
|
||
for i in range(100))),
|
||
"Adam7, filter 9 in pass 5": png(w, h, bytes(adam), interlace=1)}
|
||
for name, data in samples.items():
|
||
fname = "badfilter-%s.png" % uuid.uuid4().hex[:8]
|
||
r = c.call("POST", "/api/v1/chats/%s/attachments?filename=%s" % (chat["id"], fname), 415, actor=alice,
|
||
raw=data, headers={"Content-Type": "image/png"})
|
||
rows = int(sql("SELECT count(*) FROM attachments WHERE filename = '%s'" % fname)[0][0])
|
||
check(rows == 0, "F05 row created")
|
||
f05[name] = {"status": r["status"], "bytes": len(data), "rows_created": rows}
|
||
res["F05"] = f05
|
||
log.step("F05 bad PNG filters -> 415, no job", **f05)
|
||
|
||
# --- F09 -------------------------------------------------------------------------
|
||
att = upload(c, alice, chat, SAMPLE_PNG, "integrity.png")
|
||
ready = wait_status(c, bob, att["id"], {"ready", "failed"}, 60)
|
||
check(ready["status"] == "ready", "F09 not ready")
|
||
key = ops("inspect", att["id"])["attachment"]["object_key"]
|
||
compose("--profile", "tools", "run", "--rm", "-T", "checks", "/app/student-tests/s3_tamper.py", key)
|
||
st, hd, data = raw(args.base_url, "GET", "/api/v1/attachments/%s/content" % att["id"],
|
||
headers={"Authorization": "Bearer " + bob["token"]})
|
||
thumb = c.call("GET", "/api/v1/attachments/%s/content?variant=thumbnail" % att["id"], actor=bob)
|
||
tv = [v for v in ready["variants"] if v["name"] == "thumbnail"][0]
|
||
import hashlib
|
||
res["F09"] = {"attachment_id": att["id"], "tampered_original_status": st,
|
||
"code": json.loads(data)["error"]["code"] if st >= 400 else None,
|
||
"untouched_thumbnail_status": thumb["status"],
|
||
"thumbnail_sha_ok": hashlib.sha256(thumb["bytes"]).hexdigest() == tv["sha256"]}
|
||
check(st == 503 and res["F09"]["thumbnail_sha_ok"], "F09: %s" % res["F09"])
|
||
log.step("F09 corrupted object -> 503, intact variant still 200", **res["F09"])
|
||
|
||
# --- F02 -------------------------------------------------------------------------
|
||
before = api_state()
|
||
proc = compose("--profile", "tools", "run", "--rm", "-T", "checks", "/app/student-tests/progressive_upload.py",
|
||
"--base-url", "http://lb:8080", "--output", "/app/evidence/progressive-upload.json", ok=False,
|
||
timeout=600)
|
||
after = api_state()
|
||
res["F02"] = {"checks_exit": proc.returncode, "api_before": before, "api_after": after,
|
||
"tail": proc.stdout.strip().splitlines()[-1:]}
|
||
check(proc.returncode == 0 and before == after and not any(v["oom_killed"] for v in after.values()),
|
||
"F02: %s" % res["F02"])
|
||
log.step("F02 concurrent ~40 Mpx progressive JPEG: all ready, API not restarted", **res["F02"])
|
||
log.save(args.output, results=res)
|
||
print("PASS: F02, F05, F08, F09, F17 regressions")
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|