forked from mtusi2026/mtusi2026lab7
grade4 fix
This commit is contained in:
@@ -4,12 +4,14 @@ 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://localhost:4318/v1/traces")
|
||||
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]:
|
||||
@@ -44,9 +46,20 @@ def test_unprivileged_client_rejected_on_api() -> None:
|
||||
|
||||
|
||||
def test_unprivileged_client_rejected_on_collector() -> None:
|
||||
if not os.getenv("COLLECTOR_URL"):
|
||||
return
|
||||
kind, status = _try_request(COLLECTOR_URL, UNPRIV_CRT, UNPRIV_KEY, data=b"{}")
|
||||
assert kind == "tls_or_conn_error" or status in (401, 403), (
|
||||
f"Expected unprivileged client to be rejected on Collector, got kind={kind}, status={status}"
|
||||
)
|
||||
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}"
|
||||
Reference in New Issue
Block a user