feat: complete lab 7 grade 3 security controls
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Verify trusted TLS on data-plane endpoints, from the Linux Docker host."""
|
||||
import json
|
||||
import socket
|
||||
import ssl
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
TLS = Path(__file__).resolve().parents[1] / "secrets/tls"
|
||||
ENDPOINTS = (
|
||||
("s3", 9000, None, False),
|
||||
("broker", 5671, None, False),
|
||||
("redis", 6379, None, False),
|
||||
("db", 5432, None, True),
|
||||
("otel-collector", 4318, "otel-api", False),
|
||||
("otel-collector", 8889, "prometheus-scraper", False),
|
||||
("prometheus", 9090, "grafana-reader", False),
|
||||
("loki", 3100, "collector-exporter", False),
|
||||
("loki", 3100, "grafana-reader", False),
|
||||
("tempo", 4318, "collector-exporter", False),
|
||||
("tempo", 3200, "grafana-reader", False),
|
||||
("grafana", 3000, None, False),
|
||||
("openbao", 8200, None, False),
|
||||
)
|
||||
|
||||
|
||||
def probe(address, service, port, identity, pg, ca):
|
||||
context = ssl.create_default_context(cafile=str(TLS / f"{ca}.crt"))
|
||||
if identity:
|
||||
context.load_cert_chain(TLS / f"{identity}.crt", TLS / f"{identity}.key")
|
||||
with socket.create_connection((address, port), timeout=5) as conn:
|
||||
if pg:
|
||||
conn.sendall(bytes.fromhex("0000000804d2162f"))
|
||||
assert conn.recv(1) == b"S", "PostgreSQL did not accept TLS negotiation"
|
||||
with context.wrap_socket(conn, server_hostname=service) as encrypted:
|
||||
assert encrypted.version() in {"TLSv1.2", "TLSv1.3"}
|
||||
if identity:
|
||||
encrypted.sendall(b"GET / HTTP/1.0\r\n\r\n")
|
||||
encrypted.recv(1)
|
||||
|
||||
|
||||
def main():
|
||||
for service, port, identity, pg in ENDPOINTS:
|
||||
container = subprocess.check_output(["docker", "compose", "ps", "-q", service], text=True).strip()
|
||||
networks = json.loads(subprocess.check_output(
|
||||
["docker", "inspect", "-f", "{{json .NetworkSettings.Networks}}", container], text=True))
|
||||
address = next(network["IPAddress"] for network in networks.values() if network["IPAddress"])
|
||||
probe(address, service, port, identity, pg, "lab7-ca")
|
||||
try:
|
||||
probe(address, service, port, identity, pg, "untrusted-ca")
|
||||
except ssl.SSLCertVerificationError:
|
||||
pass
|
||||
else:
|
||||
raise AssertionError(f"{service}:{port} accepted a foreign CA")
|
||||
if identity:
|
||||
try:
|
||||
probe(address, service, port, "wrong-role", pg, "lab7-ca")
|
||||
except ssl.SSLError:
|
||||
pass
|
||||
else:
|
||||
raise AssertionError(f"{service}:{port} accepted a foreign client identity")
|
||||
print(f"PASS {service}:{port}: verified service SAN; foreign CA rejected"
|
||||
+ ("; foreign client identity rejected" if identity else ""))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user