#!/usr/bin/env python3 """Prove that the API identity is bucket-scoped and lacks admin operations.""" import subprocess from pathlib import Path import sys import uuid ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "tests")) from client import Client, arguments, check, run def compose(*args, input=None): return subprocess.check_output(["docker", "compose", *args], cwd=ROOT, text=True, input=input, stderr=subprocess.DEVNULL) def main(): args = arguments(__doc__) client = Client(args.base_url, args.ca_file) alice, _bob, _eve, chat = client.bootstrap() data = (ROOT / "tests/fixtures/sample.png").read_bytes() client.call("POST", "/api/v1/chats/" + chat["id"] + "/attachments?filename=permission.png", 202, actor=alice, raw=data, headers={"Content-Type": "image/png"}, schema="Attachment") bucket = "permission-probe-" + uuid.uuid4().hex code = """from botocore.exceptions import ClientError from src.storage.s3 import S3Settings, S3Store import sys s3 = S3Store(S3Settings.load()) try: try: s3.client.create_bucket(Bucket=sys.stdin.read().strip()) except ClientError as error: assert error.response['Error']['Code'] in {'AccessDenied', 'Unauthorized'}, error.response else: raise AssertionError('API S3 identity unexpectedly has admin bucket creation') finally: s3.client.close() """ compose("exec", "-T", "api-a", "/app/.venv/bin/python", "-c", code, input=bucket) admin = """from src.storage.s3 import S3Settings, S3Store import sys s3 = S3Store(S3Settings.load()) try: s3.client.list_buckets() finally: s3.client.close() """ compose("run", "--rm", "--no-deps", "s3-init", "/app/.venv/bin/python", "-c", admin) print("PASS: API identity cannot create buckets; maintenance identity has separate admin access") if __name__ == "__main__": run(main)