Files
mtusi2026lab4/infra/minio/check-private-bucket.sh
T
2026-11-10 19:24:33 +03:00

42 lines
1.5 KiB
Bash

#!/bin/sh
# In-network proof that the bucket is private and API credentials cannot see other buckets.
# Run via: docker compose --env-file .env --profile ops run --rm check-private-bucket
set -eu
bucket=${S3_BUCKET:?S3_BUCKET is required}
endpoint=${MINIO_ENDPOINT:-http://minio:9000}
read_secret() {
tr -d '\r\n' < "$1"
}
root_user=$(read_secret /run/secrets/minio_root_user)
root_password=$(read_secret /run/secrets/minio_root_password)
access_key=$(read_secret /run/secrets/s3_access_key_id)
secret_key=$(read_secret /run/secrets/s3_secret_access_key)
mc alias set local "$endpoint" "$root_user" "$root_password" >/dev/null
# MINIO_BROWSER=off: nothing should answer on the console port.
if curl -sf --connect-timeout 2 "http://minio:9001/" >/dev/null 2>&1; then
echo "MinIO console answered on :9001" >&2
exit 1
fi
probe_key=".bootstrap/private-check"
printf 'probe' | mc pipe "local/${bucket}/${probe_key}" >/dev/null
code=$(curl -s -o /dev/null -w '%{http_code}' "${endpoint}/${bucket}/${probe_key}")
mc rm --force "local/${bucket}/${probe_key}" >/dev/null
if [ "$code" != "403" ]; then
echo "unauthenticated GET returned ${code}, expected 403" >&2
exit 1
fi
mc alias set api "$endpoint" "$access_key" "$secret_key" >/dev/null
if mc ls "api/not-this-bucket" >/dev/null 2>&1; then
echo "API credentials can list a different bucket" >&2
exit 1
fi
echo "PASS: bucket ${bucket} is private; unauthenticated GET is 403; API user cannot list another bucket; console is closed"