56 lines
2.3 KiB
Bash
Executable File
56 lines
2.3 KiB
Bash
Executable File
#!/bin/sh
|
|
# Grade 4: API uses limited S3 user via *_FILE secrets, not MinIO root.
|
|
set -eu
|
|
. "$(dirname "$0")/lib.sh"
|
|
|
|
COMPOSE="docker compose --env-file $ROOT/.env -f $ROOT/compose.yaml"
|
|
|
|
# API process env must expose limited-key secret paths, never root password vars.
|
|
env_dump=$($COMPOSE exec -T api sh -c 'env' | sort)
|
|
|
|
printf '%s\n' "$env_dump" | grep -q '^S3_ACCESS_KEY_ID_FILE=' \
|
|
|| die "API missing S3_ACCESS_KEY_ID_FILE"
|
|
printf '%s\n' "$env_dump" | grep -q '^S3_SECRET_ACCESS_KEY_FILE=' \
|
|
|| die "API missing S3_SECRET_ACCESS_KEY_FILE"
|
|
|
|
# Root MinIO credentials must not be injected into the API container.
|
|
if printf '%s\n' "$env_dump" | grep -E '^(MINIO_ROOT_USER|MINIO_ROOT_PASSWORD)=' >/dev/null; then
|
|
die "API container has MinIO root credentials in env"
|
|
fi
|
|
|
|
# Secret file contents must differ from root user (compare access key id only).
|
|
api_key=$($COMPOSE exec -T api sh -c 'cat "$S3_ACCESS_KEY_ID_FILE"' | tr -d '\r\n')
|
|
root_user=$(grep -E '^MINIO_ROOT_USER=' "$ROOT/.env" | head -n1 | cut -d= -f2- | tr -d '\r\n"')
|
|
[ -n "$api_key" ] || die "empty limited access key"
|
|
[ -n "$root_user" ] || die "MINIO_ROOT_USER missing in .env"
|
|
[ "$api_key" != "$root_user" ] || die "API S3 access key equals MinIO root user"
|
|
|
|
# Limited user cannot delete the bucket (admin action) — policy is object-scoped.
|
|
# Probe via mc with the limited credentials from secrets (same values as API).
|
|
lim_key=$api_key
|
|
lim_secret=$($COMPOSE exec -T api sh -c 'cat "$S3_SECRET_ACCESS_KEY_FILE"' | tr -d '\r\n')
|
|
bucket=$($COMPOSE exec -T api sh -c 'printf %s "$S3_BUCKET"')
|
|
|
|
out=$($COMPOSE run --rm --no-deps \
|
|
-e LIM_KEY="$lim_key" -e LIM_SECRET="$lim_secret" -e S3_BUCKET="$bucket" \
|
|
--entrypoint /bin/sh minio-init -c '
|
|
set +e
|
|
mc alias set lim "$S3_ENDPOINT" "$LIM_KEY" "$LIM_SECRET" >/dev/null
|
|
mc rb --force "lim/$S3_BUCKET" 2>/tmp/rb.err
|
|
rc=$?
|
|
cat /tmp/rb.err
|
|
exit $rc
|
|
' 2>&1) || true
|
|
|
|
printf '%s\n' "$out" | grep -qiE 'AccessDenied|denied|not allowed|InvalidAccessKeyId|error' \
|
|
|| die "expected AccessDenied on bucket delete by limited user; out=$out"
|
|
|
|
# Positive: limited user can still PutObject (smoke via API upload).
|
|
png="$FIXTURES/tiny.png"
|
|
register_user alice
|
|
create_chat 'creds' "[\"$USER_ID\"]"
|
|
upload_file "$CHAT_ID" "$png" 'tiny.png' 'image/png'
|
|
expect_code 201
|
|
|
|
printf 'PASS test_attachment_limited_creds\n'
|