JWT Key Rotation Procedure
This document describes the procedure for rotating JWT (JSON Web Token) signing keys in the EFKO Kernel system.
Overview
The system uses JWT for authentication between the Gateway and backend services. Keys are configured via environment variables:
- JWT_ACCESS_SECRET — secret key for signing access tokens
- JWT_REFRESH_SECRET — secret key for signing refresh tokens (if implemented)
- JWT_ACCESS_ISSUER — issuer identifier for JWT validation
Why Rotate Keys?
- Security: Regular rotation limits the exposure window if a key is compromised
- Compliance: Many security standards require periodic key rotation
- Best Practice: Industry standard for production systems
Rotation Strategy
Option 1: Graceful Rotation with Dual Key Support
This approach allows both old and new keys to be valid during a transition period.
Steps:
-
Generate new keys
-
Update configuration with dual key support
Modify JWT configuration to support multiple keys:
// In security.module.ts or app.module.ts
JwtModule.registerAsync({
secret: process.env.JWT_ACCESS_SECRET,
signOptions: {
issuer: process.env.JWT_ACCESS_ISSUER,
},
verifyOptions: {
issuer: process.env.JWT_ACCESS_ISSUER,
algorithms: ['HS256'],
},
})
- Deploy new key to all services simultaneously
- Update environment variables for: gateway, auth-service
-
Restart services
-
Monitor for 1-2 weeks
- Watch for authentication failures
-
Check logs for JWT validation errors
-
Remove old key
- Update environment variables to remove old secret
- Restart services
Option 2: Immediate Rotation (Recommended for Non-Critical Systems)
This approach is simpler but causes a brief disruption where existing tokens become invalid.
Steps:
-
Generate new key
-
Update environment variables
-
Restart all services
-
Notify users
- Existing sessions will be invalidated
- Users must re-login
Pre-Rotation Checklist
- [ ] Generate new secret key(s)
- [ ] Backup current secrets
- [ ] Schedule maintenance window (if using immediate rotation)
- [ ] Prepare rollback plan
- [ ] Notify stakeholders
- [ ] Test new key in staging environment
Post-Rotation Verification
# Test authentication with new token
curl -X POST http://localhost:3000/api/v1/auth/login \
-H "Content-Type: application/json" \
-d '{"email":"test@example.com","password":"password"}'
# Verify token is signed with new key
# Decode JWT at jwt.io to check issuer and structure
Rollback Procedure
If issues occur after rotation:
- Revert to previous secret in environment variables
- Restart all services
- Investigate logs for root cause
- Retry rotation after fixing issues
Recommended Rotation Schedule
- Development: Rotate every 30 days
- Staging: Rotate every 60 days
- Production: Rotate every 90 days
Key Storage Best Practices
- Never commit secrets to git — use environment variables or secret management
- Use strong secrets — minimum 32 bytes (256 bits) for HS256
- Rotate keys regularly — follow schedule above
- Monitor for unauthorized access — check for unexpected authentication failures
- Use different secrets per environment — dev, staging, production
Emergency Rotation
If a key is suspected to be compromised:
- Immediate rotation — use Option 2 (immediate rotation)
- Investigate logs — identify when compromise occurred
- Notify security team — if applicable
- Review access logs — identify potentially affected users
- Force password reset — if user credentials may be compromised