"""Run in an ephemeral operator test container, never in production app containers.""" import base64 import http.client import json from pathlib import Path import socket import ssl import time import uuid import os ROOT=Path('/audit') CA=str(ROOT/'secrets/ca.crt') def context(role=None): ctx=ssl.create_default_context(cafile=CA) if role: if role=='foreign': ctx.load_cert_chain(str(ROOT/'operator/foreign-client.crt'),str(ROOT/'operator/foreign-client.key')) else: ctx.load_cert_chain(str(ROOT/f'secrets/{role}/service.crt'),str(ROOT/f'secrets/{role}/service.key')) return ctx def request(host, port, path, method='GET', body=None, headers=None, role=None, target=None): c=http.client.HTTPSConnection(host,port,context=context(role),timeout=5) c.sock=context(role).wrap_socket(socket.create_connection((target or host,port),timeout=5),server_hostname=host) try: c.request(method,path,body=body,headers=headers or {}) r=c.getresponse(); data=r.read(); return r.status,dict(r.getheaders()),json.loads(data) if data else None finally: c.close() addresses=sorted({x[4][0] for x in socket.getaddrinfo('api',8080,type=socket.SOCK_STREAM)}) assert len(addresses)>=2 for ip in addresses: assert request('api',8080,'/health/ready',role='lb',target=ip)[0]==200 for role in (None,'foreign','wrong-role'): try: request('api',8080,'/health/ready',role=role,target=ip) except ssl.SSLError: pass else: raise AssertionError('mTLS allowed forbidden identity') print('PASS API replica: LB accepted; absent cert, foreign CA, same-CA wrong SAN rejected') for host,port in [('valkey',6379),('broker',6379),('s3',9443),('otel-collector',4438),('otel-collector',8899),('otel-collector',8898),('loki',3443),('tempo',3443),('tempo',4438),('prometheus',9443),('grafana',3443),('openbao',8200)]: client_role = 'api' if host=='otel-collector' else 'grafana' if host in ('loki','tempo','prometheus') else None with context(client_role).wrap_socket(socket.create_connection((host,port),timeout=5),server_hostname=host): pass try: with context(client_role).wrap_socket(socket.create_connection((host,port),timeout=5),server_hostname='forbidden.example'): pass except ssl.SSLCertVerificationError: pass else: raise AssertionError('hostname unchecked') print(f'PASS verified TLS + hostname rejection: {host}:{port}') for host,port,good in [('otel-collector',4438,'api'),('loki',3443,'otel-collector'),('tempo',4438,'otel-collector'),('prometheus',9443,'grafana')]: def probe(role): with context(role).wrap_socket(socket.create_connection((host,port),timeout=5),server_hostname=host) as conn: conn.sendall(b'GET / HTTP/1.1\r\nHost: test\r\nConnection: close\r\n\r\n') return conn.recv(64) assert probe(good).startswith(b'HTTP/') for bad in (None,'foreign','wrong-role'): try: result=probe(bad) except (ssl.SSLError,ConnectionResetError,BrokenPipeError): pass else: assert not result, 'telemetry wrong identity allowed' print(f'PASS telemetry {host}: authorized HTTP works; no-cert/foreign/wrong-URI rejected') for host,ports in [('s3',[9000,9333,8080,8888,19000,19333,18080,18888]),('otel-collector',[4318,8889,8888]),('loki',[3100,9095]),('tempo',[3200,4318,9095]),('prometheus',[9090]),('grafana',[3000])]: for port in ports: try: socket.create_connection((host,port),timeout=1).close() except OSError: pass else: raise AssertionError(f'plaintext bypass {host}:{port}') print('PASS all declared loopback plaintext hops inaccessible from app network') for role in ('api','worker','dispatcher'): status,_,auth=request('openbao',8200,'/v1/auth/cert/login','POST',json.dumps({'name':role}),{'Content-Type':'application/json'},role=role) assert status==200 and auth['auth']['lease_duration']<=300 headers={'X-Vault-Token':auth['auth']['client_token']} assert request('openbao',8200,'/v1/secret/data/'+role,headers=headers,role=role)[0]==200 for path in ('secret/data/'+('worker' if role=='api' else 'api'),'sys/mounts'): assert request('openbao',8200,'/v1/'+path,headers=headers,role=role)[0]==403 assert request('openbao',8200,'/v1/auth/token/revoke-self','POST','{}',headers,role=role)[0] in (200,204) assert request('openbao',8200,'/v1/secret/data/'+role,headers=headers,role=role)[0]==403 print(f'PASS Bao {role}: cert auth, TTL, own read, cross-role/admin deny, revoked token deny') assert request('openbao',8200,'/v1/auth/cert/login','POST','{"name":"api"}',role='wrong-role')[0] in (400,403) print('PASS Bao wrong identity cannot authenticate as API') import psycopg import redis import boto3 from botocore.exceptions import ClientError bundles=json.loads((ROOT/'operator/bundles.json').read_text()) for role in ('api','worker','dispatcher'): dsn=bundles[role]['database_url'].replace('/tls/ca.crt',CA) with psycopg.connect(dsn,autocommit=True) as db: assert db.execute('SELECT ssl FROM pg_stat_ssl WHERE pid=pg_backend_pid()').fetchone()[0] try: db.execute('CREATE TABLE forbidden_probe(id int)') except psycopg.errors.InsufficientPrivilege: pass else: raise AssertionError('DB DDL privilege') if role!='api': try: db.execute('SELECT password_hash FROM users LIMIT 1') except psycopg.errors.InsufficientPrivilege: pass else: raise AssertionError('worker read password hashes') print(f'PASS DB {role}: TLS verified and least-privilege DDL/password isolation') try: psycopg.connect(bundles['api']['database_url'].split('?')[0]+'?sslmode=disable',connect_timeout=3) except psycopg.OperationalError: pass else: raise AssertionError('DB plaintext accepted') for role in ('worker','dispatcher'): r=redis.Redis.from_url(bundles[role]['broker_url'],ssl_ca_certs=CA,ssl_check_hostname=True) assert r.ping() try: r.set('forbidden-key','test') except redis.exceptions.NoPermissionError: pass else: raise AssertionError('broker privilege escalation') print(f'PASS broker {role}: TLS auth + unrelated command/key denied') for role in ('api','worker'): b=bundles[role] s3=boto3.client('s3',endpoint_url='https://s3:9443',aws_access_key_id=b['s3_access_key_id'],aws_secret_access_key=b['s3_secret_access_key'],region_name='us-east-1',verify=CA) try: s3.list_objects_v2(Bucket='messenger-attachments') except ClientError as e: assert e.response['ResponseMetadata']['HTTPStatusCode']==403 else: raise AssertionError('S3 listing allowed') print(f'PASS S3 {role}: bucket listing denied') if os.getenv('EXPIRY_TARGET'): target=os.environ['EXPIRY_TARGET'] user='expiry_'+uuid.uuid4().hex[:16]; password='SyntheticExpiryPassword123' assert request('api',8080,'/api/v1/users','POST',json.dumps({'username':user,'display_name':'Expiry','password':password}),{'Content-Type':'application/json'},role='lb',target=target)[0]==201 status,_,session=request('api',8080,'/api/v1/auth/sessions','POST',headers={'Authorization':'Basic '+base64.b64encode((user+':'+password).encode()).decode()},role='lb',target=target) assert status==201 headers={'Authorization':'Bearer '+session['token']} assert request('api',8080,'/api/v1/users/me',headers=headers,role='lb',target=target)[0]==200 time.sleep(3) assert request('api',8080,'/api/v1/users/me',headers=headers,role='lb',target=target)[0]==401 print('PASS real service session expiry: valid token then 401 after TTL=2s')