#!/bin/sh # Проверка приватности S3 вне API: анонимный доступ, права отдельных credentials # API, закрытая консоль. Секреты не печатаются. set -u cd "$(dirname "$0")/../.." env_file="${COMPOSE_ENV_FILE:-.env}" # shellcheck disable=SC1090 set -a; . "./$env_file"; set +a endpoint="${S3_PUBLIC_ENDPOINT:-http://localhost:9000}" bucket="${S3_BUCKET:-messenger-attachments}" compose="docker compose --env-file $env_file" fail=0 report() { # status name if [ "$1" = ok ]; then echo "PASS $2"; else echo "FAIL $2"; fail=1; fi } key=$($compose exec -T postgres psql -U "${POSTGRES_USER:-messenger}" -d "${POSTGRES_DB:-messenger}" -At \ -c "SELECT object_key FROM attachments WHERE status = 'ready' ORDER BY created_at DESC LIMIT 1") if [ -z "$key" ]; then echo "FAIL no ready attachment to check; upload one first (make test)" exit 1 fi code=$(curl -s -o /dev/null -w '%{http_code}' "$endpoint/$bucket/$key") [ "$code" = 403 ] && report ok "anonymous GET of a ready object -> 403" || report fail "anonymous GET of a ready object -> $code (want 403)" code=$(curl -s -o /dev/null -w '%{http_code}' "$endpoint/$bucket/") [ "$code" = 403 ] && report ok "anonymous list of the bucket -> 403" || report fail "anonymous list of the bucket -> $code (want 403)" code=$(curl -s -o /dev/null -w '%{http_code}' -X PUT --data-binary x "$endpoint/$bucket/attachments/anonymous-write") [ "$code" = 403 ] && report ok "anonymous PUT -> 403" || report fail "anonymous PUT -> $code (want 403)" # The API credentials only, inside a one-off container (secrets come from files). api_mc() { $compose run --rm --no-deps -T --entrypoint /bin/sh s3-init -c ' IFS= read -r k < /run/secrets/s3_api_access_key_id IFS= read -r s < /run/secrets/s3_api_secret_access_key export MC_CONFIG_DIR=/tmp/.mc MC_HOST_api="http://$k:$s@s3:9000" exec mc "$@"' sh "$@" >/dev/null 2>&1 } api_mc mb api/forbidden-bucket && report fail "API credentials can create buckets" || report ok "API credentials cannot create buckets" api_mc admin info api && report fail "API credentials can use admin API" || report ok "API credentials cannot use admin API" api_mc stat "api/$bucket/$key" && report ok "API credentials can read attachments/*" || report fail "API credentials cannot read attachments/*" api_mc stat "api/$bucket/outside-prefix" ; rc=$? api_mc cp /etc/hostname "api/$bucket/outside-prefix" && report fail "API credentials can write outside attachments/" || report ok "API credentials cannot write outside attachments/" : "$rc" port=$($compose port s3 9001 2>/dev/null || true) [ -z "$port" ] && report ok "console port 9001 is not published" || report fail "console port 9001 is published ($port)" if curl -s -o /dev/null --max-time 3 http://localhost:9001; then report fail "console reachable on localhost:9001"; else report ok "console not reachable on localhost:9001"; fi exit "$fail"