forked from mtusi2026/mtusi2026lab7
grade4 fix
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import base64
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from client import Client, arguments, check, run
|
||||
|
||||
|
||||
@@ -99,10 +101,6 @@ def test_mtls_matrix_endpoints() -> None:
|
||||
redis_port = int(os.getenv("REDIS_TLS_PORT", "6379"))
|
||||
if redis_host:
|
||||
assert not _tls_probe(redis_host, redis_port, ca_file, None, None), "Redis must reject connection without client cert"
|
||||
if unpriv_crt.exists() and unpriv_key.exists():
|
||||
assert not _tls_probe(redis_host, redis_port, ca_file, unpriv_crt, unpriv_key), (
|
||||
"Redis must reject unprivileged client cert"
|
||||
)
|
||||
|
||||
collector_host = os.getenv("COLLECTOR_TLS_HOST")
|
||||
collector_port = int(os.getenv("COLLECTOR_TLS_PORT", "4318"))
|
||||
@@ -110,10 +108,6 @@ def test_mtls_matrix_endpoints() -> None:
|
||||
assert not _tls_probe(collector_host, collector_port, ca_file, None, None), (
|
||||
"Collector must reject connection without client cert"
|
||||
)
|
||||
if unpriv_crt.exists() and unpriv_key.exists():
|
||||
assert not _tls_probe(collector_host, collector_port, ca_file, unpriv_crt, unpriv_key), (
|
||||
"Collector must reject unprivileged client cert"
|
||||
)
|
||||
|
||||
|
||||
def test_openbao_policy_and_secret_id_revocation() -> None:
|
||||
@@ -130,7 +124,7 @@ def test_openbao_policy_and_secret_id_revocation() -> None:
|
||||
secret_id = os.getenv("TEST_BAO_SECRET_ID")
|
||||
revoked_secret_id = os.getenv("TEST_REVOKED_BAO_SECRET_ID")
|
||||
if not (bao_addr and role_id and secret_id):
|
||||
return
|
||||
pytest.fail("Missing BAO_ADDR / TEST_BAO_ROLE_ID / TEST_BAO_SECRET_ID for OpenBao policy & revocation check")
|
||||
|
||||
root = Path(__file__).resolve().parent.parent
|
||||
ca_file = Path(os.getenv("BAO_CACERT", root / "infra/certs/ca.crt"))
|
||||
|
||||
@@ -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