"""Create a synthetic upload and check its signals without persisting secret canaries.""" import base64 import json from pathlib import Path import secrets import ssl import subprocess import sys import time import urllib.request from urllib.error import HTTPError ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "tests")) from client import Client, check CA = ROOT / "secrets/authority/ca.crt" def main(): start = time.time_ns() c = Client(ca_file=str(CA)) alice = c.user("telemetry") chat = c.call("POST", "/api/v1/chats", 201, actor=alice, body={"title": "Проверка сигналов", "member_ids": [alice["id"]]}, schema="Chat")["json"] body = "message-canary-" + secrets.token_hex(16) message = c.message(alice, chat, body) original = (ROOT / "tests/fixtures/sample.png").read_bytes() filename = "filename-canary-" + secrets.token_hex(12) + ".png" uploaded = c.call("POST", "/api/v1/chats/" + chat["id"] + "/attachments?filename=" + filename, 202, actor=alice, raw=original, headers={"Content-Type": "image/png"}, schema="Attachment") c.wait_ready(alice, uploaded["json"]) login = alice["registration"] basic = base64.b64encode(f'{login["username"]}:{login["password"]}'.encode()).decode() canaries = [alice["token"], login["password"], basic, body, filename, base64.b64encode(original).decode()] payload = {"start": start, "trace_id": uploaded["headers"]["X-Trace-Id"], "request_id": uploaded["headers"]["X-Request-Id"], "canaries": canaries, "message_trace_id": message["headers"]["X-Trace-Id"]} subprocess.run(["docker", "compose", "run", "--rm", "--no-deps", "-T", "audit", "python", "/app/student-tests/telemetry_probe.py"], input=json.dumps(payload), text=True, check=True) logs = subprocess.check_output(["docker", "compose", "logs", "--since", "3m"], text=True) check(not any(value in logs for value in canaries), "sensitive synthetic value in container logs") print("PASS no synthetic sensitive values in Compose logs") # Grafana must actually query its provisioned datasources through mTLS. ctx = ssl.create_default_context(cafile=str(CA)) password = (ROOT / "secrets/bootstrap/grafana/admin").read_text() headers = {"Authorization": "Basic " + base64.b64encode(("admin:" + password).encode()).decode()} base = "https://localhost:3007" try: urllib.request.urlopen(base + "/api/datasources", context=ctx, timeout=5) except HTTPError as exc: check(exc.code == 401, "Grafana endpoint did not require authentication") else: raise AssertionError("Grafana allows unauthenticated datasource list") print("PASS Grafana anonymous datasource list denied") paths = ( "prometheus/api/v1/query?query=up", "loki/loki/api/v1/labels", "tempo/api/search?limit=1", ) for path in paths: req = urllib.request.Request(base + "/api/datasources/proxy/uid/" + path, headers=headers) with urllib.request.urlopen(req, context=ctx, timeout=8) as res: check(res.status == 200, "Grafana datasource proxy failure") print("PASS Grafana HTTPS/mTLS proxy", path.split("/")[0]) req = urllib.request.Request(base + "/api/dashboards/uid/lab7", headers=headers) with urllib.request.urlopen(req, context=ctx, timeout=8) as res: dashboard = json.load(res)["dashboard"] check(dashboard["title"].startswith("Lab7") and len(dashboard["panels"]) > 0, "Grafana dashboard provisioning") print("PASS provisioned Lab7 Grafana dashboard", len(dashboard["panels"]), "panels") print("upload_trace_id", payload["trace_id"]) if __name__ == "__main__": main()