222 lines
6.0 KiB
Python
222 lines
6.0 KiB
Python
#!/usr/bin/env python3
|
|
"""Explicit operator probes; test identities never become application mounts."""
|
|
|
|
import argparse
|
|
import json
|
|
import os
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
def run(args, *, output=None):
|
|
result = subprocess.run(args, cwd=ROOT, check=True, capture_output=True, text=True)
|
|
if output:
|
|
(ROOT / "evidence" / output).write_text(result.stdout)
|
|
if output.endswith(".json"):
|
|
record = json.loads(result.stdout)
|
|
if record.get("passed") is False:
|
|
raise RuntimeError("Security probe reported a failed invariant: " + output)
|
|
else:
|
|
print(result.stdout, end="")
|
|
return result.stdout
|
|
|
|
|
|
def foreign_certificate():
|
|
path = ROOT / "secrets/pki/foreign"
|
|
path.mkdir(exist_ok=True, mode=0o755)
|
|
run(
|
|
[
|
|
"openssl",
|
|
"req",
|
|
"-x509",
|
|
"-newkey",
|
|
"ec",
|
|
"-pkeyopt",
|
|
"ec_paramgen_curve:P-256",
|
|
"-nodes",
|
|
"-keyout",
|
|
str(path / "key.pem"),
|
|
"-out",
|
|
str(path / "cert.pem"),
|
|
"-days",
|
|
"1",
|
|
"-subj",
|
|
"/CN=foreign",
|
|
"-addext",
|
|
"subjectAltName=DNS:lb,DNS:worker,DNS:api",
|
|
]
|
|
)
|
|
for item in path.iterdir():
|
|
item.chmod(0o644)
|
|
|
|
|
|
def audit_container(module, *, material=False):
|
|
command = [
|
|
"docker",
|
|
"compose",
|
|
"run",
|
|
"--rm",
|
|
"--no-deps",
|
|
"-e",
|
|
"BAO_ROLE=",
|
|
"-e",
|
|
"OTEL_ENABLED=0",
|
|
"-v",
|
|
str(ROOT / "secrets/pki") + ":/run/audit-identities:ro",
|
|
]
|
|
if material:
|
|
# Only selected synthetic/admin S3 credential files, never Bao root/shares.
|
|
path = ROOT / "operator-private/s3-study-material"
|
|
path.mkdir(exist_ok=True, mode=0o755)
|
|
for name in (
|
|
"admin_s3_access",
|
|
"admin_s3_secret",
|
|
"study_wide_s3_access",
|
|
"study_wide_s3_secret",
|
|
"worker_s3_access",
|
|
"worker_s3_secret",
|
|
):
|
|
(path / name).write_bytes((ROOT / "secrets" / name).read_bytes())
|
|
(path / name).chmod(0o644)
|
|
command += ["-v", str(path) + ":/run/audit-material:ro"]
|
|
command += ["api", "python", "-m", module]
|
|
return command
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("probe", choices=["transport", "policies", "baseline", "revoked", "kdf"])
|
|
args = parser.parse_args()
|
|
os.umask(0o077)
|
|
if args.probe == "transport":
|
|
foreign_certificate()
|
|
run(audit_container("messenger.transport_probe"), output="transport-matrix.json")
|
|
elif args.probe == "policies":
|
|
run(
|
|
[
|
|
"docker",
|
|
"compose",
|
|
"run",
|
|
"--rm",
|
|
"--no-deps",
|
|
"worker",
|
|
"python",
|
|
"-m",
|
|
"messenger.security_probe",
|
|
],
|
|
output="worker-policy-final.json",
|
|
)
|
|
elif args.probe == "baseline":
|
|
run(
|
|
[
|
|
"docker",
|
|
"compose",
|
|
"run",
|
|
"--rm",
|
|
"--no-deps",
|
|
"migrate",
|
|
"python",
|
|
"-m",
|
|
"messenger.baseline_db_probe",
|
|
],
|
|
output="baseline-db.json",
|
|
)
|
|
run(
|
|
[
|
|
"docker",
|
|
"compose",
|
|
"exec",
|
|
"-T",
|
|
"broker",
|
|
"valkey-server",
|
|
"--port",
|
|
"6378",
|
|
"--bind",
|
|
"127.0.0.1",
|
|
"--save",
|
|
"",
|
|
"--appendonly",
|
|
"no",
|
|
"--daemonize",
|
|
"yes",
|
|
"--pidfile",
|
|
"/tmp/security-study.pid",
|
|
"--logfile",
|
|
"/tmp/security-study.log",
|
|
]
|
|
)
|
|
broker = run(["docker", "compose", "ps", "-q", "broker"]).strip()
|
|
config = json.loads(
|
|
subprocess.check_output(
|
|
["docker", "compose", "config", "--format", "json"], cwd=ROOT, text=True
|
|
)
|
|
)
|
|
app_image = config["services"]["api"]["image"]
|
|
run(
|
|
[
|
|
"docker",
|
|
"run",
|
|
"--rm",
|
|
"--network",
|
|
"container:" + broker,
|
|
"--read-only",
|
|
"--cap-drop",
|
|
"ALL",
|
|
"--security-opt",
|
|
"no-new-privileges",
|
|
"--memory",
|
|
"128m",
|
|
"--cpus",
|
|
"0.5",
|
|
app_image,
|
|
"python",
|
|
"-m",
|
|
"messenger.baseline_queue_probe",
|
|
],
|
|
output="baseline-queue.json",
|
|
)
|
|
run(
|
|
audit_container("messenger.baseline_s3_probe", material=True), output="baseline-s3.json"
|
|
)
|
|
elif args.probe == "revoked":
|
|
run(
|
|
[
|
|
"docker",
|
|
"compose",
|
|
"run",
|
|
"--rm",
|
|
"--no-deps",
|
|
"-v",
|
|
str(ROOT / "operator-private/revoked-worker") + ":/run/old-worker:ro",
|
|
"worker",
|
|
"python",
|
|
"-m",
|
|
"messenger.revoked_probe",
|
|
],
|
|
output="revoked-identity.json",
|
|
)
|
|
else:
|
|
run(
|
|
[
|
|
"docker",
|
|
"compose",
|
|
"run",
|
|
"--rm",
|
|
"--no-deps",
|
|
"-e",
|
|
"OTEL_ENABLED=0",
|
|
"api",
|
|
"python",
|
|
"-m",
|
|
"messenger.kdf_probe",
|
|
],
|
|
output="argon2-budget.json",
|
|
)
|
|
print(json.dumps({"probe": args.probe, "passed": True}))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|