Files
mtusi2026lab4/infra/s3/verify.sh
T

36 lines
1.6 KiB
Bash

#!/bin/sh
set -eu
# Synthetic fixture only. Credentials are not printed.
bucket="${S3_BUCKET:?}"
key=attachments/infrastructure-check.txt
mc alias set api http://s3:9000 "$(cat /run/secrets/s3_access_key_id)" "$(cat /run/secrets/s3_secret_access_key)" >/dev/null 2>&1
printf 'lab4-private-storage-check\n' > /tmp/expected
if [ "${1:-check}" = seed ]; then
mc cp /tmp/expected "api/$bucket/$key" >/dev/null
echo 'PASS: API credentials can upload fixture.'
exit 0
fi
mc cat "api/$bucket/$key" > /tmp/actual
cmp /tmp/expected /tmp/actual
echo 'PASS: API credentials read original bytes after bootstrap/restart.'
# BusyBox wget makes a real unsigned HTTP request, bypassing the application.
if wget -S -O /tmp/unsigned "http://s3:9000/$bucket/$key" 2>/tmp/http-status; then
echo 'FAIL: anonymous download succeeded' >&2; exit 1
fi
grep -q '403 Forbidden' /tmp/http-status
echo 'PASS: unsigned GET of existing object returns HTTP 403.'
if mc --json cp /tmp/expected "api/$bucket/outside-api-prefix.txt" >/tmp/denied 2>&1; then
echo 'FAIL: API wrote outside permitted prefix' >&2; exit 1
fi
grep -qi 'AccessDenied\|Access Denied\|Insufficient permissions' /tmp/denied
echo 'PASS: API cannot write outside attachments prefix.'
# mc admin info can emit a JSON error while returning exit code 0.
mc --json admin info api >/tmp/denied 2>&1 || true
grep -q '"status":"error"' /tmp/denied
grep -qi 'AccessDenied\|Access Denied' /tmp/denied
echo 'PASS: API cannot access administrative server information.'
mc rm "api/$bucket/$key" >/dev/null
echo 'PASS: API can delete its test object; fixture removed.'