Files
mtusi2026lab6/scripts/lab4/check_s3_security.sh
T
ilya.tyunin 6889e77ab4 lab6: базовый коммит из lab5 (0790599)
Перенесено дерево lab5 целиком: NestJS-приложение (API + worker,
RabbitMQ, outbox, S3), Dockerfile, compose.yaml, infra/{haproxy,minio,
rabbitmq}, стендовые скрипты, scripts/lab4, scripts/lab5,
evidence/lab4, evidence/lab5 и contracts/extensions.openapi.json.

Задание lab6 оставлено своим: README, REPORT, COURSE, lab.json,
contracts/{openapi,infrastructure,jobs.schema,attachment-states,
examples,observability}, tests, compose.example.yaml, infra/otel,
.github/pull_request_template.md.

Адаптации при переносе:
- docker-проект mtusi-lab5 -> mtusi-lab6 и тег образа
  mtusi-messenger:lab6 (compose.yaml, scripts/stop-one.sh, .env.example,
  infra/README.md), чтобы тома и контейнеры не пересекались с lab5;
- Makefile: цели lab5 сохранены целиком (skeleton lab6 не отличается
  от skeleton lab5);
- .env.example: переменные приложения и стенда lab5 + блок lab6
  (OTEL_*) из skeleton.
2026-11-09 16:44:21 +03:00

55 lines
2.9 KiB
Bash
Executable File

#!/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"