standard_audit 0.11.1 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 697434bf73f4588971d9fda7503f13f53a848ab4fe058fbc05be5b567a186d6f
4
- data.tar.gz: e5bad881dc6a5ab13ae22d13777176120967161f40356dcd6522ab4616492c1f
3
+ metadata.gz: 80719c54fd8a1d3859f52741cfc4a478ec670114ad5ab025b1b9b14a10a80625
4
+ data.tar.gz: d5baa91d4e29844e0e0c375e0ef6c9b3cd4f2d6eba0284696911891270f3befa
5
5
  SHA512:
6
- metadata.gz: feb2b3354808c948aff042e0a3354ac2344c3e4ac1b03f0c375645ff7b1aaf1ec27253e87e764652ee7d52710a4c7aecd7748ba13475afa001a0c27f55a2ca79
7
- data.tar.gz: a83422f4ef6b6ca319c29c450ae7cf82457cc5ad342345947a867e2243ef701c2bb0310a5b1d99f5cbf9c322b756f5094c42e77e53eb6ea3b828a834f94bfacc
6
+ metadata.gz: 3fc6d5267408dd0858b0cecac9e449c00b6ef6e75b12cb9cb74167634769cd09f95fa51805a948f9a20e100b7bbd43237f473f4508d211a8c66293c0af2bbe3f
7
+ data.tar.gz: cdb5c7130f54d124da4e2b02586e510b5a75693c729d30d495403d4e948b7d6fcc81b640cb241a138292d452ebb5a52f0d84a6a0f178e8ecb982d7a209c179ea
data/CHANGELOG.md CHANGED
@@ -7,6 +7,41 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.12.0] - 2026-09-24
11
+
12
+ ### Upgrade steps
13
+
14
+ 1. Bump the gem and run `rails generate standard_audit:add_anonymized_at && rails db:migrate` (new nullable `audit_logs.anonymized_at`; idempotent, no table rewrite, strong_migrations-safe). Optional — without it everything works as in 0.11, and anonymized rows keep failing `verify_chain`.
15
+ 2. StandardId apps: delete `config.current_actor_resolver = -> { Current.account }` and `config.current_session_id_resolver = -> { Current.session&.id }` — they are now the defaults.
16
+ 3. If your `metadata_builder` must NOT run on direct `StandardAudit.record` / `audit!` writes, make it idempotent or move that logic (see Changed).
17
+ 4. Optionally adopt `current_scope_resolver`, `before_write`, `raise: false` and `require "standard_audit/rspec"`'s `have_audited` / baseline shared example — each README section says which host code it replaces.
18
+
19
+ ### Added
20
+
21
+ - **`config.before_write = ->(entry) { … }`** — runs on every write path (direct `record`, block form, `Auditable#record_audit`, `Operation#audit!`, both subscribers; sync, async and batched), after the Current resolvers and `metadata_builder` and before dereferencing/redaction. Mutate the entry in place; raising aborts the write (direct callers see the error; subscribers rescue and report it). Replaces host wrappers such as fundbright's `AuditWriting#record_audit!` and its `audit!` PII-guard/`surface` override.
22
+ - **`config.current_scope_resolver`** (default `nil`) — fallback audit scope when a write names none. Explicit `scope:` and `scope_extractor` results always win. Applied on every write path.
23
+ - **`StandardAudit.record(..., raise: false)`** — a failed write is logged, reported to `Rails.error` as handled, and returns nil. Replaces the `AuditAuthFailure` rescue-and-report wrappers.
24
+ - **`audit_logs.anonymized_at`** and the `standard_audit:add_anonymized_at` generator; the install migration now includes the column. `AuditLog#anonymized?` and `AuditLog.anonymization_column?`.
25
+ - **`verify_chain` result gains `redacted:`** — rows stamped `anonymized_at` are counted there instead of as `digest_mismatch` failures. Their stored checksum is kept, so the chain still links through them, and their declared parent is still checked for `missing_parent`. `rake standard_audit:verify` prints the count when nonzero.
26
+ - **RSpec support:** `have_audited("event").by(actor).on(target).within(scope).with_metadata(...).once` block matcher, and the `"a standard_audit baseline"` shared example. Loaded by `require "standard_audit/rspec"`, or individually from `standard_audit/rspec/matchers` and `standard_audit/rspec/baseline`.
27
+
28
+ ### Changed
29
+
30
+ - **One write path.** Every entry point now ends in `StandardAudit.write_entry`. `StandardAudit::Subscriber` no longer re-implements the write, which has three visible effects:
31
+ - **`metadata_builder` now runs on direct `StandardAudit.record` calls** (and therefore `audit!` / `record_audit`), not only on the subscriber paths. Builders that inject context (`engine_scope`) now reach direct writes, closing the gap fundbright-web's initializer documents. A builder that is not idempotent would now see operation metadata too — review before upgrading.
32
+ - **Events handled by the ActiveSupport::Notifications subscriber inside `StandardAudit.batch` are now buffered** and flushed with the batch, like the `Rails.event` subscriber and direct calls already were. `before_checksum` hooks run for them (see Fixed).
33
+ - Subscriber failures are logged as `[StandardAudit] Error creating audit log for <event>: …` and reported with the same context as 0.11.1.
34
+ - **Default resolvers match StandardId.** `current_actor_resolver` tries `Current.account`, then `Current.user`; `current_session_id_resolver` tries `Current.session&.id`, then `Current.session_id`. Each is `respond_to?`-guarded, so `Current.user` apps are unaffected. The README's zero-config claim for StandardId is now true.
35
+ - `Rails.event` reserved metadata (`_tags`, `_source`) is still merged after `metadata_builder`, so builders never see it.
36
+
37
+ ### Fixed
38
+
39
+ - **`before_checksum` hooks now run on batched writes.** `StandardAudit.batch` flushes with `insert_all!`, which never built a model, so hooks silently skipped every batched row. The flush now runs each buffered row through the hooks (per-hook isolation included) before checksumming, so batched rows get the same derived columns as `save!` and still verify.
40
+
41
+ ### Deprecated
42
+
43
+ - **`standard_audit:add_checksums` generator** — the 0.2 → 0.3 upgrade path. It still works but warns; removal is planned.
44
+
10
45
  ## [0.11.1] - 2026-09-24
11
46
 
12
47
  ### Fixed
data/README.md CHANGED
@@ -96,7 +96,32 @@ StandardAudit.record("orders.created",
96
96
  )
97
97
  ```
98
98
 
99
- When `actor` is omitted, it falls back to the configured `current_actor_resolver` (which reads from `Current.user` by default).
99
+ When `actor` is omitted, it falls back to the configured `current_actor_resolver` (which reads `Current.account`, then `Current.user`, by default).
100
+
101
+ #### Non-fatal writes: `raise: false`
102
+
103
+ Where a missing audit row must never break the request — logging an
104
+ authentication failure, say — pass `raise: false`. A failed write is logged,
105
+ reported to `Rails.error` as handled (context
106
+ `{ <audit_error_context_key> => event_type, source: "StandardAudit.record" }`),
107
+ and `record` returns nil:
108
+
109
+ ```ruby
110
+ StandardAudit.record("auth.token_invalid",
111
+ actor: token,
112
+ metadata: { error_code: "AUTH_001", path: request.path },
113
+ raise: false)
114
+ ```
115
+
116
+ The default is `raise: true` (unchanged). In block form the option only
117
+ governs the audit write; errors from your block always propagate.
118
+
119
+ **Replace your host code with** `raise: false`. It supersedes the
120
+ `AuditAuthFailure#record_auth_failure` rescue-and-report wrapper
121
+ (sidekick-web, luminality-web, nutripod-web
122
+ `app/controllers/concerns/audit_auth_failure.rb`); set
123
+ `config.audit_error_context_key = :audit_event` to keep those apps' Sentry
124
+ grouping key.
100
125
 
101
126
  ### ActiveSupport::Notifications
102
127
 
@@ -125,6 +150,46 @@ end
125
150
 
126
151
  This uses `ActiveSupport::Notifications.instrument` under the hood.
127
152
 
153
+ ### One write path, and `before_write`
154
+
155
+ Every entry point — `StandardAudit.record` (plain and block form),
156
+ `Auditable#record_audit`, `Operation#audit!`, the ActiveSupport::Notifications
157
+ subscriber and the `Rails.event` subscriber — ends in the same write path.
158
+ Actor/session/scope resolution, `metadata_builder`, `before_write`, record
159
+ dereferencing, redaction, `StandardAudit.batch` and `async` therefore behave
160
+ identically whichever way a row is written. (Before 0.12.0 the notifications
161
+ subscriber carried its own copy: it ignored `batch`, and `metadata_builder`
162
+ never ran on direct `record` calls.)
163
+
164
+ `config.before_write` is the seam for host policy that must apply to every row:
165
+
166
+ ```ruby
167
+ config.before_write = ->(entry) {
168
+ # entry: { event_type:, actor:, target:, scope:, metadata:,
169
+ # request_id:, ip_address:, user_agent:, session_id: }
170
+ AuditMetadataPii.verify!(entry[:metadata]) if StandardAudit::Operation::Audit.verify?
171
+ if (surface = Current.audit_surface).present?
172
+ entry[:metadata] = entry[:metadata].merge(surface: surface)
173
+ end
174
+ }
175
+ ```
176
+
177
+ It runs after the `Current` resolvers and `metadata_builder`, and **before**
178
+ dereferencing and `sensitive_keys` redaction, so anything it injects is still
179
+ filtered. Mutate `entry` in place; the return value is ignored. Raising aborts
180
+ the write: direct callers see the error, the subscribers rescue and report it.
181
+
182
+ **Replace your host code with** a `before_write`. It supersedes:
183
+
184
+ - a hand-built job-side wrapper that runs a guard before `StandardAudit.record`
185
+ (fundbright-web `AuditWriting#record_audit!`);
186
+ - an `audit!` override that runs a PII guard and injects metadata
187
+ (fundbright-web `ApplicationOperation#audit!`);
188
+ - a `metadata_builder` whose injection (`engine_scope`) is documented as
189
+ missing direct writes (fundbright-web / luminality-web initializers) —
190
+ `metadata_builder` now applies to direct writes too, so no change is needed
191
+ beyond deleting the caveat.
192
+
128
193
  ## Model Concerns
129
194
 
130
195
  ### Auditable
@@ -284,6 +349,69 @@ outside `app/operations/`, which would otherwise always look orphaned.
284
349
  The registry can only see loaded classes, so the shared example calls
285
350
  `Rails.application.eager_load!` by default (`eager_load: false` to opt out).
286
351
 
352
+ ## Testing
353
+
354
+ ```ruby
355
+ # spec/rails_helper.rb
356
+ require "standard_audit/rspec"
357
+ ```
358
+
359
+ This resets StandardAudit state before every example (so declare your
360
+ configuration with `configure(baseline: true)`), and loads two helpers. Each is
361
+ also loadable on its own: `standard_audit/rspec/matchers`,
362
+ `standard_audit/rspec/baseline`.
363
+
364
+ ### `have_audited`
365
+
366
+ ```ruby
367
+ expect { Orders::Create.call(order) }
368
+ .to have_audited("order.created")
369
+ .by(account) # a record, an RSpec matcher, or no arg = "any resolvable actor"
370
+ .on(order) # target, same forms
371
+ .within(organisation) # scope, same forms
372
+ .with_metadata(total: 100, tags: include("vip")) # subset; values may be matchers
373
+ .once # or .times(n); default "at least one"
374
+
375
+ expect { noop }.not_to have_audited("order.created")
376
+ ```
377
+
378
+ Only rows persisted during the block count. On failure it lists the rows that
379
+ were written, so a wrong actor or metadata shows up directly.
380
+
381
+ **Replace your host code with** `have_audited`. It supersedes hand-rolled
382
+ helpers such as `expect_well_formed_audit(action)` (sidekick-web
383
+ `spec/support/audit_log_coverage.rb`) — the equivalent is
384
+ `have_audited(action).by.on` — and the
385
+ `change(StandardAudit::AuditLog, :count)` + `AuditLog.last` pairs across the
386
+ five apps' coverage specs.
387
+
388
+ ### `"a standard_audit baseline"`
389
+
390
+ Guards that your configuration survives the per-example reset:
391
+
392
+ ```ruby
393
+ RSpec.describe "StandardAudit configuration baseline" do
394
+ it_behaves_like "a standard_audit baseline",
395
+ subscriptions: [/\Astandard_id\./, /\Aauthorization\./],
396
+ settings: { retention_days: 1826, raise_on_audit_write_error: true, filter_nested_metadata: true },
397
+ catalogue: -> { AuditCatalogue::ACTIONS },
398
+ sensitive_keys: %i[source_payload],
399
+ sensitive_key_patterns: [/secret/i],
400
+ present: %i[metadata_builder before_write current_scope_resolver]
401
+ end
402
+ ```
403
+
404
+ It checks the baseline is registered, that each value holds, and that it is
405
+ restored after a mutation plus `reset_configuration!`. Behaviour held in
406
+ lambdas can only be checked for presence; keep an app-specific example for
407
+ anything whose *result* matters.
408
+
409
+ **Replace your host code with** the shared example. It supersedes the bulk of
410
+ each app's `spec/initializers/standard_audit_baseline_spec.rb` (or
411
+ `spec/config/…`). The `Current.account` / `Current.session&.id` resolver
412
+ examples can go too once those overrides are deleted (they are the 0.12.0
413
+ defaults).
414
+
287
415
  ## Configuration Reference
288
416
 
289
417
  Use `configure(baseline: true)` in your initializer. It remembers the block so
@@ -309,11 +437,12 @@ StandardAudit.configure(baseline: true) do |config|
309
437
  # -- Current Attribute Resolvers --
310
438
  # Fallbacks used when payload values are nil.
311
439
  # Designed to work with Rails Current attributes.
312
- config.current_actor_resolver = -> { Current.user }
440
+ # (Defaults shown in simplified form — each is respond_to?-guarded.)
441
+ config.current_actor_resolver = -> { Current.account || Current.user }
313
442
  config.current_request_id_resolver = -> { Current.request_id }
314
443
  config.current_ip_address_resolver = -> { Current.ip_address }
315
444
  config.current_user_agent_resolver = -> { Current.user_agent }
316
- config.current_session_id_resolver = -> { Current.session_id }
445
+ config.current_session_id_resolver = -> { Current.session&.id || Current.session_id }
317
446
 
318
447
  # -- Sensitive Data --
319
448
  # Keys automatically stripped from metadata. Matching is EXACT on the key
@@ -340,7 +469,7 @@ StandardAudit.configure(baseline: true) do |config|
340
469
  # Run between the UUID assignment and the checksum computation, so a hook MAY
341
470
  # set a checksummed column and the row still passes `verify_chain`. No
342
471
  # `prepend: true` needed. Each hook is rescued individually and can never fail
343
- # the audit write. Not run on the batched `insert_all!` path.
472
+ # the audit write. Also run on batched writes, at flush time (since 0.12.0).
344
473
  config.before_checksum { |log| log.scope = MyApp.derive_scope(log) }
345
474
  config.before_checksum :backfill_scope # an AuditLog instance method
346
475
 
@@ -362,9 +491,18 @@ StandardAudit.configure(baseline: true) do |config|
362
491
  }
363
492
 
364
493
  # -- Metadata Builder --
365
- # Optional proc to transform metadata before storage.
494
+ # Optional proc to transform metadata before storage. Runs on EVERY write
495
+ # path (since 0.12.0 — previously only the two subscribers).
366
496
  config.metadata_builder = ->(metadata) { metadata.slice(:relevant_key) }
367
497
 
498
+ # -- Ambient scope --
499
+ # Fallback tenant when a write names none. See "Multi-Tenancy".
500
+ config.current_scope_resolver = -> { Current.organisation }
501
+
502
+ # -- before_write --
503
+ # Runs on every write path, before redaction. See "One write path".
504
+ config.before_write = ->(entry) { entry[:metadata] = entry[:metadata].merge("surface" => Current.surface) }
505
+
368
506
  # -- Async Processing --
369
507
  # Offload audit log creation to ActiveJob.
370
508
  config.async = false
@@ -386,7 +524,26 @@ end
386
524
 
387
525
  ### Default Current Attribute Resolvers
388
526
 
389
- Out of the box, StandardAudit reads from `Current` if it responds to the relevant method. This means if your app (or an auth library like StandardId) populates `Current.user`, `Current.request_id`, etc., audit logs automatically capture request context with zero configuration.
527
+ Out of the box, StandardAudit reads from a top-level `Current` if it responds to the relevant method:
528
+
529
+ | Column | Default resolution (first non-nil wins) |
530
+ |--------------|--------------------------------------------------|
531
+ | actor | `Current.account`, then `Current.user` |
532
+ | session_id | `Current.session&.id`, then `Current.session_id` |
533
+ | request_id | `Current.request_id` |
534
+ | ip_address | `Current.ip_address` |
535
+ | user_agent | `Current.user_agent` |
536
+
537
+ The `account`/`session` pair is what StandardId's `Current` exposes, so a StandardId app gets actor and session attribution with zero configuration (since 0.12.0 — earlier versions read only `Current.user`/`Current.session_id`, which StandardId does not define). Apps following the Rails generator convention (`Current.user`) keep working unchanged.
538
+
539
+ **Replace your host code with:** nothing. If your initializer carries
540
+
541
+ ```ruby
542
+ config.current_actor_resolver = -> { Current.account }
543
+ config.current_session_id_resolver = -> { Current.session&.id }
544
+ ```
545
+
546
+ both lines are now the defaults and can be deleted.
390
547
 
391
548
  ## Query Interface
392
549
 
@@ -471,6 +628,28 @@ StandardAudit::AuditLog.for_scope(current_organisation)
471
628
 
472
629
  The scope is stored as a GlobalID string, so it works with any model class.
473
630
 
631
+ ### Ambient scope: `current_scope_resolver`
632
+
633
+ When most writes happen inside a tenant-scoped request, resolve the scope from
634
+ `Current` instead of threading it through every call:
635
+
636
+ ```ruby
637
+ config.current_scope_resolver = -> { Current.channel || Current.organisation }
638
+ ```
639
+
640
+ It is a *fallback*: an explicit `scope:` and a scope found by `scope_extractor`
641
+ always win. It applies on every write path (direct `record`, `audit!`,
642
+ `record_audit`, both subscribers; sync, async and batched). Default `nil`.
643
+
644
+ **Replace your host code with** the one line above. It supersedes:
645
+
646
+ - a `scope_extractor` that falls back to `Current` (nutripod-web:
647
+ `->(payload) { payload[:scope] || Current.channel || Current.organisation }`),
648
+ which only ever covered the subscriber path — keep `scope_extractor` for
649
+ reading the payload, move the `Current` fallback here;
650
+ - a `before_checksum` hook that back-fills `log.scope` from `Current`
651
+ (sidekick-web), which before 0.12.0 never ran on the batched path.
652
+
474
653
  ## Async Processing
475
654
 
476
655
  For high-throughput applications, offload audit log creation to a background job:
@@ -509,6 +688,37 @@ GlobalID raises `ArgumentError`.
509
688
  StandardAudit::AuditLog.anonymize_actor!("gid://myapp/User/123")
510
689
  ```
511
690
 
691
+ #### Anonymization and the checksum chain
692
+
693
+ Anonymizing rewrites checksummed columns, so an anonymized row can no longer
694
+ reproduce its own digest. Since 0.12.0 its stored `checksum` is left untouched
695
+ (the rows after it still link to it) and, when the table has an
696
+ `anonymized_at` column, the row is stamped. `verify_chain` then counts it under
697
+ `redacted` instead of reporting a `digest_mismatch`:
698
+
699
+ ```ruby
700
+ StandardAudit::AuditLog.verify_chain
701
+ # => { valid: true, verified: 5576, recovered: 0, redacted: 1, failures: [] }
702
+ ```
703
+
704
+ A redacted row's declared parent is still checked, so deleting the row before
705
+ it is still reported as `missing_parent`. Reconcile a nonzero `redacted`
706
+ against your erasure records: anyone who can write `anonymized_at` can hide an
707
+ edit behind it.
708
+
709
+ Existing installs add the column with:
710
+
711
+ ```bash
712
+ rails generate standard_audit:add_anonymized_at
713
+ rails db:migrate
714
+ ```
715
+
716
+ (nullable, no default, idempotent; safe under strong_migrations). Without it,
717
+ `anonymize_actor!` works exactly as before and anonymized rows keep failing
718
+ `verify_chain` as `digest_mismatch`. Rows anonymized before the migration are
719
+ not stamped retroactively — they are indistinguishable from tampered rows after
720
+ the fact; stamp them by hand if your erasure log identifies them.
721
+
512
722
  ### Right to Access (Export)
513
723
 
514
724
  Export all audit data for a specific user:
@@ -584,9 +794,12 @@ own digest, so editing it invalidates the row.
584
794
 
585
795
  ```ruby
586
796
  result = StandardAudit::AuditLog.verify_chain
587
- # => { valid: true, verified: 5577, recovered: 0, failures: [] }
797
+ # => { valid: true, verified: 5577, recovered: 0, redacted: 0, failures: [] }
588
798
  ```
589
799
 
800
+ `redacted` counts rows anonymized by `anonymize_actor!` — see "Anonymization
801
+ and the checksum chain".
802
+
590
803
  - `failures` carries `reason: :digest_mismatch` (the row's fields no longer
591
804
  produce its digest) or `reason: :missing_parent` (the row it was appended to
592
805
  is no longer in the log). Retention pruning does not trip `:missing_parent`:
@@ -638,6 +851,15 @@ verification — which is the point.
638
851
  re-signs rows from their current contents, so it attests only that a script ran.
639
852
  It is for rows that never had a checksum at all (pre-feature data).
640
853
 
854
+ ## Generators
855
+
856
+ | Generator | Purpose |
857
+ |-----------|---------|
858
+ | `standard_audit:install` | `audit_logs` migration + initializer (new installs) |
859
+ | `standard_audit:add_previous_checksum` | Adds `previous_checksum` (upgrading from < 0.8) |
860
+ | `standard_audit:add_anonymized_at` | Adds `anonymized_at` (upgrading from < 0.12) |
861
+ | `standard_audit:add_checksums` | **Deprecated** (0.12.0; to be removed). The 0.2 → 0.3 upgrade path; warns when run |
862
+
641
863
  ## Rake Tasks
642
864
 
643
865
  ```bash
@@ -740,10 +962,10 @@ key you want on an audit row, while the value serialises with
740
962
  happens to hold. Audit rows are append-only, so an unsafe default cannot be
741
963
  walked back.
742
964
 
743
- On the notifications path, records are dereferenced **after**
744
- `metadata_builder` runs, so a builder that needs real attributes still gets the
745
- record (`metadata_builder` has never applied to a direct `StandardAudit.record`
746
- call — pass the attributes you want in `metadata` there):
965
+ Records are dereferenced **after** `metadata_builder` (and `before_write`)
966
+ run, so a builder that needs real attributes still gets the record. Since
967
+ 0.12.0 this holds on every write path, including direct `StandardAudit.record`
968
+ calls:
747
969
 
748
970
  ```ruby
749
971
  config.metadata_builder = ->(metadata) {
@@ -95,12 +95,21 @@ module StandardAudit
95
95
  # `subject` may be a record, a GlobalID, or a GlobalID string
96
96
  # ("gid://app/User/1"). A string is parsed, never located, so erasure and
97
97
  # export still work after the subject's own row has been deleted.
98
+ #
99
+ # Anonymization rewrites CHECKSUM_FIELDS, so an anonymized row can no
100
+ # longer reproduce its own digest. Its stored `checksum` is deliberately
101
+ # left untouched, so the rows after it still link to it. When the host has
102
+ # the `anonymized_at` column (`standard_audit:add_anonymized_at`), the row
103
+ # is stamped and `verify_chain` counts it as `redacted` rather than as a
104
+ # `digest_mismatch` failure. Without the column, anonymization works
105
+ # exactly as before and such rows still fail verification.
98
106
  def self.anonymize_actor!(subject)
99
107
  gid = subject_gid_for(subject)
100
108
  logs = where("actor_gid = ? OR target_gid = ?", gid, gid)
101
109
  count = logs.count
102
110
 
103
111
  anonymizable_keys = StandardAudit.config.anonymizable_metadata_keys.map(&:to_s)
112
+ stamp = anonymization_column? ? Time.current : nil
104
113
 
105
114
  logs.find_each do |log|
106
115
  attrs = {
@@ -119,12 +128,26 @@ module StandardAudit
119
128
  attrs[:metadata] = cleaned_metadata
120
129
  end
121
130
 
131
+ attrs[:anonymized_at] = log.anonymized_at || stamp if stamp
132
+
122
133
  log.update_columns(attrs)
123
134
  end
124
135
 
125
136
  count
126
137
  end
127
138
 
139
+ # True when the table carries `anonymized_at` (the
140
+ # `standard_audit:add_anonymized_at` migration, or a 0.12+ install).
141
+ def self.anonymization_column?
142
+ column_names.include?("anonymized_at")
143
+ end
144
+
145
+ # True when this row's checksummed fields were rewritten by
146
+ # `anonymize_actor!`, so its digest is unverifiable by construction.
147
+ def anonymized?
148
+ has_attribute?(:anonymized_at) && anonymized_at.present?
149
+ end
150
+
128
151
  def self.export_for_actor(subject)
129
152
  gid = subject_gid_for(subject)
130
153
  logs = where("actor_gid = ? OR target_gid = ?", gid, gid).chronological
@@ -190,6 +213,20 @@ module StandardAudit
190
213
  OpenSSL::Digest::SHA256.hexdigest(canonical)
191
214
  end
192
215
 
216
+ # Runs the configured `before_checksum` hooks against a row that will be
217
+ # written by `insert_all!` (the `StandardAudit.batch` flush), so batched
218
+ # writes get the same derived columns as a `save!`. The row is loaded into
219
+ # an unsaved instance, the hooks run exactly as on `before_create`, and the
220
+ # resulting attributes are returned for checksumming. With no hooks
221
+ # registered the row is returned untouched and no model is built.
222
+ def self.apply_before_checksum_hooks(row)
223
+ return row if StandardAudit.config.before_checksum_hooks.blank?
224
+
225
+ log = new(row)
226
+ log.send(:run_before_checksum_hooks)
227
+ log.attributes.symbolize_keys.except(:checksum, :previous_checksum)
228
+ end
229
+
193
230
  # The checksum of the most recent row — the node a new row links to.
194
231
  def self.chain_tip_checksum
195
232
  order(created_at: :desc, id: :desc).limit(1).pick(:checksum)
@@ -204,9 +241,18 @@ module StandardAudit
204
241
  end
205
242
 
206
243
  # Verifies the integrity of the audit log. Returns a result hash with
207
- # :valid (boolean), :verified (count), :recovered (count) and :failures
208
- # (array of hashes carrying :id, :event_type, :created_at, :expected,
209
- # :actual and :reason).
244
+ # :valid (boolean), :verified (count), :recovered (count), :redacted
245
+ # (count) and :failures (array of hashes carrying :id, :event_type,
246
+ # :created_at, :expected, :actual and :reason).
247
+ #
248
+ # A row stamped `anonymized_at` (GDPR erasure via `anonymize_actor!`) is
249
+ # counted in :redacted instead of being digest-checked: its checksummed
250
+ # fields were rewritten on purpose, so its digest cannot reproduce. Its
251
+ # stored checksum is unchanged, so the rest of the chain still links
252
+ # through it, and a declared parent is still checked for presence. Treat a
253
+ # nonzero :redacted as something to reconcile against your erasure
254
+ # records — anyone able to write `anonymized_at` can also hide an edit
255
+ # behind it.
210
256
  #
211
257
  # Records are processed in (created_at, id) order. Records without a
212
258
  # checksum (pre-feature data) reset the walk — the next checksummed record
@@ -253,6 +299,7 @@ module StandardAudit
253
299
  previous_checksum = nil
254
300
  verified = 0
255
301
  recovered = 0
302
+ redacted = 0
256
303
  failures = []
257
304
  window = []
258
305
  first_row = true
@@ -265,10 +312,20 @@ module StandardAudit
265
312
  next
266
313
  end
267
314
 
268
- verified += 1
269
315
  declared = record.previous_checksum if declared_parents
270
316
 
271
- if declared.present?
317
+ if record.anonymized?
318
+ redacted += 1
319
+ # The digest cannot be checked, but the parent it declares can.
320
+ if declared.present? && check_parents && !parent_present?(declared, window, relation)
321
+ if first_row
322
+ pruned_parents << declared
323
+ elsif !pruned_parents.include?(declared)
324
+ failures << chain_failure(record, expected: nil, reason: :missing_parent)
325
+ end
326
+ end
327
+ elsif declared.present?
328
+ verified += 1
272
329
  expected = record.compute_checksum_value(previous_checksum: declared)
273
330
 
274
331
  if record.checksum != expected
@@ -286,6 +343,7 @@ module StandardAudit
286
343
  end
287
344
  end
288
345
  else
346
+ verified += 1
289
347
  expected = record.compute_checksum_value(previous_checksum: previous_checksum)
290
348
 
291
349
  if record.checksum == expected
@@ -303,7 +361,7 @@ module StandardAudit
303
361
  window.shift if window.size > recovery_window
304
362
  end
305
363
 
306
- { valid: failures.empty?, verified: verified, recovered: recovered, failures: failures }
364
+ { valid: failures.empty?, verified: verified, recovered: recovered, redacted: redacted, failures: failures }
307
365
  end
308
366
 
309
367
  # Records, for every row that does not already carry one, the parent digest
@@ -0,0 +1,20 @@
1
+ module StandardAudit
2
+ module Generators
3
+ # Adds `audit_logs.anonymized_at` (0.12.0). With it, rows erased by
4
+ # `AuditLog.anonymize_actor!` are reported by `verify_chain` as `redacted`
5
+ # rather than as `digest_mismatch` failures.
6
+ class AddAnonymizedAtGenerator < Rails::Generators::Base
7
+ include Rails::Generators::Migration
8
+ source_root File.expand_path("templates", __dir__)
9
+
10
+ def self.next_migration_number(dirname)
11
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
12
+ end
13
+
14
+ def copy_migration
15
+ migration_template "add_anonymized_at_to_audit_logs.rb.erb",
16
+ "db/migrate/add_anonymized_at_to_audit_logs.rb"
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,15 @@
1
+ class AddAnonymizedAtToAuditLogs < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>]
2
+ def change
3
+ # A nullable column with no default: a metadata-only change on PostgreSQL,
4
+ # no table rewrite, nothing for StrongMigrations to object to. Idempotent,
5
+ # so it is safe on a host that already has the column (a 0.12+ install).
6
+ #
7
+ # Rows anonymized BEFORE this migration keep a NULL stamp and still fail
8
+ # `verify_chain` as `digest_mismatch`; there is no way to tell them apart
9
+ # from tampered rows after the fact. If your erasure records identify them,
10
+ # stamp them by hand (`update_columns(anonymized_at: ...)`).
11
+ return if column_exists?(:audit_logs, :anonymized_at)
12
+
13
+ add_column :audit_logs, :anonymized_at, :datetime
14
+ end
15
+ end
@@ -1,13 +1,27 @@
1
1
  module StandardAudit
2
2
  module Generators
3
+ # DEPRECATED (0.12.0); scheduled for removal. This is the 0.2 -> 0.3
4
+ # upgrade path that added the `checksum` column. Every install since 0.3
5
+ # creates the column via `standard_audit:install`, and no known host still
6
+ # needs it. Run `standard_audit:add_previous_checksum` on hosts that
7
+ # predate 0.8 instead.
3
8
  class AddChecksumsGenerator < Rails::Generators::Base
4
9
  include Rails::Generators::Migration
5
10
  source_root File.expand_path("templates", __dir__)
6
11
 
12
+ DEPRECATION_MESSAGE = "standard_audit:add_checksums is deprecated and will be removed in a " \
13
+ "future release. It only upgrades pre-0.3 installs; the install " \
14
+ "generator has created the checksum column since 0.3.".freeze
15
+
7
16
  def self.next_migration_number(dirname)
8
17
  Time.now.utc.strftime("%Y%m%d%H%M%S")
9
18
  end
10
19
 
20
+ def warn_deprecated
21
+ say_status :deprecated, DEPRECATION_MESSAGE, :yellow
22
+ warn "[StandardAudit] DEPRECATION: #{DEPRECATION_MESSAGE}"
23
+ end
24
+
11
25
  def copy_migration
12
26
  migration_template "add_checksum_to_audit_logs.rb.erb", "db/migrate/add_checksum_to_audit_logs.rb"
13
27
  end
@@ -21,6 +21,9 @@ class CreateAuditLogs < ActiveRecord::Migration[<%= ActiveRecord::Migration.curr
21
21
  # share a parent, so the log is a DAG rather than a strict line; storing
22
22
  # the parent is what keeps every row verifiable when it forks.
23
23
  t.string :previous_checksum, limit: 64
24
+ # Set by AuditLog.anonymize_actor! (GDPR erasure). verify_chain counts a
25
+ # stamped row as `redacted` instead of a digest failure.
26
+ t.datetime :anonymized_at
24
27
  t.timestamps
25
28
  end
26
29