#!/usr/bin/env python3 """Verify the real Grafana data paths over authenticated HTTPS.""" import base64 import json import ssl import sys import time import uuid from pathlib import Path from urllib.error import HTTPError from urllib.parse import urlencode from urllib.request import Request, urlopen ROOT = Path(__file__).resolve().parents[1] CA = ROOT / "secrets/tls/trust/ca.crt" def grafana(uid, path, **params): password = (ROOT / "secrets/grafana_password").read_text().strip() basic = base64.b64encode(("admin:" + password).encode()).decode() url = f"https://localhost:3007/api/datasources/proxy/uid/{uid}{path}" if params: url += "?" + urlencode(params) request = Request(url, headers={"Authorization": "Basic " + basic}) with urlopen(request, context=ssl.create_default_context(cafile=CA), timeout=5) as response: return json.load(response) def until(fn): end = time.monotonic() + 35 while time.monotonic() < end: try: value = fn() except HTTPError as exc: if exc.code not in (404, 502, 503): raise else: if value: return value time.sleep(0.5) raise AssertionError("Timed out waiting for Grafana datasource results") def logs(service, field, value): # The query is restricted to a generated ID, which is validated by the probe. return grafana( "loki", "/loki/api/v1/query_range", query=f'{{service_name="messenger-{service}"}} | {field}="{value}"', start=str(time.time_ns() - 120_000_000_000), limit=10, )["data"]["result"] def main(): sys.path.insert(0, str(ROOT / "tests")) from client import Client client = Client("https://localhost:8443", str(CA)) actor, _, _, chat = client.bootstrap() marker = "probe_" + uuid.uuid4().hex client.message(actor, chat, marker) upload = client.call( "POST", f"/api/v1/chats/{chat['id']}/attachments?filename={marker}.png", 202, actor=actor, headers={"Content-Type": "image/png"}, raw=(ROOT / "tests/fixtures/sample.png").read_bytes(), schema="Attachment", ) client.wait_ready(actor, upload["json"]) request_id = str(uuid.UUID(upload["headers"]["X-Request-Id"])) api_logs = until(lambda: logs("api", "request_id", request_id)) stream = api_logs[0] value = stream["values"][0] trace_id = (value[2] if len(value) > 2 else stream["stream"])["trace_id"] assert len(trace_id) == 32 and all(ch in "0123456789abcdef" for ch in trace_id) worker_logs = until(lambda: logs("worker", "trace_id", trace_id)) trace = until(lambda: grafana("tempo", "/api/traces/" + trace_id)) collected = json.dumps([api_logs, worker_logs, trace]) assert "jobs.consume" in collected and "s3.put" in collected and "db.transaction" in collected for forbidden in (marker, actor["registration"]["password"], actor["token"]): assert forbidden not in collected until( lambda: grafana("prometheus", "/api/v1/query", query="sum(messenger_http_requests_total)")[ "data" ]["result"] ) print("PASS: three Grafana datasources over mTLS; correlated payloads are clean.") if __name__ == "__main__": main()