MODULE 01 // LESSON 05
Key Management

Key Lifecycle

Your API key is the skeleton key to your behavioral intelligence system. Managing keys isn't just about keeping them secret—it's about rotation, access control, monitoring, and graceful transitions.

VISUAL: Digital vault with rotating holographic keys // Automated rotation gears // Security audit trails
THE COMPLETE LIFECYCLE

API key management encompasses the entire lifecycle: generation, storage, distribution, rotation, revocation, and audit logging. Keys should never be hardcoded or committed to version control; instead, they belong in environment variables, secrets managers (AWS Secrets Manager, HashiCorp Vault, Azure Key Vault), or secure configuration services.

Rotation policies should enforce regular key updates (every 90 days minimum), with automated rotation systems that generate new keys, test them in production, gradually shift traffic, and retire old keys only after confirming zero usage.

Access control means limiting key visibility to only necessary personnel and services, with different keys for different environments (dev, staging, production). Comprehensive audit logs track every key usage, failed auth attempts, and administrative actions for security monitoring and compliance.

01

GENERATION

Cryptographically secure random key generation using crypto.randomBytes(). Each key gets unique ID, expiry timestamp, and metadata tags.

02

STORAGE

Store in secrets manager (Vault, AWS Secrets Manager) or secure environment variables. Never commit to version control or hardcode.

03

DISTRIBUTION

Securely distribute to authorized services via encrypted channels. Use principle of least privilege for access control.

04

MONITORING

Track usage patterns, failed attempts, and expiry warnings. Alert on anomalies and approaching expiration dates.

05

ROTATION

Generate new key, test in staging, deploy to production with grace period. Old key remains valid during migration window.

06

REVOCATION

Immediate invalidation for compromised keys. Emergency revocation bypasses grace period. Generate replacement key immediately.

// SECURITY CHECKLIST

Never hardcode keys

Use environment variables or secrets managers. Never commit keys to version control.

Rotate regularly

Every 90 days minimum. Automate rotation to eliminate human error.

Use different keys

Separate keys for dev, staging, production. Isolate blast radius.

Implement grace periods

7 days minimum. Allow smooth migrations during rotation.

Audit everything

Log all key operations and usage. Enable forensic investigation.

Monitor expiry

Alert 30, 14, and 7 days before expiration. Prevent downtime.

Revoke immediately

If compromise suspected, revoke and rotate. Speed is critical.

Least privilege

Limit key access to necessary services only. Defense in depth.

KNOWLEDGE CHECK // Q05
Why is a grace period important during API key rotation, and what should be the minimum duration?