Files
mtusi2026lab4/infra/bootstrap-s3.sh
T
2026-11-11 18:55:53 +03:00

36 lines
1.4 KiB
Bash

#!/bin/sh
set -eu
read_secret() {
tr -d '\r\n' < "/run/secrets/$1"
}
ROOT_ACCESS=$(read_secret s3_root_access)
ROOT_SECRET=$(read_secret s3_root_secret)
API_ACCESS=$(read_secret s3_access_key_id)
API_SECRET=$(read_secret s3_secret_access_key)
# Bound bootstrap wait: fail with diagnostic rather than stall forever.
count=0
until output=$(timeout 4 mc alias set local http://s3:9000 "$ROOT_ACCESS" "$ROOT_SECRET" 2>&1) && output=$(timeout 4 mc admin info local 2>&1); do
count=$((count + 1))
if [ "$count" -ge 12 ]; then
echo "ERROR: MinIO did not become reachable after 12 attempts; inspect docker compose logs s3" >&2
if [ -n "$output" ]; then printf '%s\n' "$output" >&2; fi
exit 1
fi
sleep 2
done
mc mb --ignore-existing local/messenger-attachments
mc anonymous set none local/messenger-attachments
# Repeatable bootstrap: policy may exist already after restarts.
mc admin policy create local messenger-app /etc/bootstrap/s3-policy.json
mc admin user add local "$API_ACCESS" "$API_SECRET"
if ! mc admin policy attach local messenger-app --user "$API_ACCESS"; then
mc admin policy entities local messenger-app --user "$API_ACCESS" --json | grep -Fq "\"$API_ACCESS\""
fi
mc anonymous get local/messenger-attachments
mc alias set api http://s3:9000 "$API_ACCESS" "$API_SECRET" >/dev/null
mc ls api/messenger-attachments >/dev/null
printf 'S3 bootstrap completed (private bucket, limited API identity).\n'