133 lines
5.0 KiB
Python
133 lines
5.0 KiB
Python
"""Operator-only network probe: real TLS, exact role authorization and closed raw ports."""
|
|
|
|
import json
|
|
import socket
|
|
import ssl
|
|
from pathlib import Path
|
|
|
|
CA = "/run/tls/ca.pem"
|
|
IDENTITIES = Path("/run/audit-identities")
|
|
|
|
|
|
def exchange(host, port, role, payload, *, hostname=None, trust=True):
|
|
context = (
|
|
ssl.create_default_context(cafile=CA) if trust else ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
|
|
)
|
|
if role:
|
|
context.load_cert_chain(IDENTITIES / role / "cert.pem", IDENTITIES / role / "key.pem")
|
|
context.set_alpn_protocols(["h2"] if port == 44317 else ["http/1.1"])
|
|
try:
|
|
with socket.create_connection((host, port), 2) as raw:
|
|
with context.wrap_socket(raw, server_hostname=hostname or host) as secured:
|
|
secured.settimeout(3)
|
|
secured.sendall(payload)
|
|
result = secured.recv(4096)
|
|
return bool(result), result
|
|
except (OSError, ssl.SSLError):
|
|
return False, b""
|
|
|
|
|
|
def main():
|
|
checks = {}
|
|
# Every gateway is tested positively with its intended role and negatively
|
|
# with another valid certificate from the SAME CA, no certificate, foreign
|
|
# CA certificate, empty trust and wrong server hostname.
|
|
targets = [
|
|
(
|
|
"api",
|
|
9443,
|
|
"lb",
|
|
b"GET /health/ready HTTP/1.1\r\nHost: api\r\nConnection: close\r\n\r\n",
|
|
),
|
|
("broker", 6380, "worker", b"*1\r\n$4\r\nPING\r\n"),
|
|
("cache", 6380, "api", b"*1\r\n$4\r\nPING\r\n"),
|
|
(
|
|
"storage",
|
|
8443,
|
|
"worker",
|
|
b"HEAD /messenger HTTP/1.1\r\nHost: storage:8443\r\nConnection: close\r\n\r\n",
|
|
),
|
|
(
|
|
"collector",
|
|
4318,
|
|
"api",
|
|
b"POST /v1/traces HTTP/1.1\r\nHost: collector\r\nContent-Type: application/json\r\nContent-Length: 2\r\nConnection: close\r\n\r\n{}",
|
|
),
|
|
(
|
|
"collector",
|
|
9464,
|
|
"prometheus",
|
|
b"GET /metrics HTTP/1.1\r\nHost: collector\r\nConnection: close\r\n\r\n",
|
|
),
|
|
(
|
|
"collector",
|
|
8888,
|
|
"prometheus",
|
|
b"GET /metrics HTTP/1.1\r\nHost: collector\r\nConnection: close\r\n\r\n",
|
|
),
|
|
(
|
|
"prometheus",
|
|
9443,
|
|
"grafana",
|
|
b"GET /api/v1/query?query=up HTTP/1.1\r\nHost: prometheus\r\nConnection: close\r\n\r\n",
|
|
),
|
|
(
|
|
"loki",
|
|
9443,
|
|
"grafana",
|
|
b"GET /loki/api/v1/labels HTTP/1.1\r\nHost: loki\r\nConnection: close\r\n\r\n",
|
|
),
|
|
(
|
|
"tempo",
|
|
9443,
|
|
"grafana",
|
|
b"GET /api/search HTTP/1.1\r\nHost: tempo\r\nConnection: close\r\n\r\n",
|
|
),
|
|
]
|
|
for host, port, role, payload in targets:
|
|
key = f"{host}_{port}"
|
|
ok, response = exchange(host, port, role, payload)
|
|
checks[key + "_allowed_identity"] = ok and (
|
|
response.startswith(b"HTTP/1.1") or response.startswith(b"-NOAUTH")
|
|
)
|
|
wrong = "worker" if host == "api" else "lb"
|
|
for label, identity, hostname, trust in (
|
|
("wrong_trusted_identity", wrong, None, True),
|
|
("no_client_cert", None, None, True),
|
|
("foreign_ca_client", "foreign", None, True),
|
|
("wrong_server_hostname", role, "invalid.example", True),
|
|
("empty_trust", role, None, False),
|
|
):
|
|
ok, _ = exchange(host, port, identity, payload, hostname=hostname, trust=trust)
|
|
checks[key + "_" + label + "_denied"] = not ok
|
|
otlp = b"POST /v1/traces HTTP/1.1\r\nHost: tempo\r\nContent-Type: application/json\r\nContent-Length: 2\r\nConnection: close\r\n\r\n{}"
|
|
checks["tempo_otlp_collector_allowed"] = exchange("tempo", 44318, "collector", otlp)[0]
|
|
checks["tempo_otlp_wrong_role_denied"] = not exchange("tempo", 44318, "worker", otlp)[0]
|
|
for host, ports in (
|
|
("api", [8080]),
|
|
("storage", [8333, 8888, 9333]),
|
|
("broker", [6379]),
|
|
("cache", [6379]),
|
|
("collector", [14318, 19464, 18888, 13133]),
|
|
("prometheus", [9090]),
|
|
("loki", [3100]),
|
|
("tempo", [3200, 4317, 4318]),
|
|
):
|
|
for port in ports:
|
|
try:
|
|
with socket.create_connection((host, port), 1):
|
|
checks[f"raw_{host}_{port}_closed"] = False
|
|
except OSError:
|
|
checks[f"raw_{host}_{port}_closed"] = True
|
|
# Datasource roles cannot submit arbitrary telemetry or mutate Prometheus.
|
|
forbidden = b"POST /otlp/v1/logs HTTP/1.1\r\nHost: loki\r\nContent-Length: 2\r\nConnection: close\r\n\r\n{}"
|
|
ok, body = exchange("loki", 9443, "grafana", forbidden)
|
|
checks["grafana_loki_write_denied"] = ok and b" 403 " in body.split(b"\r\n", 1)[0]
|
|
print(json.dumps({"checks": checks, "passed": all(checks.values())}, sort_keys=True))
|
|
if not all(checks.values()):
|
|
raise SystemExit(1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|