forked from mtusi2026/mtusi2026lab7
65 lines
2.9 KiB
Python
65 lines
2.9 KiB
Python
import os
|
|
import ssl
|
|
import urllib.error
|
|
import urllib.request
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
ROOT_DIR = Path(__file__).resolve().parent.parent
|
|
CA_FILE = Path(os.getenv("CA_FILE", ROOT_DIR / "infra/certs/ca.crt"))
|
|
UNPRIV_CRT = Path(os.getenv("UNPRIV_CRT", ROOT_DIR / "infra/certs/unprivileged-client.crt"))
|
|
UNPRIV_KEY = Path(os.getenv("UNPRIV_KEY", ROOT_DIR / "infra/certs/unprivileged-client.key"))
|
|
API_BASE_URL = os.getenv("BASE_URL", "https://localhost:8443").rstrip("/")
|
|
COLLECTOR_URL = os.getenv("COLLECTOR_URL", "https://otel-collector:4318/v1/traces")
|
|
|
|
|
|
def _try_request(url: str, cert_file: Path | None, key_file: Path | None, data: bytes | None = None) -> tuple[str, int | None]:
|
|
ctx = ssl.create_default_context(cafile=str(CA_FILE))
|
|
if cert_file and key_file:
|
|
ctx.load_cert_chain(str(cert_file), str(key_file))
|
|
req = urllib.request.Request(url, data=data, method="POST" if data is not None else "GET")
|
|
if data is not None:
|
|
req.add_header("Content-Type", "application/json")
|
|
try:
|
|
with urllib.request.urlopen(req, context=ctx, timeout=5) as resp:
|
|
return ("ok", resp.status)
|
|
except urllib.error.HTTPError as exc:
|
|
return ("http_error", exc.code)
|
|
except (ssl.SSLError, urllib.error.URLError, OSError) as exc:
|
|
return ("tls_or_conn_error", None)
|
|
|
|
|
|
def test_unprivileged_client_rejected_on_api() -> None:
|
|
assert CA_FILE.exists(), f"Missing CA file: {CA_FILE}"
|
|
assert UNPRIV_CRT.exists() and UNPRIV_KEY.exists(), "Missing unprivileged client cert/key"
|
|
kind, status = _try_request(f"{API_BASE_URL}/health/ready", None, None)
|
|
assert kind == "ok" and status == 200, (
|
|
f"Expected public TLS ingress to accept client without mTLS cert, got kind={kind}, status={status}"
|
|
)
|
|
mtls_url = os.getenv("DIRECT_API_MTLS_URL")
|
|
if mtls_url:
|
|
m_kind, m_status = _try_request(mtls_url, UNPRIV_CRT, UNPRIV_KEY)
|
|
assert m_kind == "tls_or_conn_error" or m_status in (401, 403), (
|
|
f"Expected unprivileged client to be rejected on direct API mTLS, got kind={m_kind}, status={m_status}"
|
|
)
|
|
|
|
|
|
def test_unprivileged_client_rejected_on_collector() -> None:
|
|
assert CA_FILE.exists(), f"Missing CA file: {CA_FILE}"
|
|
assert UNPRIV_CRT.exists() and UNPRIV_KEY.exists(), "Missing unprivileged client cert/key"
|
|
kind, status = _try_request(COLLECTOR_URL, UNPRIV_CRT, UNPRIV_KEY)
|
|
if kind == "connection_error":
|
|
pytest.fail(
|
|
f"Collector unreachable at {COLLECTOR_URL}; "
|
|
f"run inside compose network or set COLLECTOR_URL"
|
|
)
|
|
if kind == "ok":
|
|
pytest.fail(
|
|
"unprivileged-client.crt unexpectedly succeeded on Collector"
|
|
)
|
|
assert kind in ("tls_error", "tls_or_conn_error", "http_error"), (
|
|
f"Unexpected response: kind={kind}, status={status}"
|
|
)
|
|
if kind == "http_error":
|
|
assert status in (401, 403), f"Expected 401/403 on collector, got {status}" |