Video in progress

How do you protect personal data in the database (passport, phone, card): encryption, masking and layers of permissions?

I use several layers: disk encryption (at rest), TLS, application-side encryption of the most sensitive columns, limiting access with roles, and making sure PII doesn't end up in logs and backups. For fields that must be searchable, I store a hash next to the encrypted value.

📖 Answer

Each layer protects against a different threat, so one isn't enough.

Disk and network. Volume encryption (cloud KMS or LUKS) protects against a stolen disk or snapshot. It doesn't protect against someone who gets into the running database. Connections use sslmode=verify-full.

Column-level encryption. A passport number or card data is encrypted in the application with AES-GCM, with the key in KMS/Vault:

@Convert(converter = EncryptedStringConverter.class)
@Column(name = "passport_no_enc")
private String passportNo;

@Column(name = "passport_no_hash")   // for searching: HMAC-SHA256(key, value)
private String passportNoHash;

Data read by a DBA, through a backup, or via SQL injection stays encrypted. The cost: no LIKE, sorting or range queries on that column. For exact-match search you need a keyed HMAC, not a plain hash: a small set like phone numbers is easy to brute-force from a plain hash.

Encrypting in the database with pgcrypto is also possible, but the key travels to the server inside the SQL query and may end up in the log through log_statement.

Permissions. The application role gets only the tables it needs, the reporting role a view without the PII columns. Personal access to prod is temporary only, and audited.

Leaks around the edges. Hibernate's SQL log with its parameters, pg_stat_statements, the slow query log, values in error messages, a prod dump copied to a test environment: in practice PII often leaks from these places. I design key rotation in advance too: next to the encrypted value I store the key version.