#!/bin/sh set -eu project_root=$(CDPATH='' cd -- "$(dirname -- "$0")/.." && pwd) cd "$project_root" ./tests/security_grade3.sh run_test_shell() { docker compose --profile tests run --rm --no-deps --entrypoint sh security-test -ec "$1" } expect_broker_denied() { name=$1 cert_args=$2 if run_test_shell "printf 'AMQP\\000\\000\\011\\001' | timeout 5 openssl s_client -quiet -verify_return_error -CAfile /run/secrets/ca.crt -servername dependency-gateway $cert_args -connect dependency-gateway:5671 2>/dev/null | grep -a -q AMQPLAIN"; then printf '%s\n' "FAIL: broker accepted $name" >&2 exit 1 fi printf '%s\n' "PASS: broker rejected $name" } expect_broker_allowed() { name=$1 cert=$2 key=$3 run_test_shell "printf 'AMQP\\000\\000\\011\\001' | timeout 5 openssl s_client -quiet -verify_return_error -CAfile /run/secrets/ca.crt -servername dependency-gateway -cert $cert -key $key -connect dependency-gateway:5671 2>/dev/null | grep -a -q AMQPLAIN" printf '%s\n' "PASS: broker accepted scoped $name identity" } expect_broker_denied "missing client certificate" "" expect_broker_denied "same-CA intruder identity" "-cert /run/secrets/wrong-client.crt -key /run/secrets/wrong-client.key" expect_broker_denied "API identity without a broker edge" "-cert /run/secrets/api-client.crt -key /run/secrets/api-client.key" expect_broker_allowed worker /run/secrets/worker-client.crt /run/secrets/worker-client.key expect_broker_allowed dispatcher /run/secrets/dispatcher-client.crt /run/secrets/dispatcher-client.key otel_status() { cert_args=$1 run_test_shell "curl --silent --show-error --output /dev/null --write-out '%{http_code}' --cacert /run/secrets/ca.crt $cert_args -H 'Content-Type: application/x-protobuf' --data-binary '' https://telemetry-gateway:4318/v1/traces" } if status=$(otel_status "" 2>/dev/null) && [ "$status" = 200 ]; then printf '%s\n' "FAIL: Collector accepted a missing client certificate" >&2 exit 1 fi printf '%s\n' "PASS: Collector rejected a missing client certificate" status=$(otel_status "--cert /run/secrets/wrong-client.crt --key /run/secrets/wrong-client.key") if [ "$status" != 403 ]; then printf '%s\n' "FAIL: Collector wrong same-CA identity returned HTTP $status, expected 403" >&2 exit 1 fi printf '%s\n' "PASS: Collector rejected a same-CA intruder identity" for identity in api worker dispatcher; do status=$(otel_status "--cert /run/secrets/$identity-client.crt --key /run/secrets/$identity-client.key") if [ "$status" != 200 ]; then printf '%s\n' "FAIL: Collector rejected $identity with HTTP $status" >&2 exit 1 fi printf '%s\n' "PASS: Collector accepted scoped $identity identity" done for service in api worker dispatcher; do for container_id in $(docker compose ps -q "$service"); do readonly=$(docker inspect -f '{{.HostConfig.ReadonlyRootfs}}' "$container_id") no_new_privileges=$(docker inspect -f '{{json .HostConfig.SecurityOpt}}' "$container_id") cap_drop=$(docker inspect -f '{{json .HostConfig.CapDrop}}' "$container_id") [ "$readonly" = true ] || { printf '%s\n' "FAIL: $service root filesystem is writable" >&2; exit 1; } printf '%s' "$no_new_privileges" | grep -q 'no-new-privileges' || { printf '%s\n' "FAIL: $service lacks no-new-privileges" >&2; exit 1; } printf '%s' "$cap_drop" | grep -q 'ALL' || { printf '%s\n' "FAIL: $service does not drop all capabilities" >&2; exit 1; } done done printf '%s\n' "PASS: application containers are read-only, no-new-privileges, cap-drop=ALL" worker_id=$(docker compose ps -q worker) worker_memory=$(docker inspect -f '{{.HostConfig.Memory}}' "$worker_id") worker_nano_cpus=$(docker inspect -f '{{.HostConfig.NanoCpus}}' "$worker_id") worker_environment=$(docker inspect -f '{{json .Config.Env}}' "$worker_id") [ "$worker_memory" -eq 536870912 ] || { printf '%s\n' "FAIL: worker memory limit is $worker_memory" >&2; exit 1; } [ "$worker_nano_cpus" -eq 1000000000 ] || { printf '%s\n' "FAIL: worker CPU limit is $worker_nano_cpus" >&2; exit 1; } printf '%s' "$worker_environment" | grep -q 'WORKER_CONCURRENCY=1' || { printf '%s\n' "FAIL: worker concurrency is not 1" >&2; exit 1; } printf '%s' "$worker_environment" | grep -q 'JOB_TIMEOUT_SECONDS=60' || { printf '%s\n' "FAIL: worker timeout is not 60s" >&2; exit 1; } printf '%s' "$worker_environment" | grep -q 'GOMEMLIMIT=448MiB' || { printf '%s\n' "FAIL: worker Go memory limit is not 448MiB" >&2; exit 1; } printf '%s\n' "PASS: worker has 512 MiB RAM, 1 CPU, concurrency=1, GOMEMLIMIT=448MiB and 60s job timeout"