forked from mtusi2026/mtusi2026lab7
48 lines
2.1 KiB
Bash
48 lines
2.1 KiB
Bash
#!/bin/sh
|
|
set -eu
|
|
export MC_CONFIG_DIR=/tmp/mc
|
|
mkdir -p "$MC_CONFIG_DIR/certs/CAs"
|
|
cp /trust/ca.crt "$MC_CONFIG_DIR/certs/CAs/lab.crt"
|
|
for role in admin api worker; do
|
|
mc alias set "$role" https://minio:9000 "$(cat "/bootstrap/$role/S3_ACCESS_KEY")" \
|
|
"$(cat "/bootstrap/$role/S3_SECRET_KEY")" --api S3v4 >/dev/null
|
|
done
|
|
suffix="lab7-audit-$(date +%s)-$$"
|
|
stage="staging/$suffix"
|
|
original="original/$suffix"
|
|
foreign="restricted/$suffix"
|
|
trap 'mc rm "admin/attachments/$stage" "admin/attachments/$original" "admin/attachments/$foreign" >/dev/null 2>&1 || true' EXIT
|
|
put() { printf '%s' 'synthetic test fixture' | mc pipe "$1" >/dev/null; }
|
|
deny() {
|
|
if error=$("$@" 2>&1); then
|
|
printf '%s\n' 'Worker operation unexpectedly permitted' >&2
|
|
exit 1
|
|
fi
|
|
case "$error" in *"Access Denied"*|*"Access denied"*|*"not have sufficient permissions"*|*"Insufficient permissions"*|*"Access to the resource is denied"*) ;;
|
|
*) printf 'Unexpected S3 denial for %s %s\n' "$1" "$2" >&2; exit 1;;
|
|
esac
|
|
}
|
|
put "api/attachments/$stage"
|
|
put "admin/attachments/$foreign"
|
|
[ "$(mc cat "worker/attachments/$stage")" = 'synthetic test fixture' ]
|
|
put "worker/attachments/$original"
|
|
[ "$(mc cat "api/attachments/$original")" = 'synthetic test fixture' ]
|
|
deny mc cat "worker/attachments/$foreign"
|
|
deny put "worker/attachments/staging/${suffix}-write"
|
|
deny put "api/attachments/original/${suffix}-write"
|
|
deny mc rm "api/attachments/$original"
|
|
deny mc admin info worker
|
|
printf '%s\n' 'PASS S3 HTTPS: worker can read staging and write originals. Foreign prefixes, staging writes and admin denied.'
|
|
if [ "${1:-}" = rotated ]; then
|
|
if error=$(mc alias set retired https://minio:9000 "$(cat /retired/S3_ACCESS_KEY)" \
|
|
"$(cat /retired/S3_SECRET_KEY)" --api S3v4 2>&1) &&
|
|
error=$(mc cat "retired/attachments/$original" 2>&1); then
|
|
printf '%s\n' 'Retired S3 account accepted' >&2
|
|
exit 1
|
|
fi
|
|
case "$error" in *"Access Key Id you provided does not exist"*|*"InvalidAccessKeyId"*) ;;
|
|
*) printf '%s\n' 'Unexpected retired S3 authentication result' >&2; exit 1;;
|
|
esac
|
|
printf '%s\n' 'PASS MinIO rejects the retired worker access key.'
|
|
fi
|