#!/bin/sh # Idempotent RustFS bootstrap: create the private bucket, a scoped IAM user and # a policy that only allows object operations under the attachments/ prefix. set -eu : "${S3_ENDPOINT:?S3_ENDPOINT is required}" : "${S3_BUCKET:?S3_BUCKET is required}" : "${S3_ROOT_ACCESS_KEY:?S3_ROOT_ACCESS_KEY is required}" : "${S3_ROOT_SECRET_KEY:?S3_ROOT_SECRET_KEY is required}" : "${S3_ACCESS_KEY_ID:?S3_ACCESS_KEY_ID is required}" : "${S3_SECRET_ACCESS_KEY:?S3_SECRET_ACCESS_KEY is required}" : "${S3_APP_USER:?S3_APP_USER is required}" : "${S3_APP_POLICY:?S3_APP_POLICY is required}" : "${S3_POLICY_TEMPLATE:?S3_POLICY_TEMPLATE is required}" # AGENT: rc stores its alias file in $HOME. The image runs as an unprivileged # user, so point HOME at a writable temporary directory. export HOME="${HOME:-/tmp}" ALIAS=local rc alias set "$ALIAS" "$S3_ENDPOINT" "$S3_ROOT_ACCESS_KEY" "$S3_ROOT_SECRET_KEY" >/dev/null # Compose waits for the RustFS healthcheck, but the S3 API can become ready a # moment later. A bounded wait keeps this service deterministic without hiding # a genuinely broken deployment. attempt=0 until rc ready "$ALIAS" >/dev/null 2>&1; do attempt=$((attempt + 1)) if [ "$attempt" -ge 60 ]; then echo "RustFS did not become ready within 60 seconds" >&2 exit 1 fi sleep 1 done rc bucket create -p "$ALIAS/$S3_BUCKET" >/dev/null # Explicitly converge anonymous access to private. This is both a policy # assertion and a guard against a bucket created public by a previous run. rc bucket anonymous set private "$ALIAS/$S3_BUCKET" >/dev/null # The policy is rendered from a template because the bucket name is part of # every resource ARN and is configurable in .env. sed "s|__BUCKET__|$S3_BUCKET|g" "$S3_POLICY_TEMPLATE" > /tmp/api-policy.json # All three admin calls are replaces/updates, not creates that fail on a second # run: policy create replaces the named document, user add updates the secret, # and policy attach is idempotent. rc admin policy create "$ALIAS" "$S3_APP_POLICY" /tmp/api-policy.json >/dev/null rc admin user add "$ALIAS" "$S3_APP_USER" "$S3_SECRET_ACCESS_KEY" >/dev/null rc admin policy attach "$ALIAS" "$S3_APP_POLICY" --user "$S3_APP_USER" >/dev/null echo "S3 bootstrap complete: bucket=$S3_BUCKET user=$S3_APP_USER policy=$S3_APP_POLICY"