forked from mtusi2026/mtusi2026lab5
153 lines
5.9 KiB
Python
153 lines
5.9 KiB
Python
"""Реальный MinIO: повтор bootstrap, private bucket, ограниченные права и отказ."""
|
|
|
|
import json
|
|
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 выбранного проекта",
|
|
)
|
|
|
|
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()
|
|
aliases = {replica.instance_id for replica in project.replicas() if replica.running}
|
|
live = client.call("GET", "/health/live", schema="Health")
|
|
check(
|
|
live["headers"].get("X-Instance-Id") in aliases,
|
|
"BASE_URL does not belong to the selected Compose project",
|
|
)
|
|
attachment, original = client.attachment(alice, chat)
|
|
attachment = client.wait_ready(alice, attachment)
|
|
|
|
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_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 __name__ == "__main__":
|
|
run(main)
|