43 lines
1.8 KiB
Bash
43 lines
1.8 KiB
Bash
#!/bin/sh
|
|
set -eu
|
|
mkdir -p "$MC_CONFIG_DIR/certs/CAs"
|
|
cp /run/lab-ca/ca.crt "$MC_CONFIG_DIR/certs/CAs/lab.crt"
|
|
login() {
|
|
mc alias set --api S3v4 local https://s3:9000 \
|
|
"$(cat /run/secrets/s3_root_user)" "$(cat /run/secrets/s3_root_password)" >/dev/null 2>&1
|
|
}
|
|
attempt=0
|
|
until login && mc ready local >/dev/null 2>&1; do
|
|
attempt=$((attempt + 1))
|
|
if [ "$attempt" -ge 20 ]; then echo 'S3 TLS readiness failed' >&2; exit 1; fi
|
|
sleep 1
|
|
done
|
|
attach_policy() {
|
|
if ! mc admin policy attach local "$1" --user "$2" >/dev/null 2>&1; then
|
|
mc admin policy entities local "$1" --user "$2" 2>/dev/null | grep -Fq "$2"
|
|
fi
|
|
}
|
|
if [ "${1:-}" = rotate-worker ]; then
|
|
# The operator supplies the new key over stdin, never as a Compose argument.
|
|
IFS= read -r user
|
|
IFS= read -r password
|
|
mc admin user add local "$user" "$password" >/dev/null 2>&1 || { echo 'Worker S3 user update failed' >&2; exit 1; }
|
|
attach_policy messenger-worker "$user" || { echo 'Worker S3 policy verification failed' >&2; exit 1; }
|
|
echo 'Worker S3 key updated'
|
|
exit 0
|
|
fi
|
|
mc mb --ignore-existing local/messenger-attachments >/dev/null
|
|
mc anonymous set none local/messenger-attachments >/dev/null
|
|
for role in api dispatcher worker; do
|
|
case "$role" in api) policy=policy;; *) policy="$role-policy";; esac
|
|
case "$role" in api) file=s3-policy.json;; *) file="s3-$policy.json";; esac
|
|
user="$(cat "/run/s3-setup/${role}_user")"
|
|
# Existing users may have been rotated. Provisioning must not reset them.
|
|
if ! mc admin user info local "$user" >/dev/null 2>&1; then
|
|
mc admin user add local "$user" "$(cat "/run/s3-setup/${role}_key")" >/dev/null 2>&1
|
|
fi
|
|
mc admin policy create local "messenger-$role" "/bootstrap/$file" >/dev/null
|
|
attach_policy "messenger-$role" "$user"
|
|
done
|
|
echo 'Private bucket and restricted service accounts ready'
|