Files
mtusi2026lab4/infra/s3/bootstrap.sh
T

32 lines
1.3 KiB
Bash

#!/bin/sh
set -eu
# Do not enable shell tracing: arguments below contain credentials.
bucket="${S3_BUCKET:?S3_BUCKET is required}"
case "$bucket" in
''|*[!a-z0-9.-]*) echo 'Invalid bucket name' >&2; exit 1 ;;
esac
root_user="$(cat /run/secrets/s3_root_user)"
root_password="$(cat /run/secrets/s3_root_password)"
api_user="$(cat /run/secrets/s3_access_key_id)"
api_password="$(cat /run/secrets/s3_secret_access_key)"
# Bound startup wait to 60 attempts. Also retries if the server is not ready
# when Docker reports that its process has started.
attempt=0
until mc alias set local http://s3:9000 "$root_user" "$root_password" >/dev/null 2>&1; do
attempt=$((attempt + 1))
if [ "$attempt" -ge 60 ]; then
echo 'S3 bootstrap: server unavailable' >&2
exit 1
fi
sleep 2
done
mc mb --ignore-existing "local/$bucket" >/dev/null
mc anonymous set none "local/$bucket" >/dev/null
sed "s/BUCKET_NAME/$bucket/g" /bootstrap/api-policy.json > /tmp/api-policy.json
mc admin policy create local attachments-api /tmp/api-policy.json >/dev/null
# Reapplying with the same local credentials preserves the user identity.
mc admin user add local "$api_user" "$api_password" >/dev/null
mc admin policy attach local attachments-api --user "$api_user" >/dev/null
echo 'PASS: bucket configured private; API user policy applied.'