forked from mtusi2026/mtusi2026lab7
Описать модель угроз, матрицу доверия и запуск стенда.
Отрицательная проверка mTLS вынесена в student-tests и не меняет публичные тесты. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Negative checks for the public TLS listener and the LB client identity."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
The stand must already be up. Private keys are read from certs/ and are not printed.
|
||||
"""
|
||||
|
||||
import socket
|
||||
import ssl
|
||||
import sys
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
CERTS = ROOT / "certs"
|
||||
HOST = "127.0.0.1"
|
||||
PORT = 8443
|
||||
|
||||
|
||||
def handshake(cert: Path | None, key: Path | None, ca: Path | None) -> str:
|
||||
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
|
||||
context.minimum_version = ssl.TLSVersion.TLSv1_2
|
||||
if ca is not None:
|
||||
context.load_verify_locations(ca)
|
||||
context.check_hostname = True
|
||||
context.verify_mode = ssl.CERT_REQUIRED
|
||||
else:
|
||||
context.check_hostname = False
|
||||
context.verify_mode = ssl.CERT_NONE
|
||||
if cert is not None and key is not None:
|
||||
context.load_cert_chain(cert, key)
|
||||
try:
|
||||
with socket.create_connection((HOST, PORT), timeout=5) as raw:
|
||||
with context.wrap_socket(raw, server_hostname="localhost") as tls:
|
||||
request = b"GET /health/ready HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n"
|
||||
tls.sendall(request)
|
||||
data = b""
|
||||
while True:
|
||||
chunk = tls.recv(4096)
|
||||
if not chunk:
|
||||
break
|
||||
data += chunk
|
||||
head = data.split(b"\r\n", 1)[0].decode("ascii", "replace")
|
||||
return head or "empty"
|
||||
except ssl.SSLError as exc:
|
||||
return "ssl:" + exc.__class__.__name__
|
||||
except OSError as exc:
|
||||
return "os:" + exc.__class__.__name__
|
||||
|
||||
|
||||
def expect_fail(label: str, result: str) -> None:
|
||||
if result.startswith("HTTP/1.1 200"):
|
||||
raise SystemExit("FAIL %s unexpectedly returned %s" % (label, result))
|
||||
print("PASS", label, result)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
ca = CERTS / "ca.crt"
|
||||
if not ca.is_file():
|
||||
raise SystemExit("missing %s; run scripts/bootstrap.ps1" % ca)
|
||||
ok = handshake(CERTS / "lb-client.crt", CERTS / "lb-client.key", ca)
|
||||
if not ok.startswith("HTTP/1.1 200"):
|
||||
raise SystemExit("FAIL allowed LB identity: %s" % ok)
|
||||
print("PASS allowed LB identity", ok)
|
||||
expect_fail("no client cert", handshake(None, None, ca))
|
||||
expect_fail("other CA", handshake(CERTS / "other-client.crt", CERTS / "other-client.key", ca))
|
||||
expect_fail("same CA wrong identity", handshake(CERTS / "stranger.crt", CERTS / "stranger.key", ca))
|
||||
expect_fail("revoked fingerprint", handshake(CERTS / "lb-revoked.crt", CERTS / "lb-revoked.key", ca))
|
||||
try:
|
||||
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
|
||||
context.check_hostname = True
|
||||
context.verify_mode = ssl.CERT_REQUIRED
|
||||
urllib.request.urlopen("https://localhost:%s/health/ready" % PORT, context=context, timeout=5)
|
||||
raise SystemExit("FAIL empty trust store was accepted")
|
||||
except urllib.error.URLError as exc:
|
||||
if not isinstance(exc.reason, ssl.SSLCertVerificationError):
|
||||
raise SystemExit("FAIL empty trust store failed for a non-certificate reason") from exc
|
||||
print("PASS empty trust store rejected")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in New Issue
Block a user