66 lines
2.8 KiB
Python
66 lines
2.8 KiB
Python
"""Inside the isolated test network. Every permitted edge and rejected identity.
|
|
|
|
A timeout/refused connection is never counted as a valid negative TLS result.
|
|
"""
|
|
import json
|
|
from pathlib import Path
|
|
import socket
|
|
import ssl
|
|
import struct
|
|
import time
|
|
|
|
IDENTITIES=Path('/audit-identities')
|
|
TRUST=Path('/trust')
|
|
|
|
|
|
def context(role=None):
|
|
ctx=ssl.create_default_context(cafile=str(TRUST/'bundle.pem'))
|
|
ctx.verify_flags |= ssl.VERIFY_CRL_CHECK_LEAF
|
|
if role:ctx.load_cert_chain(IDENTITIES/role/'cert.pem',IDENTITIES/role/'key.pem')
|
|
return ctx
|
|
|
|
|
|
def probe(host,port,role,hostname=None):
|
|
ctx=context(role)
|
|
with socket.create_connection((host,port),timeout=5) as raw:
|
|
with ctx.wrap_socket(raw,server_hostname=hostname or host) as conn:
|
|
conn.settimeout(5)
|
|
if host=='db':payload=struct.pack('!II',8,80877103)
|
|
elif host in ('cache','broker'):payload=b'*1\r\n$4\r\nPING\r\n'
|
|
else:payload=b'GET /health/live HTTP/1.1\r\nHost: '+host.encode()+b'\r\nConnection: close\r\n\r\n'
|
|
conn.sendall(payload)
|
|
return conn.recv(4096)
|
|
|
|
|
|
def rejected(host,port,role):
|
|
try:return not probe(host,port,role)
|
|
except (ssl.SSLError,ConnectionResetError,BrokenPipeError):return True
|
|
|
|
|
|
def main():
|
|
start=time.monotonic();results=[]
|
|
for file in sorted(Path('/mesh').glob('*.json')):
|
|
host=file.stem
|
|
for route in json.loads(file.read_text()):
|
|
if route['mode']!='server':continue
|
|
port=route['listen']
|
|
for role in route['allow']:
|
|
assert probe(host,port,role),f'Permitted edge failed: {role}->{host}:{port}'
|
|
results.append({'source':role,'destination':host,'port':port,'allowed':True})
|
|
for role in (None,'outsider','alien','expired','revoked'):
|
|
assert rejected(host,port,role),f'Forbidden edge accepted: {role}->{host}:{port}'
|
|
# Same-CA hostname mismatch must fail client validation, not timeout.
|
|
try:probe(host,port,route['allow'][0],hostname='outsider')
|
|
except ssl.SSLCertVerificationError:pass
|
|
else:raise AssertionError('Wrong server identity accepted')
|
|
# Plaintext has no protocol response from a TLS-only ingress.
|
|
with socket.create_connection((host,port),timeout=5) as raw:
|
|
raw.settimeout(5);raw.sendall(b'GET / HTTP/1.1\r\nHost: x\r\n\r\n')
|
|
try:assert raw.recv(100)==b''
|
|
except ConnectionResetError:pass
|
|
results.append({'destination':host,'port':port,'denied':['no_cert','wrong_CA','wrong_SAN','expired','revoked','wrong_server_SAN','plaintext']})
|
|
print(json.dumps({'passed':True,'seconds':round(time.monotonic()-start,2),'edges':results},indent=2))
|
|
|
|
|
|
if __name__=='__main__':main()
|