#!/bin/sh # Идемпотентная инициализация MinIO: приватный bucket, ограниченная политика и # отдельный пользователь API. Выполняется в контейнере minio/mc. set -eu IFS= read -r root_user < /run/secrets/minio_root_user || [ -n "$root_user" ] IFS= read -r root_password < /run/secrets/minio_root_password || [ -n "$root_password" ] IFS= read -r api_key < /run/secrets/s3_api_access_key_id || [ -n "$api_key" ] IFS= read -r api_secret < /run/secrets/s3_api_secret_access_key || [ -n "$api_secret" ] IFS= read -r worker_key < /run/secrets/s3_worker_access_key_id || [ -n "$worker_key" ] IFS= read -r worker_secret < /run/secrets/s3_worker_secret_access_key || [ -n "$worker_secret" ] bucket="${S3_BUCKET:?}" export MC_CONFIG_DIR=/tmp/.mc export MC_HOST_stand="http://${root_user}:${root_password}@s3:9000" mc mb --ignore-existing "stand/$bucket" mc anonymous set none "stand/$bucket" # The API owns the staging prefix only: it writes the accepted upload there and # can remove it after the worker commits the result. The final prefix is read # for variant/original downloads and legacy lab4 originals, but never written. printf '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":["s3:GetObject","s3:PutObject","s3:DeleteObject"],"Resource":["arn:aws:s3:::%s/staging/*"]},{"Effect":"Allow","Action":["s3:GetObject","s3:DeleteObject"],"Resource":["arn:aws:s3:::%s/attachments/*"]},{"Effect":"Allow","Action":["s3:ListBucket"],"Resource":["arn:aws:s3:::%s"]}]}' "$bucket" "$bucket" "$bucket" > /tmp/messenger-api-policy.json # The worker reads staging, writes the final original and variants, and removes # staging after a safe commit. It never deletes the final prefix. printf '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":["s3:GetObject","s3:DeleteObject"],"Resource":["arn:aws:s3:::%s/staging/*"]},{"Effect":"Allow","Action":["s3:GetObject","s3:PutObject"],"Resource":["arn:aws:s3:::%s/attachments/*"]},{"Effect":"Allow","Action":["s3:ListBucket"],"Resource":["arn:aws:s3:::%s"]}]}' "$bucket" "$bucket" "$bucket" > /tmp/messenger-worker-policy.json ensure_user() { name=$1 secret=$2 if ! mc admin user info stand "$name" >/dev/null 2>&1; then mc admin user add stand "$name" "$secret" fi } # `mc admin policy create` does not overwrite and a policy attached to a user # cannot be removed. Detach, remove and recreate makes the script converge on # the current policy even when the stand volume predates lab5. install_policy() { name=$1 file=$2 user=$3 if mc admin policy info stand "$name" >/dev/null 2>&1; then mc admin policy detach stand "$name" --user "$user" >/dev/null 2>&1 || true mc admin policy remove stand "$name" >/dev/null 2>&1 || true fi mc admin policy create stand "$name" "$file" mc admin policy attach stand "$name" --user "$user" } ensure_user "$api_key" "$api_secret" ensure_user "$worker_key" "$worker_secret" install_policy messenger-api /tmp/messenger-api-policy.json "$api_key" install_policy messenger-worker /tmp/messenger-worker-policy.json "$worker_key" echo "bootstrap ok: bucket=$bucket policies=messenger-api,messenger-worker"