Encryption and Secrets Management
Encryption in transit and at rest, envelope encryption and KMS, key rotation, HSMs, tokenization, field-level encryption, and secrets management patterns with Vault and cloud services.
Encryption in transit
- TLS 1.2+ (prefer 1.3) for all external and internal traffic; HSTS for web.
- mTLS between services where identity matters (mesh or libraries).
- Automate certificates (ACME/Let's Encrypt, cert-manager, cloud certificate managers); monitor expiry.
Encryption at rest
| Level | Protects against | Example |
|---|---|---|
| Disk/volume | Stolen disks | Cloud default encryption |
| Database/TDE | Storage-level access | RDS/Azure SQL encryption with KMS keys |
| Application/field-level | DBAs, backups, compromised DB access | Encrypt card tokens, national IDs in the app |
| Client-side | The service provider itself | E2EE messaging, client-side encrypted backups |
Envelope encryption
flowchart LR KMS[KMS / HSM<br/>key encryption key KEK<br/>never leaves HSM] -->|generate data key| APP[Application] APP -->|encrypt data with DEK| DATA[(Ciphertext)] APP -->|store DEK encrypted by KEK| DATA APP -->|to decrypt: send encrypted DEK| KMS
Benefits: large data encrypted locally with fast symmetric keys; KMS only wraps/unwraps small data keys; rotate KEKs without re-encrypting all data; access to KMS is audited.
Tokenization
Replace sensitive values (card numbers) with tokens; the mapping lives in a secure vault. Systems that handle only tokens fall out of PCI DSS scope for card data.
Secrets management
| Practice | Detail |
|---|---|
| Central secret store | HashiCorp Vault / OpenBao, AWS Secrets Manager, Azure Key Vault, Google Secret Manager |
| No secrets in code, images or Git | Secret scanning in CI and pre-commit |
| Workload identity instead of secrets | IRSA/Pod Identity, managed identities, Workload Identity Federation |
| Dynamic, short-lived credentials | Vault database secrets engine issues per-app DB credentials with TTL |
| Rotation | Automated; applications reload without restart |
| Kubernetes | External Secrets Operator or Secrets Store CSI Driver; encrypt etcd secrets with KMS |
# External Secrets Operator: sync a secret from AWS Secrets Manager into Kubernetes
apiVersion: external-secrets.io/v1
kind: ExternalSecret
metadata: { name: orders-db }
spec:
refreshInterval: 1h
secretStoreRef: { name: aws-secrets, kind: ClusterSecretStore }
target: { name: orders-db-credentials }
data:
- secretKey: password
remoteRef: { key: prod/orders/db, property: password }Interview questions and answers
How do you rotate a database password without downtime?
Use dual credentials (two users) or a secrets manager rotation that creates the new password, updates the database, and lets applications fetch the new value with a grace period; better, use short-lived dynamic credentials or IAM database authentication.
What is the difference between KMS and an HSM?
An HSM is tamper-resistant hardware that protects keys. Cloud KMS services are managed key services usually backed by HSMs (validated to FIPS 140 levels); dedicated HSM services (CloudHSM, Azure Managed HSM, Cloud HSM) give single-tenant control for stricter requirements.