Files
mtusi2026lab7/scripts/prove-telemetry.py
2026-11-18 11:36:21 +03:00

29 lines
2.7 KiB
Python

#!/usr/bin/env python3
"""Verify real signal storage through TLS and scan captured telemetry for synthetic secret canaries."""
import json,re,sys,time,uuid
from pathlib import Path
ROOT=Path(__file__).resolve().parents[1];sys.path.insert(0,str(ROOT/'scripts'));import security
def main():
name,password,token,user=security.actor();auth={'Authorization':'Bearer '+token};canary='body-canary-'+uuid.uuid4().hex
status,_,b=security.request('/api/v1/chats','POST',json.dumps({'title':'Synthetic','member_ids':[user]}).encode(),dict(auth,**{'Content-Type':'application/json'}));assert status==201;chat=json.loads(b)['id']
status,_,_=security.request('/api/v1/chats/'+chat+'/messages','POST',json.dumps({'text':canary}).encode(),dict(auth,**{'Content-Type':'application/json','Idempotency-Key':uuid.uuid4().hex}));assert status==201
# A fresh upload produces a real worker trace after any backend restart.
fixture=(ROOT/'tests/fixtures/sample.png').read_bytes();status,_,b=security.request('/api/v1/chats/'+chat+'/attachments?filename=signals.png','POST',fixture,dict(auth,**{'Content-Type':'image/png'}));assert status==202;aid=json.loads(b)['id']
for _ in range(60):
if json.loads(security.request('/api/v1/attachments/'+aid,headers=auth)[2]).get('status')=='ready':break
time.sleep(1)
else:raise AssertionError('telemetry upload did not finish')
logs=security.compose('logs','--no-color','--tail=80','worker').stdout
trace=re.findall(r'"trace_id":"([a-f0-9]{32})"',logs)[-1]
time.sleep(8)
data=security.compose('run','--rm','-T','--no-deps','--entrypoint','securityprobe','-e','BAO_ADDR=','-e','PROBE_MODE=telemetry','-e','PROBE_TRACE_ID='+trace,'-v',str(security.S/'grafana')+':/run/identity:ro','worker').stdout
signals=json.loads(data);assert signals['metrics']['data']['result'];assert signals['logs']['data']['result'];assert signals['traces'].get('batches') or signals['traces'].get('resourceSpans')
captured=security.compose('logs','--no-color').stdout+data
needles=[password,token,canary,'BEGIN PRIVATE KEY','BEGIN RSA PRIVATE KEY']
for p in (security.S/'operator').iterdir():
if p.is_file() and (p.name.endswith('_secret') or p.name.endswith('_password')):needles.append(p.read_text().strip())
assert not any(v and v in captured for v in needles),'secret or body found in telemetry'
(ROOT/'evidence/telemetry.json').write_text(json.dumps({'signals':signals,'secret_canaries_absent':True,'checked_container_logs':True,'checked_backend_sample':True,'limitations':'Checks cover captured logs and sampled backend signals, not a mathematical proof for every historical span.'},indent=2)+'\n');print('PASS: metrics, worker logs and real trace queried over TLS; secret/body canaries absent')
if __name__=='__main__':main()