Auditing and encryption
Overview
Audit relevant actions with identity, time, object and outcome while minimizing sensitive content. Protect audit records against unauthorized alteration and define retention. TLS protects data in transit when peer identity is verified; at-rest encryption protects stored data within its threat model. Objective: observe a transactional audit event without recording secret values.
First example
BEGIN;UPDATE sec_doc SET title='Revised' WHERE tenant=1 AND id=2;SELECT tenant,doc_id,action FROM sec_audit;ROLLBACK;Observed SQLite result (rows in JSON):
[ [ 1, 2, "title_changed" ]]How it works
The audit trigger captures document identity and action, not private_note. Its event rolls back with the change, so it is not a complete security audit of failed attempts or tamper-resistant logging. Encryption does not replace access control; manage keys separately, protect backups and avoid storing plaintext passwords. Password hashing requires purpose-built salted password KDFs, not ordinary SQL hashes. Deletion does not guarantee erasure from backups or storage media.
Practical example
SELECT COUNT(*) FROM sec_audit;Observed SQLite result (rows in JSON):
[ [ 0 ]]Practice and mistakes
Practice: update one title within a transaction and inspect audit rows before rollback. Hint 1: use both key columns. Hint 2: inspect sec_audit before and after rollback. Check: one event before, zero after. Mistake: treating this trigger as a complete audit service or as encryption.
PostgreSQL 17 example — NOT RUN
SELECT ssl,version,cipher FROM pg_stat_ssl WHERE pid=pg_backend_pid();Expected result, not executed: "Reports TLS status of this connection; does not prove certificate validation or at-rest encryption."