forked from mtusi2026/mtusi2026lab4
55 lines
1.4 KiB
Bash
55 lines
1.4 KiB
Bash
#!/bin/sh
|
|
# Idempotent private bucket and least-privilege API user. Root credentials stay in this init container.
|
|
set -eu
|
|
|
|
: "${MINIO_ROOT_USER:?}"
|
|
: "${MINIO_ROOT_PASSWORD:?}"
|
|
: "${S3_ACCESS_KEY_ID:?}"
|
|
: "${S3_SECRET_ACCESS_KEY:?}"
|
|
: "${S3_BUCKET:?}"
|
|
|
|
export MC_CONFIG_DIR=/tmp/mc
|
|
export MC_HOST_local="http://${MINIO_ROOT_USER}:${MINIO_ROOT_PASSWORD}@minio:9000"
|
|
|
|
i=0
|
|
until mc mb --ignore-existing "local/${S3_BUCKET}" >/dev/null 2>&1; do
|
|
i=$((i + 1))
|
|
if [ "$i" -gt 40 ]; then
|
|
echo "bucket bootstrap failed" >&2
|
|
exit 1
|
|
fi
|
|
sleep 1
|
|
done
|
|
|
|
mc anonymous set none "local/${S3_BUCKET}" >/dev/null
|
|
|
|
cat > /tmp/messenger-api-policy.json <<EOF
|
|
{
|
|
"Version": "2012-10-17",
|
|
"Statement": [
|
|
{
|
|
"Effect": "Allow",
|
|
"Action": ["s3:GetObject", "s3:PutObject", "s3:DeleteObject"],
|
|
"Resource": ["arn:aws:s3:::${S3_BUCKET}/*"]
|
|
},
|
|
{
|
|
"Effect": "Allow",
|
|
"Action": ["s3:ListBucket", "s3:GetBucketLocation"],
|
|
"Resource": ["arn:aws:s3:::${S3_BUCKET}"]
|
|
}
|
|
]
|
|
}
|
|
EOF
|
|
|
|
if ! mc admin policy info local messenger-api >/dev/null 2>&1; then
|
|
mc admin policy create local messenger-api /tmp/messenger-api-policy.json >/dev/null
|
|
fi
|
|
|
|
if ! mc admin user info local "${S3_ACCESS_KEY_ID}" >/dev/null 2>&1; then
|
|
mc admin user add local "${S3_ACCESS_KEY_ID}" "${S3_SECRET_ACCESS_KEY}" >/dev/null
|
|
fi
|
|
|
|
mc admin policy attach local messenger-api --user "${S3_ACCESS_KEY_ID}" >/dev/null 2>&1 || true
|
|
|
|
echo "bucket bootstrap ok"
|