Files
mtusi2026lab5/infra/bootstrap-s3.sh
T

32 lines
1.6 KiB
Bash

#!/bin/sh
set -eu
# Never print mc arguments/errors: they can include access key identifiers.
exec 3>&1
exec > /tmp/bootstrap.log 2>&1
trap 'printf "%s\n" "S3 bootstrap failed; credentials suppressed" >&3' EXIT
bucket="$S3_BUCKET"
case "$bucket" in ''|*[!a-z0-9.-]*) exit 2;; esac
mc --config-dir /tmp/mc alias set local http://s3:9000 "$(cat /run/secrets/s3_root_user)" "$(cat /run/secrets/s3_root_password)"
mc --config-dir /tmp/mc mb --ignore-existing "local/$bucket"
mc --config-dir /tmp/mc anonymous set none "local/$bucket"
for role in api worker reconciler test; do
target_bucket="$bucket"
policy_source=/policies/api-policy.json
if [ "$role" = reconciler ]; then policy_source=/policies/reconciler-policy.json; fi
if [ "$role" = worker ]; then policy_source=/policies/worker-policy.json; fi
if [ "$role" = test ]; then
if [ "${CREATE_TEST_BUCKET:-0}" != 1 ]; then continue; fi
policy_source=/policies/test-policy.json
target_bucket="$bucket-tests"
mc --config-dir /tmp/mc mb --ignore-existing "local/$target_bucket"
mc --config-dir /tmp/mc anonymous set none "local/$target_bucket"
fi
sed "s/BUCKET/$target_bucket/g" "$policy_source" > "/tmp/$role-policy.json"
mc --config-dir /tmp/mc admin policy create local "messenger-$role" "/tmp/$role-policy.json"
key="$(cat /run/secrets/s3_${role}_access_key_id)"
mc --config-dir /tmp/mc admin user add local "$key" "$(cat /run/secrets/s3_${role}_secret_access_key)"
mc --config-dir /tmp/mc admin policy attach local "messenger-$role" --user "$key"
done
trap - EXIT
printf '%s\n' 'PASS: private bucket and scoped identities ready' >&3