forked from mtusi2026/mtusi2026lab7
164 lines
5.2 KiB
Bash
Executable File
164 lines
5.2 KiB
Bash
Executable File
#!/bin/sh
|
|
|
|
set -eu
|
|
|
|
script_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
|
|
repo_dir=$(CDPATH= cd -- "$script_dir/.." && pwd)
|
|
security_dir=${SECURITY_DIR:-$repo_dir/.runtime/security}
|
|
public_port=${PUBLIC_TLS_PORT:-8443}
|
|
public_host=${PUBLIC_TLS_HOST:-localhost}
|
|
api_address=${API_TLS_ADDRESS:-api:8443}
|
|
metrics_address=${METRICS_TLS_ADDRESS:-api:9091}
|
|
empty_ca="${TMPDIR:-/tmp}/lab7-empty-ca.$$"
|
|
|
|
cleanup() {
|
|
rm -f "$empty_ca"
|
|
}
|
|
|
|
trap cleanup EXIT HUP INT TERM
|
|
|
|
require_file() {
|
|
if [ ! -r "$1" ]; then
|
|
echo "missing security material: $1" >&2
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
expect_failure() {
|
|
name=$1
|
|
shift
|
|
|
|
if "$@" >/dev/null 2>&1; then
|
|
echo "FAIL: $name unexpectedly succeeded" >&2
|
|
exit 1
|
|
fi
|
|
echo "PASS: $name"
|
|
}
|
|
|
|
expect_tls_success() {
|
|
name=$1
|
|
certificate=$2
|
|
key=$3
|
|
|
|
if curl --fail --silent --show-error --noproxy '*' \
|
|
--connect-timeout 5 --max-time 10 \
|
|
--cacert "$security_dir/internal-ca/ca.crt" \
|
|
--cert "$certificate" --key "$key" \
|
|
"https://$api_address/health/ready" >/dev/null 2>&1; then
|
|
echo "PASS: $name"
|
|
return
|
|
fi
|
|
echo "FAIL: $name" >&2
|
|
exit 1
|
|
}
|
|
|
|
expect_tls_failure() {
|
|
name=$1
|
|
shift
|
|
|
|
if curl --fail --silent --show-error --noproxy '*' \
|
|
--connect-timeout 5 --max-time 10 \
|
|
--cacert "$security_dir/internal-ca/ca.crt" \
|
|
"$@" "https://$api_address/health/ready" >/dev/null 2>&1; then
|
|
echo "FAIL: $name unexpectedly succeeded" >&2
|
|
exit 1
|
|
fi
|
|
echo "PASS: $name"
|
|
}
|
|
|
|
expect_public_hostname_failure() {
|
|
if openssl s_client \
|
|
-connect "$public_host:$public_port" \
|
|
-servername wrong.local \
|
|
-verify_hostname wrong.local \
|
|
-verify_return_error \
|
|
-CAfile "$security_dir/public-ca/ca.crt" </dev/null >/dev/null 2>&1; then
|
|
echo "FAIL: public TLS with wrong hostname unexpectedly succeeded" >&2
|
|
exit 1
|
|
fi
|
|
echo "PASS: public TLS with wrong hostname"
|
|
}
|
|
|
|
for file in \
|
|
"$security_dir/public-ca/ca.crt" \
|
|
"$security_dir/internal-ca/ca.crt" \
|
|
"$security_dir/foreign-ca/ca.crt" \
|
|
"$security_dir/load-balancer/tls.crt" \
|
|
"$security_dir/load-balancer/tls.key" \
|
|
"$security_dir/prometheus/tls.crt" \
|
|
"$security_dir/prometheus/tls.key" \
|
|
"$security_dir/forbidden-client/tls.crt" \
|
|
"$security_dir/forbidden-client/tls.key" \
|
|
"$security_dir/foreign-client/tls.crt" \
|
|
"$security_dir/foreign-client/tls.key"; do
|
|
require_file "$file"
|
|
done
|
|
|
|
: >"$empty_ca"
|
|
chmod 0600 "$empty_ca"
|
|
|
|
curl --fail --silent --show-error --noproxy '*' \
|
|
--connect-timeout 5 --max-time 10 \
|
|
--cacert "$security_dir/public-ca/ca.crt" \
|
|
"https://$public_host:$public_port/health/ready" >/dev/null
|
|
echo "PASS: public TLS with trusted CA and hostname"
|
|
|
|
expect_failure "public TLS with empty CA" \
|
|
curl --fail --silent --show-error --noproxy '*' \
|
|
--connect-timeout 5 --max-time 10 \
|
|
--cacert "$empty_ca" \
|
|
"https://$public_host:$public_port/health/ready"
|
|
|
|
expect_failure "public TLS with foreign CA" \
|
|
curl --fail --silent --show-error --noproxy '*' \
|
|
--connect-timeout 5 --max-time 10 \
|
|
--cacert "$security_dir/foreign-ca/ca.crt" \
|
|
"https://$public_host:$public_port/health/ready"
|
|
|
|
expect_public_hostname_failure
|
|
|
|
expect_tls_failure "API mTLS without client certificate"
|
|
expect_tls_failure "API mTLS with forbidden trusted identity" \
|
|
--cert "$security_dir/forbidden-client/tls.crt" \
|
|
--key "$security_dir/forbidden-client/tls.key"
|
|
expect_tls_failure "API mTLS with client certificate from foreign CA" \
|
|
--cert "$security_dir/foreign-client/tls.crt" \
|
|
--key "$security_dir/foreign-client/tls.key"
|
|
expect_tls_success "API mTLS with load-balancer identity" \
|
|
"$security_dir/load-balancer/tls.crt" \
|
|
"$security_dir/load-balancer/tls.key"
|
|
|
|
expect_tls_failure "business API with Prometheus identity" \
|
|
--cert "$security_dir/prometheus/tls.crt" \
|
|
--key "$security_dir/prometheus/tls.key"
|
|
|
|
expect_failure "metrics mTLS without client certificate" \
|
|
curl --fail --silent --show-error --noproxy '*' \
|
|
--connect-timeout 5 --max-time 10 \
|
|
--cacert "$security_dir/internal-ca/ca.crt" \
|
|
"https://$metrics_address/metrics"
|
|
|
|
expect_failure "metrics mTLS with load-balancer identity" \
|
|
curl --fail --silent --show-error --noproxy '*' \
|
|
--connect-timeout 5 --max-time 10 \
|
|
--cacert "$security_dir/internal-ca/ca.crt" \
|
|
--cert "$security_dir/load-balancer/tls.crt" \
|
|
--key "$security_dir/load-balancer/tls.key" \
|
|
"https://$metrics_address/metrics"
|
|
|
|
expect_failure "metrics mTLS with client certificate from foreign CA" \
|
|
curl --fail --silent --show-error --noproxy '*' \
|
|
--connect-timeout 5 --max-time 10 \
|
|
--cacert "$security_dir/internal-ca/ca.crt" \
|
|
--cert "$security_dir/foreign-client/tls.crt" \
|
|
--key "$security_dir/foreign-client/tls.key" \
|
|
"https://$metrics_address/metrics"
|
|
|
|
curl --fail --silent --show-error --noproxy '*' \
|
|
--connect-timeout 5 --max-time 10 \
|
|
--cacert "$security_dir/internal-ca/ca.crt" \
|
|
--cert "$security_dir/prometheus/tls.crt" \
|
|
--key "$security_dir/prometheus/tls.key" \
|
|
"https://$metrics_address/metrics" >/dev/null
|
|
echo "PASS: metrics mTLS with Prometheus identity"
|