62 lines
3.7 KiB
Python
62 lines
3.7 KiB
Python
"""Verify lab6 minimum survived TLS; query all real stores over mTLS."""
|
|
import base64
|
|
import json
|
|
from pathlib import Path
|
|
import sys
|
|
import time
|
|
import urllib.request
|
|
import urllib.parse
|
|
ROOT=Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0,str(ROOT/'student-tests'))
|
|
from security import context,Client,CA,BASE,check,save,value
|
|
|
|
|
|
def get(host,port,path):
|
|
with urllib.request.urlopen('https://'+host+':'+str(port)+path,context=context('operator'),timeout=10) as response:
|
|
return json.load(response)
|
|
|
|
|
|
def main():
|
|
c=Client(BASE,CA,timeout=10)
|
|
a,b,e,chat=c.bootstrap()
|
|
raw=(ROOT/'tests/fixtures/sample.png').read_bytes()
|
|
r=c.call('POST','/api/v1/chats/'+chat['id']+'/attachments?filename=sample.png',202,actor=a,raw=raw,headers={'Content-Type':'image/png'},schema='Attachment')
|
|
tid=r['headers']['X-Trace-Id'];c.wait_ready(a,r['json'])
|
|
until=time.monotonic()+60
|
|
while True:
|
|
try:
|
|
trace=get('tempo',3443,'/api/traces/'+tid)
|
|
found=[(batch['resource'],span) for batch in trace['batches'] for scope in batch['scopeSpans'] for span in scope['spans']]
|
|
if any(span['kind']=='SPAN_KIND_CONSUMER' for _,span in found):break
|
|
except Exception:pass
|
|
check(time.monotonic()<until,'TLS trace not stored');time.sleep(2)
|
|
q=urllib.parse.urlencode({'query':'{service_name=~"messenger-api|messenger-worker|messenger-publisher"} |= "'+tid+'"','limit':100})
|
|
logs=get('loki',3443,'/loki/api/v1/query_range?'+q)
|
|
check(logs['data']['result'],'TLS logs not stored')
|
|
metrics=get('prometheus',9443,'/api/v1/query?'+urllib.parse.urlencode({'query':'http_server_requests_total{service_name="messenger-api"}'}))
|
|
check(metrics['data']['result'],'TLS metrics not stored')
|
|
targets=get('prometheus',9443,'/api/v1/targets')['data']['activeTargets']
|
|
check(all(x['health']=='up' and x['scrapeUrl'].startswith('https:') for x in targets),'insecure scrape or unavailable source')
|
|
text=json.dumps(trace)+json.dumps(logs)+json.dumps(metrics)
|
|
private=[a['token'],a['registration']['password'],'Bearer '+a['token'],'PRIVATE KEY']
|
|
check(not any(x in text for x in private),'telemetry leak')
|
|
save('tls-observability-trace.json',trace)
|
|
save('tls-observability-logs.json',logs)
|
|
save('tls-observability.json',{'result':'PASS','trace_id':tid,'stored_spans':len(found),'api_worker_chain':True,'metrics_series':len(metrics['data']['result']),'scrape_targets':targets,'private_probes':len(private),'leaks':0})
|
|
password=value('grafana_password')
|
|
req=urllib.request.Request('https://grafana:3443/api/dashboards/uid/messenger-lab7',headers={'Authorization':'Basic '+base64.b64encode(('admin:'+password).encode()).decode()})
|
|
with urllib.request.urlopen(req,context=context(),timeout=10) as response:dash=json.load(response)
|
|
check(len(dash['dashboard']['panels'])==20,'dashboard missing')
|
|
datasource_probes=[]
|
|
for uid,path in [('prometheus','/api/v1/query?query=up'),('loki','/loki/api/v1/labels'),('tempo','/api/search?limit=1')]:
|
|
req=urllib.request.Request('https://grafana:3443/api/datasources/proxy/uid/'+uid+path,
|
|
headers={'Authorization':'Basic '+base64.b64encode(('admin:'+password).encode()).decode()})
|
|
with urllib.request.urlopen(req,context=context(),timeout=15) as response:
|
|
check(response.status==200,'Grafana datasource proxy failed')
|
|
datasource_probes.append({'datasource':uid,'status':response.status})
|
|
save('grafana-datasource-access.json',{'result':'PASS','probes':datasource_probes,'CA_and_client_identity':'configured in runtime provisioning, tlsSkipVerify=false'})
|
|
print('PASS: real API/worker spans and logs, metrics, TLS scrape and Grafana dashboard; privacy probes clean.')
|
|
|
|
|
|
if __name__=='__main__':main()
|