"""Verify provisioned dashboard/datasources over native HTTPS and authenticated operator API.""" import base64 import json import ssl from urllib.parse import urlencode import urllib.request from pathlib import Path import os ROOT=Path(__file__).resolve().parents[1] settings=json.loads((ROOT/'operator/settings.json').read_text()) context=ssl.create_default_context(cafile=str(ROOT/'pki/grafana/ca.crt')) op=urllib.request.build_opener(urllib.request.ProxyHandler({}),urllib.request.HTTPSHandler(context=context)) base=os.environ.get('GRAFANA_URL','https://localhost:13407') def read(path,auth=True): headers={} if auth:headers['Authorization']='Basic '+base64.b64encode(('operator:'+settings['grafana']).encode()).decode() req=urllib.request.Request(base+path,headers=headers) with op.open(req,timeout=8) as r:return json.load(r) listing=read('/api/datasources');assert len(listing)==3 sources=[read('/api/datasources/uid/'+s['uid']) for s in listing] for s in sources: assert s['url'].startswith('https://') and not s['jsonData'].get('tlsSkipVerify') assert s['jsonData']['tlsAuth'] and s['secureJsonFields']['tlsClientKey'] dash=read('/api/dashboards/uid/messenger-lab7') assert len(dash['dashboard']['panels'])>=12 up=read('/api/datasources/proxy/uid/prometheus/api/v1/query?'+urlencode({'query':'up'}))['data']['result'] assert len(up)>=2 and all(float(x['value'][1])==1 for x in up) try:read('/api/datasources',False) except urllib.error.HTTPError as e:assert e.code==401 else:raise AssertionError('Unauthenticated admin UI API allowed') report={'native_https_verified_ca_hostname':True,'anonymous_datasource_api_401':True,'authenticated_operator_api':True, 'tls_mtls_datasources':sorted(s['name'] for s in sources),'dashboard_uid':'messenger-lab7','panels':len(dash['dashboard']['panels']), 'actual_scrape_targets_up':len(up),'loki_trace_link_provisioned':any(s['name']=='Loki' and s['jsonData'].get('derivedFields') for s in sources)} assert report['loki_trace_link_provisioned'] (ROOT/'evidence/grafana-tls.json').write_text(json.dumps(report,indent=2)+'\n') print('PASS: native Grafana HTTPS/operator auth, all TLS client datasources, real Prometheus data and trace link configuration.')