лр7: Docker-прогон, evidence, исправления

This commit is contained in:
kalibr456
2026-11-07 20:17:11 +03:00
parent 20afc2c790
commit cb38fd5231
249 changed files with 114500 additions and 82 deletions
+120
View File
@@ -0,0 +1,120 @@
"""lab7: секреты не попадают в логи и телеметрию.
python student-tests/leak_check.py
1. Создаёт пользователя с уникальным паролем-маркером, входит (получает токен-маркер), делает запросы с
этим токеном, неверный вход, загрузку файла — с явным traceparent, чтобы найти трассы.
2. Ищет маркеры (пароль, токен, base64 Basic, «Bearer <токен>») в:
- логах контейнеров (docker compose logs всех сервисов);
- Loki (полнотекстовый поиск по логам всех messenger-*);
- трассах этих запросов в Tempo (полный JSON всех спанов и атрибутов);
- метках Prometheus (значения label не содержат токен/UUID пользователя).
Также — что в логах нет секретов из OpenBao: ищется фрагмент «password=»/«secret_access_key» со значением.
Итог — evidence/leak-check.json. Ни один маркер не печатается целиком.
"""
import base64
import json
import os
import time
import uuid
from pathlib import Path
import images
from common import EVIDENCE, basic, call, check, compose_output, new_chat, upload
try:
import obs
except Exception: # noqa: BLE001
obs = None
def main():
marker_password = "LeakMarker-" + uuid.uuid4().hex
username = "leak_" + uuid.uuid4().hex[:10]
user = call("POST", "/api/v1/users", 201, body={"username": username, "display_name": "Leak",
"password": marker_password}).json
trace_ids = []
def traced():
trace_id = uuid.uuid4().hex
trace_ids.append(trace_id)
return {"traceparent": f"00-{trace_id}-{uuid.uuid4().hex[:16]}-01"}
token = call("POST", "/api/v1/auth/sessions", 201, headers={"Authorization": basic(username, marker_password),
**traced()}).json["token"]
call("POST", "/api/v1/auth/sessions", 401, headers={"Authorization": basic(username, marker_password + "x"),
**traced()})
me = {"id": user["id"], "token": token}
call("GET", "/api/v1/users/me", 200, token=token, headers=traced())
chat = new_chat({**user, "token": token})
upload({**user, "token": token}, chat["id"], images.sample_png())
call("GET", "/api/v1/users/me", 401, headers={"Authorization": "Bearer " + token + "x", **traced()})
markers = {"password": marker_password, "token": token,
"basic_b64": base64.b64encode(f"{username}:{marker_password}".encode()).decode()[:24]}
time.sleep(20) # батчи SDK (2-5 с) + Collector (2 с) + индексация
found = {}
logs = compose_output("logs", "--no-log-prefix")
found["container_logs"] = {name: logs.count(value) for name, value in markers.items()}
found["container_logs_bearer_or_basic_header"] = logs.count("Authorization: Bearer") + logs.count(
"Authorization: Basic")
sources = ["container logs"]
if obs is not None:
try:
loki_hits = {}
for name, value in markers.items():
rows = obs.loki_query('{service_name=~"messenger-.+"} |= `' + value + '`', minutes=30, limit=50)
loki_hits[name] = len(rows)
found["loki"] = loki_hits
sources.append("Loki")
except Exception as exc: # noqa: BLE001
found["loki"] = f"unavailable: {type(exc).__name__}"
try:
tempo_hits, fetched = {name: 0 for name in markers}, 0
for trace_id in trace_ids:
try:
raw = json.dumps(obs.tempo_trace(trace_id))
except Exception: # noqa: BLE001
continue
fetched += 1
for name, value in markers.items():
tempo_hits[name] += raw.count(value)
tempo_hits.setdefault("authorization_attribute", 0)
tempo_hits["authorization_attribute"] += raw.lower().count("http.request.header.authorization")
found["tempo"] = {"traces_fetched": fetched, **tempo_hits}
sources.append("Tempo")
except Exception as exc: # noqa: BLE001
found["tempo"] = f"unavailable: {type(exc).__name__}"
try:
labels = obs.get_json(f"{obs.PROMETHEUS_URL}/api/v1/series?match[]="
+ "http_server_request_duration_seconds_count")
text = json.dumps(labels)
found["prometheus_labels"] = {"token": text.count(token), "user_id": text.count(me["id"])}
sources.append("Prometheus labels")
except Exception as exc: # noqa: BLE001
found["prometheus_labels"] = f"unavailable: {type(exc).__name__}"
dump_dir = os.environ.get("OTLP_DUMP_DIR") # нативная песочница: всё, что пришло в заглушку Collector
if dump_dir:
raw = b"".join(path.read_bytes() for path in Path(dump_dir).glob("*.bin"))
found["otlp_payloads"] = {name: raw.count(value.encode()) for name, value in markers.items()}
sources.append(f"raw OTLP payloads ({len(raw) // 1024} KiB)")
(EVIDENCE / "leak-check.json").write_text(json.dumps(found, indent=1), encoding="utf-8")
print(json.dumps(found))
leaks = []
for source, hits in found.items():
if isinstance(hits, dict):
leaks += [f"{source}.{k}" for k, v in hits.items() if k != "traces_fetched" and v]
elif isinstance(hits, int) and hits:
leaks.append(source)
check(not leaks, f"secrets found in: {leaks}")
print(f"searched: {', '.join(sources)}; traces fetched: "
f"{found.get('tempo', {}).get('traces_fetched') if isinstance(found.get('tempo'), dict) else 'n/a'}")
print("PASS: no password, session token or Authorization value in logs or telemetry")
if __name__ == "__main__":
try:
main()
except AssertionError as exc:
print(f"FAIL: {exc}")
raise SystemExit(1)