Files
mtusi2026lab4/student-tests/permissions.py
T

17 lines
1.2 KiB
Python

#!/usr/bin/env python3
"""Prove the app S3 identity cannot manage users or modify bucket policy."""
import json,subprocess
from pathlib import Path
ROOT=Path(__file__).resolve().parents[1]
prefix='mc alias set app http://s3:9000 "$(cat /run/secrets/s3_access_key_id)" "$(cat /run/secrets/s3_secret_access_key)" >/dev/null; '
checks={}
try:
for name,command in [('admin_denied','mc admin info app'),('policy_change_denied','mc anonymous set download app/messenger-attachments')]:
r=subprocess.run(['docker','compose','run','--rm','--no-deps','--entrypoint','/bin/sh','s3-init','-c',prefix+command],cwd=ROOT,stdout=subprocess.PIPE,stderr=subprocess.STDOUT,text=True)
checks[name]=r.returncode!=0 and ('denied' in r.stdout.lower() or 'not allowed' in r.stdout.lower())
assert checks[name],name+' was unexpectedly permitted'
finally:
# Always re-apply private policy with the bootstrap identity, including failure.
subprocess.run(['docker','compose','run','--rm','--no-deps','s3-init'],cwd=ROOT,check=True,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
(ROOT/'evidence/permissions.json').write_text(json.dumps(checks,indent=2)+'\n');print('PASS: S3 application identity denied admin and bucket-policy changes')