forked from mtusi2026/mtusi2026lab7
Щербацевич-БПИ2302-на 3
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Negative and positive probes for LB-to-API mTLS. Run inside the lab network."""
|
||||
import socket
|
||||
import ssl
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
HOST = "api"
|
||||
PORT = 8443
|
||||
CA = Path("/certs/ca.crt")
|
||||
LB_CERT = Path("/certs/lb-client.crt")
|
||||
LB_KEY = Path("/certs/lb-client.key")
|
||||
INTRUDER_CERT = Path("/certs/intruder.crt")
|
||||
INTRUDER_KEY = Path("/certs/intruder.key")
|
||||
FOREIGN_CERT = Path("/certs/foreign-client.crt")
|
||||
FOREIGN_KEY = Path("/certs/foreign-client.key")
|
||||
FOREIGN_CA = Path("/certs/foreign-ca.crt")
|
||||
|
||||
|
||||
def ips():
|
||||
found = sorted({item[4][0] for item in socket.getaddrinfo(HOST, PORT, type=socket.SOCK_STREAM)})
|
||||
if len(found) < 2:
|
||||
raise SystemExit("expected at least two API addresses, got %s" % found)
|
||||
return found
|
||||
|
||||
|
||||
def context(ca, cert=None, key=None):
|
||||
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
|
||||
ctx.minimum_version = ssl.TLSVersion.TLSv1_2
|
||||
ctx.check_hostname = True
|
||||
ctx.verify_mode = ssl.CERT_REQUIRED
|
||||
ctx.load_verify_locations(cafile=str(ca))
|
||||
if cert and key:
|
||||
ctx.load_cert_chain(str(cert), str(key))
|
||||
return ctx
|
||||
|
||||
|
||||
def http_get(ip, ctx):
|
||||
sock = socket.create_connection((ip, PORT), timeout=5)
|
||||
try:
|
||||
tls = ctx.wrap_socket(sock, server_hostname=HOST)
|
||||
except ssl.SSLError as exc:
|
||||
return "handshake", type(exc).__name__
|
||||
try:
|
||||
tls.sendall(b"GET /health/live HTTP/1.1\r\nHost: api\r\nConnection: close\r\n\r\n")
|
||||
data = b""
|
||||
while True:
|
||||
chunk = tls.recv(4096)
|
||||
if not chunk:
|
||||
break
|
||||
data += chunk
|
||||
if len(data) > 65536:
|
||||
break
|
||||
except (ssl.SSLError, OSError, ConnectionError) as exc:
|
||||
return "closed", type(exc).__name__
|
||||
finally:
|
||||
try:
|
||||
tls.close()
|
||||
except OSError:
|
||||
pass
|
||||
status = data.split(b" ", 2)[1].decode("ascii", "replace") if data.startswith(b"HTTP/") and b" " in data else "none"
|
||||
return "http", status
|
||||
|
||||
|
||||
def expect(ip, label, ca, cert, key, kind):
|
||||
outcome, detail = http_get(ip, context(ca, cert, key))
|
||||
if kind == "reject-handshake" and outcome == "handshake":
|
||||
print("PASS %s %s handshake rejected (%s)" % (ip, label, detail))
|
||||
return
|
||||
if kind == "reject-http" and outcome != "http" and detail != "200":
|
||||
print("PASS %s %s rejected (%s %s)" % (ip, label, outcome, detail))
|
||||
return
|
||||
if kind == "reject-http" and outcome == "http" and detail != "200":
|
||||
print("PASS %s %s rejected status %s" % (ip, label, detail))
|
||||
return
|
||||
if kind == "allow" and outcome == "http" and detail == "200":
|
||||
print("PASS %s %s accepted" % (ip, label))
|
||||
return
|
||||
raise SystemExit("FAIL %s %s -> %s %s" % (ip, label, outcome, detail))
|
||||
|
||||
|
||||
def main():
|
||||
for ip in ips():
|
||||
expect(ip, "no-cert", CA, None, None, "reject-handshake")
|
||||
expect(ip, "foreign-ca", CA, FOREIGN_CERT, FOREIGN_KEY, "reject-handshake")
|
||||
expect(ip, "wrong-identity", CA, INTRUDER_CERT, INTRUDER_KEY, "reject-http")
|
||||
expect(ip, "lb-identity", CA, LB_CERT, LB_KEY, "allow")
|
||||
print("PASS mTLS identity checks")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
main()
|
||||
except Exception as exc:
|
||||
print("FAIL %s" % type(exc).__name__, file=sys.stderr)
|
||||
raise
|
||||
Reference in New Issue
Block a user