98 lines
5.1 KiB
Bash
Executable File
98 lines
5.1 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Проверка приватности S3 вне API (lab7 HTTPS + CA + OpenBao-managed API IAM):
|
|
# анонимный доступ -> 403, права отдельного API-пользователя из OpenBao,
|
|
# закрытая консоль :9001. Секреты не печатаются.
|
|
set -u
|
|
cd "$(dirname "$0")/../.."
|
|
env_file="${COMPOSE_ENV_FILE:-.env}"
|
|
if [ -f "./$env_file" ]; then
|
|
# shellcheck disable=SC1090
|
|
set -a; . "./$env_file"; set +a
|
|
fi
|
|
|
|
endpoint="${S3_PUBLIC_ENDPOINT:-https://localhost:${S3_PUBLIC_PORT:-9000}}"
|
|
bucket="${S3_BUCKET:-messenger-attachments}"
|
|
ca_file="${CA_FILE:-$(pwd)/secrets/pki/ca.crt}"
|
|
operator_dir="${LAB7_OPERATOR_DIR:-${XDG_STATE_HOME:-$HOME/.local/state}/mtusi-lab7-operator}"
|
|
bao_addr="${BAO_ADDR_HOST:-https://localhost:${OPENBAO_PORT:-8200}}"
|
|
compose=(docker compose --env-file "$env_file")
|
|
mc_image="minio/mc:RELEASE.2025-08-13T08-35-41Z@sha256:a7fe349ef4bd8521fb8497f55c6042871b2ae640607cf99d9bede5e9bdf11727"
|
|
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 -h /var/run/postgresql -U postgres -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 --cacert "$ca_file" -o /dev/null -w '%{http_code}' "$endpoint/$bucket/$key")
|
|
[ "$code" = 403 ] && report ok "anonymous HTTPS GET of a ready object -> 403" || report fail "anonymous HTTPS GET of a ready object -> $code (want 403)"
|
|
|
|
code=$(curl -s --cacert "$ca_file" -o /dev/null -w '%{http_code}' "$endpoint/$bucket/")
|
|
[ "$code" = 403 ] && report ok "anonymous HTTPS list of the bucket -> 403" || report fail "anonymous HTTPS list of the bucket -> $code (want 403)"
|
|
|
|
code=$(curl -s --cacert "$ca_file" -o /dev/null -w '%{http_code}' -X PUT --data-binary x "$endpoint/$bucket/attachments/anonymous-write")
|
|
[ "$code" = 403 ] && report ok "anonymous HTTPS PUT -> 403" || report fail "anonymous HTTPS PUT -> $code (want 403)"
|
|
|
|
# Fetch API S3 credentials cleanly via OpenBao operator mTLS auth (zero static app secrets on disk).
|
|
tmp_dir="$(mktemp -d "${TMPDIR:-/tmp}/lab7-s3sec.XXXXXX")"
|
|
trap 'rm -rf "$tmp_dir"' EXIT
|
|
op_token="$(curl -fsS --cacert "$ca_file" \
|
|
--cert "${operator_dir}/operator/operator.crt" \
|
|
--key "${operator_dir}/operator/operator.key" \
|
|
-X POST -H 'Content-Type: application/json' \
|
|
--data '{"name":"messenger-operator"}' \
|
|
"${bao_addr}/v1/auth/cert/login" | jq -r '.auth.client_token // empty')"
|
|
printf 'header = "X-Vault-Token: %s"\n' "$op_token" > "${tmp_dir}/auth.cfg"
|
|
chmod 0600 "${tmp_dir}/auth.cfg"
|
|
api_kv="$(curl -fsS --cacert "$ca_file" -K "${tmp_dir}/auth.cfg" \
|
|
"${bao_addr}/v1/secret/data/messenger/api" | jq -c '.data.data')"
|
|
curl -fsS --cacert "$ca_file" -K "${tmp_dir}/auth.cfg" -X POST \
|
|
"${bao_addr}/v1/auth/token/revoke-self" >/dev/null 2>&1 || true
|
|
|
|
api_key="$(printf '%s' "$api_kv" | jq -r '.s3_access_key_id')"
|
|
api_sec="$(printf '%s' "$api_kv" | jq -r '.s3_secret_access_key')"
|
|
|
|
s3_cid="$("${compose[@]}" ps -q s3 | head -n 1)"
|
|
app_data_net="$(docker inspect --format '{{range $k,$v := .NetworkSettings.Networks}}{{$k}} {{end}}' "$s3_cid" \
|
|
| tr ' ' '\n' | grep '_app_data$' | head -n 1)"
|
|
|
|
api_mc() {
|
|
printf '%s\n%s\n' "$api_key" "$api_sec" \
|
|
| docker run --rm -i \
|
|
--network "$app_data_net" \
|
|
--user 1000:1000 \
|
|
--read-only --cap-drop ALL --security-opt no-new-privileges:true \
|
|
--tmpfs /tmp:size=16m,noexec,nosuid,nodev \
|
|
-v "${ca_file}:/run/certs/ca.crt:ro" \
|
|
--entrypoint /bin/sh "$mc_image" -c '
|
|
set -eu
|
|
IFS= read -r k; IFS= read -r s
|
|
export HOME=/tmp MC_CONFIG_DIR=/tmp/.mc
|
|
mkdir -p "$MC_CONFIG_DIR/certs/CAs"
|
|
cp /run/certs/ca.crt "$MC_CONFIG_DIR/certs/CAs/ca.crt"
|
|
export MC_HOST_api="https://${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/* over HTTPS" || report fail "API credentials cannot read attachments/*"
|
|
api_mc cp /etc/hostname "api/$bucket/outside-prefix" && report fail "API credentials can write outside staging/" || report ok "API credentials cannot write outside staging/"
|
|
|
|
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 https://localhost:9001 --cacert "$ca_file"; then
|
|
report fail "console reachable on localhost:9001"
|
|
else
|
|
report ok "console not reachable on localhost:9001"
|
|
fi
|
|
|
|
exit "$fail"
|