forked from mtusi2026/mtusi2026lab5
56 lines
2.9 KiB
Bash
56 lines
2.9 KiB
Bash
#!/bin/sh
|
|
# Idempotent bootstrap of the private bucket, the restricted application user
|
|
# and the separate sweeper user (safe to re-run).
|
|
set -eu
|
|
|
|
: "${S3_ENDPOINT:?}" "${S3_BUCKET:?}" "${MINIO_ROOT_USER:?}" "${MINIO_ROOT_PASSWORD:?}"
|
|
: "${S3_ACCESS_KEY_ID:?}" "${S3_SECRET_ACCESS_KEY:?}"
|
|
: "${SWEEPER_S3_ACCESS_KEY_ID:?}" "${SWEEPER_S3_SECRET_ACCESS_KEY:?}"
|
|
|
|
[ "$S3_ACCESS_KEY_ID" != "$MINIO_ROOT_USER" ] || { echo "app key must differ from root user" >&2; exit 1; }
|
|
[ "$SWEEPER_S3_ACCESS_KEY_ID" != "$MINIO_ROOT_USER" ] || { echo "sweeper key must differ from root user" >&2; exit 1; }
|
|
[ "$S3_ACCESS_KEY_ID" != "$SWEEPER_S3_ACCESS_KEY_ID" ] || { echo "app and sweeper keys must differ" >&2; exit 1; }
|
|
|
|
i=0
|
|
until mc alias set local "$S3_ENDPOINT" "$MINIO_ROOT_USER" "$MINIO_ROOT_PASSWORD" >/dev/null 2>&1; do
|
|
i=$((i+1)); [ "$i" -lt 60 ] || { echo "S3 not reachable" >&2; exit 1; }; sleep 1
|
|
done
|
|
|
|
mc mb --ignore-existing "local/$S3_BUCKET"
|
|
mc anonymous set none "local/$S3_BUCKET"
|
|
printf ok | mc pipe "local/$S3_BUCKET/attachments/.ready"
|
|
|
|
|
|
# --- app policy: exact keys only, NO ListBucket ---
|
|
cat > /tmp/policy-app.json <<POLICY
|
|
{"Version":"2012-10-17","Statement":[
|
|
{"Effect":"Allow","Action":["s3:PutObject","s3:GetObject","s3:DeleteObject"],"Resource":["arn:aws:s3:::$S3_BUCKET/attachments/*","arn:aws:s3:::$S3_BUCKET/staging/*"]}]}
|
|
POLICY
|
|
|
|
# --- sweeper policy: ListBucket for enumeration + Get/Delete, NO PutObject ---
|
|
cat > /tmp/policy-sweeper.json <<POLICY
|
|
{"Version":"2012-10-17","Statement":[
|
|
{"Effect":"Allow","Action":["s3:ListBucket","s3:GetBucketLocation"],"Resource":["arn:aws:s3:::$S3_BUCKET"]},
|
|
{"Effect":"Allow","Action":["s3:GetObject","s3:DeleteObject"],"Resource":["arn:aws:s3:::$S3_BUCKET/attachments/*","arn:aws:s3:::$S3_BUCKET/staging/*"]}]}
|
|
POLICY
|
|
|
|
# MinIO's `policy create` does not overwrite an existing policy, so remove first.
|
|
mc admin policy remove local messenger-app --force >/dev/null 2>&1 || true
|
|
mc admin policy remove local messenger-sweeper --force >/dev/null 2>&1 || true
|
|
mc admin policy create local messenger-app /tmp/policy-app.json
|
|
mc admin policy create local messenger-sweeper /tmp/policy-sweeper.json
|
|
|
|
# `user add` is idempotent (re-running updates the secret).
|
|
mc admin user add local "$S3_ACCESS_KEY_ID" "$S3_SECRET_ACCESS_KEY"
|
|
mc admin user add local "$SWEEPER_S3_ACCESS_KEY_ID" "$SWEEPER_S3_SECRET_ACCESS_KEY"
|
|
|
|
out=$(mc admin policy attach local messenger-app --user "$S3_ACCESS_KEY_ID" 2>&1) || {
|
|
echo "$out" | grep -qi "already" || { echo "$out" >&2; exit 1; }
|
|
}
|
|
out=$(mc admin policy attach local messenger-sweeper --user "$SWEEPER_S3_ACCESS_KEY_ID" 2>&1) || {
|
|
echo "$out" | grep -qi "already" || { echo "$out" >&2; exit 1; }
|
|
}
|
|
|
|
echo "PASS: bucket $S3_BUCKET private"
|
|
echo " app=$S3_ACCESS_KEY_ID (Put/Get/Delete on attachments/* and staging/*, no ListBucket)"
|
|
echo " sweeper=$SWEEPER_S3_ACCESS_KEY_ID (ListBucket + Get/Delete, no PutObject)" |