diff --git a/.env.example b/.env.example index 939134e2..4d9bbc4a 100644 --- a/.env.example +++ b/.env.example @@ -16,6 +16,14 @@ IDEMPOTENCY_TTL_SECONDS=86400 S3_ENDPOINT=http://s3:9000 S3_REGION=us-east-1 S3_BUCKET=messenger-attachments +# compose.yaml gives minio MINIO_ROOT_USER_FILE / MINIO_ROOT_PASSWORD_FILE +# (secrets from MINIO_ROOT_USER / MINIO_ROOT_PASSWORD). Root is bootstrap-only. +# The API user is created by minio-init and limited to S3_BUCKET. +# App containers should read the files below, not these values as env vars. +MINIO_ROOT_USER=messenger-root +MINIO_ROOT_PASSWORD=change-me-minio-root +S3_ACCESS_KEY_ID=messenger-api +S3_SECRET_ACCESS_KEY=change-me-minio-api S3_ACCESS_KEY_ID_FILE=/run/secrets/s3_access_key_id S3_SECRET_ACCESS_KEY_FILE=/run/secrets/s3_secret_access_key MAX_UPLOAD_BYTES=10485760 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..dfdb8b77 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +*.sh text eol=lf diff --git a/compose.yaml b/compose.yaml index 7db346c9..e818d0c3 100644 --- a/compose.yaml +++ b/compose.yaml @@ -1,6 +1,6 @@ -# Client -> lb (HAProxy) -> api x API_REPLICAS -> postgres. -# Only the LB publishes a port. Networks: edge = lb<->api, data = api/migrate<->postgres (no LB, no internet). -# The DB password comes from .env (not committed) as a Compose secret file, never as a container env var. +# Client -> lb (HAProxy) -> api x API_REPLICAS -> postgres (metadata) + minio (private bucket). +# Only the LB publishes a port. Networks: edge = lb<->api, data = api/migrate/minio (no LB, no internet). +# DB and MinIO passwords come from .env (not committed) as Compose secret files, never as container env vars. name: messenger-lab4 x-db-client: &db-client @@ -70,6 +70,72 @@ services: memory: 128M restart: "no" + # MinIO Community RELEASE.2025-09-07T16-13-09Z. The digest is the official + # multi-arch manifest list (linux/amd64 inside it is sha256:a1a8bd4a…). + # docker.io/minio/minio and quay.io/minio/minio no longer allow an anonymous + # pull; this GHCR mirror still serves that same manifest list. + # No host ports. MINIO_BROWSER=off: the admin console is not started. + # S3 API is http://minio:9000, also http://s3:9000 (network alias). + minio: + image: ghcr.io/tsouza/cerberus-mirror/minio/minio:RELEASE.2025-09-07T16-13-09Z@sha256:14cea493d9a34af32f524e538b8346cf79f3321eff8e708c1e2960462bd8936e + command: ["server", "/data"] + environment: + MINIO_ROOT_USER_FILE: /run/secrets/minio_root_user + MINIO_ROOT_PASSWORD_FILE: /run/secrets/minio_root_password + MINIO_BROWSER: "off" + MINIO_UPDATE: "off" + secrets: + - minio_root_user + - minio_root_password + volumes: + - miniodata:/data + networks: + data: + aliases: + - s3 + healthcheck: + test: ["CMD", "curl", "-fsS", "http://127.0.0.1:9000/minio/health/ready"] + interval: 2s + timeout: 5s + retries: 30 + start_period: 10s + deploy: + resources: + limits: + cpus: "${MINIO_CPUS:-1.0}" + memory: "${MINIO_MEMORY:-512M}" + reservations: + cpus: "0.25" + memory: 256M + restart: unless-stopped + + # Creates the private bucket and the bucket-scoped API user. Re-running is a no-op. + minio-init: + image: ghcr.io/tsouza/cerberus-mirror/minio/minio:RELEASE.2025-09-07T16-13-09Z@sha256:14cea493d9a34af32f524e538b8346cf79f3321eff8e708c1e2960462bd8936e + entrypoint: ["/bin/sh", "/bootstrap/minio-bootstrap.sh"] + environment: + S3_BUCKET: ${S3_BUCKET:?set in .env} + S3_REGION: ${S3_REGION:-us-east-1} + MINIO_ENDPOINT: http://minio:9000 + secrets: + - minio_root_user + - minio_root_password + - s3_access_key_id + - s3_secret_access_key + volumes: + - ./infra/minio/bootstrap.sh:/bootstrap/minio-bootstrap.sh:ro + networks: + - data + depends_on: + minio: + condition: service_healthy + deploy: + resources: + limits: + cpus: "0.5" + memory: 128M + restart: "no" + api: image: messenger-lab4-app:${APP_VERSION:-dev} build: *app-build @@ -107,6 +173,8 @@ services: condition: service_healthy migrate: condition: service_completed_successfully + minio-init: + condition: service_completed_successfully # Docker health gates startup order and rolling update; the LB runs its own checks (infra/haproxy/haproxy.cfg). healthcheck: test: @@ -160,6 +228,15 @@ networks: secrets: postgres_password: environment: POSTGRES_PASSWORD + minio_root_user: + environment: MINIO_ROOT_USER + minio_root_password: + environment: MINIO_ROOT_PASSWORD + s3_access_key_id: + environment: S3_ACCESS_KEY_ID + s3_secret_access_key: + environment: S3_SECRET_ACCESS_KEY volumes: pgdata: + miniodata: diff --git a/infra/minio/bootstrap.sh b/infra/minio/bootstrap.sh new file mode 100644 index 00000000..5dfaac06 --- /dev/null +++ b/infra/minio/bootstrap.sh @@ -0,0 +1,112 @@ +#!/bin/sh +# Idempotent private-bucket bootstrap for MinIO. Root credentials are read from +# files and are not printed. A second run leaves the bucket, policy and API user in place. +set -eu + +bucket=${S3_BUCKET:?S3_BUCKET is required} +region=${S3_REGION:-us-east-1} +endpoint=${MINIO_ENDPOINT:-http://minio:9000} +policy_name=attachments-api + +case "$bucket" in + [a-z0-9][a-z0-9.-]*[a-z0-9]) ;; + *) + echo "S3_BUCKET must be a DNS-style name" >&2 + exit 1 + ;; +esac + +read_secret() { + tr -d '\r\n' < "$1" +} + +root_user=$(read_secret /run/secrets/minio_root_user) +root_password=$(read_secret /run/secrets/minio_root_password) +access_key=$(read_secret /run/secrets/s3_access_key_id) +secret_key=$(read_secret /run/secrets/s3_secret_access_key) + +if [ -z "$root_user" ] || [ -z "$root_password" ] || [ -z "$access_key" ] || [ -z "$secret_key" ]; then + echo "MinIO bootstrap secrets are empty" >&2 + exit 1 +fi + +i=0 +until mc alias set local "$endpoint" "$root_user" "$root_password" >/dev/null; do + i=$((i + 1)) + if [ "$i" -ge 30 ]; then + echo "MinIO is not accepting the root credentials" >&2 + exit 1 + fi + sleep 1 +done + +# A new MinIO bucket has no anonymous policy. Set none explicitly so a later +# public policy cannot survive a repeated bootstrap. +mc mb --ignore-existing --region "$region" "local/${bucket}" >/dev/null +mc anonymous set none "local/${bucket}" >/dev/null + +anon=$(mc anonymous get "local/${bucket}") +case "$anon" in + *private*) ;; + *) + echo "bucket ${bucket} is not private" >&2 + exit 1 + ;; +esac + +probe_key=".bootstrap/private-probe" +printf 'probe' | mc pipe "local/${bucket}/${probe_key}" >/dev/null +code=$(curl -s -o /dev/null -w '%{http_code}' "${endpoint}/${bucket}/${probe_key}") +mc rm --force "local/${bucket}/${probe_key}" >/dev/null +if [ "$code" != "403" ]; then + echo "unauthenticated GET returned ${code}, expected 403" >&2 + exit 1 +fi + +policy_file=/tmp/attachments-api-policy.json +cat > "$policy_file" </dev/null 2>&1; then + mc admin policy create local "$policy_name" "$policy_file" >/dev/null +fi + +if ! mc admin user info local "$access_key" >/dev/null 2>&1; then + mc admin user add local "$access_key" "$secret_key" >/dev/null +fi + +mc admin policy attach local "$policy_name" --user "$access_key" >/dev/null + +mc alias set api "$endpoint" "$access_key" "$secret_key" >/dev/null +api_key=".bootstrap/api-probe" +printf 'ok' | mc pipe "api/${bucket}/${api_key}" >/dev/null +mc cat "api/${bucket}/${api_key}" >/dev/null +mc rm --force "api/${bucket}/${api_key}" >/dev/null + +if mc ls "api/not-this-bucket" >/dev/null 2>&1; then + echo "API credentials can list a different bucket" >&2 + exit 1 +fi + +echo "bucket ${bucket} is private" diff --git a/scripts/restart.sh b/scripts/restart.sh index b0d4268e..efc12d4e 100644 --- a/scripts/restart.sh +++ b/scripts/restart.sh @@ -1,8 +1,8 @@ #!/bin/sh set -eu cd "$(dirname "$0")/.." -# Restart the LB, every API replica and PostgreSQL. The pgdata volume stays; nothing is re-seeded. +# Restart the LB, every API replica, PostgreSQL and MinIO. Volumes stay; nothing is re-seeded. docker compose --env-file .env stop lb api -docker compose --env-file .env restart postgres +docker compose --env-file .env restart postgres minio # Brings back all replicas (including one left stopped by stop-one.sh); migrate re-runs as a no-op. docker compose --env-file .env up -d --wait