"""TLS/mTLS-аудит trust matrix изнутри внутренних сетей (lab7). Запуск на стенде: python student-tests/test_tls_matrix.py (сам вызывает этот файл в контейнере probe) Вручную: docker compose --profile tools run --rm probe python /probe/tls_probe.py matrix Контейнер probe подключён ко всем внутренним сетям и получает только: CA, сертификаты test/* (чужой CA, чужая identity нашего CA, истёкший) и клиентские сертификаты, которыми пробует войти «не туда» (lb, api, worker, dispatcher, janitor, grafana, otel-collector, operator). Ключа CA, паролей и токенов OpenBao у него нет. Для каждой связи: правильная identity проходит, остальные — отказ, причём отказ именно проверки (сертификат/identity/права), а не «сервер недоступен». Вывод — JSON-строка на проверку и итог; код выхода 1, если хоть одно ожидание не выполнено. """ import http.client import json import os import socket import ssl import sys import time PKI = os.environ.get("PKI", "/pki") CA = f"{PKI}/public/ca.crt" ROGUE_CA = f"{PKI}/test/rogue-ca/ca.crt" SANDBOX = os.environ.get("SANDBOX") == "1" # Адреса: в Docker — имена сервисов; в нативной песочнице — loopback-адреса процессов (имя для SNI то же). ADDR = {"api": ("127.0.1.11" if SANDBOX else "api"), "postgres": "postgres", "redis": "redis", "s3": "s3", "rabbitmq": "rabbitmq", "openbao": "openbao", "otel-collector": "otel-collector", "prometheus": "prometheus", "loki": "loki", "tempo": "tempo", "grafana": "grafana"} RESULTS = [] def cert(name): return (f"{PKI}/{name}/tls.crt", f"{PKI}/{name}/tls.key") if name else None def ctx(client=None, ca=CA, check_hostname=True): context = ssl.create_default_context(cafile=ca) context.check_hostname = check_hostname context.minimum_version = ssl.TLSVersion.TLSv1_2 if client: context.load_cert_chain(*cert(client)) return context def classify(exc): """Причина отказа: проверка сертификата/identity — засчитывается; сеть — нет.""" text = f"{type(exc).__name__}: {exc}" lowered = text.lower() if isinstance(exc, ssl.SSLCertVerificationError) or "certificate verify failed" in lowered: return "client_rejected_server_cert", text for marker in ("certificate required", "unknown ca", "bad certificate", "certificate expired", "certificate revoked", "handshake failure", "access denied", "certificate unknown"): if marker in lowered: return "server_rejected_client_cert", text if isinstance(exc, (ConnectionResetError, BrokenPipeError)) or "eof occurred" in lowered \ or "unexpected eof" in lowered or "connection reset" in lowered or isinstance(exc, ssl.SSLZeroReturnError): return "connection_closed_after_handshake", text if isinstance(exc, (ConnectionRefusedError, socket.gaierror)) or "timed out" in lowered: return "unreachable", text return "error", text def record(link, case, expect, outcome, detail=""): ok = outcome in expect if isinstance(expect, (list, tuple, set)) else outcome == expect RESULTS.append({"link": link, "case": case, "expect": expect, "observed": outcome, "pass": ok, "detail": detail[:220]}) print(json.dumps(RESULTS[-1], ensure_ascii=False), flush=True) def https_get(host, port, path, client=None, ca=CA, timeout=5, method="GET", body=None, headers=None): """HTTPS-запрос: TCP к адресу сервиса, проверка сертификата по CA и ИМЕНИ сервиса (SNI = host).""" conn = http.client.HTTPSConnection(host, port, timeout=timeout) def connect(): raw = socket.create_connection((ADDR.get(host, host), port), timeout) conn.sock = ctx(client, ca).wrap_socket(raw, server_hostname=host) conn.connect = connect try: conn.request(method, path, body=body, headers=headers or {}) reply = conn.getresponse() return reply.status, reply.read(300) finally: conn.close() def http_case(link, case, host, port, path, client, expect, ca=CA, method="GET", body=None, headers=None): try: status, _ = https_get(host, port, path, client, ca, method=method, body=body, headers=headers) record(link, case, expect, f"http_{status}") except Exception as exc: # noqa: BLE001 outcome, text = classify(exc) record(link, case, expect, outcome, text) def plaintext_case(link, host, port, payload=b"GET / HTTP/1.0\r\n\r\n"): """Незашифрованный клиент на TLS-порт: ответа в открытом виде быть не должно.""" try: with socket.create_connection((ADDR.get(host, host), port), timeout=5) as sock: sock.sendall(payload) data = sock.recv(200) # HTTP-сервер Go отвечает 400 «client sent an HTTP request to an HTTPS server» — это не обход if b"HTTP/1.0 400" in data or b"HTTP/1.1 400" in data or not data: record(link, "plaintext client", ["no_plaintext_service"], "no_plaintext_service", repr(data[:60])) elif data.startswith(b"\x15\x03"): # TLS alert record(link, "plaintext client", ["no_plaintext_service"], "no_plaintext_service", "tls alert") else: record(link, "plaintext client", ["no_plaintext_service"], "plaintext_answer", repr(data[:80])) except Exception as exc: # noqa: BLE001 outcome, text = classify(exc) record(link, "plaintext client", ["no_plaintext_service"], "no_plaintext_service" if outcome != "unreachable" else "unreachable", text) # ---------------- связи ---------------- def lb_api(): link = "LB -> API (mTLS, allowlist CN=lb)" rejected = ["server_rejected_client_cert", "connection_closed_after_handshake"] http_case(link, "valid LB identity (CN=lb)", "api", 8443, "/health/ready", "lb", "http_200") http_case(link, "no client certificate", "api", 8443, "/health/ready", None, rejected) http_case(link, "foreign CA, CN=lb", "api", 8443, "/health/ready", "test/rogue-lb", rejected) http_case(link, "our CA, expired, CN=lb", "api", 8443, "/health/ready", "test/expired-lb", rejected) http_case(link, "our CA, CN=intruder", "api", 8443, "/health/ready", "test/intruder", rejected) http_case(link, "our CA, CN=worker (other service)", "api", 8443, "/health/ready", "worker", rejected) http_case(link, "our CA, CN=api (server's own identity)", "api", 8443, "/health/ready", "api", rejected) http_case(link, "server identity: client trusts only foreign CA", "api", 8443, "/health/ready", "lb", "client_rejected_server_cert", ca=ROGUE_CA) if os.path.exists(f"{PKI}/lb/previous/tls.crt"): http_case(link, "previous (rotated, revoked) LB leaf", "api", 8443, "/health/ready", "lb/previous", rejected) plaintext_case(link, "api", 8443) def broker(): import pika link = "API/worker -> RabbitMQ (mTLS + EXTERNAL, CN = broker user)" def connect(client, ca=CA): params = pika.ConnectionParameters(host=ADDR["rabbitmq"], port=5671, virtual_host="messenger", ssl_options=pika.SSLOptions(ctx(client, ca), server_hostname="rabbitmq"), credentials=pika.credentials.ExternalCredentials(), connection_attempts=1, socket_timeout=5, blocked_connection_timeout=5) return pika.BlockingConnection(params) def attempt(case, expect, client, action=None, ca=CA): try: connection = connect(client, ca) try: if action: action(connection.channel()) record(link, case, expect, "allowed") finally: try: connection.close() except Exception: # noqa: BLE001 pass except pika.exceptions.ProbableAuthenticationError as exc: record(link, case, expect, "login_refused", str(exc)) except pika.exceptions.ChannelClosedByBroker as exc: record(link, case, expect, f"access_refused_{exc.reply_code}", str(exc)) except Exception as exc: # noqa: BLE001 cause = exc if isinstance(exc, pika.exceptions.AMQPConnectionError) and exc.args: cause = exc.args[0] if isinstance(exc.args[0], Exception) else exc outcome, text = classify(cause) if outcome == "error" and "ssl" in str(exc).lower(): outcome = "server_rejected_client_cert" record(link, case, expect, outcome, text) rejected = ["server_rejected_client_cert", "connection_closed_after_handshake", "error"] attempt("worker identity consumes its queue", "allowed", "worker", lambda ch: ch.queue_declare("attachments.process.v1", passive=True)) attempt("dispatcher identity publishes to exchange", "allowed", "dispatcher", lambda ch: (ch.confirm_delivery(), ch.exchange_declare("attachments", passive=True))) attempt("CN=api (no broker user, API uses outbox)", "login_refused", "api") attempt("our CA, CN=intruder", "login_refused", "test/intruder") attempt("our CA, CN=lb", "login_refused", "lb") attempt("foreign CA, CN=worker", rejected, "test/rogue-worker") attempt("no client certificate", rejected, None) attempt("worker publishes (not allowed)", "access_refused_403", "worker", lambda ch: (ch.confirm_delivery(), ch.basic_publish("attachments", "attachment.process.v1", b"{}", mandatory=True))) attempt("dispatcher consumes main queue (not allowed)", "access_refused_403", "dispatcher", lambda ch: ch.basic_get("attachments.process.v1")) attempt("worker declares a queue (no configure)", "access_refused_403", "worker", lambda ch: ch.queue_declare("probe-evil-queue")) plaintext_case(link, "rabbitmq", 5671, b"AMQP\x00\x00\x09\x01") def collector(): link = "API/worker -> OTel Collector (mTLS via otel-gate, allowlist app CNs)" rejected = ["server_rejected_client_cert", "connection_closed_after_handshake"] otlp = {"Content-Type": "application/x-protobuf"} for identity in ("api", "worker", "dispatcher", "janitor"): http_case(link, f"CN={identity} sends OTLP logs", "otel-collector", 4318, "/v1/logs", identity, ["http_200", "http_400"], method="POST", body=b"", headers=otlp) for identity in ("lb", "grafana", "test/intruder", "operator"): http_case(link, f"our CA, CN={identity.split('/')[-1]} (not an app) sends OTLP", "otel-collector", 4318, "/v1/logs", identity, "http_403", method="POST", body=b"", headers=otlp) http_case(link, "foreign CA, CN=worker", "otel-collector", 4318, "/v1/logs", "test/rogue-worker", rejected, method="POST", body=b"", headers=otlp) http_case(link, "no client certificate", "otel-collector", 4318, "/v1/logs", None, rejected, method="POST", body=b"", headers=otlp) # песочница: на 127.0.0.1:8888 вместо Collector отвечает filer SeaweedFS (общий loopback) — важно лишь «не 403» http_case(link, "Collector self-metrics, CN=operator", "otel-collector", 8889, "/metrics", "operator", ["http_200", "http_404"] if SANDBOX else "http_200") http_case(link, "Collector self-metrics, CN=api (not allowed)", "otel-collector", 8889, "/metrics", "api", "http_403") plaintext_case(link, "otel-collector", 4318) def postgres(): import psycopg link = "API/worker -> PostgreSQL (TLS verify-full, SCRAM, roles)" def attempt(case, expect, **kwargs): params = {"host": ADDR["postgres"], "port": 5432, "dbname": "messenger", "user": "messenger_api", "password": "wrong-password-probe", "connect_timeout": 5, "sslcertmode": "disable"} params.update(kwargs) try: psycopg.connect(**params).close() record(link, case, expect, "connected") except psycopg.OperationalError as exc: text = str(exc) lowered = text.lower() if "password authentication failed" in lowered: outcome = "tls_ok_auth_failed" elif "no pg_hba.conf entry" in lowered or "no encryption" in lowered: outcome = "rejected_no_tls" elif "certificate verify failed" in lowered or "server certificate for" in lowered \ or "does not match host name" in lowered: outcome = "client_rejected_server_cert" else: outcome = "error" record(link, case, expect, outcome, text.replace("\n", " ")) attempt("TLS verify-full with our CA (wrong password on purpose)", "tls_ok_auth_failed", sslmode="verify-full", sslrootcert=CA) attempt("plaintext (sslmode=disable)", "rejected_no_tls", sslmode="disable") attempt("trust foreign CA only", "client_rejected_server_cert", sslmode="verify-full", sslrootcert=ROGUE_CA) if not SANDBOX: import socket as _s attempt("connect by IP: name does not match certificate", "client_rejected_server_cert", host=_s.gethostbyname("postgres"), sslmode="verify-full", sslrootcert=CA) def redis_link(): link = "API -> Redis (TLS + client cert of our CA + ACL user)" def attempt(case, expect, client, ca=CA, command=b"*1\r\n$4\r\nPING\r\n"): try: with socket.create_connection((ADDR["redis"], 6379), timeout=5) as raw: with ctx(client, ca).wrap_socket(raw, server_hostname="redis") as tls: tls.sendall(command) reply = tls.recv(200) outcome = "noauth" if b"NOAUTH" in reply else ("wrongpass" if b"WRONGPASS" in reply else ("pong" if b"PONG" in reply else "reply")) record(link, case, expect, outcome, reply[:80].decode("latin-1")) except Exception as exc: # noqa: BLE001 outcome, text = classify(exc) record(link, case, expect, outcome, text) rejected = ["server_rejected_client_cert", "connection_closed_after_handshake"] attempt("our CA client cert, no AUTH", "noauth", "test/intruder") attempt("our CA, AUTH api with a wrong password", "wrongpass", "worker", command=b"*3\r\n$4\r\nAUTH\r\n$3\r\napi\r\n$5\r\nwrong\r\n") attempt("no client certificate", rejected, None) attempt("foreign CA client certificate", rejected, "test/rogue-worker") attempt("trust foreign CA only", "client_rejected_server_cert", "worker", ca=ROGUE_CA) plaintext_case(link, "redis", 6379, b"*1\r\n$4\r\nPING\r\n") def s3(): link = "API/worker -> S3 (HTTPS + per-service keys by prefix)" http_case(link, "HTTPS anonymous list (no signature)", "s3", 8333, "/messenger-attachments?list-type=2", None, "http_403") http_case(link, "trust foreign CA only", "s3", 8333, "/", None, "client_rejected_server_cert", ca=ROGUE_CA) plaintext_case(link, "s3", 8333) # gRPC шлюза SeaweedFS: только mTLS c CN=s3 (security.toml) for case, client, expect in (("S3 gRPC :18333, CN=intruder (our CA)", "test/intruder", ["server_rejected_client_cert", "connection_closed_after_handshake"]), ("S3 gRPC :18333, CN=api (our CA)", "api", ["server_rejected_client_cert", "connection_closed_after_handshake"]), ("S3 gRPC :18333, no client certificate", None, ["server_rejected_client_cert", "connection_closed_after_handshake"])): try: context = ctx(client) context.set_alpn_protocols(["h2"]) with socket.create_connection((ADDR["s3"] if not SANDBOX else "127.0.1.20", 18333), timeout=5) as raw: with context.wrap_socket(raw, server_hostname="s3") as tls: tls.sendall(b"PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n" + b"\x00\x00\x00\x04\x00\x00\x00\x00\x00") data = tls.recv(100) data += tls.recv(100) record(link, case, expect, "accepted" if data else "connection_closed_after_handshake") except Exception as exc: # noqa: BLE001 outcome, text = classify(exc) record(link, case, expect, outcome, text) def openbao(): link = "Services -> OpenBao (TLS + cert auth by CN + policy)" def login(case, expect, client, role): try: status, body = https_get("openbao", 8200, "/v1/auth/cert/login", client, method="POST", body=json.dumps({"name": role}).encode(), headers={"Content-Type": "application/json"}) record(link, case, expect, f"http_{status}") except Exception as exc: # noqa: BLE001 outcome, text = classify(exc) record(link, case, expect, outcome, text) login("CN=worker logs in as role worker", "http_200", "worker", "worker") login("CN=worker logs in as role api", "http_400", "worker", "api") login("our CA, CN=intruder as role api", "http_400", "test/intruder", "api") login("foreign CA, CN=worker as role worker", ["http_400", "server_rejected_client_cert"], "test/rogue-worker", "worker") login("no client certificate as role api", "http_400", None, "api") http_case(link, "trust foreign CA only", "openbao", 8200, "/v1/sys/health", None, "client_rejected_server_cert", ca=ROGUE_CA) plaintext_case(link, "openbao", 8200) def telemetry_backends(): rejected = ["server_rejected_client_cert", "connection_closed_after_handshake"] link = "Collector/Grafana/operator -> Prometheus (mTLS + allowed SANs)" http_case(link, "CN=operator", "prometheus", 9090, "/-/ready", "operator", "http_200") http_case(link, "CN=grafana", "prometheus", 9090, "/-/ready", "grafana", "http_200") http_case(link, "CN=otel-collector", "prometheus", 9090, "/-/ready", "otel-collector", "http_200") http_case(link, "our CA, CN=api (SAN not allowed)", "prometheus", 9090, "/-/ready", "api", rejected) http_case(link, "our CA, CN=intruder (SAN not allowed)", "prometheus", 9090, "/-/ready", "test/intruder", rejected) http_case(link, "no client certificate", "prometheus", 9090, "/-/ready", None, rejected) plaintext_case(link, "prometheus", 9090) if SANDBOX: record("Loki/Tempo/Grafana", "not available in the native sandbox", "skipped", "skipped") return for host, port, path in (("loki", 3100, "/ready"), ("tempo", 3200, "/ready")): link = f"Collector/Grafana/operator -> {host.title()} (mTLS, CA)" # 503 = mTLS пройден, но сервис ещё не готов (Loki после старта ~15 с отвечает «Ingester not ready»): # ждём готовности до 30 с; любой отказ TLS не повторяется, ожидание остаётся http_200. deadline = time.monotonic() + 30 while time.monotonic() < deadline: try: if https_get(host, port, path, "operator")[0] != 503: break except Exception: # noqa: BLE001 — исход TLS запишет http_case ниже break time.sleep(2) http_case(link, "CN=operator", host, port, path, "operator", "http_200") http_case(link, "no client certificate", host, port, path, None, rejected) http_case(link, "foreign CA client certificate", host, port, path, "test/rogue-worker", rejected) plaintext_case(link, host, port) link = "Collector -> Tempo OTLP gRPC :4317 (mTLS)" for case, client, expect in (("no client certificate", None, rejected), ("foreign CA client certificate", "test/rogue-worker", rejected), ("CN=otel-collector", "otel-collector", "accepted")): try: context = ctx(client) context.set_alpn_protocols(["h2"]) with socket.create_connection(("tempo", 4317), timeout=5) as raw: with context.wrap_socket(raw, server_hostname="tempo") as tls: tls.sendall(b"PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n" + b"\x00\x00\x00\x04\x00\x00\x00\x00\x00") data = tls.recv(100) record(link, case, expect, "accepted" if data else "connection_closed_after_handshake") except Exception as exc: # noqa: BLE001 outcome, text = classify(exc) record(link, case, expect, outcome, text) link = "Operator -> Grafana (HTTPS)" http_case(link, "HTTPS with our CA", "grafana", 3000, "/api/health", None, "http_200") http_case(link, "trust foreign CA only", "grafana", 3000, "/api/health", None, "client_rejected_server_cert", ca=ROGUE_CA) plaintext_case(link, "grafana", 3000) GROUPS = {"lb-api": lb_api, "broker": broker, "collector": collector, "postgres": postgres, "redis": redis_link, "s3": s3, "openbao": openbao, "telemetry": telemetry_backends} def main(argv): names = argv[1:] if len(argv) > 1 and argv[0] == "matrix" else list(GROUPS) if argv and argv[0] != "matrix": names = argv started = time.monotonic() for name in names: try: GROUPS[name]() except Exception as exc: # noqa: BLE001 record(name, "probe crashed", "no_crash", "crash", f"{type(exc).__name__}: {exc}") failed = [r for r in RESULTS if not r["pass"]] print(json.dumps({"summary": True, "checks": len(RESULTS), "failed": len(failed), "seconds": round(time.monotonic() - started, 1)})) return 1 if failed else 0 if __name__ == "__main__": sys.exit(main(sys.argv[1:] or ["matrix"]))