import base64 import json from pathlib import Path import ssl import time from urllib.error import HTTPError, URLError from urllib.request import Request, urlopen ROOT = Path(__file__).resolve().parents[1] def main(): context = ssl.create_default_context(cafile=str(ROOT / "secrets/operator/ca.pem")) password = (ROOT / "secrets/grafana_password").read_text().strip() auth = base64.b64encode(("admin:" + password).encode()).decode() paths = { "Prometheus": "/api/datasources/uid/prometheus/health", "Loki": "/api/datasources/uid/loki/health", "Tempo": "/api/datasources/proxy/uid/tempo/api/search", } for name, path in paths.items(): deadline = time.monotonic() + 90 while True: request = Request("https://localhost:13007" + path, headers={"Authorization": "Basic " + auth}) try: with urlopen(request, context=context, timeout=15) as response: result = json.load(response) if name == "Tempo": assert isinstance(result.get("traces"), list) else: assert result.get("status") == "OK" print("PASS: Grafana queries " + name + " through verified mTLS", flush=True) break except (HTTPError, URLError, TimeoutError, AssertionError): if time.monotonic() >= deadline: raise time.sleep(2) if __name__ == "__main__": main()