forked from mtusi2026/mtusi2026lab7
126 lines
3.6 KiB
Bash
Executable File
126 lines
3.6 KiB
Bash
Executable File
#!/bin/sh
|
|
|
|
set -eu
|
|
|
|
: "${MINIO_ROOT_USER:?MINIO_ROOT_USER is required}"
|
|
: "${MINIO_ROOT_PASSWORD:?MINIO_ROOT_PASSWORD is required}"
|
|
: "${S3_BUCKET:?S3_BUCKET is required}"
|
|
: "${S3_API_ACCESS_KEY:?S3_API_ACCESS_KEY is required}"
|
|
: "${S3_API_SECRET_KEY:?S3_API_SECRET_KEY is required}"
|
|
: "${S3_WORKER_ACCESS_KEY:?S3_WORKER_ACCESS_KEY is required}"
|
|
: "${S3_WORKER_SECRET_KEY:?S3_WORKER_SECRET_KEY is required}"
|
|
: "${S3_RECONCILER_ACCESS_KEY:?S3_RECONCILER_ACCESS_KEY is required}"
|
|
: "${S3_RECONCILER_SECRET_KEY:?S3_RECONCILER_SECRET_KEY is required}"
|
|
|
|
export SSL_CERT_FILE=${SSL_CERT_FILE:-/run/security/internal-ca/ca.crt}
|
|
|
|
alias_name=bootstrap
|
|
policy_dir=/tmp/minio-policies
|
|
mkdir -p "$policy_dir"
|
|
|
|
mc alias set "$alias_name" https://s3:9000 "$MINIO_ROOT_USER" "$MINIO_ROOT_PASSWORD"
|
|
mc mb --ignore-existing "$alias_name/$S3_BUCKET"
|
|
mc anonymous set none "$alias_name/$S3_BUCKET"
|
|
|
|
cat >"$policy_dir/messenger-api.json" <<EOF
|
|
{
|
|
"Version": "2012-10-17",
|
|
"Statement": [
|
|
{
|
|
"Effect": "Allow",
|
|
"Action": ["s3:GetBucketLocation"],
|
|
"Resource": ["arn:aws:s3:::$S3_BUCKET"]
|
|
},
|
|
{
|
|
"Effect": "Allow",
|
|
"Action": ["s3:ListBucket"],
|
|
"Resource": ["arn:aws:s3:::$S3_BUCKET"],
|
|
"Condition": {"StringLike": {"s3:prefix": ["staging/*", "attachments/*"]}}
|
|
},
|
|
{
|
|
"Effect": "Allow",
|
|
"Action": ["s3:GetObject"],
|
|
"Resource": ["arn:aws:s3:::$S3_BUCKET/attachments/*"]
|
|
},
|
|
{
|
|
"Effect": "Allow",
|
|
"Action": ["s3:GetObject", "s3:PutObject", "s3:DeleteObject"],
|
|
"Resource": ["arn:aws:s3:::$S3_BUCKET/staging/*"]
|
|
}
|
|
]
|
|
}
|
|
EOF
|
|
|
|
cat >"$policy_dir/messenger-worker.json" <<EOF
|
|
{
|
|
"Version": "2012-10-17",
|
|
"Statement": [
|
|
{
|
|
"Effect": "Allow",
|
|
"Action": ["s3:GetBucketLocation"],
|
|
"Resource": ["arn:aws:s3:::$S3_BUCKET"]
|
|
},
|
|
{
|
|
"Effect": "Allow",
|
|
"Action": ["s3:ListBucket"],
|
|
"Resource": ["arn:aws:s3:::$S3_BUCKET"],
|
|
"Condition": {"StringLike": {"s3:prefix": ["staging/*", "attachments/*"]}}
|
|
},
|
|
{
|
|
"Effect": "Allow",
|
|
"Action": ["s3:GetObject", "s3:DeleteObject"],
|
|
"Resource": ["arn:aws:s3:::$S3_BUCKET/staging/*"]
|
|
},
|
|
{
|
|
"Effect": "Allow",
|
|
"Action": ["s3:GetObject", "s3:PutObject", "s3:DeleteObject"],
|
|
"Resource": ["arn:aws:s3:::$S3_BUCKET/attachments/*"]
|
|
}
|
|
]
|
|
}
|
|
EOF
|
|
|
|
cat >"$policy_dir/messenger-reconciler.json" <<EOF
|
|
{
|
|
"Version": "2012-10-17",
|
|
"Statement": [
|
|
{
|
|
"Effect": "Allow",
|
|
"Action": ["s3:GetBucketLocation"],
|
|
"Resource": ["arn:aws:s3:::$S3_BUCKET"]
|
|
},
|
|
{
|
|
"Effect": "Allow",
|
|
"Action": ["s3:ListBucket"],
|
|
"Resource": ["arn:aws:s3:::$S3_BUCKET"],
|
|
"Condition": {"StringLike": {"s3:prefix": ["staging/*", "attachments/*"]}}
|
|
},
|
|
{
|
|
"Effect": "Allow",
|
|
"Action": ["s3:GetObject", "s3:DeleteObject"],
|
|
"Resource": [
|
|
"arn:aws:s3:::$S3_BUCKET/staging/*",
|
|
"arn:aws:s3:::$S3_BUCKET/attachments/*"
|
|
]
|
|
}
|
|
]
|
|
}
|
|
EOF
|
|
|
|
create_user() {
|
|
access_key=$1
|
|
secret_key=$2
|
|
policy=$3
|
|
|
|
mc admin policy create "$alias_name" "$policy" "$policy_dir/$policy.json"
|
|
mc admin user add "$alias_name" "$access_key" "$secret_key"
|
|
mc admin policy attach "$alias_name" "$policy" --user "$access_key"
|
|
}
|
|
|
|
create_user "$S3_API_ACCESS_KEY" "$S3_API_SECRET_KEY" messenger-api
|
|
create_user "$S3_WORKER_ACCESS_KEY" "$S3_WORKER_SECRET_KEY" messenger-worker
|
|
create_user "$S3_RECONCILER_ACCESS_KEY" "$S3_RECONCILER_SECRET_KEY" messenger-reconciler
|
|
|
|
mc alias set application https://s3:9000 "$S3_API_ACCESS_KEY" "$S3_API_SECRET_KEY"
|
|
mc ls "application/$S3_BUCKET/staging/" >/dev/null
|