forked from mtusi2026/mtusi2026lab7
92 lines
3.0 KiB
Python
92 lines
3.0 KiB
Python
import importlib
|
|
import json
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
from pathlib import Path
|
|
|
|
import security_operator as operator
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / "tests"))
|
|
Client = importlib.import_module("client").Client
|
|
|
|
|
|
def execute(*args, data=None):
|
|
result = subprocess.run(args, input=data, check=True, capture_output=True, text=True)
|
|
return result.stdout
|
|
|
|
|
|
def comparison():
|
|
data = operator.configuration("migrate")
|
|
data.update(
|
|
S3_ACCESS_KEY_ID=operator.secret("s3_root_user"),
|
|
S3_SECRET_ACCESS_KEY=operator.secret("s3_root_password"),
|
|
BROKER_URL="amqp://messenger-ops:"
|
|
+ operator.secret("rabbitmq_ops_password")
|
|
+ "@127.0.0.1:5672/messenger",
|
|
)
|
|
result = execute(
|
|
"docker",
|
|
"compose",
|
|
"run",
|
|
"--rm",
|
|
"--no-deps",
|
|
"-T",
|
|
"--entrypoint",
|
|
"python",
|
|
"security-audit",
|
|
"/audit/privilege_compare.py",
|
|
data=json.dumps(data),
|
|
)
|
|
(ROOT / "evidence/security/broad-rights.json").write_text(result)
|
|
|
|
|
|
def leakage():
|
|
client = Client("https://localhost:18443", str(ROOT / "secrets/pki/operator/ca.crt"))
|
|
actor = client.user("leakcheck")
|
|
material = [actor["token"], actor["registration"]["password"]]
|
|
identities = []
|
|
for path in (ROOT / "secrets").iterdir():
|
|
if path.is_file():
|
|
value = path.read_text().strip()
|
|
if len(value) >= 12 and "\n" not in value:
|
|
if path.name.endswith("_access_key_id") or path.name == "s3_root_user":
|
|
identities.append(value)
|
|
else:
|
|
material.append(value)
|
|
init = json.loads((operator.OPERATOR / "init.json").read_text())
|
|
material.extend([init["root_token"], *init["keys_base64"]])
|
|
time.sleep(12)
|
|
logs = execute("docker", "compose", "logs", "--no-color")
|
|
raw = execute(
|
|
"docker", "compose", "run", "--rm", "--no-deps", "security-audit", "telemetry-dump"
|
|
)
|
|
telemetry = json.loads(raw)
|
|
metrics = telemetry["metrics"]["data"]["result"]
|
|
streams = telemetry["logs"]["data"]["result"]
|
|
traces = telemetry["traces"].get("traces", [])
|
|
leaks = sum(value in logs or value in raw for value in material)
|
|
output = {
|
|
"known_secret_values_checked": len(material),
|
|
"nonsecret_s3_identity_matches": sum(value in logs or value in raw for value in identities),
|
|
"compose_log_bytes": len(logs),
|
|
"telemetry_bytes": len(raw),
|
|
"metric_series": len(metrics),
|
|
"log_streams": len(streams),
|
|
"traces": len(traces),
|
|
"exact_secret_matches": leaks,
|
|
"passed": leaks == 0 and bool(metrics) and bool(streams) and bool(traces),
|
|
}
|
|
(ROOT / "evidence/security/telemetry-secrets.json").write_text(
|
|
json.dumps(output, indent=2) + "\n"
|
|
)
|
|
if not output["passed"]:
|
|
raise RuntimeError("Telemetry presence/leakage audit failed")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
comparison()
|
|
leakage()
|
|
print("Broad permissions and telemetry leakage checks passed.")
|