"""Operator-triggered negative mTLS audit, inside isolated Compose networks. Tests real new TLS connections (not cached sessions); no verify=False fallback. Needs `docker compose --profile audit run --rm tls-audit` after services healthy. """ import socket import ssl from pathlib import Path CERT_DIR=Path('/run/audit-pki') CA=CERT_DIR/'ca.crt' def check_target(label,host,port,*,path='/',method='GET',cert=None,expected=None,server_name=None): ctx=ssl.create_default_context(cafile=str(CA)) ctx.check_hostname=True ctx.verify_mode=ssl.CERT_REQUIRED if cert: ctx.load_cert_chain(str(CERT_DIR/(cert+'.crt')),str(CERT_DIR/(cert+'.key'))) try: with socket.create_connection((host,port),timeout=6) as tcp: with ctx.wrap_socket(tcp,server_hostname=server_name or host) as tls: tls.settimeout(6) # Must send something; an mTLS or ACL reject can arrive after the TLS handshake. if method=='AMQP': tls.sendall(b'AMQP\x00\x00\x09\x01') data=tls.recv(1024) # RabbitMQ answers the client protocol header with an AMQP Method frame (type 1). allowed=bool(data) and data[0]==1 else: body=b'x' if method=='POST' else b'' req=(f'{method} {path} HTTP/1.1\r\nHost: {host}\r\n' f'Content-Length: {len(body)}\r\nConnection: close\r\n\r\n').encode()+body tls.sendall(req) data=tls.recv(256) status=data.split(b' ',2)[1] if data.startswith(b'HTTP/') else b'' # For OTLP POST malformed bytes should reach Collector, yielding a 4xx response # other than an authorization 403. For HTTP health require real 200. allowed=(status==b'200' if method=='GET' else status in (b'400',b'415')) except (ssl.SSLError,OSError,ValueError,ConnectionError): allowed=False if allowed!=expected: raise AssertionError(f'{label}: expected {"ALLOW" if expected else "DENY"}, got {"ALLOW" if allowed else "DENY"}') print(f'PASS {label}: {"allowed" if allowed else "rejected"}') def main(): for api in ('api1','api2'): for cert,yes in ((None,False),('foreign-client',False),('rogue-client',False),('worker-client',False),('lb-client',True)): check_target(f'{api}/{cert or "no-cert"}',api,8443,path='/health/live',cert=cert,expected=yes,server_name=api+'-tls') for cert,yes in ((None,False),('foreign-client',False),('rogue-client',False),('lb-client',False),('worker-client',True)): check_target(f'broker-gw/{cert or "no-cert"}','broker-gw',5671,method='AMQP',cert=cert,expected=yes) for cert,yes in ((None,False),('foreign-client',False),('rogue-client',False),('lb-client',False),('worker-client',True)): check_target(f'otel-gw/{cert or "no-cert"}','otel-gw',4318,path='/v1/traces',method='POST',cert=cert,expected=yes) # Collector→Loki uses its own identity, not the worker/API certs. for cert,yes in ((None,False),('foreign-client',False),('worker-client',False),('collector-client',True)): check_target(f'metrics-loki/{cert or "no-cert"}','metrics-gw',3102,path='/metrics',cert=cert,expected=yes) # A valid certificate for the wrong server name must never validate. check_target('api1/wrong-server-SAN','api1',8443,path='/health/live',cert='lb-client',expected=False,server_name='wrong-api-host') if (CERT_DIR/'old-lb-client.crt').is_file(): for api in ('api1','api2'): check_target(f'{api}/revoked-lb',api,8443,path='/health/live', cert='old-lb-client',expected=False,server_name=api+'-tls') print('PASS: mTLS identity matrix (no cert / foreign CA / wrong role / positive / wrong server SAN)') if __name__=='__main__': main()