orfeas_lyra 0.9.3 → 1.0.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.
Files changed (93) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1923 -17
  3. data/README.md +15 -8
  4. data/Rakefile +40 -1
  5. data/app/controllers/lyra/application_controller.rb +108 -0
  6. data/app/controllers/lyra/dashboard_controller.rb +17 -15
  7. data/app/controllers/lyra/flow_controller.rb +30 -33
  8. data/app/controllers/lyra/privacy_controller.rb +57 -18
  9. data/app/views/lyra/dashboard/discrepancies.html.erb +5 -5
  10. data/app/views/lyra/dashboard/index.html.erb +1 -0
  11. data/app/views/lyra/flow/crud_mapping.html.erb +1 -1
  12. data/app/views/lyra/privacy/pii_detection.html.erb +1 -1
  13. data/app/views/lyra/privacy/policy.html.erb +7 -0
  14. data/app/views/lyra/privacy/rights.html.erb +78 -0
  15. data/config/privacy_policies.rb +0 -3
  16. data/config/routes.rb +1 -0
  17. data/lib/lyra/access_log.rb +187 -6
  18. data/lib/lyra/advisory_lock.rb +236 -0
  19. data/lib/lyra/aggregate.rb +1 -1
  20. data/lib/lyra/associations/habtm_links.rb +382 -19
  21. data/lib/lyra/associations/maintenance.rb +229 -0
  22. data/lib/lyra/attribution.rb +48 -0
  23. data/lib/lyra/bypass_events.rb +426 -45
  24. data/lib/lyra/command_handler.rb +19 -14
  25. data/lib/lyra/configuration.rb +237 -10
  26. data/lib/lyra/consistency/read_your_writes.rb +4 -2
  27. data/lib/lyra/correlation.rb +121 -0
  28. data/lib/lyra/domain_events.rb +4 -0
  29. data/lib/lyra/dual_view.rb +53 -16
  30. data/lib/lyra/encrypted_attributes.rb +5 -0
  31. data/lib/lyra/engine.rb +67 -0
  32. data/lib/lyra/erasure.rb +1002 -116
  33. data/lib/lyra/event.rb +35 -1
  34. data/lib/lyra/event_data.rb +38 -0
  35. data/lib/lyra/event_flow.rb +31 -15
  36. data/lib/lyra/event_store_adapter.rb +28 -0
  37. data/lib/lyra/event_window.rb +58 -0
  38. data/lib/lyra/genesis.rb +548 -44
  39. data/lib/lyra/interceptors/association_interceptor.rb +689 -34
  40. data/lib/lyra/interceptors/crud_interceptor.rb +547 -95
  41. data/lib/lyra/internal.rb +26 -0
  42. data/lib/lyra/mode_sync.rb +51 -3
  43. data/lib/lyra/mode_transition.rb +245 -34
  44. data/lib/lyra/model_selection.rb +35 -0
  45. data/lib/lyra/own_connection.rb +17 -2
  46. data/lib/lyra/privacy/adapters/pam.rb +25 -2
  47. data/lib/lyra/privacy/gdpr_compliance.rb +264 -36
  48. data/lib/lyra/privacy/interface.rb +27 -7
  49. data/lib/lyra/privacy/subject_links.rb +143 -0
  50. data/lib/lyra/privacy/subject_records.rb +369 -0
  51. data/lib/lyra/projection.rb +7 -1
  52. data/lib/lyra/projections/async_checkpoint.rb +32 -0
  53. data/lib/lyra/projections/async_projection_job.rb +166 -23
  54. data/lib/lyra/projections/cached_joins.rb +33 -18
  55. data/lib/lyra/projections/cached_projection.rb +74 -33
  56. data/lib/lyra/projections/cached_relation.rb +3175 -321
  57. data/lib/lyra/projections/default_scopes.rb +254 -0
  58. data/lib/lyra/projections/deferred_relation.rb +23 -3
  59. data/lib/lyra/projections/event_store_reader.rb +47 -8
  60. data/lib/lyra/projections/foreign_keys.rb +92 -0
  61. data/lib/lyra/projections/lazy_projection.rb +165 -27
  62. data/lib/lyra/projections/model_projection.rb +165 -14
  63. data/lib/lyra/projections/rebuild.rb +34 -6
  64. data/lib/lyra/projections/reference_links.rb +463 -72
  65. data/lib/lyra/projections/typed_values.rb +629 -0
  66. data/lib/lyra/projections/unique_indexes.rb +595 -0
  67. data/lib/lyra/projections/uniqueness.rb +784 -0
  68. data/lib/lyra/projections/value_digest.rb +93 -0
  69. data/lib/lyra/purpose_bound_reads.rb +912 -77
  70. data/lib/lyra/repair.rb +119 -28
  71. data/lib/lyra/retention.rb +114 -8
  72. data/lib/lyra/rights/consent_store.rb +415 -0
  73. data/lib/lyra/rights/erasure_guard.rb +135 -0
  74. data/lib/lyra/rights/pending.rb +74 -0
  75. data/lib/lyra/rights/queries.rb +269 -0
  76. data/lib/lyra/rights/store.rb +512 -0
  77. data/lib/lyra/rights/subject_keys.rb +224 -0
  78. data/lib/lyra/rights.rb +281 -0
  79. data/lib/lyra/saves_without_effect.rb +69 -0
  80. data/lib/lyra/stream_tail.rb +319 -0
  81. data/lib/lyra/strict_data_access.rb +906 -107
  82. data/lib/lyra/temporal.rb +4 -2
  83. data/lib/lyra/touch_registry.rb +404 -0
  84. data/lib/lyra/verification/bypass_workflow.rb +452 -42
  85. data/lib/lyra/verification/rights_workflows.rb +170 -0
  86. data/lib/lyra/version.rb +1 -1
  87. data/lib/lyra/visualization/event_graph.rb +1 -1
  88. data/lib/lyra/visualization/timeline.rb +2 -2
  89. data/lib/lyra.rb +43 -2
  90. data/lib/tasks/lyra_mode.rake +107 -8
  91. data/lib/tasks/lyra_projections.rake +43 -2
  92. metadata +34 -4
  93. data/lib/lyra/associations/event_aware.rb +0 -224
@@ -0,0 +1,415 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lyra
4
+ module Rights
5
+ # PamDsl.consent_store, durable: consent (the CS of PAM's runtime context)
6
+ # in Lyra's event store, shared by every policy and every process.
7
+ #
8
+ # Each request, grant and withdrawal is an event (ConsentRequested,
9
+ # ConsentGranted, ConsentWithdrawn) in Lyra::Consent$<subject key>, linked
10
+ # to the index stream Lyra::Consent, appended with the subject stream's
11
+ # expected version, and lyra_consents (one row per subject key and
12
+ # purpose: the record's state, its times and expires_at) is rewritten in
13
+ # the same transaction. The transitions are PAM's (PamDsl::ConsentRecord):
14
+ # a request while one is pending or granted is refused, a grant on a
15
+ # pending record grants it and otherwise starts a fresh one (a grant that
16
+ # repeats the granted record's times records nothing), a withdrawal of
17
+ # anything but a granted record is refused, and of nothing is a no-op.
18
+ # Expiry is not an event: the grant records expires_at (from the
19
+ # requirement's expires_in), and the state is :expired once it has passed,
20
+ # as in PAM (the guard of t2).
21
+ #
22
+ # Whose consent. A grant is recorded under the subject's first key (a
23
+ # record's own key, or the subject as a string). A lookup (record_for:
24
+ # Condition 4, and the :consent exception of Art. 18(2)) for a record
25
+ # answers the record of the one person it names (Lyra::Rights::SubjectKeys),
26
+ # or its own key's when it names no one. A record that names more than one
27
+ # person raises SharedRecordConsentError: whose consent such a record needs
28
+ # (all of them, or each for the fields about them) is not decided in 1.0.0,
29
+ # so the access is refused rather than allowed on one person's consent.
30
+ # Any other subject answers its key's record.
31
+ #
32
+ # Lookups are remembered per process for config.rights_refresh_interval
33
+ # seconds, together with whether any consent at all is recorded for a
34
+ # subject type and purpose (so a read of many records whose type has none
35
+ # costs one query): a withdrawal made in another process applies here
36
+ # within that interval. A transaction that records consent reads the keys
37
+ # it wrote afresh until it ends, and nothing it reads is remembered for
38
+ # other threads (see Rights::Pending).
39
+ class ConsentStore
40
+ include Pending
41
+
42
+ # Consent is checked for a record that names several people.
43
+ class SharedRecordConsentError < PamDsl::ConsentRequiredError; end
44
+
45
+ COLUMNS = %w[subject_key subject_type subject_id purpose state requested_at granted_at expires_at
46
+ withdrawn_at stream_version updated_at].freeze
47
+ CACHE_LIMIT = 10_000
48
+ RETRIES = 3
49
+
50
+ attr_reader :generation
51
+
52
+ def initialize(event_store: nil)
53
+ @event_store = event_store
54
+ @mutex = Mutex.new
55
+ @cache = {}
56
+ @generation = 0
57
+ @table_ready = false
58
+ init_pending
59
+ end
60
+
61
+ def event_store
62
+ @event_store || Lyra.config.event_store
63
+ end
64
+
65
+ def durable?
66
+ true
67
+ end
68
+
69
+ # ── PAM's ConsentStore interface ──
70
+
71
+ def request(purpose:, subject:)
72
+ purpose = purpose.to_sym
73
+ key = key_for!(subject)
74
+ record!(key, purpose) do |current|
75
+ if current && %i[pending granted].include?(current.state)
76
+ raise PamDsl::Error,
77
+ "Cannot request consent: record for (#{purpose}, #{subject}) already in state '#{current.state}'"
78
+ end
79
+ ["ConsentRequested", { at: Time.current }]
80
+ end
81
+ end
82
+
83
+ def grant(purpose:, subject:, granted_at: Time.current, expires_at: nil)
84
+ purpose = purpose.to_sym
85
+ key = key_for!(subject)
86
+ record!(key, purpose) do |current|
87
+ next nil if repeated_grant?(current, granted_at, expires_at)
88
+
89
+ ["ConsentGranted", { at: Time.current, granted_at: granted_at, expires_at: expires_at }]
90
+ end
91
+ end
92
+
93
+ def withdraw(purpose:, subject:)
94
+ purpose = purpose.to_sym
95
+ key = key_for!(subject)
96
+ record!(key, purpose) do |current|
97
+ next nil unless current
98
+
99
+ unless current.state == :granted
100
+ raise PamDsl::Error,
101
+ "Cannot withdraw: consent for (#{purpose}, #{subject}) is in state '#{current.state}'"
102
+ end
103
+ ["ConsentWithdrawn", { at: Time.current }]
104
+ end
105
+ end
106
+
107
+ def record_for(purpose, subject)
108
+ purpose = purpose.to_sym
109
+ keys = PamDsl.subject_keys(subject)
110
+ return nil if keys.empty?
111
+
112
+ people = people_of(subject, keys)
113
+ return lookup(purpose, keys.first) if people.empty?
114
+ return lookup(purpose, people.first) if people.size == 1
115
+
116
+ raise SharedRecordConsentError,
117
+ "Consent for purpose '#{purpose}' cannot be checked for #{keys.first}: the record names several people " \
118
+ "(#{people.join(', ')}), and consent for a record shared by several people is not supported yet"
119
+ end
120
+
121
+ def granted?(purpose, subject)
122
+ record_for(purpose, subject)&.state == :granted
123
+ end
124
+
125
+ # ── For the consent audit ──
126
+
127
+ # The consent records kept under +subject_key+, one per purpose, with
128
+ # their histories: what GDPRCompliance#consent_audit lists.
129
+ def consents_for(subject_key)
130
+ events = events_for(subject_key)
131
+ events.group_by { |event| event[:purpose] }.map do |purpose, list|
132
+ record = fold(list, purpose, subject_key)
133
+ {
134
+ purpose: purpose, subject_key: subject_key, state: record&.state,
135
+ requested_at: list.select { |e| e[:type] == "ConsentRequested" }.last&.dig(:at),
136
+ granted_at: record&.granted_at, expires_at: record&.expires_at, withdrawn_at: record&.withdrawn_at,
137
+ history: list.map { |e| e.slice(:type, :at, :granted_at, :expires_at) }
138
+ }
139
+ end
140
+ end
141
+
142
+ # The subject's consent events, oldest first, as hashes.
143
+ def events_for(subject_key)
144
+ PurposeBoundReads.internal(INTERNAL) do
145
+ event_store.read.stream(Rights.consent_stream_for(subject_key)).to_a.map { |record| decode(record) }
146
+ end
147
+ end
148
+
149
+ # Forget remembered lookups.
150
+ def forget!
151
+ @mutex.synchronize { @cache = {} }
152
+ end
153
+
154
+ # Rewrites lyra_consents from the index stream. Returns the rows written.
155
+ def rebuild!
156
+ ensure_table!
157
+ written = 0
158
+ keys = []
159
+ ActiveRecord::Base.transaction(requires_new: true) do
160
+ ActiveRecord::Base.with_connection { |c| Rights.lock_table(c, CONSENT_TABLE) }
161
+ keys = event_store.read.stream(CONSENT_STREAM).to_a.map { |record| decode(record)[:subject_key] }.uniq
162
+ ActiveRecord::Base.with_connection { |c| c.execute("DELETE FROM #{CONSENT_TABLE}") }
163
+ keys.each do |key|
164
+ events = events_for(key)
165
+ events.map { |e| e[:purpose] }.uniq.each do |purpose|
166
+ list = events.select { |e| e[:purpose] == purpose }
167
+ write_row(key, purpose, fold(list, purpose, key), list, events.size)
168
+ written += 1
169
+ end
170
+ end
171
+ end
172
+ changed!(keys.map { |key| [nil, key] })
173
+ written
174
+ end
175
+
176
+ private
177
+
178
+ def key_for!(subject)
179
+ key = PamDsl.subject_keys(subject).first
180
+ raise ArgumentError, "Consent is given by one data subject: #{subject.inspect} names none" unless key
181
+
182
+ key
183
+ end
184
+
185
+ # The people a record names, when Lyra's resolver mapped it.
186
+ def people_of(subject, keys)
187
+ return [] unless PamDsl.subject_key_resolver.equal?(SubjectKeys) && subject.is_a?(ActiveRecord::Base)
188
+
189
+ keys.drop(1)
190
+ end
191
+
192
+ def repeated_grant?(current, granted_at, expires_at)
193
+ current&.state == :granted && same_time?(current.granted_at, granted_at) &&
194
+ same_time?(current.expires_at, expires_at)
195
+ end
196
+
197
+ def same_time?(a, b)
198
+ return a.nil? && b.nil? if a.nil? || b.nil?
199
+
200
+ (a.to_r - b.to_r).abs < Rational(1, 1_000_000)
201
+ end
202
+
203
+ # Read the subject's stream, fold the purpose's record, ask the block for
204
+ # the event to append ([type, data], or nil for none), append it with the
205
+ # stream's expected version and rewrite the row. Retried when another
206
+ # process appended to the subject's stream in between.
207
+ def record!(key, purpose)
208
+ attempts = 0
209
+ begin
210
+ events = events_for(key)
211
+ current = fold(events.select { |e| e[:purpose] == purpose }, purpose, key)
212
+ type, data = yield(current)
213
+ return current unless type
214
+
215
+ append(key, purpose, type, data, events)
216
+ rescue PamDsl::RightsConcurrencyError
217
+ attempts += 1
218
+ retry if attempts < RETRIES
219
+ raise
220
+ end
221
+ end
222
+
223
+ def append(key, purpose, type, data, events)
224
+ ensure_table!
225
+ record = Events.const_get(type).new(
226
+ data: {
227
+ "subject_key" => key, "purpose" => purpose.to_s,
228
+ "at" => Codec.encode(data[:at]),
229
+ "granted_at" => Codec.encode(data[:granted_at]), "expires_at" => Codec.encode(data[:expires_at])
230
+ },
231
+ metadata: { source: "lyra_consent", correlation_id: Lyra::Correlation.current_id }.compact
232
+ )
233
+ all = events + [decode(record)]
234
+ list = all.select { |e| e[:purpose] == purpose }
235
+ folded = fold(list, purpose, key)
236
+ ActiveRecord::Base.transaction(requires_new: true) do
237
+ event_store.append([record], stream_name: Rights.consent_stream_for(key),
238
+ expected_version: events.empty? ? :none : events.size - 1)
239
+ event_store.link([record.event_id], stream_name: CONSENT_STREAM)
240
+ write_row(key, purpose, folded, list, all.size)
241
+ end
242
+ changed!([[purpose, key]])
243
+ folded
244
+ rescue RubyEventStore::WrongExpectedEventVersion, ActiveRecord::RecordNotUnique
245
+ raise PamDsl::RightsConcurrencyError, "The consent of #{key} changed while this request was handled"
246
+ end
247
+
248
+ # The purpose's current record from its events (PAM's transitions,
249
+ # without re-checking them against the clock).
250
+ def fold(events, purpose, key)
251
+ events.reduce(nil) do |record, event|
252
+ case event[:type]
253
+ when "ConsentRequested"
254
+ PamDsl::ConsentRecord.restore(purpose: purpose, subject: key, state: :pending)
255
+ when "ConsentGranted"
256
+ PamDsl::ConsentRecord.restore(purpose: purpose, subject: key, state: :granted,
257
+ granted_at: event[:granted_at], expires_at: event[:expires_at])
258
+ when "ConsentWithdrawn"
259
+ next record unless record
260
+
261
+ PamDsl::ConsentRecord.restore(purpose: purpose, subject: key, state: :withdrawn,
262
+ granted_at: record.granted_at, expires_at: record.expires_at,
263
+ withdrawn_at: event[:at])
264
+ else record
265
+ end
266
+ end
267
+ end
268
+
269
+ def decode(record)
270
+ data = record.data.to_h.transform_keys(&:to_s)
271
+ {
272
+ type: record.class.name.demodulize, subject_key: data["subject_key"], purpose: data["purpose"]&.to_sym,
273
+ at: Codec.decode(data["at"]), granted_at: Codec.decode(data["granted_at"]),
274
+ expires_at: Codec.decode(data["expires_at"]), event_id: record.event_id
275
+ }
276
+ end
277
+
278
+ def restore(row)
279
+ PamDsl::ConsentRecord.restore(
280
+ purpose: row["purpose"], subject: row["subject_key"], state: row["state"],
281
+ granted_at: time(row["granted_at"]), expires_at: time(row["expires_at"]),
282
+ withdrawn_at: time(row["withdrawn_at"])
283
+ )
284
+ end
285
+
286
+ def time(value)
287
+ return nil if value.nil?
288
+ return value.to_time if value.respond_to?(:to_time) && !value.is_a?(String)
289
+
290
+ ActiveRecord::Type::DateTime.new.cast(value)&.to_time
291
+ end
292
+
293
+ # The record of (purpose, key): read afresh when this connection's open
294
+ # transaction wrote it, else remembered for the refresh interval.
295
+ def lookup(purpose, key)
296
+ pending = pending_here
297
+ if pending&.include?([purpose, key]) || pending&.include?([nil, key])
298
+ row = fetch_row(purpose, key)
299
+ return row && restore(row)
300
+ end
301
+ return nil unless any_for_type?(purpose, key, pending)
302
+
303
+ row = remembered([purpose, key], pending) { fetch_row(purpose, key) }
304
+ row && restore(row)
305
+ end
306
+
307
+ # Whether any consent for +purpose+ is recorded for the key's type (a
308
+ # bare key, with no type, is always looked up).
309
+ def any_for_type?(purpose, key, pending)
310
+ type, _id = Rights.split_key(key)
311
+ return true if type.nil?
312
+
313
+ remembered([:type, purpose, type], pending) do
314
+ next false unless table_ready?
315
+
316
+ ActiveRecord::Base.with_connection do |c|
317
+ !c.select_value("SELECT 1 FROM #{CONSENT_TABLE} WHERE purpose = #{c.quote(purpose.to_s)} " \
318
+ "AND subject_type = #{c.quote(type)} LIMIT 1").nil?
319
+ end
320
+ end
321
+ rescue ActiveRecord::NoDatabaseError, ActiveRecord::ConnectionNotEstablished
322
+ false
323
+ end
324
+
325
+ # The cached value for +cache_key+, or the block's, remembered unless
326
+ # this connection's open transaction wrote consent (what it reads may
327
+ # yet roll back).
328
+ def remembered(cache_key, pending)
329
+ entry = @cache[cache_key]
330
+ return entry[0] if entry && Rights.current?(entry[1])
331
+
332
+ value = yield
333
+ return value if pending
334
+
335
+ @mutex.synchronize do
336
+ @cache = {} if @cache.size >= CACHE_LIMIT
337
+ @cache[cache_key] = [value, Rights.now]
338
+ end
339
+ value
340
+ end
341
+
342
+ def fetch_row(purpose, key)
343
+ return nil unless table_ready?
344
+
345
+ ActiveRecord::Base.with_connection do |c|
346
+ c.select_all("SELECT * FROM #{CONSENT_TABLE} WHERE subject_key = #{c.quote(key)} " \
347
+ "AND purpose = #{c.quote(purpose.to_s)}").to_a.first
348
+ end
349
+ rescue ActiveRecord::NoDatabaseError, ActiveRecord::ConnectionNotEstablished
350
+ nil
351
+ end
352
+
353
+ def write_row(key, purpose, record, list, version)
354
+ type, id = Rights.split_key(key)
355
+ ActiveRecord::Base.with_connection do |c|
356
+ c.execute("DELETE FROM #{CONSENT_TABLE} WHERE subject_key = #{c.quote(key)} AND purpose = #{c.quote(purpose.to_s)}")
357
+ next unless record
358
+
359
+ state = if record.withdrawn_at then :withdrawn elsif record.granted_at then :granted else :pending end
360
+ requested_at = list.select { |e| e[:type] == "ConsentRequested" }.last&.dig(:at)
361
+ values = [key, type, id, purpose.to_s, state.to_s, requested_at,
362
+ record.granted_at, record.expires_at, record.withdrawn_at, version, Time.current]
363
+ c.execute("INSERT INTO #{CONSENT_TABLE} (#{COLUMNS.join(', ')}) VALUES " \
364
+ "(#{values.map { |v| Rights.quote(c, v) }.join(', ')})")
365
+ end
366
+ end
367
+
368
+ # A change by this thread: its keys are read afresh through its
369
+ # connection until its transaction ends; then nothing remembered stands.
370
+ def changed!(pairs)
371
+ @mutex.synchronize do
372
+ @generation += 1
373
+ @cache = {}
374
+ end
375
+ Rights.note_write!
376
+ mark_pending(pairs)
377
+ end
378
+
379
+ def settled(_pairs)
380
+ forget!
381
+ end
382
+
383
+ def table_ready?
384
+ return true if @table_ready
385
+
386
+ @table_ready = Rights.table_exists?(CONSENT_TABLE)
387
+ end
388
+
389
+ def ensure_table!
390
+ return if @table_ready && table_ready?
391
+
392
+ ready = Rights.with_table(CONSENT_TABLE) do |c|
393
+ c.create_table(CONSENT_TABLE) do |t|
394
+ t.string :subject_key, null: false
395
+ t.string :subject_type
396
+ t.string :subject_id
397
+ t.string :purpose, null: false
398
+ t.string :state, null: false
399
+ t.datetime :requested_at, precision: 6
400
+ t.datetime :granted_at, precision: 6
401
+ t.datetime :expires_at, precision: 6
402
+ t.datetime :withdrawn_at, precision: 6
403
+ t.integer :stream_version, null: false
404
+ t.datetime :updated_at, precision: 6, null: false
405
+ end
406
+ c.add_index CONSENT_TABLE, %i[subject_key purpose], unique: true
407
+ c.add_index CONSENT_TABLE, %i[purpose subject_type]
408
+ end
409
+ raise ActiveRecord::ConnectionNotEstablished, "no database for #{CONSENT_TABLE}" unless ready
410
+
411
+ @table_ready = true
412
+ end
413
+ end
414
+ end
415
+ end
@@ -0,0 +1,135 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lyra
4
+ module Rights
5
+ # Lyra::Erasure.erase! for a restricted subject (Art. 18 with Art. 17).
6
+ #
7
+ # A restriction asks the controller to keep the data and to process it
8
+ # only for storage (Art. 18(2)): under Art. 18(1)(b) the subject opposed
9
+ # erasure and asked for restriction instead, under 18(1)(c) the subject
10
+ # needs the data for legal claims. Erasing it is processing, so erase!
11
+ # refuses a record that a restriction in force covers: a restriction of
12
+ # the record itself, or of a person it names (Rights::SubjectKeys), or of
13
+ # a key passed as access_log_subjects:; for all of the subject's data, or
14
+ # for a field being erased (+fields:+, or every personal field when none
15
+ # are given). An objection's 18(1)(d) restriction covers one purpose and
16
+ # does not refuse an erasure (Art. 17(1)(c) may be why the data goes).
17
+ #
18
+ # The refusal is a RestrictedProcessingError naming each restriction.
19
+ # When the controller has decided to erase all the same (a later Art. 17
20
+ # request that prevails, the claims settled), it records the Art. 18(3)
21
+ # notice (PamDsl.rights.notify_lift!) and calls erase! with +lifting:+
22
+ # the restriction's id, or ids: each is lifted (reason :erased, by
23
+ # +erased_by+) and the record erased in one transaction, so a failed
24
+ # erasure leaves the restriction in force. Lifting needs the notice, as
25
+ # lift! does (RightsTransitionError), and names restrictions of this
26
+ # record: any other covering restriction still refuses.
27
+ #
28
+ # Lyra::Erasure.erase! asks check! at its entry (with PAM DSL loaded); with
29
+ # nothing in force that costs the store's any_active?. Lifting runs in the
30
+ # erasure's own transaction (lift!).
31
+ module ErasureGuard
32
+ class << self
33
+ # The restrictions to lift before erasing (+lifting+ among those
34
+ # covering the record); raises RestrictedProcessingError when one
35
+ # that covers it is not lifted, RightsTransitionError when one to lift
36
+ # has no notice recorded.
37
+ def check!(model, id, fields:, subjects:, lifting:)
38
+ lifting = Array(lifting).map(&:to_s)
39
+ return [] if lifting.empty? && !PamDsl.rights_store.any_active?
40
+
41
+ lifted = Thread.current[:lyra_erasure_lifted] || []
42
+ covering = covering(model, id, fields, subjects).reject { |r| lifted.include?(r.id) }
43
+ lifts = covering.select { |r| lifting.include?(r.id) }
44
+ refused = covering - lifts
45
+ unless refused.empty?
46
+ raise PamDsl::RestrictedProcessingError,
47
+ "#{model.name} #{id} cannot be erased: its processing is restricted " \
48
+ "(#{refused.map { |r| describe(r) }.join('; ')}). Erasure is processing beyond storage " \
49
+ "(Art. 18(2)). To erase all the same, record the Art. 18(3) notice " \
50
+ "(PamDsl.rights.notify_lift!) and pass lifting: the restriction's id"
51
+ end
52
+ unknown = lifting - lifts.map(&:id)
53
+ if unknown.any?
54
+ raise PamDsl::RestrictedProcessingError,
55
+ "lifting: #{unknown.join(', ')} is not a restriction in force on #{model.name} #{id}"
56
+ end
57
+ own = Lyra.stream_name(model, id)
58
+ wider = lifts.reject { |r| r.subject_key == own }
59
+ if wider.any?
60
+ raise PamDsl::RestrictedProcessingError,
61
+ "lifting: #{wider.map(&:id).join(', ')} restricts #{wider.map(&:subject_key).uniq.join(', ')}, not only " \
62
+ "#{own}: erasing #{own} lifts only a restriction of that record. To make all of that subject's data " \
63
+ "processable again, lift it explicitly (PamDsl.rights.lift!), or erase that subject's own record " \
64
+ "with lifting:"
65
+ end
66
+ lifts.each do |r|
67
+ next if r.state == :lift_notified
68
+
69
+ raise PamDsl::RightsTransitionError,
70
+ "Restriction #{r.id} cannot be lifted for the erasure before the subject is told (Art. 18(3)): " \
71
+ "record the notice with PamDsl.rights.notify_lift! first"
72
+ end
73
+ lifts
74
+ end
75
+
76
+ # Lift +restrictions+ for an erasure (reason :erased), in the caller's
77
+ # transaction.
78
+ def lift!(restrictions, by: nil)
79
+ restrictions.each { |restriction| PamDsl.rights.lift!(restriction.id, reason: :erased, by: by) }
80
+ end
81
+
82
+ # Run the erasure with +restrictions+ taken as lifted (lifted already
83
+ # in this transaction, or, for a dry run, to be lifted): check! does
84
+ # not refuse it for them again.
85
+ def lifted(restrictions)
86
+ previous = Thread.current[:lyra_erasure_lifted]
87
+ Thread.current[:lyra_erasure_lifted] = (previous || []) + restrictions.map(&:id)
88
+ yield
89
+ ensure
90
+ Thread.current[:lyra_erasure_lifted] = previous
91
+ end
92
+
93
+ # The restrictions in force that keep +record+ (or its +fields+;
94
+ # nil: all of them) from being erased: Lyra::Retention skips it.
95
+ def covering_record(record, fields: nil)
96
+ return [] unless PamDsl.rights_store.any_active?
97
+
98
+ select_covering(SubjectKeys.keys_for(record), fields)
99
+ end
100
+
101
+ def describe(restriction)
102
+ what = restriction.fields.empty? ? "all data" : restriction.fields.join(", ")
103
+ "#{restriction.id}: #{restriction.subject_key}, #{what}, #{restriction.article} #{restriction.ground}, " \
104
+ "#{restriction.state}"
105
+ end
106
+
107
+ private
108
+
109
+ def covering(model, id, fields, subjects)
110
+ select_covering(keys_of(model, id) + Array(subjects).map(&:to_s), fields)
111
+ end
112
+
113
+ def select_covering(keys, fields)
114
+ fields = fields && Array(fields).map(&:to_sym)
115
+ PamDsl.rights_store.active_for(keys.uniq).select do |request|
116
+ request.kind == :restriction && request.objection_id.nil? &&
117
+ (request.fields.empty? || fields.nil? || request.fields.intersect?(fields))
118
+ end
119
+ end
120
+
121
+ # The record's keys, from its row or its replayed state; when it has
122
+ # neither, its people cannot be known, so the erasure is refused
123
+ # (fail closed: a right is in force, or check! would not ask).
124
+ def keys_of(model, id)
125
+ record = SubjectKeys.find_record(model, id)
126
+ return SubjectKeys.keys_for(record) if record
127
+
128
+ raise UnresolvedSubjectError,
129
+ "#{Lyra.stream_name(model, id)} has no row and no events to read the people it names from, so " \
130
+ "whether a restriction covers it cannot be told; erasure refused"
131
+ end
132
+ end
133
+ end
134
+ end
135
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "set"
4
+
5
+ module Lyra
6
+ module Rights
7
+ # What a connection has recorded in its open transaction, per store.
8
+ #
9
+ # A store's snapshot or cache is shared by the threads of the process, and
10
+ # each thread reads through its own connection. Rights or consent recorded
11
+ # in an open transaction are seen by that transaction only: so the keys a
12
+ # connection wrote are read afresh through it until its transaction ends,
13
+ # the shared snapshot is never filled from such a connection, and when the
14
+ # transaction commits or rolls back the keys are dropped from the shared
15
+ # snapshot (the next read takes them from the database).
16
+ module Pending
17
+ private
18
+
19
+ def init_pending
20
+ @pending = {}.compare_by_identity
21
+ @pending_mutex = Mutex.new
22
+ end
23
+
24
+ def current_connection
25
+ ActiveRecord::Base.connection_pool.active_connection
26
+ end
27
+
28
+ # The keys this thread's connection wrote in its open transaction, or nil.
29
+ def pending_here
30
+ connection = current_connection
31
+ return nil unless connection
32
+
33
+ keys = @pending_mutex.synchronize { @pending[connection] }
34
+ return nil unless keys
35
+ return keys if connection.transaction_open?
36
+
37
+ # Its transaction ended without telling us (a dropped callback).
38
+ settle(connection)
39
+ nil
40
+ end
41
+
42
+ # After a write of +keys+: remember them for this connection's open
43
+ # transaction, and settle when it ends. Outside a transaction the write
44
+ # is committed already: settle at once.
45
+ def mark_pending(keys)
46
+ connection = current_connection
47
+ unless connection&.transaction_open?
48
+ settled(keys)
49
+ return
50
+ end
51
+
52
+ @pending_mutex.synchronize { (@pending[connection] ||= Set.new).merge(keys) }
53
+ watch(connection)
54
+ end
55
+
56
+ def watch(connection)
57
+ transaction = ActiveRecord::Base.current_transaction
58
+ return unless transaction.open?
59
+
60
+ transaction.after_commit { settle(connection) }
61
+ transaction.after_rollback do
62
+ # A savepoint rolled back: the transaction around it goes on, and its
63
+ # keys stay read afresh until it ends.
64
+ connection.transaction_open? ? watch(connection) : settle(connection)
65
+ end
66
+ end
67
+
68
+ def settle(connection)
69
+ keys = @pending_mutex.synchronize { @pending.delete(connection) }
70
+ settled(keys.to_a) if keys
71
+ end
72
+ end
73
+ end
74
+ end