diff --git a/compose.yaml b/compose.yaml index 213072e..43fd2be 100644 --- a/compose.yaml +++ b/compose.yaml @@ -110,57 +110,42 @@ services: restart: unless-stopped s3: + <<: *lockdown image: pgsty/minio:RELEASE.2026-08-04T00-00-00Z@sha256:b6bfe7239bfc83fb90d31612d9704d86039dd714f7904b3f1ad68f211e602372 - command: ["server", "/data", "--address", ":9000"] + command: ["server", "/data", "--address", ":9000", "--console-address", "127.0.0.1:9001", "--certs-dir", "/run/tls"] environment: - MINIO_ROOT_USER_FILE: /run/secrets/s3_root_user - MINIO_ROOT_PASSWORD_FILE: /run/secrets/s3_root_password + MINIO_ROOT_USER_FILE: /run/tls/root_user + MINIO_ROOT_PASSWORD_FILE: /run/tls/root_password MINIO_BROWSER: "off" volumes: - s3data:/data - - { type: volume, source: secrets, target: /run/secrets, read_only: true, volume: { subpath: s3 } } + - {type: volume, source: pki, target: /run/tls, read_only: true, volume: {subpath: s3}} + healthcheck: + test: ["CMD", "curl", "-fsS", "--cacert", "/run/tls/ca.crt", "https://s3:9000/minio/health/live"] + interval: 3s + timeout: 3s + retries: 15 + depends_on: + bootstrap: {condition: service_completed_successfully} + restart: unless-stopped deploy: resources: - limits: { cpus: "0.5", memory: 512M } - healthcheck: - test: ["CMD", "curl", "-fsS", "-o", "/dev/null", "http://127.0.0.1:9000/minio/health/live"] - interval: 5s - timeout: 3s - retries: 10 - depends_on: - secrets-init: - condition: service_completed_successfully - restart: unless-stopped + limits: {cpus: "0.5", memory: 512M} s3-init: + <<: *lockdown image: pgsty/mc:RELEASE.2026-10-26T00-00-00Z@sha256:ce62c9053c69ce12a527a322345b5ad9f44b5f8a7b8189f82e516f2e3f30f1d8 - entrypoint: ["sh", "-ec"] - command: - - | - cd /run/secrets - export MC_HOST_root="http://$$(cat s3_root_user):$$(cat s3_root_password)@s3:9000" - mc mb --ignore-existing "root/$$S3_BUCKET" - mc anonymous set none "root/$$S3_BUCKET" - cat > /tmp/policy.json < /dev/null - mc admin policy attach root messenger-api --user "$$(cat s3_access_key_id)" > /dev/null - echo "bucket $$S3_BUCKET ready" + entrypoint: ["sh", "/work/init.sh"] environment: S3_BUCKET: ${S3_BUCKET:-messenger-attachments} volumes: - - { type: volume, source: secrets, target: /run/secrets, read_only: true, volume: { subpath: s3-init } } + - ./infra/s3/init.sh:/work/init.sh:ro + - {type: volume, source: pki, target: /run/tls, read_only: true, volume: {subpath: s3-init}} + depends_on: + s3: {condition: service_healthy} deploy: resources: - limits: { cpus: "0.25", memory: 256M } - depends_on: - s3: - condition: service_healthy + limits: {cpus: "0.25", memory: 192M} migrate: <<: *lockdown @@ -190,11 +175,12 @@ services: SHUTDOWN_DELAY_MS: 3000 DATABASE_URL_FILE: /run/tls/database_url CACHE_URL_FILE: /run/secrets/cache_url - S3_ENDPOINT: http://s3:9000 + NODE_EXTRA_CA_CERTS: /run/tls/ca.crt + S3_ENDPOINT: https://s3:9000 S3_REGION: ${S3_REGION:-us-east-1} S3_BUCKET: ${S3_BUCKET:-messenger-attachments} - S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_access_key_id - S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_secret_access_key + S3_ACCESS_KEY_ID_FILE: /run/tls/s3_access_key_id + S3_SECRET_ACCESS_KEY_FILE: /run/tls/s3_secret_access_key MAX_UPLOAD_BYTES: ${MAX_UPLOAD_BYTES:-10485760} MAX_IMAGE_PIXELS: ${MAX_IMAGE_PIXELS:-40000000} JOB_MAX_ATTEMPTS: ${JOB_MAX_ATTEMPTS:-5} @@ -228,11 +214,12 @@ services: environment: DATABASE_URL_FILE: /run/tls/database_url STAGING_RETENTION_SECONDS: ${STAGING_RETENTION_SECONDS:-86400} - S3_ENDPOINT: http://s3:9000 + NODE_EXTRA_CA_CERTS: /run/tls/ca.crt + S3_ENDPOINT: https://s3:9000 S3_REGION: ${S3_REGION:-us-east-1} S3_BUCKET: ${S3_BUCKET:-messenger-attachments} - S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_access_key_id - S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_secret_access_key + S3_ACCESS_KEY_ID_FILE: /run/tls/s3_access_key_id + S3_SECRET_ACCESS_KEY_FILE: /run/tls/s3_secret_access_key volumes: - { type: volume, source: secrets, target: /run/secrets, read_only: true, volume: { subpath: reconciler } } - { type: volume, source: pki, target: /run/tls, read_only: true, volume: { subpath: reconciler } } @@ -307,11 +294,12 @@ services: environment: DATABASE_URL_FILE: /run/tls/database_url BROKER_URL_FILE: /run/secrets/broker_url - S3_ENDPOINT: http://s3:9000 + NODE_EXTRA_CA_CERTS: /run/tls/ca.crt + S3_ENDPOINT: https://s3:9000 S3_REGION: ${S3_REGION:-us-east-1} S3_BUCKET: ${S3_BUCKET:-messenger-attachments} - S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_access_key_id - S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_secret_access_key + S3_ACCESS_KEY_ID_FILE: /run/tls/s3_access_key_id + S3_SECRET_ACCESS_KEY_FILE: /run/tls/s3_secret_access_key MAX_UPLOAD_BYTES: ${MAX_UPLOAD_BYTES:-10485760} MAX_IMAGE_PIXELS: ${MAX_IMAGE_PIXELS:-40000000} WORKER_CONCURRENCY: ${WORKER_CONCURRENCY:-2} diff --git a/infra/bootstrap.py b/infra/bootstrap.py index 915a5d6..12d9de1 100644 --- a/infra/bootstrap.py +++ b/infra/bootstrap.py @@ -71,7 +71,7 @@ def main(): print("Local identities already initialized") return os.umask(0o077) - names = ("operator", "lb", "checks", "postgres", "migrate", *ROLES) + names = ("operator", "lb", "checks", "postgres", "migrate", "s3", "s3-init", *ROLES) directories = {name: ROOT / name for name in names} for directory in directories.values(): directory.mkdir(exist_ok=True) @@ -108,6 +108,20 @@ def main(): write(directories["migrate"] / "database_url", db_url("migrate", passwords["migrate"], 2)) for role, limit in ROLES.items(): write(directories[role] / "database_url", db_url(role, passwords[role], limit)) + s3 = directories["s3"] + s3_key, s3_crt = leaf(s3, "s3", server_ca) + s3_key.rename(s3 / "private.key") + s3_crt.rename(s3 / "public.crt") + for filename, value in {"root_user": secrets.token_hex(10), + "root_password": secrets.token_hex(24)}.items(): + write(s3 / filename, value) + write(directories["s3-init"] / filename, value) + + for role in ("api", "worker", "reconciler"): + password = secrets.token_hex(24) + write(directories[role] / "s3_access_key_id", f"messenger-{role}") + write(directories[role] / "s3_secret_access_key", password) + write(directories["s3-init"] / f"{role}_password", password) shutil.copyfile(api / "api.crt", checks / "api.crt") owners = {"lb": 1000, "postgres": 70, "migrate": 1000, **dict.fromkeys(ROLES, 1000)} for name, uid in owners.items(): diff --git a/infra/s3/init.sh b/infra/s3/init.sh new file mode 100644 index 0000000..76c5135 --- /dev/null +++ b/infra/s3/init.sh @@ -0,0 +1,29 @@ +#!/bin/sh +set -eu +cd /run/tls +export MC_CONFIG_DIR=/tmp/mc +mkdir -p "$MC_CONFIG_DIR/certs/CAs" +cp ca.crt "$MC_CONFIG_DIR/certs/CAs/lab7.crt" +export MC_HOST_root="https://$(cat root_user):$(cat root_password)@s3:9000" +mc mb --ignore-existing "root/$S3_BUCKET" +mc anonymous set none "root/$S3_BUCKET" >/dev/null + +policy() { + role="$1" + staging="$2" + final="$3" + cat > /tmp/policy.json </dev/null + mc admin user add root "messenger-$role" "$(cat "$role"_password)" >/dev/null + mc admin policy attach root "messenger-$role" --user "messenger-$role" >/dev/null +} +policy api '"s3:PutObject","s3:DeleteObject"' '"s3:GetObject"' +policy worker '"s3:GetObject","s3:DeleteObject"' '"s3:GetObject","s3:PutObject"' +policy reconciler '"s3:DeleteObject"' '"s3:DeleteObject"' +echo "Private bucket and role policies ready" diff --git a/scripts/check_api_security.py b/scripts/check_api_security.py new file mode 100644 index 0000000..e43b34d --- /dev/null +++ b/scripts/check_api_security.py @@ -0,0 +1,33 @@ +"""Verify attachment and user access controls over HTTPS.""" + +import sys +import uuid +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests")) +from client import Client, arguments, run + + +def main(): + args = arguments(__doc__) + client = Client(args.base_url, args.ca_file) + alice, bob, eve, chat = client.bootstrap() + attachment, _ = client.attachment(alice, chat) + attachment = client.wait_ready(alice, attachment) + base = "/api/v1/attachments/" + attachment["id"] + for suffix in ("", "/content"): + client.call("GET", base + suffix, 404, actor=eve, headers={"X-User-Id": alice["id"]}) + other = client.call("POST", "/api/v1/chats", 201, actor=bob, body={ + "title": "Separate chat", "member_ids": [bob["id"]], + }, schema="Chat")["json"] + client.call("POST", f"/api/v1/chats/{other['id']}/messages", 404, actor=bob, body={ + "text": "", "attachment_ids": [attachment["id"]], + }, headers={"Idempotency-Key": uuid.uuid4().hex}) + client.call("GET", "/api/v1/users/me", 401, headers={"X-User-Id": alice["id"]}) + client.call("DELETE", "/api/v1/auth/sessions/current", 204, actor=alice) + client.call("GET", "/api/v1/users/me", 401, actor=alice) + print("PASS: private attachment metadata/content, cross-chat reference, Bearer precedence and logout") + + +if __name__ == "__main__": + run(main)