"""Реальный MinIO: повтор bootstrap, private bucket, ограниченные права и отказ.""" import json import base64 from pathlib import Path import subprocess import time from scripts.deployment import ComposeProject from tests.client import Client, arguments, check, run PROBE = r''' import asyncio, json, sys from pathlib import Path from uuid import UUID from urllib.request import Request, urlopen from urllib.error import HTTPError, URLError from app.core.config import Settings from app.core.database import create_db_engine, create_session_factory from app.storage.s3 import create_s3_storage from app.models.attachment import Attachment from minio.error import S3Error async def main(): settings = Settings() engine = create_db_engine(settings) storage = create_s3_storage(settings.s3) try: async with create_session_factory(engine)() as session: record = await session.get(Attachment, UUID(json.load(sys.stdin)['attachment_id'])) key = record.object_key results = {} for method in ('GET', 'PUT'): request = Request(settings.s3.endpoint + '/' + settings.s3.bucket + '/' + key, method=method, data=b'x' if method == 'PUT' else None) try: with urlopen(request, timeout=3) as response: results[method] = response.status except HTTPError as error: results[method] = error.code error.close() try: storage._client.get_bucket_policy(settings.s3.bucket) results['admin_denied'] = False except S3Error as error: results['admin_denied'] = error.code == 'AccessDenied' try: buckets = storage._client.list_buckets() results['other_buckets_denied'] = all(bucket.name == settings.s3.bucket for bucket in buckets) except S3Error as error: results['other_buckets_denied'] = error.code == 'AccessDenied' try: with urlopen(settings.s3.endpoint.replace(':9000', ':9001'), timeout=1): results['console_unreachable'] = False except HTTPError as error: results['console_unreachable'] = False error.close() except URLError: results['console_unreachable'] = True results['temp_originals_absent'] = not any(Path('/tmp').glob('messenger-upload-*')) results['root_files_absent'] = not any(Path('/run/secrets', name).exists() for name in ('minio_root_user', 'minio_root_password')) print(json.dumps(results)) finally: await storage.aclose() await engine.dispose() asyncio.run(main()) ''' def main(): def configure(parser): parser.add_argument('--with-outage', action='store_true', help='временно остановить MinIO выбранного проекта') parser.add_argument('--with-faults', action='store_true', help='проверить компенсацию и очистку на реальных S3/БД') args = arguments('MinIO private bucket and failure checks', configure) project = ComposeProject() client = Client(args.base_url, args.ca_file, timeout=40) alice, _, _, chat = client.bootstrap() attachment, original = client.attachment(alice, chat) # Повторный bootstrap не должен очистить bucket или сменить используемые credentials. for _ in range(2): project.run('run', '--rm', '--no-deps', 'minio-bootstrap', timeout=100) replicas = project.replicas() check(bool(replicas), 'API not running') result = subprocess.run(['docker', 'exec', '-i', replicas[0].id, 'python', '-c', PROBE], input=json.dumps({'attachment_id': attachment['id']}), text=True, capture_output=True, timeout=20) check(result.returncode == 0, 'private bucket probe failed (details hidden)') statuses = json.loads(result.stdout) check(statuses['GET'] == statuses['PUT'] == 403, 'anonymous S3 access allowed') check(statuses['admin_denied'] and statuses['other_buckets_denied'] and statuses['root_files_absent'], 'API has excessive access') check(statuses['console_unreachable'], 'console reachable from API network') check(statuses['temp_originals_absent'], 'API retained temporary originals') check(not project.config['services']['minio'].get('ports'), 'MinIO publishes host ports') env = project.config['services']['minio']['environment'] check(env['MINIO_BROWSER'] == 'off', 'browser enabled') check('127.0.0.1:9001' in project.config['services']['minio']['command'], 'console is not bound to loopback') path = '/api/v1/attachments/' + attachment['id'] + '/content' check(client.call('GET', path, actor=alice)['bytes'] == original, 'bootstrap changed original') if args.with_faults: project.run('stop', 'cleanup') try: fault_script = Path(__file__).with_name('s3_faults.py').read_text() result = subprocess.run( ['docker', 'exec', '-i', replicas[0].id, 'python', '-c', fault_script], input=json.dumps({'user': alice['user'], 'chat_id': chat['id'], 'image': base64.b64encode(original).decode()}), text=True, capture_output=True, timeout=60, ) check(result.returncode == 0, 'real S3/database compensation probe failed (details hidden)') check(all(json.loads(result.stdout).values()), 'real S3/database invariants failed') finally: project.run('start', 'cleanup') check(client.call('GET', path, actor=alice)['bytes'] == original, 'cleanup damaged confirmed original') if args.with_outage: try: project.run('stop', 'minio') client.call('GET', path, expected=503, actor=alice) finally: project.run('start', 'minio') deadline = time.monotonic() + 60 while True: try: client.call('GET', '/health/ready', schema='Health') break except Exception: if time.monotonic() >= deadline: raise time.sleep(0.5) check(client.call('GET', path, actor=alice)['bytes'] == original, 'original lost after MinIO restart') print('PASS: idempotent bootstrap, anonymous GET/PUT denied, restricted API credentials, private console' + (', MinIO outage/recovery' if args.with_outage else '')) if args.with_faults: print('PASS: real S3 PUT/database failure, compensation, durable journal, grace period, dry-run, repeated cleanup') if __name__ == '__main__': run(main)