forked from mtusi2026/mtusi2026lab7
200 lines
9.2 KiB
Bash
Executable File
200 lines
9.2 KiB
Bash
Executable File
#!/bin/sh
|
|
|
|
set -eu
|
|
|
|
: "${SECURITY_DIR:=/run/security-source}"
|
|
: "${POSTGRES_HOST:=postgres}"
|
|
: "${VALKEY_HOST:=redis-gateway}"
|
|
: "${VALKEY_PORT:=16379}"
|
|
: "${REDIS_HOST:=redis-gateway}"
|
|
: "${REDIS_DISPATCHER_PORT:=26379}"
|
|
: "${REDIS_WORKER_PORT:=36379}"
|
|
: "${S3_HOST:=s3}"
|
|
: "${OTEL_HOST:=otel-gateway}"
|
|
: "${POSTGRES_DB:=messenger}"
|
|
: "${S3_BUCKET:=messenger-attachments}"
|
|
|
|
required_commands="curl mc openssl psql redis-cli"
|
|
for command in $required_commands; do
|
|
command -v "$command" >/dev/null 2>&1 || {
|
|
echo "$command is required" >&2
|
|
exit 1
|
|
}
|
|
done
|
|
|
|
ca=$SECURITY_DIR/internal-ca/ca.crt
|
|
|
|
expect_failure() {
|
|
description=$1
|
|
shift
|
|
if "$@" >/dev/null 2>&1; then
|
|
echo "expected failure: $description" >&2
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
expect_redis_denied() {
|
|
description=$1
|
|
shift
|
|
output=$("$@" 2>&1 || true)
|
|
case "$output" in
|
|
*NOPERM*|*DENIED*) ;;
|
|
*) echo "expected Redis ACL denial: $description" >&2; exit 1 ;;
|
|
esac
|
|
}
|
|
|
|
expect_redis_allowed() {
|
|
description=$1
|
|
shift
|
|
output=$("$@" 2>&1) || {
|
|
echo "expected Redis ACL allow: $description: $output" >&2
|
|
exit 1
|
|
}
|
|
case "$output" in
|
|
*NOPERM*|*DENIED*) echo "expected Redis ACL allow: $description: $output" >&2; exit 1 ;;
|
|
esac
|
|
}
|
|
|
|
check_server() {
|
|
host=$1
|
|
port=$2
|
|
identity=${3:-}
|
|
client_options=
|
|
if [ -n "$identity" ]; then
|
|
client_options="-cert $SECURITY_DIR/$identity/tls.crt -key $SECURITY_DIR/$identity/tls.key"
|
|
fi
|
|
# client_options contains only paths assembled by this script.
|
|
# shellcheck disable=SC2086
|
|
printf '' | openssl s_client $client_options \
|
|
-connect "$host:$port" \
|
|
-servername "$host" \
|
|
-verify_hostname "$host" \
|
|
-verify_return_error \
|
|
-CAfile "$ca" >/dev/null 2>&1
|
|
}
|
|
|
|
check_server "$POSTGRES_HOST" 5432 api-client
|
|
check_server "$VALKEY_HOST" "$VALKEY_PORT" api-client
|
|
check_server "$REDIS_HOST" "$REDIS_WORKER_PORT" worker-client
|
|
check_server "$S3_HOST" 9000
|
|
|
|
: "${POSTGRES_API_PASSWORD:?POSTGRES_API_PASSWORD is required}"
|
|
: "${POSTGRES_WORKER_PASSWORD:?POSTGRES_WORKER_PASSWORD is required}"
|
|
: "${POSTGRES_DISPATCHER_PASSWORD:?POSTGRES_DISPATCHER_PASSWORD is required}"
|
|
|
|
pg_url() {
|
|
role=$1
|
|
cert=$2
|
|
password=$3
|
|
printf 'postgresql://%s:%s@%s:5432/%s?sslmode=verify-full&sslrootcert=%s&sslcert=%s&sslkey=%s' \
|
|
"$role" "$password" "$POSTGRES_HOST" "$POSTGRES_DB" "$ca" \
|
|
"$SECURITY_DIR/$cert/tls.crt" "$SECURITY_DIR/$cert/tls.key"
|
|
}
|
|
|
|
api_db=$(pg_url messenger_api api-client "$POSTGRES_API_PASSWORD")
|
|
worker_db=$(pg_url messenger_worker worker-client "$POSTGRES_WORKER_PASSWORD")
|
|
dispatcher_db=$(pg_url messenger_dispatcher dispatcher-client "$POSTGRES_DISPATCHER_PASSWORD")
|
|
api_with_worker_cert=$(pg_url messenger_api worker-client "$POSTGRES_API_PASSWORD")
|
|
|
|
psql "$api_db" --set=ON_ERROR_STOP=1 --command='SELECT count(*) FROM users' >/dev/null
|
|
psql "$worker_db" --set=ON_ERROR_STOP=1 --command='SELECT count(*) FROM attachment_jobs' >/dev/null
|
|
psql "$worker_db" --set=ON_ERROR_STOP=1 --command='UPDATE attachment_jobs SET updated_at = updated_at WHERE false' >/dev/null
|
|
psql "$dispatcher_db" --set=ON_ERROR_STOP=1 --command='SELECT count(*) FROM outbox_events' >/dev/null
|
|
expect_failure "worker reads users" psql "$worker_db" --set=ON_ERROR_STOP=1 --command='SELECT count(*) FROM users'
|
|
expect_failure "dispatcher reads attachments" psql "$dispatcher_db" --set=ON_ERROR_STOP=1 --command='SELECT count(*) FROM attachments'
|
|
expect_failure "API role uses worker certificate" psql "$api_with_worker_cert" --set=ON_ERROR_STOP=1 --command='SELECT 1'
|
|
|
|
: "${VALKEY_API_PASSWORD:?VALKEY_API_PASSWORD is required}"
|
|
: "${REDIS_DISPATCHER_PASSWORD:?REDIS_DISPATCHER_PASSWORD is required}"
|
|
: "${REDIS_WORKER_PASSWORD:?REDIS_WORKER_PASSWORD is required}"
|
|
|
|
expect_failure "broker without client certificate" env REDISCLI_AUTH="$REDIS_WORKER_PASSWORD" \
|
|
redis-cli --tls --cacert "$ca" --user messenger_worker -h "$REDIS_HOST" -p "$REDIS_WORKER_PORT" PING
|
|
for identity in foreign-client api-client; do
|
|
expect_failure "broker rejects $identity" env REDISCLI_AUTH="$REDIS_WORKER_PASSWORD" \
|
|
redis-cli --tls --cacert "$ca" --cert "$SECURITY_DIR/$identity/tls.crt" \
|
|
--key "$SECURITY_DIR/$identity/tls.key" --user messenger_worker \
|
|
-h "$REDIS_HOST" -p "$REDIS_WORKER_PORT" PING
|
|
done
|
|
|
|
redis_tls() {
|
|
identity=$1
|
|
shift
|
|
redis-cli --tls --cacert "$ca" \
|
|
--cert "$SECURITY_DIR/$identity/tls.crt" \
|
|
--key "$SECURITY_DIR/$identity/tls.key" "$@"
|
|
}
|
|
|
|
REDISCLI_AUTH=$VALKEY_API_PASSWORD redis_tls api-client \
|
|
--user messenger_api -h "$VALKEY_HOST" -p "$VALKEY_PORT" SET session:acl-test value EX 30 >/dev/null
|
|
expect_redis_denied "session identity writes an arbitrary key" env REDISCLI_AUTH="$VALKEY_API_PASSWORD" \
|
|
redis-cli --tls --cacert "$ca" --cert "$SECURITY_DIR/api-client/tls.crt" \
|
|
--key "$SECURITY_DIR/api-client/tls.key" --user messenger_api -h "$VALKEY_HOST" -p "$VALKEY_PORT" SET forbidden value
|
|
|
|
REDISCLI_AUTH=$REDIS_DISPATCHER_PASSWORD redis_tls dispatcher-client \
|
|
--user messenger_dispatcher -h "$REDIS_HOST" -p "$REDIS_DISPATCHER_PORT" XADD attachment-jobs '*' payload acl-test >/dev/null
|
|
expect_redis_denied "dispatcher consumes jobs" env REDISCLI_AUTH="$REDIS_DISPATCHER_PASSWORD" \
|
|
redis-cli --tls --cacert "$ca" --cert "$SECURITY_DIR/dispatcher-client/tls.crt" \
|
|
--key "$SECURITY_DIR/dispatcher-client/tls.key" --user messenger_dispatcher -h "$REDIS_HOST" -p "$REDIS_DISPATCHER_PORT" \
|
|
XREAD COUNT 1 STREAMS attachment-jobs 0
|
|
REDISCLI_AUTH=$REDIS_WORKER_PASSWORD redis_tls worker-client \
|
|
--user messenger_worker -h "$REDIS_HOST" -p "$REDIS_WORKER_PORT" XGROUP DESTROY attachment-jobs acl-worker-check >/dev/null 2>&1 || true
|
|
expect_redis_allowed "worker creates its consumer group" env REDISCLI_AUTH="$REDIS_WORKER_PASSWORD" \
|
|
redis-cli --tls --cacert "$ca" --cert "$SECURITY_DIR/worker-client/tls.crt" \
|
|
--key "$SECURITY_DIR/worker-client/tls.key" --user messenger_worker -h "$REDIS_HOST" -p "$REDIS_WORKER_PORT" \
|
|
XGROUP CREATE attachment-jobs acl-worker-check '$' MKSTREAM
|
|
expect_redis_allowed "worker consumes its stream" env REDISCLI_AUTH="$REDIS_WORKER_PASSWORD" \
|
|
redis-cli --tls --cacert "$ca" --cert "$SECURITY_DIR/worker-client/tls.crt" \
|
|
--key "$SECURITY_DIR/worker-client/tls.key" --user messenger_worker -h "$REDIS_HOST" -p "$REDIS_WORKER_PORT" \
|
|
XREADGROUP GROUP acl-worker-check acl-worker-consumer COUNT 1 STREAMS attachment-jobs '>'
|
|
expect_redis_allowed "worker acknowledges its stream" env REDISCLI_AUTH="$REDIS_WORKER_PASSWORD" \
|
|
redis-cli --tls --cacert "$ca" --cert "$SECURITY_DIR/worker-client/tls.crt" \
|
|
--key "$SECURITY_DIR/worker-client/tls.key" --user messenger_worker -h "$REDIS_HOST" -p "$REDIS_WORKER_PORT" \
|
|
XACK attachment-jobs acl-worker-check 0-1
|
|
REDISCLI_AUTH=$REDIS_WORKER_PASSWORD redis_tls worker-client \
|
|
--user messenger_worker -h "$REDIS_HOST" -p "$REDIS_WORKER_PORT" XGROUP DESTROY attachment-jobs acl-worker-check >/dev/null
|
|
expect_redis_denied "worker accesses an unrelated stream" env REDISCLI_AUTH="$REDIS_WORKER_PASSWORD" \
|
|
redis-cli --tls --cacert "$ca" --cert "$SECURITY_DIR/worker-client/tls.crt" \
|
|
--key "$SECURITY_DIR/worker-client/tls.key" --user messenger_worker -h "$REDIS_HOST" -p "$REDIS_WORKER_PORT" XADD unrelated '*' payload denied
|
|
|
|
: "${S3_API_ACCESS_KEY:?S3_API_ACCESS_KEY is required}"
|
|
: "${S3_API_SECRET_KEY:?S3_API_SECRET_KEY is required}"
|
|
: "${S3_WORKER_ACCESS_KEY:?S3_WORKER_ACCESS_KEY is required}"
|
|
: "${S3_WORKER_SECRET_KEY:?S3_WORKER_SECRET_KEY is required}"
|
|
|
|
export SSL_CERT_FILE=$ca
|
|
export MC_CONFIG_DIR=/tmp/mc-acl-test
|
|
test_file=/tmp/acl-test-object
|
|
printf 'acl-test' >"$test_file"
|
|
mc alias set api "https://$S3_HOST:9000" "$S3_API_ACCESS_KEY" "$S3_API_SECRET_KEY" >/dev/null
|
|
mc alias set worker "https://$S3_HOST:9000" "$S3_WORKER_ACCESS_KEY" "$S3_WORKER_SECRET_KEY" >/dev/null
|
|
mc cp "$test_file" "api/$S3_BUCKET/staging/acl-test" >/dev/null
|
|
expect_failure "API writes processed objects" mc cp "$test_file" "api/$S3_BUCKET/attachments/acl-test"
|
|
mc cp "$test_file" "worker/$S3_BUCKET/attachments/acl-test" >/dev/null
|
|
expect_failure "worker writes outside attachment prefixes" mc cp "$test_file" "worker/$S3_BUCKET/private/acl-test"
|
|
mc rm "api/$S3_BUCKET/staging/acl-test" >/dev/null
|
|
mc rm "worker/$S3_BUCKET/attachments/acl-test" >/dev/null
|
|
|
|
expect_failure "OTel without client certificate" curl --silent --show-error \
|
|
--cacert "$ca" "https://$OTEL_HOST:4318/"
|
|
expect_failure "OTel with foreign CA client certificate" curl --silent --show-error \
|
|
--cacert "$ca" --cert "$SECURITY_DIR/foreign-client/tls.crt" \
|
|
--key "$SECURITY_DIR/foreign-client/tls.key" "https://$OTEL_HOST:4318/"
|
|
|
|
curl --silent --show-error --fail-with-body \
|
|
--cacert "$ca" \
|
|
--cert "$SECURITY_DIR/api-client/tls.crt" \
|
|
--key "$SECURITY_DIR/api-client/tls.key" \
|
|
"https://$OTEL_HOST:4318/" >/dev/null 2>&1 || status=$?
|
|
case "${status:-0}" in
|
|
0|22) ;;
|
|
*) echo "allowed OTel identity failed TLS" >&2; exit 1 ;;
|
|
esac
|
|
expect_failure "Prometheus same-CA identity reaches OTel" curl --silent --show-error \
|
|
--cacert "$ca" \
|
|
--cert "$SECURITY_DIR/prometheus/tls.crt" \
|
|
--key "$SECURITY_DIR/prometheus/tls.key" \
|
|
"https://$OTEL_HOST:4318/"
|
|
|
|
echo "dependency TLS and least-privilege matrix passed"
|