Cloud Security In progress
Intermediate 18 min read Cloud Security
KMS and Cloud Encryption
Turn 'encrypted at rest' from a checkbox into engineering: key hierarchy, rotation, envelope encryption, and who actually holds the keys.
KMSenvelope encryptionCMKkey rotationdata keyHSM
Introduction
What is this?
Cloud encryption is about keys, not algorithms: a key management service (KMS) stores master keys, issues short-lived data keys, rotates material, and audits usage with IAM policies.
Motivation
Why do you need it?
- Encrypted-at-rest without key management is theater — a leaked master key ruins the encryption either way.
- Envelope encryption (KMS wraps a data key) is the pattern behind every cloud storage service; you need it to read how data flows.
- Key rotation and customer-managed-keys are audit questions regulators ask before they ask about the algorithm.
Think about it
Mental model
Imagine
A vault full of safes
Think of
One hotkey to rule all the safes
Protect the hotkey better than every safe, and rotate it so a stolen copy expires.
Mechanics
How it works
- A KMS holds master keys (CMKs); it never exports them outside the HSM-backed vault.
- To encrypt an object, the app calls the KMS, which returns a data key (plaintext + ciphertext) usable for that operation.
- The ciphertext data key rides with the data; the plaintext half lives only in the app's memory briefly.
- Rotation changes the master key material while keeping the same logical key id — old data stays decryptable via versioned keys.
Envelope encryption in one look
Data keyKMS generates a short-lived key
Encryptapp encrypts the object with the data key
Wrapdata key encrypted by the master key
Storeciphertext + wrapped key ride together
Decryptapp asks KMS to unwrap when allowed
Real world
In production
- A bank enables customer-managed keys on object storage so supply-chain incidents discover nothing to steal.
- An org enables automatic KMS rotation and schedules a manual key-material rotation for legacy systems.
- A data pipeline keys partitions per-date buckets so analyst access is scoped by partition-level policy.
Guidelines
Best practices
- Use KMS-managed keys for anything 'at rest' and customer-managed keys where compliance requires control.
- Prefer automatic rotation on the cadence your policy demands; review key versions after rotation.
- Lock KMS admin to break-glass paths and enable key usage auditing.
- Never hardcode plaintext keys; always issue through KMS or a secrets manager.
Watch out
Common mistakes
- Turning on 'encryption' with the provider's default service key and calling it a control — you never rotate it.
- Copying a plaintext data key into logs 'for debugging'.
- Forgetting that a lost customer-managed key means permanent data irretrievability — a DR plan must mirror it.
- Pinning to key version IDs so rotation silently breaks every pipeline.
When things break
Debugging tips
- When a decrypt fails after rotation, check the key version referenced in the error vs the current one.
- Enable KMS KeyEvents/audit logs and search for repeated 'AccessDenied' — that is usually a policy issue.
- Test rotation in a scratch key namespace before touching production data.
Security
Security considerations
- A KMS key is a fail-closed control: losing access rights is data loss until policy is restored.
- Think of key access as identity: a KMS-granted app role is as valuable as a DB password.
- Only ever test with keys and data you own; never wrap someone else's ciphertext without scope.
Interviews
Interview checkpoint
Practice
Hands-on lab
Envelope-Encrypt a File, Then Lose Access to Prove the DR Rule
40 min · intermediate
Objectives
- Generate a master key and a data key locally with openssl.
- Envelope-encrypt a file you own.
- Destroy the wrap key and prove the file becomes unrecoverable — and write the DR mirror plan.
Envelope-Encrypt a File, Then Lose Access to Prove the DR Rule
40 min · intermediate
0/4 steps
0%
sandbox · Linux sandbox
$
Try these commands
Test yourself
Quick quiz
0/2
1
The data key in envelope encryption is...
2
A lost customer-managed KMS key means...
Go deeper