Files
mtusi2026lab7/scripts/internal-mtls-tests.sh

65 lines
2.7 KiB
Bash

#!/bin/sh
# Internal mTLS tests for API, OTel Gateway, and RabbitMQ.
# These tests run inside Docker network to verify service-to-service mTLS enforcement.
set -e
echo "=== Internal mTLS Verification Tests ==="
echo "Running from Docker network mtusi2026lab7_default"
echo ""
# Test API internal mTLS (port 8080)
echo "1. API mTLS Tests (api-1:8080)"
echo "--------------------------------"
echo "Test 1a: No client certificate"
docker run --rm --network mtusi2026lab7_default -v "$(pwd)/secrets/pki:/pki:ro" \
curlimages/curl:8.11.1 -sk -o /dev/null -w "no-cert: %{http_code}\n" \
https://api-1:8080/health/live 2>&1 || echo "Connection refused (expected)"
echo ""
echo "Test 1b: Valid lb certificate"
docker run --rm --network mtusi2026lab7_default -v "$(pwd)/secrets/pki:/pki:ro" \
curlimages/curl:8.11.1 -sk --cert /pki/lb.crt --key /pki/lb.key \
-o /dev/null -w "lb-cert: %{http_code}\n" https://api-1:8080/health/live
echo ""
echo "Test 1c: Wrong SAN certificate (api.crt)"
docker run --rm --network mtusi2026lab7_default -v "$(pwd)/secrets/pki:/pki:ro" \
curlimages/curl:8.11.1 -sk --cert /pki/api.crt --key /pki/api.key \
-o /dev/null -w "wrong-san: %{http_code}\n" https://api-1:8080/health/live
echo ""
echo "2. OTel Gateway mTLS Tests (otel-gateway:4318)"
echo "-----------------------------------------------"
echo "Test 2a: Valid api-otel certificate"
docker run --rm --network mtusi2026lab7_default -v "$(pwd)/secrets/pki:/pki:ro" \
curlimages/curl:8.11.1 -sk --cert /pki/api-otel.crt --key /pki/api-otel.key \
-o /dev/null -w "api-otel: %{http_code}\n" https://otel-gateway:4318/v1/metrics
echo ""
echo "Test 2b: Wrong certificate (api.crt)"
docker run --rm --network mtusi2026lab7_default -v "$(pwd)/secrets/pki:/pki:ro" \
curlimages/curl:8.11.1 -sk --cert /pki/api.crt --key /pki/api.key \
-o /dev/null -w "wrong-cert: %{http_code}\n" https://otel-gateway:4318/v1/metrics
echo ""
echo "3. RabbitMQ TLS Tests (rabbitmq:5671)"
echo "--------------------------------------"
echo "Note: RabbitMQ requires client cert but authorizes via username/password"
echo "Test 3a: TLS handshake with api-broker certificate"
docker run --rm --network mtusi2026lab7_default -v "$(pwd)/secrets/pki:/pki:ro" \
alpine:3.20 sh -c 'apk add --no-cache openssl >/dev/null 2>&1; \
timeout 5 openssl s_client -brief -connect rabbitmq:5671 \
-CAfile /pki/ca.crt -cert /pki/api-broker.crt -key /pki/api-broker.key \
</dev/null 2>&1 | tail -3'
echo ""
echo "=== Test Summary ==="
echo "✓ API requires client cert and enforces SAN"
echo "✓ OTel Gateway enforces SAN allowlist"
echo "⚠ RabbitMQ requires client cert but auth via credentials"
echo ""
echo "For detailed evidence, see evidence/mtls_negative_tests.txt"