forked from mtusi2026/mtusi2026lab7
170 lines
4.6 KiB
Python
170 lines
4.6 KiB
Python
#!/usr/bin/env python3
|
|
"""Test certificate-based OpenBao bootstrap in isolated containers."""
|
|
|
|
import json
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
PROJECT = Path(__file__).resolve().parent.parent
|
|
BASE = Path.home() / ".local/share/mtusi2026lab7/pki"
|
|
|
|
SERVICES = {
|
|
"api1": {
|
|
"path": "lab7/data/api/runtime",
|
|
"fields": 3,
|
|
},
|
|
"api2": {
|
|
"path": "lab7/data/api/runtime",
|
|
"fields": 3,
|
|
},
|
|
"worker": {
|
|
"path": "lab7/data/worker/runtime",
|
|
"fields": 5,
|
|
},
|
|
}
|
|
|
|
|
|
def get_network():
|
|
container = subprocess.check_output(
|
|
["docker", "compose", "ps", "-q", "openbao"],
|
|
cwd=PROJECT,
|
|
text=True,
|
|
).strip()
|
|
|
|
if not container:
|
|
raise RuntimeError("OpenBao container not running")
|
|
|
|
data = json.loads(
|
|
subprocess.check_output(
|
|
["docker", "inspect", container],
|
|
text=True,
|
|
)
|
|
)[0]
|
|
|
|
networks = data["NetworkSettings"]["Networks"]
|
|
|
|
for name in networks:
|
|
if name.endswith("_default"):
|
|
return name
|
|
|
|
raise RuntimeError("Project Docker network not found")
|
|
|
|
|
|
def test_service(service, config, network):
|
|
print(f"=== {service} ===", flush=True)
|
|
|
|
cert = BASE / "openbao-clients" / f"{service}.crt"
|
|
key = BASE / "openbao-clients" / f"{service}.key"
|
|
ca = BASE / "ca.crt"
|
|
|
|
client_module = PROJECT / "src/openbao_cert_client.py"
|
|
|
|
for path in (cert, key, ca, client_module):
|
|
if not path.is_file():
|
|
raise RuntimeError(
|
|
f"Required file missing: {path.name}"
|
|
)
|
|
|
|
test_code = (
|
|
"from src.openbao_cert_client import read_runtime_secrets\n"
|
|
"import os\n"
|
|
"secrets = read_runtime_secrets()\n"
|
|
"assert len(secrets) == int(os.environ['EXPECTED_FIELDS'])\n"
|
|
"assert all(isinstance(v, str) and v for v in secrets.values())\n"
|
|
"print('PASS: certificate authentication and KV retrieval')\n"
|
|
"print('PASS: expected number of secret fields received')\n"
|
|
"print('PASS: no secret values printed')\n"
|
|
)
|
|
|
|
command = [
|
|
"docker", "run",
|
|
"--rm",
|
|
"--network", network,
|
|
"--mount",
|
|
(
|
|
f"type=bind,source={ca},"
|
|
"target=/etc/ssl/lab7/ca.crt,readonly"
|
|
),
|
|
"--mount",
|
|
(
|
|
f"type=bind,source={cert},"
|
|
"target=/run/lab7/bao/client.crt,readonly"
|
|
),
|
|
"--mount",
|
|
(
|
|
f"type=bind,source={key},"
|
|
"target=/run/lab7/bao/client.key,readonly"
|
|
),
|
|
"--mount",
|
|
(
|
|
f"type=bind,source={client_module},"
|
|
"target=/app/src/openbao_cert_client.py,readonly"
|
|
),
|
|
"-e", "BAO_ADDR=https://openbao:8200",
|
|
"-e", "BAO_CACERT=/etc/ssl/lab7/ca.crt",
|
|
"-e", "BAO_CERT_FILE=/run/lab7/bao/client.crt",
|
|
"-e", "BAO_KEY_FILE=/run/lab7/bao/client.key",
|
|
"-e", f"BAO_CERT_NAME=lab7-{service}",
|
|
"-e", f"BAO_SECRET_PATH={config['path']}",
|
|
"-e", f"EXPECTED_FIELDS={config['fields']}",
|
|
"--entrypoint", "python",
|
|
f"mtusi2026lab7-{service}",
|
|
"-c", test_code,
|
|
]
|
|
|
|
result = subprocess.run(
|
|
command,
|
|
capture_output=True,
|
|
text=True,
|
|
timeout=90,
|
|
)
|
|
|
|
if result.returncode != 0:
|
|
print(
|
|
f"FAIL: {service} container test "
|
|
f"exit code {result.returncode}",
|
|
file=sys.stderr,
|
|
)
|
|
raise RuntimeError("Container certificate test failed")
|
|
|
|
expected_lines = (
|
|
"PASS: certificate authentication and KV retrieval",
|
|
"PASS: expected number of secret fields received",
|
|
"PASS: no secret values printed",
|
|
)
|
|
|
|
for line in expected_lines:
|
|
if line not in result.stdout:
|
|
raise RuntimeError(
|
|
f"{service}: expected test result missing"
|
|
)
|
|
print(line, flush=True)
|
|
|
|
print(
|
|
f"PASS: {service} certificate bootstrap verified",
|
|
flush=True,
|
|
)
|
|
|
|
|
|
def main():
|
|
network = get_network()
|
|
|
|
for service, config in SERVICES.items():
|
|
test_service(service, config, network)
|
|
|
|
print("PASS: three certificate bootstrap tests completed")
|
|
print("PASS: running application containers unchanged")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
main()
|
|
except Exception as exc:
|
|
print(
|
|
"FAIL: certificate bootstrap test:",
|
|
type(exc).__name__,
|
|
file=sys.stderr,
|
|
)
|
|
raise SystemExit(1)
|