forked from mtusi2026/mtusi2026lab7
45 lines
1.7 KiB
Python
45 lines
1.7 KiB
Python
#!/usr/bin/env python3
|
|
"""Проверка списка разрешённых identity Envoy из внутренней сети стенда."""
|
|
import socket
|
|
import ssl
|
|
import sys
|
|
|
|
PORT = 8443
|
|
PKI = "/run/pki"
|
|
|
|
|
|
def connect(host, cert=None, key=None):
|
|
context = ssl.create_default_context(cafile=f"{PKI}/ca.crt")
|
|
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=host) as tls:
|
|
request = f"GET /health/ready HTTP/1.1\r\nHost: {host}\r\nConnection: close\r\n\r\n"
|
|
tls.sendall(request.encode("ascii"))
|
|
return tls.recv(256).split(b"\r\n", 1)[0]
|
|
|
|
|
|
def rejected(host, label, **kwargs):
|
|
try:
|
|
connect(host, **kwargs)
|
|
except (ssl.SSLError, ssl.SSLCertVerificationError):
|
|
print(f"PASS: {host}: {label} rejected during TLS identity validation")
|
|
return
|
|
except OSError as exc:
|
|
print(f"FAIL: {host}: {label} transport error: {type(exc).__name__}")
|
|
raise SystemExit(1)
|
|
print(f"FAIL: {host}: {label} unexpectedly connected")
|
|
raise SystemExit(1)
|
|
|
|
for host in ("api1", "api2"):
|
|
rejected(host, "missing client certificate")
|
|
rejected(host, "foreign client CA", cert=f"{PKI}/foreign-client.crt",
|
|
key=f"{PKI}/foreign-client.key")
|
|
rejected(host, "trusted CA with wrong URI SAN", cert=f"{PKI}/wrong-client.crt",
|
|
key=f"{PKI}/wrong-client.key")
|
|
status = connect(host, cert=f"{PKI}/lb-client.crt", key=f"{PKI}/lb-client.key")
|
|
if status != b"HTTP/1.1 200 OK":
|
|
print(f"FAIL: {host}: allowed LB identity did not reach ready API")
|
|
sys.exit(1)
|
|
print(f"PASS: {host}: allowlisted LB identity reached the API")
|