C.W.K.
Stream
Lesson 04 of 05 · published

Encryption at Rest — When Auth Isn't Enough

~15 min · encryption-at-rest, fernet, filevault

Level 0Greenhorn
0 XP0/53 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

Auth controls who can ask. Encryption controls what's readable if someone walks past auth (stolen disk, compromised host, rogue cloud admin, badly-permissioned backup).

The three tiers

TierWhat it protects againstEffort
Disk-level (FileVault, LUKS)Physical theft of the powered-off deviceOne-time, free, on by default on most modern OSes
Database-level (SQLCipher, Postgres TDE)Adversary with file access but no app accessLibrary swap; ~1 day
Field-level (encrypt sensitive columns)Rogue DBA, leaked backup, app-level XSS-extracted dumpsMost work; key management becomes the new problem

The key-management reality

Encryption pushes the security problem from "the data" to "the key". The new questions:

  • Where does the key live? (OS keychain, KMS, HSM, env var)
  • What happens during deploy / restart? (the service must fetch it on startup)
  • What happens if you lose the key? (the data is gone — forever; back up the key separately)
  • How do you rotate? (re-encrypt all data with the new key; non-trivial at scale)

What's worth encrypting at field level

Not everything. Encrypting indexes/foreign keys breaks query patterns. The pragmatic list: API keys / tokens stored on behalf of the user; personal notes / journal entries; health / financial data; session tokens (one-way hash is often fine here); anything covered by a compliance regime (HIPAA, GDPR special categories).

Code

Field-level encryption with cryptography.Fernet·python
from cryptography.fernet import Fernet

# Key from keychain, NEVER in repo
fernet = Fernet(get_keychain("APP_DATA_KEY"))

def encrypt_field(value: str) -> bytes:
    return fernet.encrypt(value.encode())

def decrypt_field(blob: bytes) -> str:
    return fernet.decrypt(blob).decode()

# DB stores blobs; app encrypts on write, decrypts on read.
# A leaked backup is unreadable without APP_DATA_KEY.

External links

Exercise

Verify FileVault is on (System Settings → Privacy & Security → FileVault) on every Mac you use for development. If off, turn it on tonight. That single setting eliminates the 'powered-off-stolen-laptop' threat from your model entirely. Free win.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.