"""lab4–lab5, уровень 4: S3 приватный, у API минимальные права, консоль закрыта, bootstrap идемпотентен. make s3-checks (= python student-tests/s3_checks.py) Все проверки идут ВНЕ API (напрямую в S3, в PostgreSQL и в файловую систему контейнеров): 1. анонимный GET объекта, список объектов bucket и список bucket'ов -> 403 (bucket приватный); 2. с ключами API объект читается (байты действительно в S3), но создать bucket, сделать bucket публичным, удалить bucket или получить список всех bucket'ов нельзя -> AccessDenied; 3. веб-интерфейсы SeaweedFS (master :9333, filer :8888, volume :8080) не слушают в сети, порт S3 не опубликован на хосте; 4. в таблице attachments нет столбца с байтами (bytea), а на дисках реплик API и worker нет файла оригинала (у worker есть только временная копия на время обработки, она удаляется); 5. s3-init запускается ещё два раза: оба раза код 0, объекты на месте, файл скачивается; 6. (lab5) сообщение для брокера (строка outbox) содержит только поля contracts/jobs.schema.json: ID и служебные данные, без байтов, ключей S3, URL и паролей. """ import hashlib import json import socket import subprocess import uuid from filegen import SAMPLE_PNG, cleanup_cli, exec_python, in_api, psql, upload_when_ready, wait_status from helpers import ROOT, Client, check, run from stand import api_containers, compose_output DIRECT = """ import hashlib, json, socket, urllib.error, urllib.request from botocore.exceptions import ClientError from messenger import config, objects base, bucket, key = config.S3_ENDPOINT, config.S3_BUCKET, "{key}" def anonymous(url): try: return urllib.request.urlopen(url, timeout=5).status except urllib.error.HTTPError as error: return error.code out = {{"anonymous_get_object": anonymous(f"{{base}}/{{bucket}}/{{key}}"), "anonymous_list_bucket": anonymous(f"{{base}}/{{bucket}}?list-type=2"), "anonymous_list_buckets": anonymous(f"{{base}}/")}} s3 = objects.client() out["api_key_get_object_sha256"] = hashlib.sha256(s3.get_object(Bucket=bucket, Key=key)["Body"].read()).hexdigest() public = json.dumps({{"Version": "2012-10-17", "Statement": [{{"Effect": "Allow", "Principal": {{"AWS": ["*"]}}, "Action": ["s3:GetObject"], "Resource": [f"arn:aws:s3:::{{bucket}}/*"]}}]}}) try: out["api_key_list_buckets"] = sorted(b["Name"] for b in s3.list_buckets().get("Buckets", [])) except ClientError as exc: out["api_key_list_buckets"] = exc.response["Error"]["Code"] for name, call in (("create_bucket", lambda: s3.create_bucket(Bucket="lab4-forbidden-bucket")), ("make_bucket_public", lambda: s3.put_bucket_policy(Bucket=bucket, Policy=public)), ("delete_bucket", lambda: s3.delete_bucket(Bucket=bucket))): try: call() out["api_key_" + name] = "ALLOWED" except ClientError as exc: out["api_key_" + name] = exc.response["Error"]["Code"] for port in (9333, 8888, 8080): try: socket.create_connection(("s3", port), timeout=2).close() out[f"admin_port_{{port}}"] = "open" except OSError: out[f"admin_port_{{port}}"] = "closed" out["bucket"] = bucket print(json.dumps(out)) """ FIND_LOCAL_COPY = """ import hashlib, os size, wanted, found = {size}, "{sha}", [] for root, dirs, files in os.walk("/"): dirs[:] = [d for d in dirs if os.path.join(root, d) not in ("/proc", "/sys", "/dev")] for name in files: path = os.path.join(root, name) try: if os.path.isfile(path) and os.path.getsize(path) == size: with open(path, "rb") as f: if hashlib.sha256(f.read()).hexdigest() == wanted: found.append(path) except OSError: pass print(len(found)) """ def host_port_closed(port): try: socket.create_connection(("127.0.0.1", port), timeout=1).close() return False except OSError: return True def main(): api = Client() alice, bob, _eve, chat = api.bootstrap() att = upload_when_ready(api, alice, chat, name="private.png")["json"] wait_status(api, bob, att["id"]) key = psql(f"SELECT storage_key FROM attachments WHERE id = '{uuid.UUID(att['id'])}'") check(key == f"originals/{att['id']}" and "private" not in key, "object key must be server-generated") print("object keys are server-generated: staging/ -> originals/, " "variants//v1/ (filename is not used in keys)") out = json.loads(in_api(DIRECT.format(key=key))) for name in ("anonymous_get_object", "anonymous_list_bucket", "anonymous_list_buckets"): print(f"{name}: HTTP {out[name]}") check(out[name] == 403, f"{name} must be 403") check(out["api_key_get_object_sha256"] == att["sha256"], "object in S3 differs from the upload") print("API key GetObject: same SHA-256 as uploaded (the bytes are in the bucket)") # SeaweedFS на ListBuckets показывает только bucket'ы, на которые у пользователя есть право List, # к которым у пользователя есть доступ: чужих bucket'ов API в любом случае не видит. listed = out["api_key_list_buckets"] print(f"API key list_buckets: {listed}") check(listed == "AccessDenied" or set(listed) <= {"messenger-attachments", out["bucket"]}, "API key sees other buckets") for name in ("create_bucket", "make_bucket_public", "delete_bucket"): print(f"API key {name}: {out['api_key_' + name]}") check(out["api_key_" + name] != "ALLOWED", f"API key must not be allowed to {name}") for port in (9333, 8888, 8080): print(f"SeaweedFS admin/UI port {port} inside the network: {out[f'admin_port_{port}']}") check(out[f"admin_port_{port}"] == "closed", f"SeaweedFS port {port} must not be reachable") check(host_port_closed(9000) and host_port_closed(9333) and host_port_closed(8888), "S3 ports must not be published on the host") print("host 127.0.0.1:9000, :9333, :8888: closed (S3 is not published)") columns = psql("SELECT string_agg(column_name || ':' || data_type, ', ' ORDER BY ordinal_position) " "FROM information_schema.columns WHERE table_name = 'attachments'") print(f"attachments columns: {columns}") check("bytea" not in columns, "attachment bytes must not be stored in PostgreSQL") workers = sorted(compose_output("ps", "-q", "worker").split()) for role, container in [("api", c) for c in api_containers()] + [("worker", c) for c in workers]: code = FIND_LOCAL_COPY.format(size=len(SAMPLE_PNG), sha=att["sha256"]) found = exec_python(container, code) print(f"{role} {container[:12]}: files with the same SHA-256 on disk: {found}") check(found == "0", f"{role} keeps a local copy of the original") payload = json.loads(psql(f"""SELECT o.payload::text FROM outbox o JOIN attachment_jobs j ON j.job_id = o.job_id WHERE j.attachment_id = '{uuid.UUID(att['id'])}' ORDER BY o.id LIMIT 1""")) allowed = {"schema_version", "job_id", "type", "attachment_id", "pipeline_version", "attempt", "created_at", "traceparent", "tracestate"} text = json.dumps(payload) check(set(payload) <= allowed, f"unexpected fields in the broker message: {set(payload) - allowed}") check(not any(word in text for word in ("staging/", "originals/", "http", "secret", "password")), "the broker message must not contain keys, URLs or credentials") print(f"broker message ({len(text)} bytes): {sorted(payload)} — only IDs and service fields") before = cleanup_cli("--dry-run")["objects_in_bucket"] for attempt in (1, 2): result = subprocess.run(["docker", "compose", "run", "--rm", "s3-init"], cwd=ROOT, capture_output=True) last = (result.stdout.decode("utf-8", "replace").strip().splitlines() or [""])[-1] print(f"s3-init run {attempt}: exit {result.returncode}: {last}") check(result.returncode == 0, "s3-init is not idempotent") after = cleanup_cli("--dry-run")["objects_in_bucket"] got = api.call("GET", f"/api/v1/attachments/{att['id']}/content", actor=bob)["bytes"] check(before == after and hashlib.sha256(got).hexdigest() == att["sha256"], "bootstrap changed the data") print(f"objects in bucket before/after bootstrap: {before}/{after}; the file still downloads") print("PASS: bucket is private outside the API, the API S3 user has least privilege, the console is " "closed, bytes are only in S3, bucket bootstrap is idempotent") if __name__ == "__main__": run(main)