#!/bin/sh set -eu secret() { tr -d '\r\n' < "/run/secrets/$1"; } bucket="${S3_BUCKET:-messenger-attachments}" endpoint="${S3_ENDPOINT:-http://s3:9000}" policy="${S3_API_POLICY:-messenger-api}" reconciler="${S3_RECONCILER_POLICY:-messenger-reconciler}" RC_HOST_root="${endpoint%%://*}://$(secret s3_root_user):$(secret s3_root_password)@${endpoint#*://}" export RC_HOST_root grant() { sed "s/messenger-attachments/$bucket/g" "/s3/$2" > "/tmp/$2" rc admin policy create root "$1" "/tmp/$2" >/dev/null rc admin user add root "$(secret "$3")" "$(secret "$4")" >/dev/null rc admin policy attach root "$1" --user "$(secret "$3")" >/dev/null rc admin user info root "$(secret "$3")" --json | grep -q "\"$1\"" } rc ready root --timeout 10 >/dev/null rc bucket create --ignore-existing "root/$bucket" >/dev/null rc bucket anonymous set private "root/$bucket" >/dev/null grant "$policy" api-policy.json s3_access_key_id s3_secret_access_key grant "$reconciler" reconciler-policy.json s3_reconciler_access_key_id s3_reconciler_secret_access_key grant messenger-worker worker-policy.json s3_worker_access_key_id s3_worker_secret_access_key rc bucket lifecycle rule import "root/$bucket" /s3/lifecycle.json >/dev/null rc bucket anonymous get "root/$bucket" --json | grep -q '"permission": *"private"' echo "s3-init: bucket=$bucket anonymous=private policies=$policy,$reconciler lifecycle=abort-incomplete-uploads"