Files
mtusi2026lab7/tests/tls_matrix.py
T

44 lines
3.0 KiB
Python

"""Run only in trusted operator with /run/probes mounted, never in app/worker."""
import json
from pathlib import Path
import socket
import ssl
import struct
TRUST='/run/trust/ca.crt';PROBES=Path('/run/probes')
HTTP=b'GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n'
H2=b'PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n'+bytes.fromhex('000000040000000000')
ENDPOINTS=[('db',15432,'api',struct.pack('!II',8,80877103),False),('s3',19000,'worker',HTTP,False),('broker',16379,'worker',b'*1\r\n$4\r\nPING\r\n',False),('cache',16380,'api',b'*1\r\n$4\r\nPING\r\n',False),('collector',14317,'worker',H2,True),('collector',18889,'prometheus',HTTP,False),('collector',18888,'prometheus',HTTP,False),('prometheus',19090,'grafana',HTTP,False),('loki',13100,'collector',HTTP,False),('tempo',13200,'grafana',HTTP,False),('tempo',14317,'collector',H2,True)]
for addr in sorted({i[4][0] for i in socket.getaddrinfo('api',8444,type=socket.SOCK_STREAM)}):ENDPOINTS.append((addr,8444,'lb',b'GET /health/live HTTP/1.1\r\nHost: api\r\nConnection: close\r\n\r\n',False))
def probe(host,port,identity,payload,h2=False,wrong_name=False):
ctx=ssl.create_default_context(cafile=TRUST)
if identity:ctx.load_cert_chain(str(PROBES/identity/'service.pem'))
if h2:ctx.set_alpn_protocols(['h2'])
name='api' if port==8444 else host
try:
with socket.create_connection((host,port),timeout=4) as raw:
with ctx.wrap_socket(raw,server_hostname='invalid.example' if wrong_name else name) as s:
s.settimeout(4);s.sendall(payload);return bool(s.recv(4096))
except (OSError,ssl.SSLError):return False
rows=[]
for host,port,role,payload,h2 in ENDPOINTS:
accepted=probe(host,port,role,payload,h2)
assert accepted,(host,port,'allowed identity rejected')
negatives={label:not probe(host,port,identity,payload,h2) for label,identity in [('missing_cert',None),('foreign_CA','foreign-'+role),('expired','expired-'+role),('wrong_role','unauthorized')]}
negatives['wrong_server_SAN']=not probe(host,port,role,payload,h2,True)
assert all(negatives.values()),(host,port,negatives)
assert probe(host,port,role,payload,h2),(host,port,'positive control after negatives failed')
rows.append({'peer':'api' if port==8444 else host,'address':host,'port':port,'allowed_identity':role,'allowed':True,**negatives})
closed=[]
for host,ports in [('db',[5432]),('s3',[9000,9001]),('broker',[6379]),('cache',[6379]),('collector',[4317,8888,8889]),('prometheus',[9090]),('loki',[3100,9095]),('tempo',[3200,4317,9095]),('grafana',[3000])]:
for port in ports:
try:s=socket.create_connection((host,port),timeout=1);s.close();raise AssertionError((host,port,'plaintext reachable'))
except (ConnectionRefusedError,TimeoutError):closed.append(f'{host}:{port}')
for host,port,*_ in ENDPOINTS:
if port==8444:
try:s=socket.create_connection((host,8080),timeout=1);s.close();raise AssertionError('API bypass')
except (ConnectionRefusedError,TimeoutError):closed.append(f'{host}:8080')
print(json.dumps({'status':'PASS','rows':rows,'plaintext_closed':closed},indent=2))