System Design · 07. Security · 10 min · Intermediate
Encryption & Secrets
Protect data on the wire and on disk, and keep the keys and passwords that unlock it safe.
In short
TLS protects data in transit, encryption protects it at rest, and a KMS with envelope keys makes rotation cheap: 10 s instead of 83 min. Hash what you only compare (passwords), encrypt what you must read again.
Keep secrets out of code (20 clones is 20 leaks), put them in a secret manager, rotate them, and verify every call.
Key ideas
- Public-key maths to agree on a key (slow, once), a symmetric cipher for the data (fast, always). (step 2)
- Separate the key from the data, control who may use it, and log every use. (step 4)
- Authenticate and authorize every request, wherever it comes from. Assume something is already breached. (step 9)
Good to know
- Turn on encryption at rest everywhere by default: databases, volumes, backups and object storage. (step 3)
- Cloud services (S3, EBS, databases) all work this way. Big data stays where it is; only keys move. (step 5)
- Rotate on a schedule, not only after a leak, so rotation is a routine and not an emergency. (step 8)
Watch out
- Never rely on the network being private. Assume the path is hostile. (step 1)
- Do not encrypt passwords: a key that can decrypt them can leak. Do not hash data you need to read back. (step 6)
- If a secret was ever committed, treat it as leaked and rotate it. Removing it from the next commit is not enough. (step 7)
Key numbers
- Readable by
- Only who needs it
- Key rotation
- 10 s
- Copies of secret
- 1
Terms
- Data in transit
- Data moving across a network, which others on the path could read or change.
- TLS
- The protocol behind HTTPS: it proves the server's identity and encrypts all data with a shared key.
- Certificate
- A signed proof that a public key belongs to a named site.
- Encryption at rest
- Storing data on disk in encrypted form so a stolen disk or backup is unreadable.
- KMS (key management service)
- A vault that holds master keys and encrypts or decrypts for authorized callers without revealing the key.
- Envelope encryption
- Encrypt data with a data key, and encrypt that data key with a master key held in a KMS.
- Hash
- A one-way function that turns any input into a fixed-size fingerprint.
- Password hashing
- Storing passwords with a slow, salted hash such as bcrypt or Argon2, so they cannot be read back.
- Secrets in code
- Passwords or keys written in source files, which then live in every clone of the repository.
- Secret manager
- A service that stores secrets, gives them only to allowed apps at run time and supports rotation.
- Key rotation
- Replacing a key or secret with a new one on a schedule or after a leak.
- Zero trust
- No caller is trusted for its network location; every request is authenticated and authorized.