forked from mtusi2026/mtusi2026lab7
all for 3
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
FROM golang:1.24.2-bookworm AS builder
|
||||
|
||||
ENV CGO_ENABLED=0
|
||||
|
||||
WORKDIR /src
|
||||
|
||||
RUN git clone --depth 1 \
|
||||
--branch RELEASE.2025-04-22T22-12-26Z \
|
||||
https://github.com/minio/minio.git /src/minio
|
||||
|
||||
WORKDIR /src/minio
|
||||
|
||||
RUN go build -trimpath -o /out/minio .
|
||||
|
||||
WORKDIR /src
|
||||
|
||||
RUN git clone --depth 1 \
|
||||
--branch RELEASE.2025-04-16T18-13-26Z \
|
||||
https://github.com/minio/mc.git /src/mc
|
||||
|
||||
WORKDIR /src/mc
|
||||
|
||||
RUN go build -trimpath -o /out/mc .
|
||||
|
||||
|
||||
FROM debian:bookworm-slim
|
||||
|
||||
RUN apt-get update \
|
||||
&& apt-get install -y --no-install-recommends \
|
||||
ca-certificates curl \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
COPY --from=builder /out/minio /usr/local/bin/minio
|
||||
COPY --from=builder /out/mc /usr/local/bin/mc
|
||||
|
||||
RUN mkdir -p /data
|
||||
|
||||
EXPOSE 9000 9001
|
||||
|
||||
ENTRYPOINT ["minio"]
|
||||
CMD ["server", "/data", "--console-address", ":9001"]
|
||||
@@ -0,0 +1,60 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
: "${MINIO_ROOT_USER:?}"
|
||||
: "${MINIO_ROOT_PASSWORD:?}"
|
||||
: "${S3_BUCKET:?}"
|
||||
: "${S3_ACCESS_KEY:?}"
|
||||
: "${S3_SECRET_KEY:?}"
|
||||
|
||||
if [ "$S3_ACCESS_KEY" = "$MINIO_ROOT_USER" ]; then
|
||||
echo "API must use a separate MinIO user" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ "$S3_SECRET_KEY" = "$MINIO_ROOT_PASSWORD" ]; then
|
||||
echo "API and administrator passwords must differ" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
case "$S3_BUCKET" in
|
||||
""|*[!a-z0-9.-]*)
|
||||
echo "Invalid bucket name" >&2
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
mc alias set storage https://minio:9000 \
|
||||
"$MINIO_ROOT_USER" "$MINIO_ROOT_PASSWORD" >/dev/null
|
||||
|
||||
mc mb --ignore-existing "storage/$S3_BUCKET"
|
||||
mc anonymous set none "storage/$S3_BUCKET"
|
||||
|
||||
for role in api worker; do
|
||||
user="messenger_$role"
|
||||
if [ "$role" = api ]; then
|
||||
password="$S3_SECRET_KEY"
|
||||
write_prefix=staging
|
||||
else
|
||||
password="$WORKER_S3_PASSWORD"
|
||||
write_prefix=originals
|
||||
fi
|
||||
read_actions='"s3:GetObject"'
|
||||
if [ "$role" = api ]; then read_actions='"s3:GetObject","s3:DeleteObject"'; fi
|
||||
extra=""
|
||||
if [ "$role" = worker ]; then
|
||||
extra=',"arn:aws:s3:::'"$S3_BUCKET"'/variants/*"'
|
||||
fi
|
||||
cat > /tmp/policy.json <<EOF
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":["s3:GetBucketLocation","s3:ListBucket"],"Resource":["arn:aws:s3:::$S3_BUCKET"]},
|
||||
{"Effect":"Allow","Action":[$read_actions],"Resource":["arn:aws:s3:::$S3_BUCKET/originals/*","arn:aws:s3:::$S3_BUCKET/variants/*","arn:aws:s3:::$S3_BUCKET/staging/*"]},
|
||||
{"Effect":"Allow","Action":["s3:PutObject","s3:AbortMultipartUpload","s3:ListMultipartUploadParts"],"Resource":["arn:aws:s3:::$S3_BUCKET/$write_prefix/*"$extra]}]}
|
||||
EOF
|
||||
policy="messenger-$role"
|
||||
mc admin policy create storage "$policy" /tmp/policy.json >/dev/null
|
||||
mc admin user add storage "$user" "$password" >/dev/null
|
||||
if ! mc admin user info --json storage "$user" | grep -Eq '"policyName"[[:space:]]*:[[:space:]]*"'"$policy"'"'; then
|
||||
mc admin policy attach storage "$policy" --user "$user" >/dev/null
|
||||
fi
|
||||
done
|
||||
@@ -0,0 +1,98 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
: "${S3_BUCKET:?}"
|
||||
: "${S3_ACCESS_KEY:?}"
|
||||
: "${S3_SECRET_KEY:?}"
|
||||
|
||||
mc alias set app http://minio:9000 \
|
||||
"$S3_ACCESS_KEY" "$S3_SECRET_KEY" >/dev/null
|
||||
|
||||
TEST_ID="$(cat /proc/sys/kernel/random/uuid)"
|
||||
OBJECT_KEY="originals/access-check-$TEST_ID.txt"
|
||||
OUTSIDE_KEY="outside-access-check-$TEST_ID.txt"
|
||||
|
||||
cleanup() {
|
||||
mc rm --force \
|
||||
"app/$S3_BUCKET/$OBJECT_KEY" >/dev/null 2>&1 || true
|
||||
|
||||
mc rm --force \
|
||||
"app/$S3_BUCKET/$OUTSIDE_KEY" >/dev/null 2>&1 || true
|
||||
}
|
||||
|
||||
trap cleanup EXIT
|
||||
|
||||
printf 'private attachment test' > /tmp/expected.txt
|
||||
|
||||
mc cp /tmp/expected.txt \
|
||||
"app/$S3_BUCKET/$OBJECT_KEY" >/dev/null
|
||||
|
||||
mc cat "app/$S3_BUCKET/$OBJECT_KEY" > /tmp/actual.txt
|
||||
|
||||
cmp /tmp/expected.txt /tmp/actual.txt
|
||||
|
||||
echo "PASS: API credentials can upload and download originals"
|
||||
|
||||
HTTP_CODE="$(
|
||||
curl --silent --show-error \
|
||||
--connect-timeout 3 \
|
||||
--max-time 10 \
|
||||
--output /tmp/anonymous-response.xml \
|
||||
--write-out '%{http_code}' \
|
||||
"http://minio:9000/$S3_BUCKET/$OBJECT_KEY"
|
||||
)"
|
||||
|
||||
if [ "$HTTP_CODE" != "403" ]; then
|
||||
echo "FAIL: anonymous GET returned HTTP $HTTP_CODE" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! grep -q '<Code>AccessDenied</Code>' \
|
||||
/tmp/anonymous-response.xml
|
||||
then
|
||||
echo "FAIL: expected S3 AccessDenied response" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "PASS: anonymous download of an existing object is denied"
|
||||
|
||||
if mc cp /tmp/expected.txt \
|
||||
"app/$S3_BUCKET/$OUTSIDE_KEY" \
|
||||
> /tmp/outside-result.txt 2>&1
|
||||
then
|
||||
echo "FAIL: API credentials can write outside originals/" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! grep -Eiq \
|
||||
'AccessDenied|Access Denied|Insufficient permissions|not authorized|not allowed' \
|
||||
/tmp/outside-result.txt
|
||||
then
|
||||
echo "FAIL: write failed for a reason other than access denial" >&2
|
||||
cat /tmp/outside-result.txt >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "PASS: writing outside originals/ is denied"
|
||||
|
||||
if mc admin info app > /tmp/admin-result.txt 2>&1
|
||||
then
|
||||
echo "FAIL: API credentials have administrative access" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! grep -Eiq \
|
||||
'AccessDenied|Access Denied|Insufficient permissions|not authorized|not allowed' \
|
||||
/tmp/admin-result.txt
|
||||
then
|
||||
echo "FAIL: admin command failed for an unexpected reason" >&2
|
||||
cat /tmp/admin-result.txt >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "PASS: administrative access is denied"
|
||||
|
||||
mc rm "app/$S3_BUCKET/$OBJECT_KEY" >/dev/null
|
||||
|
||||
echo "PASS: API credentials can delete originals for cleanup"
|
||||
echo "PASS: S3 access checks completed"
|
||||
Reference in New Issue
Block a user