# Certificate and Secret Rotation Procedure
# Timestamp: 2026-11-06T11:45:00Z

## Scenario: Rotate Leaf Certificate Without Rebuild

### Initial State
Service: api-1
Certificate: api.crt (issued 2026-11-06, serial: [original])
Private key: api.key

### Step 1: Generate New Certificate

Commands executed on operator workstation:
```bash
cd secrets/pki
# Backup old certificate
cp api.crt api.crt.old
cp api.key api.key.old

# Generate new key and certificate with same SAN
openssl genrsa -out api.key 4096
openssl req -new -key api.key -out api.csr \
  -subj "/CN=api" \
  -addext "subjectAltName=DNS:api"
openssl x509 -req -in api.csr -CA ca.crt -CAkey ca.key \
  -CAcreateserial -out api.crt -days 365 -sha256 \
  -extfile <(printf "subjectAltName=DNS:api")
```

Result: New certificate generated with different serial number

### Step 2: Update Docker Secret

Since Docker secrets are immutable, restart required:
```bash
# Stop affected service
docker compose stop api-1

# New certificate already in secrets/pki/api.crt (mounted volume)
# OR for true secret rotation: recreate secret and service

# Restart service to pick up new certificate
docker compose start api-1
```

Result: Service restarted with new certificate

### Step 3: Verify New Certificate Active

```bash
# Check certificate serial from outside
openssl s_client -connect localhost:18080 -showcerts \
  -CAfile secrets/pki/ca.crt -cert secrets/pki/lb.crt -key secrets/pki/lb.key \
  </dev/null 2>/dev/null | openssl x509 -noout -serial

# Compare with old certificate serial
openssl x509 -in secrets/pki/api.crt.old -noout -serial
```

Expected: Different serial numbers
Result: ✓ New certificate active

### Step 4: Verify Old Certificate Rejected

Method 1: Remove old certificate from CA trust (CA rotation)
Method 2: Revoke old certificate via CRL (if CRL infrastructure exists)
Method 3: Verify old key cannot establish new connections

For this test (no CRL in simple PKI):
- Old certificate with same CA will still validate at TLS layer
- But mTLS enforcement at application layer checks SAN, not serial
- True rejection requires CRL or CA rotation

Practical verification:
```bash
# Attempt connection with OLD private key and NEW certificate
# This should fail (key mismatch)
openssl s_client -connect localhost:18080 \
  -CAfile secrets/pki/ca.crt \
  -cert secrets/pki/api.crt \
  -key secrets/pki/api.key.old
```

Result: TLS handshake failure (key mismatch)
✓ Old key cannot be used with new certificate

## OpenBao Secret Rotation (Already Tested)

See evidence/openbao_rotation.txt for KV secret rotation procedure:
- Updated kv/messenger/api password (version 1 → 2 → 3)
- Service restarted and fetched new secret
- No image rebuild required
- Old password superseded by new password in vault

## Certificate Rotation Limitations in This Lab

Full certificate revocation requires:
1. CRL (Certificate Revocation List) infrastructure - not implemented
2. OR OCSP (Online Certificate Status Protocol) - not implemented
3. OR CA rotation with old CA removal from truststore - manual operator procedure

For production: Implement automated certificate rotation via:
- cert-manager (Kubernetes)
- Vault PKI engine with TTL and auto-renewal
- ACME protocol (Let's Encrypt)

This lab demonstrates:
✓ Certificate replacement procedure
✓ Service restart with new certificate
✓ Key mismatch prevents old key usage
✗ Full CRL-based revocation (out of scope for grade 4)
