forked from mtusi2026/mtusi2026lab7
161 lines
5.2 KiB
Bash
Executable File
161 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)
|
|
env_file=${ENV_FILE:-$repo_dir/lab7.env.example}
|
|
security_dir=$repo_dir/.runtime/security
|
|
rotation_dir=$security_dir/rotation-test
|
|
old_dir=$rotation_dir/old-load-balancer
|
|
new_dir=$rotation_dir/new-load-balancer
|
|
expired_dir=$rotation_dir/expired-load-balancer
|
|
override_file=$repo_dir/.runtime/leaf-rotation.override.yaml
|
|
new_identity=spiffe://mtusi/lab7/load-balancer-v2
|
|
rotation_active=0
|
|
base_url=${BASE_URL:-https://localhost:8443}
|
|
|
|
compose() {
|
|
docker compose --env-file "$env_file" -f "$repo_dir/compose.yaml" "$@"
|
|
}
|
|
|
|
compose_rotated() {
|
|
docker compose --env-file "$env_file" \
|
|
-f "$repo_dir/compose.yaml" -f "$override_file" "$@"
|
|
}
|
|
|
|
install_security_material() {
|
|
compose run --rm --no-deps security-bootstrap >/dev/null
|
|
}
|
|
|
|
wait_for_https() {
|
|
attempts=0
|
|
while [ "$attempts" -lt 30 ]; do
|
|
if curl --silent --show-error --fail \
|
|
--cacert "$security_dir/public-ca/ca.crt" \
|
|
"$base_url/health/ready" >/dev/null 2>&1; then
|
|
return 0
|
|
fi
|
|
attempts=$((attempts + 1))
|
|
sleep 1
|
|
done
|
|
return 1
|
|
}
|
|
|
|
restore() {
|
|
status=$?
|
|
trap - EXIT HUP INT TERM
|
|
|
|
if [ "$rotation_active" -eq 1 ] && [ -f "$old_dir/tls.crt" ]; then
|
|
cp "$old_dir/tls.crt" "$security_dir/load-balancer/tls.crt"
|
|
cp "$old_dir/tls.key" "$security_dir/load-balancer/tls.key"
|
|
chmod 0600 "$security_dir/load-balancer/tls.crt" "$security_dir/load-balancer/tls.key"
|
|
install_security_material || status=1
|
|
compose up -d --no-deps --no-build --force-recreate api nginx >/dev/null || status=1
|
|
wait_for_https || status=1
|
|
fi
|
|
|
|
rm -rf "$rotation_dir"
|
|
rm -f "$override_file"
|
|
exit "$status"
|
|
}
|
|
|
|
trap restore EXIT HUP INT TERM
|
|
|
|
[ -f "$env_file" ] || {
|
|
echo "environment file not found: $env_file" >&2
|
|
exit 1
|
|
}
|
|
[ -f "$security_dir/internal-ca/ca.crt" ] || {
|
|
echo "run infra/pki/bootstrap.sh first" >&2
|
|
exit 1
|
|
}
|
|
|
|
umask 077
|
|
rm -rf "$rotation_dir"
|
|
mkdir -p "$old_dir" "$new_dir" "$expired_dir"
|
|
cp "$security_dir/load-balancer/tls.crt" "$old_dir/tls.crt"
|
|
cp "$security_dir/load-balancer/tls.key" "$old_dir/tls.key"
|
|
|
|
cat >"$rotation_dir/extensions.cnf" <<EOF
|
|
basicConstraints = critical,CA:FALSE
|
|
keyUsage = critical,digitalSignature
|
|
extendedKeyUsage = clientAuth
|
|
subjectAltName = URI:$new_identity
|
|
subjectKeyIdentifier = hash
|
|
authorityKeyIdentifier = keyid,issuer
|
|
EOF
|
|
|
|
issue_leaf() {
|
|
directory=$1
|
|
days=$2
|
|
|
|
openssl genpkey -quiet -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out "$directory/tls.key"
|
|
openssl req -new -sha256 \
|
|
-key "$directory/tls.key" \
|
|
-out "$directory/tls.csr" \
|
|
-subj "/CN=load-balancer-v2"
|
|
openssl x509 -req -sha256 -days "$days" \
|
|
-in "$directory/tls.csr" \
|
|
-CA "$security_dir/internal-ca/ca.crt" \
|
|
-CAkey "$security_dir/internal-ca/ca.key" \
|
|
-CAcreateserial \
|
|
-out "$directory/tls.crt" \
|
|
-extfile "$rotation_dir/extensions.cnf" >/dev/null
|
|
rm "$directory/tls.csr"
|
|
}
|
|
|
|
issue_leaf "$new_dir" 30
|
|
issue_leaf "$expired_dir" 0
|
|
|
|
openssl verify -purpose sslclient \
|
|
-CAfile "$security_dir/internal-ca/ca.crt" "$new_dir/tls.crt" >/dev/null
|
|
if openssl verify -purpose sslclient -attime "$(( $(date +%s) + 2 ))" \
|
|
-CAfile "$security_dir/internal-ca/ca.crt" "$expired_dir/tls.crt" >/dev/null 2>&1; then
|
|
echo "zero-lifetime certificate unexpectedly passed expiry verification" >&2
|
|
exit 1
|
|
fi
|
|
|
|
cat >"$override_file" <<EOF
|
|
services:
|
|
api:
|
|
environment:
|
|
APP_TLS_ALLOWED_CLIENT_URI: $new_identity
|
|
EOF
|
|
|
|
cp "$new_dir/tls.crt" "$security_dir/load-balancer/tls.crt"
|
|
cp "$new_dir/tls.key" "$security_dir/load-balancer/tls.key"
|
|
chmod 0600 "$security_dir/load-balancer/tls.crt" "$security_dir/load-balancer/tls.key"
|
|
rotation_active=1
|
|
|
|
install_security_material
|
|
compose_rotated up -d --no-deps --no-build --force-recreate api nginx >/dev/null
|
|
wait_for_https
|
|
|
|
compose_rotated --profile security run --rm --no-deps --entrypoint /bin/sh security-tests -c '
|
|
set -eu
|
|
curl --silent --show-error --fail \
|
|
--cacert /src/.runtime/security/internal-ca/ca.crt \
|
|
--cert /src/.runtime/security/load-balancer/tls.crt \
|
|
--key /src/.runtime/security/load-balancer/tls.key \
|
|
https://api:8443/health/ready >/dev/null
|
|
if curl --silent --show-error --fail \
|
|
--cacert /src/.runtime/security/internal-ca/ca.crt \
|
|
--cert /src/.runtime/security/rotation-test/old-load-balancer/tls.crt \
|
|
--key /src/.runtime/security/rotation-test/old-load-balancer/tls.key \
|
|
https://api:8443/health/ready >/dev/null 2>&1; then
|
|
echo "retired load-balancer identity unexpectedly reached API" >&2
|
|
exit 1
|
|
fi
|
|
if curl --silent --show-error --fail \
|
|
--cacert /src/.runtime/security/internal-ca/ca.crt \
|
|
--cert /src/.runtime/security/rotation-test/expired-load-balancer/tls.crt \
|
|
--key /src/.runtime/security/rotation-test/expired-load-balancer/tls.key \
|
|
https://api:8443/health/ready >/dev/null 2>&1; then
|
|
echo "expired load-balancer leaf unexpectedly reached API" >&2
|
|
exit 1
|
|
fi
|
|
'
|
|
|
|
echo "leaf rotation passed: new identity accepted, retired identity rejected, expired leaf rejected"
|