#!/usr/bin/env python3 """Check anonymous access to the private bucket from inside the Compose network.""" from pathlib import Path import json import subprocess import sys ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / 'tests')) from client import Failure, run def main(): code = ( "import os, urllib.error, urllib.request; " "bucket=os.environ['S3_BUCKET']; " "url='http://minio:9000/'+bucket+'?list-type=2&prefix=objects%2F'; " "req=urllib.request.Request(url); " "\ntry:\n urllib.request.urlopen(req, timeout=3)\n" "except urllib.error.HTTPError as error:\n" " raise SystemExit(0 if error.code == 403 else 1)\n" "raise SystemExit(1)" ) result = subprocess.run( ['docker', 'compose', 'exec', '-T', 'api', 'python', '-c', code], cwd=ROOT, capture_output=True, text=True, ) if result.returncode: raise Failure('anonymous MinIO bucket request did not return HTTP 403') container_id = subprocess.run( ['docker', 'compose', 'ps', '-q', 'minio'], cwd=ROOT, capture_output=True, text=True, check=True, ).stdout.strip() if not container_id: raise Failure('MinIO container is not running') bindings = subprocess.run( ['docker', 'inspect', '--format', '{{json .HostConfig.PortBindings}}', container_id], cwd=ROOT, capture_output=True, text=True, check=True, ).stdout.strip() if any(value for value in json.loads(bindings).values()): raise Failure('MinIO S3 endpoint must not be published on the host') print('PASS: anonymous bucket access is denied and the S3 port is not published.') if __name__ == '__main__': run(main)