07. Security / Lesson 3 of 3

Encryption & Secrets

Protect data on the wire and on disk, and keep the keys and passwords that unlock it safe.

  • 10 min
  • Intermediate
  • 10 steps
  • Readable byAnyone on path (bad)
  • Key rotation—
  • Copies of secret—

POST /login (plain HTTP)

email=ann@example.com

✗ password=hunter2

✗ everyone on the path reads it

listensYour laptopEavesdropperCafe Wi-Fi routerWeb server
  • Request
  • Response
  • Write
  • Abuse
  • Failed request
  • Health check
Step 1/10

Step 1 of 10: On the way, anyone can listen

Read every step as text
  1. Step 1: On the way, anyone can listen

    Your request does not go straight to the server. It passes a Wi-Fi router, your provider and many other machines. Over plain HTTP each of them can read it, and even change it.

    A login form sent this way shows the password to anyone sharing the cafe Wi-Fi. Data on the move is called data in transit.

    Never rely on the network being private. Assume the path is hostile.

  2. Step 2: TLS: agree on a secret key, then scramble everything

    In the TLS handshake the server shows a certificate (proof, signed by a trusted authority, that it really is the site), and both sides agree on a fresh secret key with public-key maths without ever sending it. TLS 1.3 needs one round trip for this: 100 ms at 100 ms per round trip.

    After that, all data is encrypted with a fast symmetric cipher (AES) using that key. The eavesdropper sees only noise and cannot change a byte without being noticed. This is the lock in the browser: HTTPS.

    Public-key maths to agree on a key (slow, once), a symmetric cipher for the data (fast, always).

  3. Step 3: Encrypt what is stored, too

    Data at rest is data on disks: the database, files and backups. Disks get stolen, old drives are thrown away and a backup bucket is sometimes left open. If the data is encrypted, a thief with the disk holds only unreadable bytes.

    Disk, database and bucket encryption is usually switched on in one setting and costs little speed. It does not help against someone who can log in to the running app: for that you need authorization.

    Turn on encryption at rest everywhere by default: databases, volumes, backups and object storage.

  4. Step 4: Where does the key live?

    Encryption only moves the problem to the key. A key stored beside the data gives a thief both. So keys live in a key management service (KMS) or a hardware module (HSM): a vault that holds the master key and never lets it leave.

    The app does not take the key. It asks the KMS to encrypt or decrypt a small piece of data and the KMS answers, after checking who is asking. Each call takes about 10 ms and goes into an audit log.

    Separate the key from the data, control who may use it, and log every use.

  5. Step 5: Envelope: a key for each file, wrapped by the master key

    Sending 1,000 GB through a KMS would be slow, so the app makes a data key for each file, encrypts the file locally, and asks the KMS to wrap (encrypt) only that small data key. The file and its wrapped key are stored together.

    Now rotating the master key means re-wrapping 1,000 small data keys: 1,000 × 10 ms = 10 s. Re-encrypting all 1,000 GB at 200 MB/s would take 5,000 s, about 83 min.

    Cloud services (S3, EBS, databases) all work this way. Big data stays where it is; only keys move.

  6. Step 6: Hashing is not encryption

    Encryption is two-way: with the key you get the original back. Use it for data you must read again, such as a card number or a file.

    A hash is one-way: SHA-256 turns any input into 32 bytes, always the same for the same input, and there is no way back. Use it to check data without keeping it, such as file checksums. For passwords use a slow, salted hash (bcrypt, Argon2): the server stores only the hash and compares, so even a leaked table does not hold passwords.

    Do not encrypt passwords: a key that can decrypt them can leak. Do not hash data you need to read back.

  7. Step 7: Do not put secrets in code

    A database password or API key written in the source file is committed to git, and git remembers it forever, even after you delete the line. Every one of 20 developers' clones holds a copy, and so do CI logs and backups.

    Public repositories are scanned by bots for exactly these keys. Rotating the leaked secret means changing code and redeploying every service that has it.

    If a secret was ever committed, treat it as leaked and rotate it. Removing it from the next commit is not enough.

  8. Step 8: A secret manager hands out secrets at run time

    Store secrets in a secret manager (Vault, AWS Secrets Manager, Kubernetes secrets with encryption). The app proves who it is, fetches the secret when it starts, and re-reads it every 60 s. Only that app is allowed to read that secret, and every read is logged.

    To rotate, change the value once in the manager. Apps pick it up within 60 s with no code change and no deploy. Better still, use short-lived secrets created on demand, so a leaked one expires soon.

    Rotate on a schedule, not only after a leak, so rotation is a routine and not an emergency.

  9. Step 9: Zero trust: no machine is trusted for its place

    The old idea: a hard shell, soft inside. Once something is on the company network, it is trusted. One stolen laptop or one hacked service then reaches everything.

    Zero trust says: never trust, always verify. Every call, even between two internal services, uses mutual TLS (both sides show a certificate), is authorized for that caller and that action, and is logged. Hacking one service does not hand over the rest.

    Authenticate and authorize every request, wherever it comes from. Assume something is already breached.

  10. Step 10: Encryption and secrets in one picture

    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.