Files
mtusi2026lab6/infra/minio/bootstrap.sh
ilya.tyunin 6889e77ab4 lab6: базовый коммит из lab5 (0790599)
Перенесено дерево lab5 целиком: NestJS-приложение (API + worker,
RabbitMQ, outbox, S3), Dockerfile, compose.yaml, infra/{haproxy,minio,
rabbitmq}, стендовые скрипты, scripts/lab4, scripts/lab5,
evidence/lab4, evidence/lab5 и contracts/extensions.openapi.json.

Задание lab6 оставлено своим: README, REPORT, COURSE, lab.json,
contracts/{openapi,infrastructure,jobs.schema,attachment-states,
examples,observability}, tests, compose.example.yaml, infra/otel,
.github/pull_request_template.md.

Адаптации при переносе:
- docker-проект mtusi-lab5 -> mtusi-lab6 и тег образа
  mtusi-messenger:lab6 (compose.yaml, scripts/stop-one.sh, .env.example,
  infra/README.md), чтобы тома и контейнеры не пересекались с lab5;
- Makefile: цели lab5 сохранены целиком (skeleton lab6 не отличается
  от skeleton lab5);
- .env.example: переменные приложения и стенда lab5 + блок lab6
  (OTEL_*) из skeleton.
2026-11-09 16:44:21 +03:00

59 lines
3.5 KiB
Bash
Executable File

#!/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"],"Resource":["arn:aws:s3:::%s/attachments/*"]},{"Effect":"Allow","Action":["s3:DeleteObject"],"Resource":["arn:aws:s3:::%s/attachments/*/original"]},{"Effect":"Allow","Action":["s3:ListBucket"],"Resource":["arn:aws:s3:::%s"],"Condition":{"StringLike":{"s3:prefix":["staging/*","attachments/*"]}}}]}' "$bucket" "$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:DeleteObject"],"Resource":["arn:aws:s3:::%s/attachments/*/v*/*"]},{"Effect":"Allow","Action":["s3:ListBucket"],"Resource":["arn:aws:s3:::%s"],"Condition":{"StringLike":{"s3:prefix":["staging/*","attachments/*"]}}}]}' "$bucket" "$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"