Audit trail
Custodian records what happens to personal data in the gdpr.audit table. That includes writes, reads, refused reads,
retention, data-subject requests and break-glass access. This page covers what an entry holds, what it never holds, how the actor differs from the person acted for, how
the hash chain detects tampering, and how system runs are audited.
What an entry holds
An entry says who did what to which kind of data, for which purpose. It holds ids, names and counts.
[Theory, EachProvider]
public async Task Signing_up_a_customer_is_audited_with_the_names_of_the_fields_written(DatabaseProvider provider)
{
await using var shops = await servers.OpenShop(provider);
await shops.Nordic.SignUp(Customers.Astrid());
var trail = await shops.Nordic.PrivacyOffice.AuditTrail();
trail.ShouldRecord(
AuditOperation.Insert,
nameof(Customer),
fields: [nameof(Customer.Name), nameof(Customer.Email), nameof(Customer.Phone)]);
}
The entry for that sign-up names the fields Name, Email and Phone. It does not hold what was written to them.
Each AuditEntry wraps an AuditEvent with these members:
| Member | Holds |
|---|---|
Tenant |
The tenant id. |
Actor |
The id of the user or system principal who acted. |
DbRole |
The database role the connection used. |
Purpose |
The declared purpose. |
Subject |
The data subject's id, when one subject is concerned. |
OnBehalfOf, Grant |
The customer and the grant of an assisted session. |
Run |
The id of a system run. |
Entity |
The entity name. |
Operation |
What happened, as an AuditOperation. |
Fields |
The names of the fields touched. |
Count |
How many rows. |
BreakGlass |
Whether the access was a break-glass override. |
CorrelationId |
The id that ties together one request or job. |
The entry adds its Sequence, a Timestamp, the PreviousHash and its own Hash.
AuditOperation covers reads and writes (Read, Insert, Update, Delete), refusals (PurposeViolation,
ErasureRefused), retention (Purge, Anonymise, Shred), data-subject rights (Report, Export, Rectify,
Restrict, Erase, Withdraw), assisted sessions, break-glass items, legal holds and SystemRunCompleted.
A refused read is recorded too. See
A_refused_read_is_audited_as_a_purpose_violation.
What an entry never holds
An entry never holds a personal value and never a plaintext identifier such as an email address. A person appears
only as a subject id. AuditEvent has no member that could carry a value, and a unit test keeps it that way
(Invariant_2_an_audit_event_has_no_member_that_can_carry_a_personal_value).
Both samples check the stored table cell by cell after a day of work.
- In the Shop,
No_personal_value_ever_reaches_the_audit_table. - In the Clinic,
After_a_morning_of_work_the_audit_table_holds_none_of_Gretas_personal_values.
The same holds for the other gdpr tables and for log output. See
After_a_busy_day_on_the_ward_no_gdpr_table_holds_a_personal_value_of_a_subject
and The_log_output_of_a_busy_day_on_the_ward_holds_no_personal_value.
Keep it that way in what you pass to Custodian. A purpose name, a role name or a break-glass reason code is stored as given, so none of them may contain personal data.
Reading the trail
IAuditLog reads and verifies the audit trail of the current tenant. The Shop tests wrap it for the privacy officer.
public Task<AuditTrail> AuditTrail(AuditQuery? query = null) =>
_work.Use<IAuditLog, AuditTrail>(async log =>
{
var entries = new List<AuditEntry>();
await foreach (var entry in log.ReadAsync(query, Cancellation))
{
entries.Add(entry);
}
return new AuditTrail(entries);
});
public Task<AuditVerification> VerifyAuditChain() =>
_work.Use<IAuditLog, AuditVerification>(log => log.VerifyAsync(Cancellation));
AuditQuery narrows a read by subject, actor, operation, correlation id and time range.
Reading the trail needs a signed-in user whose role is mapped to DbRole.Privacy. Anyone else is refused. See
Only_a_privacy_officer_reads_the_audit_trail.
Which reads are audited
Writes are always audited. By default every read is audited too. On a busy system you can audit a share of the reads
of ordinary personal data with o.Audit.SampleReads(rate), a number between 0 and 1. Reads of special-category fields
and break-glass reads are always audited, whatever the rate. With the rate at zero, a read
of health data is still recorded
(Checking_whether_a_clinician_treats_the_patient_is_audited_as_a_read_of_health_data).
Actor and on-behalf-of
The actor is always the person or machine that did the work. In an assisted session a customer-service representative
works on one customer's account. The representative is the Actor. The customer is OnBehalfOf. Nothing is recorded
as if the customer did it.
public void ShouldAttributeEveryAssistedEntryTo(StaffMember representative, ShopCustomer onBehalfOf)
{
var assisted = _entries.Where(entry => entry.Event.Grant is not null).ToList();
Assert.NotEmpty(assisted);
Assert.All(assisted, entry =>
{
Assert.Equal(representative.Actor, entry.Event.Actor);
Assert.Equal(onBehalfOf.Subject, entry.Event.OnBehalfOf);
});
}
The test Every_audit_entry_of_an_assisted_session_names_the_representative_as_actor_and_the_customer_as_on_behalf_of
runs that assertion. Assisted access covers the sessions themselves.
The hash chain
Each tenant has its own chain. Entries are numbered from 1. An entry's hash is SHA-256 over the previous entry's hash,
the sequence number, the timestamp and every field of the event. Changing any of them changes the hash
(Changing_any_event_field_changes_the_hash).
VerifyAsync walks the chain and recomputes each hash. It returns an AuditVerification with Intact,
EntriesChecked and FirstBrokenSequence. In the Clinic test someone with direct database access edits one row.
[Theory, EachProvider]
public async Task Editing_an_audit_row_breaks_the_chain_at_that_row(DatabaseProvider provider)
{
await using var clinic = await ClinicScenario.OpenAsync(servers, provider);
var greta = await clinic.Admit(Patients.Greta, toCareOf: Staff.DrLind);
await clinic.As(Staff.DrBerg).OpenRecordOf(greta);
var breakGlassRead = (await clinic.As(Staff.PrivacyOfficer).AuditTrail()).First(entry => entry.Event.BreakGlass);
await clinic.TamperWithAuditEntry(breakGlassRead.Sequence);
var verification = await clinic.As(Staff.PrivacyOfficer).VerifyAuditChain();
Assert.Equal((false, breakGlassRead.Sequence), (verification.Intact, verification.FirstBrokenSequence));
}
Verification finds these kinds of tampering:
- An edited entry. It is reported as the first broken one
(
An_edited_entry_is_the_first_broken_one). - A removed entry. The chain breaks at the entry after the gap
(
A_removed_entry_breaks_the_chain_at_the_entry_after_the_gap). - Reordered entries (
Reordered_entries_break_the_chain).
An untouched chain verifies
(The_audit_hash_chain_verifies).
The chain is tamper-evident. It does not stop someone with owner access to the database from changing rows. It makes
the change visible the next time the chain is verified. Run the check on a schedule with custodian audit verify,
which exits with code 2 on a broken chain. See Command-line tool.
System runs
A batch job does not write one entry per row. A system principal opens a run with a declared purpose, and the run is audited once when it ends, with counts.
[Theory, EachProvider]
public async Task A_label_run_is_audited_once_with_the_number_of_parcels_it_read(DatabaseProvider provider)
{
await using var shops = await servers.OpenShop(provider);
var astrid = await shops.Nordic.SignUp(Customers.Astrid());
await shops.Nordic.ShipParcelTo(astrid);
await shops.Nordic.ShipParcelTo(astrid);
var printed = await shops.Nordic.PrintShippingLabels();
var trail = await shops.Nordic.PrivacyOffice.AuditTrail();
var entry = Assert.Single(trail.ForRun(printed.Run));
Assert.Equal(AuditOperation.SystemRunCompleted, entry.Event.Operation);
Assert.Equal(2, entry.Event.Count);
}
The entry has the operation SystemRunCompleted, the run's id in Run and the rows touched in Count. A run that
works across tenants gets one entry in the chain of each tenant it touched. See
A_run_is_audited_once_with_counts_per_entity_not_once_per_row
and An_all_tenants_run_sees_every_tenant_and_is_audited_once_in_each_tenant_it_touched.
System runs covers the principals.
External sinks
o.Audit.AddSink<TSink>() also ships each entry to an IAuditSink of yours, for a log store or a SIEM. The database
remains the record.
- A sink needs a validly signed licence key. Without one the sink is not called and the database audit is still
written. See
Without_a_valid_licence_the_sink_is_not_called_and_the_database_audit_is_still_written. - A sink that throws never fails the write it audits. See
A_failing_sink_never_fails_the_write_it_audits.