Files
mtusi2026lab7/scripts/test-pki-lifecycle.sh
T
2026-11-26 23:34:55 +03:00

64 lines
2.9 KiB
Bash
Executable File

#!/usr/bin/env bash
# Destructive only to the current LB leaf: rotates it, then verifies old/expired cert denial.
set -euo pipefail
cd "$(dirname "$0")/.."
old=secrets/operator/lifecycle
mkdir -p "$old"
chmod 700 "$old"
cp certs/client-api/client-api.crt "$old/old.crt"
cp certs/client-api/client-api.key "$old/old.key"
python3 - <<'PY' &
import json,time,ssl,urllib.request,urllib.error
from pathlib import Path
ctx=ssl.create_default_context(cafile='certs/ca.crt')
counts={'ok':0,'errors':0,'first_failure_seconds':None,'recovered_seconds':None}
start=time.monotonic()
for _ in range(80):
try:
with urllib.request.urlopen('https://localhost:8443/health/ready',context=ctx,timeout=2) as r:
assert r.status==200
counts['ok']+=1
if counts['first_failure_seconds'] is not None and counts['recovered_seconds'] is None:counts['recovered_seconds']=round(time.monotonic()-start,3)
except (OSError,urllib.error.URLError,AssertionError):
counts['errors']+=1
if counts['first_failure_seconds'] is None:counts['first_failure_seconds']=round(time.monotonic()-start,3)
time.sleep(.25)
counts['elapsed_seconds']=round(time.monotonic()-start,3)
Path('evidence/pki-load.json').write_text(json.dumps(counts,indent=2)+'\n')
PY
load_pid=$!
scripts/bootstrap-pki.sh client-api
scripts/dc.sh restart api-tls api2-tls
scripts/dc.sh restart api api2
scripts/dc.sh restart lb
# Wait for fresh TLS listeners; this also checks the new LB client leaf succeeds.
for i in {1..30}; do
if curl --silent --fail --cacert certs/ca.crt https://localhost:8443/health/ready >/dev/null; then break; fi
sleep 1
done
curl --silent --show-error --fail --cacert certs/ca.crt https://localhost:8443/health/ready >/dev/null
reject() {
set +e
scripts/dc.sh run --rm -T --no-deps -v "$PWD/$old:/old-client:ro" --entrypoint curl api \
--silent --show-error --fail --connect-timeout 3 --max-time 10 --cacert /pki/ca.crt \
--cert "/old-client/$1.crt" --key "/old-client/$1.key" \
--resolve api:8443:127.0.0.1 https://api:8443/health/ready >/dev/null 2>&1
rc=$?
set -e
case "$rc" in 35|52|55|56) ;; *) echo "FAIL: expected TLS rejection for $1; got $rc"; exit 1 ;; esac
}
reject old
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out "$old/expired.key" 2>/dev/null
openssl req -new -key "$old/expired.key" -out "$old/expired.csr" -subj /CN=expired-client-api
cat > "$old/ext.cnf" <<'EOF'
basicConstraints=critical,CA:false
keyUsage=critical,digitalSignature
extendedKeyUsage=clientAuth
subjectAltName=DNS:client-api
EOF
flock secrets/operator/ca/ca.lock openssl ca -batch -notext -config secrets/operator/ca/openssl.cnf -extfile "$old/ext.cnf" \
-startdate 200101000000Z -enddate 210101000000Z -in "$old/expired.csr" -out "$old/expired.crt" >/dev/null 2>&1
reject expired
wait "$load_pid"
echo 'PASS: renewed leaf succeeds; old leaf revoked; expired leaf rejected; load results: evidence/pki-load.json'