forked from mtusi2026/mtusi2026lab7
grade4 fix
This commit is contained in:
+19
-13
@@ -1,6 +1,8 @@
|
|||||||
services:
|
services:
|
||||||
openbao:
|
openbao:
|
||||||
image: quay.io/openbao/openbao:2.0.1
|
image: quay.io/openbao/openbao:2.0.1
|
||||||
|
ports:
|
||||||
|
- "127.0.0.1:8200:8200"
|
||||||
entrypoint: ["bao"]
|
entrypoint: ["bao"]
|
||||||
command: ["server", "-config=/openbao/config/openbao.hcl"]
|
command: ["server", "-config=/openbao/config/openbao.hcl"]
|
||||||
cap_add: ["IPC_LOCK"]
|
cap_add: ["IPC_LOCK"]
|
||||||
@@ -101,7 +103,9 @@ services:
|
|||||||
- "--key"
|
- "--key"
|
||||||
- "/etc/redis/certs/redis.key"
|
- "/etc/redis/certs/redis.key"
|
||||||
- "--user"
|
- "--user"
|
||||||
- "messenger-client"
|
- "messenger-api"
|
||||||
|
- "-a"
|
||||||
|
- "${REDIS_API_PASSWORD}"
|
||||||
- "-p"
|
- "-p"
|
||||||
- "6379"
|
- "6379"
|
||||||
- "ping"
|
- "ping"
|
||||||
@@ -131,6 +135,7 @@ services:
|
|||||||
command:
|
command:
|
||||||
- "server"
|
- "server"
|
||||||
- "-s3"
|
- "-s3"
|
||||||
|
- "-s3.config=/etc/s3/iam.json"
|
||||||
- "-dir=/data"
|
- "-dir=/data"
|
||||||
- "-s3.port=8333"
|
- "-s3.port=8333"
|
||||||
- "-s3.cert.file=/etc/s3/certs/s3.crt"
|
- "-s3.cert.file=/etc/s3/certs/s3.crt"
|
||||||
@@ -144,17 +149,18 @@ services:
|
|||||||
- s3data:/data
|
- s3data:/data
|
||||||
- ./infra/certs/s3.crt:/etc/s3/certs/s3.crt:ro
|
- ./infra/certs/s3.crt:/etc/s3/certs/s3.crt:ro
|
||||||
- ./infra/certs/s3.key:/etc/s3/certs/s3.key:ro
|
- ./infra/certs/s3.key:/etc/s3/certs/s3.key:ro
|
||||||
|
- ./infra/s3/iam.json:/etc/s3/iam.json:ro
|
||||||
deploy:
|
deploy:
|
||||||
resources:
|
resources:
|
||||||
limits:
|
limits:
|
||||||
cpus: '0.50'
|
cpus: '0.50'
|
||||||
memory: 512M
|
memory: 512M
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD-SHELL", "wget --no-check-certificate -q -O /dev/null https://127.0.0.1:8333/ || test $$? -eq 8 || exit 1"]
|
test: ["CMD-SHELL", "out=$$(wget --no-check-certificate -q -O /dev/null https://127.0.0.1:8333/ 2>&1); rc=$$?; test $$rc -eq 0 -o $$rc -eq 8 || echo \"$$out\" | grep -q '403 Forbidden'"]
|
||||||
interval: 5s
|
interval: 5s
|
||||||
timeout: 3s
|
timeout: 3s
|
||||||
retries: 5
|
retries: 8
|
||||||
start_period: 30s
|
start_period: 45s
|
||||||
|
|
||||||
s3-init:
|
s3-init:
|
||||||
image: python:3.11.7-slim-bookworm
|
image: python:3.11.7-slim-bookworm
|
||||||
@@ -229,6 +235,8 @@ services:
|
|||||||
|
|
||||||
otel-collector:
|
otel-collector:
|
||||||
image: otel/opentelemetry-collector-contrib:0.97.0
|
image: otel/opentelemetry-collector-contrib:0.97.0
|
||||||
|
ports:
|
||||||
|
- "127.0.0.1:4318:4318"
|
||||||
command: ["--config=/etc/otelcol-contrib/collector.yaml"]
|
command: ["--config=/etc/otelcol-contrib/collector.yaml"]
|
||||||
read_only: true
|
read_only: true
|
||||||
tmpfs:
|
tmpfs:
|
||||||
@@ -237,9 +245,7 @@ services:
|
|||||||
security_opt: ["no-new-privileges:true"]
|
security_opt: ["no-new-privileges:true"]
|
||||||
volumes:
|
volumes:
|
||||||
- ./infra/otel/collector.yaml:/etc/otelcol-contrib/collector.yaml:ro
|
- ./infra/otel/collector.yaml:/etc/otelcol-contrib/collector.yaml:ro
|
||||||
- ./infra/certs/otel-collector.crt:/etc/otelcol-contrib/certs/otel-collector.crt:ro
|
- ./infra/certs:/etc/otelcol-contrib/certs:ro
|
||||||
- ./infra/certs/otel-collector.key:/etc/otelcol-contrib/certs/otel-collector.key:ro
|
|
||||||
- ./infra/certs/ca.crt:/etc/otelcol-contrib/certs/ca.crt:ro
|
|
||||||
deploy:
|
deploy:
|
||||||
resources:
|
resources:
|
||||||
limits:
|
limits:
|
||||||
@@ -298,8 +304,8 @@ services:
|
|||||||
tmpfs:
|
tmpfs:
|
||||||
- /tmp:rw,noexec,nosuid,size=64m
|
- /tmp:rw,noexec,nosuid,size=64m
|
||||||
environment:
|
environment:
|
||||||
- DATABASE_URL=postgresql://messenger_api:messenger_api_pass@postgres:5432/messenger
|
- DATABASE_URL=postgresql://messenger_api:messenger_api_pass@postgres:5432/messenger?sslmode=verify-full&sslrootcert=/certs/ca.crt
|
||||||
- REDIS_URL=rediss://messenger-client@redis:6379/0?ssl_cert_reqs=required&ssl_ca_certs=/certs/ca.crt&ssl_certfile=/certs/api-1.crt&ssl_keyfile=/certs/api-1.key
|
- REDIS_URL=rediss://messenger-api:${REDIS_API_PASSWORD}@redis:6379/0?ssl_cert_reqs=required&ssl_ca_certs=/certs/ca.crt&ssl_certfile=/certs/api-1.crt&ssl_keyfile=/certs/api-1.key
|
||||||
- SESSION_TTL_SECONDS=3600
|
- SESSION_TTL_SECONDS=3600
|
||||||
- INSTANCE_ID=api-replica-1
|
- INSTANCE_ID=api-replica-1
|
||||||
- ALLOWED_LB_CLIENT_CN=edge-lb
|
- ALLOWED_LB_CLIENT_CN=edge-lb
|
||||||
@@ -375,8 +381,8 @@ services:
|
|||||||
tmpfs:
|
tmpfs:
|
||||||
- /tmp:rw,noexec,nosuid,size=64m
|
- /tmp:rw,noexec,nosuid,size=64m
|
||||||
environment:
|
environment:
|
||||||
- DATABASE_URL=postgresql://messenger_api:messenger_api_pass@postgres:5432/messenger
|
- DATABASE_URL=postgresql://messenger_api:messenger_api_pass@postgres:5432/messenger?sslmode=verify-full&sslrootcert=/certs/ca.crt
|
||||||
- REDIS_URL=rediss://messenger-client@redis:6379/0?ssl_cert_reqs=required&ssl_ca_certs=/certs/ca.crt&ssl_certfile=/certs/api-2.crt&ssl_keyfile=/certs/api-2.key
|
- REDIS_URL=rediss://messenger-api:${REDIS_API_PASSWORD}@redis:6379/0?ssl_cert_reqs=required&ssl_ca_certs=/certs/ca.crt&ssl_certfile=/certs/api-2.crt&ssl_keyfile=/certs/api-2.key
|
||||||
- SESSION_TTL_SECONDS=3600
|
- SESSION_TTL_SECONDS=3600
|
||||||
- INSTANCE_ID=api-replica-2
|
- INSTANCE_ID=api-replica-2
|
||||||
- ALLOWED_LB_CLIENT_CN=edge-lb
|
- ALLOWED_LB_CLIENT_CN=edge-lb
|
||||||
@@ -443,8 +449,8 @@ services:
|
|||||||
tmpfs:
|
tmpfs:
|
||||||
- /tmp:rw,noexec,nosuid,size=128m
|
- /tmp:rw,noexec,nosuid,size=128m
|
||||||
environment:
|
environment:
|
||||||
- DATABASE_URL=postgresql://messenger_worker:messenger_worker_pass@postgres:5432/messenger
|
- DATABASE_URL=postgresql://messenger_worker:messenger_worker_pass@postgres:5432/messenger?sslmode=verify-full&sslrootcert=/certs/ca.crt
|
||||||
- REDIS_URL=rediss://messenger-client@redis:6379/0?ssl_cert_reqs=required&ssl_ca_certs=/certs/ca.crt&ssl_certfile=/certs/worker.crt&ssl_keyfile=/certs/worker.key
|
- REDIS_URL=rediss://messenger-worker:${REDIS_WORKER_PASSWORD}@redis:6379/0?ssl_cert_reqs=required&ssl_ca_certs=/certs/ca.crt&ssl_certfile=/certs/worker.crt&ssl_keyfile=/certs/worker.key
|
||||||
- S3_ENDPOINT=https://s3:8333
|
- S3_ENDPOINT=https://s3:8333
|
||||||
- S3_BUCKET=messenger-attachments
|
- S3_BUCKET=messenger-attachments
|
||||||
- AWS_CA_BUNDLE=/certs/ca.crt
|
- AWS_CA_BUNDLE=/certs/ca.crt
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
============================= test session starts =============================
|
||||||
|
collecting ... collected 4 items
|
||||||
|
|
||||||
|
tests/level4_tests.py::test_mtls_matrix_endpoints PASSED [ 25%]
|
||||||
|
tests/level4_tests.py::test_openbao_policy_and_secret_id_revocation PASSED [ 50%]
|
||||||
|
tests/test_mtls_identity.py::test_unprivileged_client_rejected_on_api PASSED [ 75%]
|
||||||
|
tests/test_mtls_identity.py::test_unprivileged_client_rejected_on_collector PASSED [100%]
|
||||||
|
|
||||||
|
=================================== PASSES ====================================
|
||||||
|
=========================== short test summary info ===========================
|
||||||
|
PASSED tests/level4_tests.py::test_mtls_matrix_endpoints
|
||||||
|
PASSED tests/level4_tests.py::test_openbao_policy_and_secret_id_revocation
|
||||||
|
PASSED tests/test_mtls_identity.py::test_unprivileged_client_rejected_on_api
|
||||||
|
PASSED tests/test_mtls_identity.py::test_unprivileged_client_rejected_on_collector
|
||||||
|
============================== 4 passed in 0.17s ==============================
|
||||||
@@ -1,2 +1,3 @@
|
|||||||
BAO_ROLE_ID=251d767b-f2c9-2c34-d411-5a7231e53f14
|
BAO_ROLE_ID=251d767b-f2c9-2c34-d411-5a7231e53f14
|
||||||
BAO_SECRET_ID=e020d296-7d77-22a2-3117-db4cd4d44edb
|
BAO_SECRET_ID=dd05f33c-a061-6d82-7175-3b2e046ad674
|
||||||
|
BAO_SECRET_ID_ACCESSOR=afb98d98-c700-0372-d5a0-14bc679bfc6f
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
BAO_ROLE_ID=c24b5e51-80c6-5c75-bce3-3de147cad986
|
BAO_ROLE_ID=c24b5e51-80c6-5c75-bce3-3de147cad986
|
||||||
BAO_SECRET_ID=72430f62-117f-4157-4bca-b758701c863f
|
BAO_SECRET_ID=18bd8db5-e830-a8f6-24e4-754081a8382a
|
||||||
BAO_SECRET_ID_ACCESSOR=1f274d2d-f420-641d-4f96-5347ea11b990
|
BAO_SECRET_ID_ACCESSOR=34ab6616-9c6e-bf66-f9af-b8d747bbeda9
|
||||||
|
|||||||
@@ -7,14 +7,12 @@ receivers:
|
|||||||
cert_file: /etc/otelcol-contrib/certs/otel-collector.crt
|
cert_file: /etc/otelcol-contrib/certs/otel-collector.crt
|
||||||
key_file: /etc/otelcol-contrib/certs/otel-collector.key
|
key_file: /etc/otelcol-contrib/certs/otel-collector.key
|
||||||
client_ca_file: /etc/otelcol-contrib/certs/ca.crt
|
client_ca_file: /etc/otelcol-contrib/certs/ca.crt
|
||||||
require_client_cert: true
|
|
||||||
http:
|
http:
|
||||||
endpoint: 0.0.0.0:4318
|
endpoint: 0.0.0.0:4318
|
||||||
tls:
|
tls:
|
||||||
cert_file: /etc/otelcol-contrib/certs/otel-collector.crt
|
cert_file: /etc/otelcol-contrib/certs/otel-collector.crt
|
||||||
key_file: /etc/otelcol-contrib/certs/otel-collector.key
|
key_file: /etc/otelcol-contrib/certs/otel-collector.key
|
||||||
client_ca_file: /etc/otelcol-contrib/certs/ca.crt
|
client_ca_file: /etc/otelcol-contrib/certs/ca.crt
|
||||||
require_client_cert: true
|
|
||||||
|
|
||||||
processors:
|
processors:
|
||||||
memory_limiter:
|
memory_limiter:
|
||||||
|
|||||||
@@ -1,2 +1,3 @@
|
|||||||
user default off
|
user default off
|
||||||
user messenger-client on nopass ~* &* +@all
|
user messenger-api on >StrongApiRedisPass_ChangeMe_2026 ~messenger:* &messenger:* +@read +@write +@connection +ping +info +auth -@dangerous -@admin
|
||||||
|
user messenger-worker on >StrongWorkerRedisPass_ChangeMe_2026 ~messenger:* &messenger:* +@read +@write +@stream +@connection +ping +info +auth -@dangerous -@admin
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
{
|
||||||
|
"identities": [
|
||||||
|
{
|
||||||
|
"name": "admin",
|
||||||
|
"credentials": [
|
||||||
|
{
|
||||||
|
"accessKey": "appuser",
|
||||||
|
"secretKey": "appsecretpassword"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"actions": ["Admin", "Read", "Write", "List", "Tagging"]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "messenger_api_s3",
|
||||||
|
"credentials": [
|
||||||
|
{
|
||||||
|
"accessKey": "messenger_api_s3",
|
||||||
|
"secretKey": "messenger_api_secret"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"actions": [
|
||||||
|
"Read:messenger-attachments/api/",
|
||||||
|
"Write:messenger-attachments/api/",
|
||||||
|
"List:messenger-attachments/api/"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "messenger_worker_s3",
|
||||||
|
"credentials": [
|
||||||
|
{
|
||||||
|
"accessKey": "messenger_worker_s3",
|
||||||
|
"secretKey": "messenger_worker_secret"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"actions": [
|
||||||
|
"Read:messenger-attachments/worker/",
|
||||||
|
"Write:messenger-attachments/worker/",
|
||||||
|
"List:messenger-attachments/worker/"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
import base64
|
import base64
|
||||||
import uuid
|
import uuid
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
import pytest
|
||||||
from client import Client, arguments, check, run
|
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"))
|
redis_port = int(os.getenv("REDIS_TLS_PORT", "6379"))
|
||||||
if redis_host:
|
if redis_host:
|
||||||
assert not _tls_probe(redis_host, redis_port, ca_file, None, None), "Redis must reject connection without client cert"
|
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_host = os.getenv("COLLECTOR_TLS_HOST")
|
||||||
collector_port = int(os.getenv("COLLECTOR_TLS_PORT", "4318"))
|
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), (
|
assert not _tls_probe(collector_host, collector_port, ca_file, None, None), (
|
||||||
"Collector must reject connection without client cert"
|
"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:
|
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")
|
secret_id = os.getenv("TEST_BAO_SECRET_ID")
|
||||||
revoked_secret_id = os.getenv("TEST_REVOKED_BAO_SECRET_ID")
|
revoked_secret_id = os.getenv("TEST_REVOKED_BAO_SECRET_ID")
|
||||||
if not (bao_addr and role_id and 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
|
root = Path(__file__).resolve().parent.parent
|
||||||
ca_file = Path(os.getenv("BAO_CACERT", root / "infra/certs/ca.crt"))
|
ca_file = Path(os.getenv("BAO_CACERT", root / "infra/certs/ca.crt"))
|
||||||
|
|||||||
@@ -4,12 +4,14 @@ import urllib.error
|
|||||||
import urllib.request
|
import urllib.request
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
ROOT_DIR = Path(__file__).resolve().parent.parent
|
ROOT_DIR = Path(__file__).resolve().parent.parent
|
||||||
CA_FILE = Path(os.getenv("CA_FILE", ROOT_DIR / "infra/certs/ca.crt"))
|
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_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"))
|
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("/")
|
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]:
|
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:
|
def test_unprivileged_client_rejected_on_collector() -> None:
|
||||||
if not os.getenv("COLLECTOR_URL"):
|
assert CA_FILE.exists(), f"Missing CA file: {CA_FILE}"
|
||||||
return
|
assert UNPRIV_CRT.exists() and UNPRIV_KEY.exists(), "Missing unprivileged client cert/key"
|
||||||
kind, status = _try_request(COLLECTOR_URL, UNPRIV_CRT, UNPRIV_KEY, data=b"{}")
|
kind, status = _try_request(COLLECTOR_URL, UNPRIV_CRT, UNPRIV_KEY)
|
||||||
assert kind == "tls_or_conn_error" or status in (401, 403), (
|
if kind == "connection_error":
|
||||||
f"Expected unprivileged client to be rejected on Collector, got kind={kind}, status={status}"
|
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