forked from mtusi2026/mtusi2026lab6
38 lines
3.0 KiB
Python
38 lines
3.0 KiB
Python
#!/usr/bin/env python3
|
|
"""Inspect real stored signals, label cardinality and synthetic privacy canaries."""
|
|
import json,sys,time,uuid
|
|
from pathlib import Path
|
|
root=Path(__file__).resolve().parents[1];sys.path[:0]=[str(root/'tests'),str(root/'scripts')]
|
|
from client import Client,check
|
|
from observations import get,grafana,query,collect
|
|
c=Client('http://127.0.0.1:18086');a,b,e,chat=c.bootstrap()
|
|
marker='private_canary_'+uuid.uuid4().hex
|
|
c.message(a,chat,marker)
|
|
c.call('POST','/api/v1/chats',400,actor=a,body={marker:marker})
|
|
att,_=c.attachment(a,chat);c.wait_ready(a,att)
|
|
time.sleep(12)
|
|
series=query('{__name__=~"messenger_.*"}')
|
|
for row in series:
|
|
labels=row['metric']
|
|
check(not set(labels).intersection({'user_id','chat_id','message_id','attachment_id','request_id','filename','url'}),'high-cardinality metric label')
|
|
check(not __import__('re').search(r'[0-9a-f]{8}-[0-9a-f]{4}-',labels.get('route','')),'raw route contains UUID')
|
|
required=['messenger_http_requests_total','messenger_http_duration_seconds_bucket','messenger_jobs_total','messenger_job_duration_seconds_bucket','messenger_job_ready_duration_seconds_bucket','messenger_db_pool_used','messenger_db_pool_waiting','messenger_worker_busy','messenger_worker_capacity','messenger_process_memory','messenger_process_cpu','messenger_queue_depth','messenger_queue_oldest_age_seconds','messenger_ready_slo_total']
|
|
found={r['metric']['__name__'] for r in series}
|
|
check(set(required)<=found,'missing required metrics: '+str(set(required)-found))
|
|
logs=get('loki','/loki/api/v1/query_range',{'query':'{service_name=~"messenger-.+"} |= "'+marker+'"','start':str(int((time.time()-180)*1e9)),'end':str(int(time.time()*1e9))})
|
|
check(not logs['data']['result'],'canary leaked into logs')
|
|
# Search trace attributes; HTTP bodies and headers are never recorded.
|
|
traces=get('tempo','/api/search',{'q':'{ resource.service.name = "messenger-api" }','limit':10})
|
|
trace_rows=[]
|
|
for tr in traces.get('traces',[]):
|
|
d=get('tempo','/api/traces/'+tr['traceID']);check(marker not in json.dumps(d),'canary leaked into traces');trace_rows.append(tr['traceID'])
|
|
check(bool(trace_rows),'trace search returned no observations')
|
|
# Verify that the imported dashboard references valid expressions.
|
|
dashboard=grafana('/api/dashboards/uid/messenger-lab6')['dashboard']
|
|
check(len(dashboard['panels'])>=18,'dashboard not provisioned')
|
|
provisioned=grafana('/api/datasources');check({'prometheus','loki','tempo'}<=set(d['uid'] for d in provisioned),'datasources not provisioned')
|
|
for panel in dashboard['panels']:
|
|
for target in panel.get('targets',[]):query(target['expr'])
|
|
out=collect();out.update({'checks':{'required_metrics_present':True,'forbidden_labels_absent':True,'raw_uuid_routes_absent':True,'synthetic_canary_absent_in_logs':True,'synthetic_canary_absent_in_traces':True,'dashboard_expressions_valid':True},'series_count':len(series),'trace_ids_checked':trace_rows})
|
|
(root/'evidence/signals.json').write_text(json.dumps(out,indent=2)+'\n');print('PASS: stored metrics, safe labels, privacy canaries, trace search and dashboard queries')
|