Files
mtusi2026lab7/tests/lab7_s3_rotation.py
2026-11-20 15:30:33 +03:00

59 lines
2.0 KiB
Python

#!/usr/bin/env python3
"""Rotate a real MinIO principal, deny old credentials and finish a new job."""
# ruff: noqa: E402 -- repository scripts are imported after setting their path.
import json
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "scripts"))
from client import Client
from securitylib import LOCAL, OPERATOR
subprocess.run([sys.executable, "scripts/lab7.py", "rotate-s3"], cwd=ROOT, check=True)
old = json.loads((OPERATOR / "last-s3-rotation.json").read_text())
code = """import sys,json,urllib3
from minio import Minio
from minio.error import S3Error
x=json.load(sys.stdin)
s=Minio('minio:9000',secure=True,access_key=x['S3_WORKER_ACCESS_KEY_ID'],secret_key=x['S3_WORKER_SECRET_ACCESS_KEY'],http_client=urllib3.PoolManager(ca_certs='/run/trust/ca.crt',cert_reqs='CERT_REQUIRED'))
try:s.bucket_exists('messenger-attachments')
except S3Error as error:assert error.code in ('AccessDenied','InvalidAccessKeyId');print('OLD_S3_DENIED')
else:raise AssertionError('Old S3 credential still works')"""
result = subprocess.check_output(
["docker", "compose", "run", "--rm", "--no-deps", "-T", "migration", "python", "-c", code],
cwd=ROOT,
text=True,
input=json.dumps(old),
)
assert "OLD_S3_DENIED" in result
from lab7_lifecycle import wait_ready
wait_ready()
c = Client("https://localhost:8443", str(LOCAL / "trust/ca.crt"))
alice, bob, eve, chat = c.bootstrap()
attachment, content = c.attachment(alice, chat)
c.wait_ready(alice, attachment)
assert (
c.call("GET", "/api/v1/attachments/" + attachment["id"] + "/content", actor=bob)["bytes"]
== content
)
(ROOT / "evidence/s3-rotation-summary.json").write_text(
json.dumps(
{
"result": "PASS",
"checks": [
"new-s3-identity-applied",
"old-s3-key-denied",
"new-job-ready-original-preserved",
],
},
indent=2,
)
+ "\n"
)
print("PASS: S3 rotation, disabled old credential and new image ready")