forked from mtusi2026/mtusi2026lab6
66 lines
2.0 KiB
Bash
Executable File
66 lines
2.0 KiB
Bash
Executable File
#!/bin/sh
|
|
set -eu
|
|
|
|
for name in rustfs_root_access_key rustfs_root_secret_key s3_access_key_id s3_secret_access_key; do
|
|
if [ ! -s "/run/secrets/$name" ]; then
|
|
echo "missing S3 secret: $name" >&2
|
|
exit 1
|
|
fi
|
|
done
|
|
|
|
root_key=$(cat /run/secrets/rustfs_root_access_key)
|
|
root_secret=$(cat /run/secrets/rustfs_root_secret_key)
|
|
app_key=$(cat /run/secrets/s3_access_key_id)
|
|
app_secret=$(cat /run/secrets/s3_secret_access_key)
|
|
|
|
if [ "$root_key" = "$app_key" ]; then
|
|
echo 'API and RustFS root access keys must differ' >&2
|
|
exit 1
|
|
fi
|
|
|
|
# These values become path and JSON fragments below.
|
|
case "$S3_BUCKET" in
|
|
messenger-attachments) ;;
|
|
*) echo 'unsupported S3_BUCKET in bootstrap' >&2; exit 1 ;;
|
|
esac
|
|
|
|
rc=/usr/bin/rc
|
|
policy=messenger-attachments-api-v1
|
|
|
|
"$rc" alias set root http://s3:9000 "$root_key" "$root_secret" \
|
|
--region "$S3_REGION" --bucket-lookup path >/dev/null
|
|
"$rc" bucket create "root/$S3_BUCKET" --ignore-existing
|
|
# A new bucket is private by default; this also removes a stale public policy.
|
|
"$rc" bucket anonymous set private "root/$S3_BUCKET"
|
|
|
|
cat > /tmp/api-policy.json <<EOF
|
|
{
|
|
"Version": "2012-10-17",
|
|
"Statement": [
|
|
{
|
|
"Effect": "Allow",
|
|
"Action": ["s3:ListBucket", "s3:GetBucketLocation"],
|
|
"Resource": "arn:aws:s3:::$S3_BUCKET"
|
|
},
|
|
{
|
|
"Effect": "Allow",
|
|
"Action": ["s3:PutObject", "s3:GetObject", "s3:DeleteObject"],
|
|
"Resource": "arn:aws:s3:::$S3_BUCKET/*"
|
|
}
|
|
]
|
|
}
|
|
EOF
|
|
|
|
if ! "$rc" admin policy info root/ "$policy" >/dev/null 2>&1; then
|
|
"$rc" admin policy create root/ "$policy" /tmp/api-policy.json
|
|
fi
|
|
if ! "$rc" admin user info root/ "$app_key" >/dev/null 2>&1; then
|
|
"$rc" admin user add root/ "$app_key" "$app_secret" >/dev/null
|
|
fi
|
|
"$rc" admin policy attach root/ "$policy" --user "$app_key" >/dev/null
|
|
|
|
"$rc" alias set app http://s3:9000 "$app_key" "$app_secret" \
|
|
--region "$S3_REGION" --bucket-lookup path >/dev/null
|
|
"$rc" bucket list "app/$S3_BUCKET" >/dev/null
|
|
echo "S3 bucket and API identity ready: $S3_BUCKET"
|