110 lines
4.7 KiB
Bash
Executable File
110 lines
4.7 KiB
Bash
Executable File
#!/bin/sh
|
|
set -eu
|
|
|
|
project_root=$(CDPATH='' cd -- "$(dirname -- "$0")/.." && pwd)
|
|
pki_dir="$project_root/secrets/pki"
|
|
cd "$project_root"
|
|
|
|
temp_dir=$(mktemp -d "${TMPDIR:-/tmp}/mtusi-lab7-leaf.XXXXXX")
|
|
case "$temp_dir" in
|
|
"${TMPDIR:-/tmp}"/mtusi-lab7-leaf.*) ;;
|
|
*) printf '%s\n' "Unexpected temporary directory" >&2; exit 1 ;;
|
|
esac
|
|
|
|
old_uri=$(sed -n '1p' "$pki_dir/api-client-uris.txt")
|
|
cp "$pki_dir/lb-client.crt" "$temp_dir/old.crt"
|
|
cp "$pki_dir/lb-client.key" "$temp_dir/old.key"
|
|
cp "$pki_dir/lb-client.pem" "$temp_dir/old.pem"
|
|
app_image_before=$(docker image inspect -f '{{.Id}}' mtusi2026lab7-app:latest)
|
|
ingress_image_before=$(docker image inspect -f '{{.Id}}' mtusi2026lab7-ingress:latest)
|
|
new_uri="spiffe://mtusi/lab7/lb-$(openssl rand -hex 8)"
|
|
completed=false
|
|
|
|
recreate_edge() {
|
|
docker compose up -d --no-deps --no-build --force-recreate api ingress >/dev/null 2>&1
|
|
}
|
|
|
|
restore_old_leaf() {
|
|
install -m 600 "$temp_dir/old.key" "$pki_dir/lb-client.key"
|
|
install -m 644 "$temp_dir/old.crt" "$pki_dir/lb-client.crt"
|
|
install -m 600 "$temp_dir/old.pem" "$pki_dir/lb-client.pem"
|
|
printf '%s' "$old_uri" > "$pki_dir/api-client-uris.txt"
|
|
chmod 600 "$pki_dir/api-client-uris.txt"
|
|
recreate_edge || true
|
|
}
|
|
|
|
cleanup() {
|
|
if [ "$completed" != true ]; then
|
|
restore_old_leaf
|
|
fi
|
|
rm -f "$temp_dir/old.crt" "$temp_dir/old.key" "$temp_dir/old.pem" \
|
|
"$temp_dir/new.crt" "$temp_dir/new.key" "$temp_dir/new.csr" "$temp_dir/new.ext" \
|
|
"$temp_dir/expired.crt" "$temp_dir/expired.key" "$temp_dir/expired.csr" "$temp_dir/expired.ext"
|
|
rmdir "$temp_dir" 2>/dev/null || true
|
|
}
|
|
trap cleanup EXIT HUP INT TERM
|
|
|
|
cat > "$temp_dir/new.ext" <<EOF
|
|
subjectAltName=URI:$new_uri
|
|
extendedKeyUsage=clientAuth
|
|
keyUsage=digitalSignature,keyEncipherment
|
|
EOF
|
|
openssl req -new -newkey rsa:2048 -sha256 -nodes -subj '/CN=load-balancer' \
|
|
-keyout "$temp_dir/new.key" -out "$temp_dir/new.csr" >/dev/null 2>&1
|
|
openssl x509 -req -sha256 -days 365 -in "$temp_dir/new.csr" \
|
|
-CA "$pki_dir/ca.crt" -CAkey "$pki_dir/ca.key" -CAserial "$pki_dir/ca.srl" \
|
|
-extfile "$temp_dir/new.ext" -out "$temp_dir/new.crt" >/dev/null 2>&1
|
|
install -m 600 "$temp_dir/new.key" "$pki_dir/lb-client.key"
|
|
install -m 644 "$temp_dir/new.crt" "$pki_dir/lb-client.crt"
|
|
cat "$temp_dir/new.crt" "$temp_dir/new.key" > "$pki_dir/lb-client.pem"
|
|
chmod 600 "$pki_dir/lb-client.pem"
|
|
printf '%s' "$new_uri" > "$pki_dir/api-client-uris.txt"
|
|
chmod 600 "$pki_dir/api-client-uris.txt"
|
|
|
|
recreate_edge
|
|
attempts=0
|
|
until curl --silent --show-error --fail --cacert "$pki_dir/ca.crt" https://127.0.0.1:8443/health/live >/dev/null 2>&1; do
|
|
attempts=$((attempts + 1))
|
|
[ "$attempts" -lt 40 ] || { printf '%s\n' "Rotated edge did not become ready" >&2; exit 1; }
|
|
sleep 1
|
|
done
|
|
|
|
if docker compose --profile tests run --rm --no-deps -v "$temp_dir:/rotation-old:ro" \
|
|
--entrypoint curl security-test --silent --show-error --fail --cacert /run/secrets/ca.crt \
|
|
--cert /rotation-old/old.crt --key /rotation-old/old.key https://api:8444/health/live >/dev/null 2>&1; then
|
|
printf '%s\n' "Old leaf identity is still accepted" >&2
|
|
exit 1
|
|
fi
|
|
docker compose --profile tests run --rm --no-deps --entrypoint curl security-test \
|
|
--silent --show-error --fail --cacert /run/secrets/ca.crt \
|
|
--cert /run/secrets/lb-client.crt --key /run/secrets/lb-client.key \
|
|
https://api:8444/health/live >/dev/null
|
|
|
|
cat > "$temp_dir/expired.ext" <<EOF
|
|
subjectAltName=URI:$new_uri
|
|
extendedKeyUsage=clientAuth
|
|
keyUsage=digitalSignature,keyEncipherment
|
|
EOF
|
|
openssl req -new -newkey rsa:2048 -sha256 -nodes -subj '/CN=load-balancer-expired' \
|
|
-keyout "$temp_dir/expired.key" -out "$temp_dir/expired.csr" >/dev/null 2>&1
|
|
openssl x509 -req -sha256 -days 0 -in "$temp_dir/expired.csr" \
|
|
-CA "$pki_dir/ca.crt" -CAkey "$pki_dir/ca.key" -CAserial "$pki_dir/ca.srl" \
|
|
-extfile "$temp_dir/expired.ext" -out "$temp_dir/expired.crt" >/dev/null 2>&1
|
|
sleep 2
|
|
if docker compose --profile tests run --rm --no-deps -v "$temp_dir:/expired:ro" \
|
|
--entrypoint curl security-test --silent --show-error --fail --cacert /run/secrets/ca.crt \
|
|
--cert /expired/expired.crt --key /expired/expired.key https://api:8444/health/live >/dev/null 2>&1; then
|
|
printf '%s\n' "Expired leaf certificate is accepted" >&2
|
|
exit 1
|
|
fi
|
|
|
|
app_image_after=$(docker image inspect -f '{{.Id}}' mtusi2026lab7-app:latest)
|
|
ingress_image_after=$(docker image inspect -f '{{.Id}}' mtusi2026lab7-ingress:latest)
|
|
[ "$app_image_before" = "$app_image_after" ] && [ "$ingress_image_before" = "$ingress_image_after" ] || {
|
|
printf '%s\n' "Image changed during leaf rotation" >&2
|
|
exit 1
|
|
}
|
|
|
|
completed=true
|
|
printf '%s\n' "PASS: LB leaf rotated without rebuild; old identity and expired leaf rejected"
|