forked from mtusi2026/mtusi2026lab4
105 lines
4.3 KiB
Bash
Executable File
105 lines
4.3 KiB
Bash
Executable File
#!/bin/sh
|
|
# Prove the bucket is private outside the API and that the API credential is
|
|
# scoped. The probe object is created with the RustFS root credential, fetched
|
|
# anonymously (must be 403), accessed with the scoped credential inside the
|
|
# allowed prefix, and removed again.
|
|
set -eu
|
|
cd "$(dirname "$0")/.."
|
|
|
|
COMPOSE="docker compose --env-file .env"
|
|
TMP_DIR="$PWD/evidence/.tmp-private-bucket.$$"
|
|
mkdir -p "$TMP_DIR"
|
|
chmod 700 "$TMP_DIR"
|
|
TMP_ENV="$TMP_DIR/env"
|
|
umask 077
|
|
# AGENT: only the S3 settings go into the temporary env file. The scoped
|
|
# credential is intentionally used for most checks; the root pair is needed
|
|
# only to create and remove the probe object.
|
|
cat > "$TMP_ENV" <<EOF
|
|
S3_ENDPOINT=${S3_ENDPOINT:-http://rustfs:9000}
|
|
S3_BUCKET=${S3_BUCKET:-messenger-attachments}
|
|
S3_ROOT_ACCESS_KEY=$(sed -n 's/^S3_ROOT_ACCESS_KEY=//p' .env)
|
|
S3_ROOT_SECRET_KEY=$(sed -n 's/^S3_ROOT_SECRET_KEY=//p' .env)
|
|
S3_ACCESS_KEY_ID=$(sed -n 's/^S3_ACCESS_KEY_ID=//p' .env)
|
|
S3_SECRET_ACCESS_KEY=$(sed -n 's/^S3_SECRET_ACCESS_KEY=//p' .env)
|
|
EOF
|
|
|
|
cleanup() {
|
|
docker run --rm --network "$network" --env-file "$TMP_ENV" -e HOME=/tmp \
|
|
-e PROBE="$PROBE" \
|
|
--entrypoint /bin/sh rustfs/rc:v0.1.36 -c \
|
|
'rc alias set root "$S3_ENDPOINT" "$S3_ROOT_ACCESS_KEY" "$S3_ROOT_SECRET_KEY" >/dev/null 2>&1 && rc rm -f --quiet "root/$S3_BUCKET/$PROBE"' \
|
|
>/dev/null 2>&1 || true
|
|
rm -rf "$TMP_DIR"
|
|
}
|
|
trap cleanup EXIT INT TERM
|
|
|
|
network=$(docker inspect -f '{{range $k,$v := .NetworkSettings.Networks}}{{$k}}{{end}}' "$($COMPOSE ps -q haproxy)")
|
|
[ -n "$network" ] || {
|
|
echo "cannot determine the Compose network; is the stack running?" >&2
|
|
exit 2
|
|
}
|
|
|
|
# shellcheck disable=SC1090
|
|
. "$TMP_ENV"
|
|
PROBE="attachments/_privacy_probe_$$"
|
|
URL="http://rustfs:9000/$S3_BUCKET/$PROBE"
|
|
|
|
echo "1/4 create a probe object with the root credential"
|
|
docker run --rm --network "$network" --env-file "$TMP_ENV" -e HOME=/tmp \
|
|
-v "$PWD/tests/fixtures:/fixtures:ro" -e PROBE="$PROBE" \
|
|
--entrypoint /bin/sh rustfs/rc:v0.1.36 -c \
|
|
'rc alias set root "$S3_ENDPOINT" "$S3_ROOT_ACCESS_KEY" "$S3_ROOT_SECRET_KEY" >/dev/null &&
|
|
rc cp --quiet /fixtures/sample.png "root/$S3_BUCKET/$PROBE"'
|
|
|
|
echo "2/4 anonymous object GET must be denied"
|
|
anon_code=$(docker run --rm --network "$network" -e URL="$URL" python:3.12-alpine \
|
|
python3 -c '
|
|
import os, sys, urllib.error, urllib.request
|
|
try:
|
|
with urllib.request.urlopen(os.environ["URL"], timeout=5) as response:
|
|
print(response.status)
|
|
except urllib.error.HTTPError as error:
|
|
print(error.code)
|
|
except Exception as error:
|
|
print(type(error).__name__, file=sys.stderr)
|
|
print(0)
|
|
')
|
|
[ "$anon_code" = "403" ] || {
|
|
echo "anonymous object GET returned $anon_code, expected 403" >&2
|
|
exit 1
|
|
}
|
|
echo " anonymous GET -> 403"
|
|
|
|
echo "3/4 scoped credential works inside attachments/ and is denied elsewhere"
|
|
docker run --rm --network "$network" --env-file "$TMP_ENV" -e HOME=/tmp \
|
|
-v "$PWD/tests/fixtures:/fixtures:ro" -e PROBE="$PROBE" \
|
|
--entrypoint /bin/sh rustfs/rc:v0.1.36 -c \
|
|
'rc alias set app "$S3_ENDPOINT" "$S3_ACCESS_KEY_ID" "$S3_SECRET_ACCESS_KEY" >/dev/null &&
|
|
rc cp --quiet /fixtures/sample.png "app/$S3_BUCKET/$PROBE" &&
|
|
rc head --quiet "app/$S3_BUCKET/$PROBE" >/dev/null'
|
|
echo " scoped PUT/HEAD inside attachments/ -> allowed"
|
|
|
|
if docker run --rm --network "$network" --env-file "$TMP_ENV" -e HOME=/tmp \
|
|
-v "$PWD/tests/fixtures:/fixtures:ro" -e PROBE="$PROBE" \
|
|
--entrypoint /bin/sh rustfs/rc:v0.1.36 -c \
|
|
'rc alias set app "$S3_ENDPOINT" "$S3_ACCESS_KEY_ID" "$S3_SECRET_ACCESS_KEY" >/dev/null &&
|
|
rc cp --quiet /fixtures/sample.png "app/$S3_BUCKET/outside-$PROBE"' >/dev/null 2>&1; then
|
|
echo "scoped credential wrote outside attachments/, expected denial" >&2
|
|
exit 1
|
|
fi
|
|
echo " scoped PUT outside attachments/ -> denied"
|
|
|
|
if docker run --rm --network "$network" --env-file "$TMP_ENV" -e HOME=/tmp \
|
|
--entrypoint /bin/sh rustfs/rc:v0.1.36 -c \
|
|
'rc alias set app "$S3_ENDPOINT" "$S3_ACCESS_KEY_ID" "$S3_SECRET_ACCESS_KEY" >/dev/null &&
|
|
rc bucket create -p "app/forbidden-'"$$"'"' >/dev/null 2>&1; then
|
|
echo "scoped credential created a bucket, expected denial" >&2
|
|
exit 1
|
|
fi
|
|
echo " scoped CreateBucket -> denied"
|
|
|
|
echo "4/4 cleanup the probe object on exit"
|
|
chmod 700 "$TMP_DIR" 2>/dev/null || true
|
|
echo "PASS: bucket is private and the API credential is limited to attachments/*"
|