An audit trail must record who changed what and when, and it must be hard to alter without detection. What to capture per event - Actor (user ID, service account, impersonating admin), tenant, timestamp in UTC from the server, action type, entity type and ID, changed fields with old and new values, reason or ticket number for sensitive actions, request or trace ID, source IP and user agent. - Mask secrets and sensitive values (passwords, full card numbers), but record that they changed. Capturing changes in Java - Write the audit event in the same database transaction as the business change, so a change cannot be committed without its audit record. Options: - Explicit domain events persisted by the service layer (most control over meaning: "LimitIncreased" is more useful than a raw column diff). - Hibernate Envers (@Audited) for automatic entity revision history. - Hibernate event listeners or JPA entity listeners for field-level diffs. - For high volume, use the transactional outbox and stream events to a dedicated audit store (Kafka topic with long retention, a separate audit database, or immutable object storage). Append-only guarantees - Database permissions: the application role can INSERT into the audit table but not UPDATE or DELETE. A trigger can also reject updates and deletes. - Hash chaining: each record stores hash(previousHash + canonicalised record content). A verification job recalculates the chain and alerts on any break. Periodically anchor the latest hash in a separate system (or sign it) so the whole chain cannot be rewritten silently. - Write-once storage for archives: S3 Object Lock (compliance mode) or Azure immutable blob storage. - Separate administrative access: the people who operate the application database should not be able to modify archived audit data, and access to audit data is itself audited. Making it useful - A searchable UI for support and compliance teams: filter by entity, actor, time range and action, with readable diffs. - Correlate audit entries with application logs using the trace ID. - Retention policies meeting regulatory requirements (for example financial records), with archival of old data. Keep audit logs (business evidence, long retention, strict integrity) separate from technical logs (debugging, short retention).
Ravindra Wadile · · 0 likes