Files
mtusi2026lab7/scripts/test-mtls.py
T
killoq 550c2e76ee
Skeleton contracts / contracts (pull_request) Blocked by required conditions
lab7
2026-12-06 15:56:06 +03:00

42 lines
1.2 KiB
Python

#!/usr/bin/env python3
import socket
import ssl
HOST = '127.0.0.1'
PORT = 8443
CA = '/certs/ca.crt'
def request(cert=None, key=None):
context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH, cafile=CA)
context.check_hostname = True
if cert:
context.load_cert_chain(cert, key)
with socket.create_connection((HOST, PORT), timeout=3) as raw:
with context.wrap_socket(raw, server_hostname='localhost') as sock:
sock.sendall(b'GET /health/live HTTP/1.0\r\nHost: localhost\r\n\r\n')
return sock.recv(1024)
print('no client certificate:')
try:
request()
except ssl.SSLError as error:
print('PASS: rejected:', error.reason)
else:
raise SystemExit('FAIL: no-certificate connection was accepted')
print('wrong identity signed by trusted CA:')
try:
request('/certs/public.crt', '/certs/public.key')
except (ssl.SSLError, ConnectionError, ConnectionResetError):
print('PASS: rejected')
else:
raise SystemExit('FAIL: wrong identity was accepted')
print('allowed LB identity:')
response = request('/certs/lb.crt', '/certs/lb.key')
if b' 200 ' not in response:
raise SystemExit('FAIL: LB identity did not receive success')
print('PASS: LB identity received HTTP 200')