orfeas_lyra 0.9.2 → 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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +2021 -17
- data/README.md +22 -9
- data/Rakefile +40 -1
- data/app/controllers/lyra/application_controller.rb +127 -0
- data/app/controllers/lyra/dashboard_controller.rb +33 -31
- data/app/controllers/lyra/flow_controller.rb +32 -34
- data/app/controllers/lyra/privacy_controller.rb +57 -18
- data/app/views/lyra/dashboard/discrepancies.html.erb +5 -5
- data/app/views/lyra/dashboard/index.html.erb +1 -0
- data/app/views/lyra/flow/crud_mapping.html.erb +1 -1
- data/app/views/lyra/privacy/pii_detection.html.erb +1 -1
- data/app/views/lyra/privacy/policy.html.erb +7 -0
- data/app/views/lyra/privacy/rights.html.erb +78 -0
- data/config/privacy_policies.rb +0 -3
- data/config/routes.rb +1 -0
- data/lib/lyra/access_log.rb +187 -6
- data/lib/lyra/advisory_lock.rb +236 -0
- data/lib/lyra/aggregate.rb +1 -1
- data/lib/lyra/associations/habtm_links.rb +382 -19
- data/lib/lyra/associations/maintenance.rb +229 -0
- data/lib/lyra/attribution.rb +48 -0
- data/lib/lyra/bypass_events.rb +446 -46
- data/lib/lyra/command_handler.rb +19 -14
- data/lib/lyra/configuration.rb +237 -10
- data/lib/lyra/consistency/read_your_writes.rb +4 -2
- data/lib/lyra/correlation.rb +121 -0
- data/lib/lyra/domain_events.rb +4 -0
- data/lib/lyra/dual_view.rb +53 -16
- data/lib/lyra/encrypted_attributes.rb +5 -0
- data/lib/lyra/engine.rb +67 -0
- data/lib/lyra/erasure.rb +1011 -106
- data/lib/lyra/event.rb +35 -1
- data/lib/lyra/event_data.rb +38 -0
- data/lib/lyra/event_flow.rb +31 -15
- data/lib/lyra/event_store_adapter.rb +28 -0
- data/lib/lyra/event_window.rb +58 -0
- data/lib/lyra/genesis.rb +548 -44
- data/lib/lyra/interceptors/association_interceptor.rb +689 -34
- data/lib/lyra/interceptors/crud_interceptor.rb +578 -91
- data/lib/lyra/internal.rb +26 -0
- data/lib/lyra/mode_sync.rb +51 -3
- data/lib/lyra/mode_transition.rb +257 -30
- data/lib/lyra/model_selection.rb +35 -0
- data/lib/lyra/own_connection.rb +17 -2
- data/lib/lyra/privacy/adapters/pam.rb +25 -2
- data/lib/lyra/privacy/gdpr_compliance.rb +314 -48
- data/lib/lyra/privacy/interface.rb +27 -7
- data/lib/lyra/privacy/subject_links.rb +143 -0
- data/lib/lyra/privacy/subject_records.rb +369 -0
- data/lib/lyra/projection.rb +7 -1
- data/lib/lyra/projections/async_checkpoint.rb +32 -0
- data/lib/lyra/projections/async_projection_job.rb +166 -23
- data/lib/lyra/projections/cached_joins.rb +33 -18
- data/lib/lyra/projections/cached_projection.rb +74 -33
- data/lib/lyra/projections/cached_relation.rb +3175 -321
- data/lib/lyra/projections/default_scopes.rb +254 -0
- data/lib/lyra/projections/deferred_relation.rb +23 -3
- data/lib/lyra/projections/event_store_reader.rb +47 -8
- data/lib/lyra/projections/foreign_keys.rb +92 -0
- data/lib/lyra/projections/lazy_projection.rb +165 -27
- data/lib/lyra/projections/model_projection.rb +165 -14
- data/lib/lyra/projections/rebuild.rb +34 -6
- data/lib/lyra/projections/reference_links.rb +463 -72
- data/lib/lyra/projections/typed_values.rb +629 -0
- data/lib/lyra/projections/unique_indexes.rb +595 -0
- data/lib/lyra/projections/uniqueness.rb +784 -0
- data/lib/lyra/projections/value_digest.rb +93 -0
- data/lib/lyra/purpose_bound_reads.rb +912 -77
- data/lib/lyra/repair.rb +119 -28
- data/lib/lyra/retention.rb +114 -8
- data/lib/lyra/rights/consent_store.rb +415 -0
- data/lib/lyra/rights/erasure_guard.rb +135 -0
- data/lib/lyra/rights/pending.rb +74 -0
- data/lib/lyra/rights/queries.rb +269 -0
- data/lib/lyra/rights/store.rb +512 -0
- data/lib/lyra/rights/subject_keys.rb +224 -0
- data/lib/lyra/rights.rb +281 -0
- data/lib/lyra/saves_without_effect.rb +69 -0
- data/lib/lyra/stream_tail.rb +319 -0
- data/lib/lyra/strict_data_access.rb +916 -106
- data/lib/lyra/temporal.rb +4 -2
- data/lib/lyra/touch_registry.rb +404 -0
- data/lib/lyra/verification/bypass_workflow.rb +452 -42
- data/lib/lyra/verification/rights_workflows.rb +170 -0
- data/lib/lyra/version.rb +1 -1
- data/lib/lyra/visualization/event_graph.rb +1 -1
- data/lib/lyra/visualization/timeline.rb +2 -2
- data/lib/lyra.rb +43 -2
- data/lib/tasks/lyra_mode.rake +110 -8
- data/lib/tasks/lyra_projections.rake +43 -2
- metadata +34 -4
- data/lib/lyra/associations/event_aware.rb +0 -224
|
@@ -2,11 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
module Lyra
|
|
4
4
|
# A read of a model with a privacy policy, made for a declared purpose, is
|
|
5
|
-
# checked against the policy: every declared attribute the
|
|
6
|
-
#
|
|
7
|
-
# validate_access
|
|
8
|
-
#
|
|
9
|
-
# (Art. 5(1)(c)) asks: select what the purpose needs. With the access log
|
|
5
|
+
# checked against the policy: every declared attribute the read uses must
|
|
6
|
+
# be allowed for that purpose (Def. 1, through the policy's
|
|
7
|
+
# validate_access!). Using a declared attribute the purpose does not need
|
|
8
|
+
# is refused, as data minimisation (Art. 5(1)(c)) asks. With the access log
|
|
10
9
|
# on (config.record_access_events), each checked read is recorded.
|
|
11
10
|
#
|
|
12
11
|
# The policy decides which attributes are checked; the purpose comes from
|
|
@@ -35,87 +34,340 @@ module Lyra
|
|
|
35
34
|
# raises the policy error from the read, audit logs it and lets the read
|
|
36
35
|
# through.
|
|
37
36
|
#
|
|
38
|
-
#
|
|
39
|
-
#
|
|
40
|
-
#
|
|
41
|
-
#
|
|
42
|
-
#
|
|
43
|
-
#
|
|
37
|
+
# What a read uses (from Lyra 1.0.0). A query uses every declared attribute
|
|
38
|
+
# it names, whatever it returns: its select or pluck list, the aggregated
|
|
39
|
+
# column, group, where, having and order, the conditions of find_by and
|
|
40
|
+
# exists?, the filter of a bulk write (update_all, delete_all, touch_all,
|
|
41
|
+
# delete_by, destroy_by), and a subquery inside any of them. A filter is a
|
|
42
|
+
# use: where(name: "Ann").exists? tells whether someone is called Ann. A
|
|
43
|
+
# count with no column (count, count(:all), size) names nothing and is not
|
|
44
|
+
# checked; count(:name) uses name. The values a write sets are not reads.
|
|
44
45
|
#
|
|
45
|
-
#
|
|
46
|
-
#
|
|
47
|
-
#
|
|
48
|
-
#
|
|
49
|
-
#
|
|
46
|
+
# The check is split by what can be known:
|
|
47
|
+
#
|
|
48
|
+
# - A record load (where(...).first, find_by, to_a) has the attributes its
|
|
49
|
+
# query names checked before it runs, without the consent step (PAM's
|
|
50
|
+
# validate_fields!), then each record returned checked as before,
|
|
51
|
+
# consent included, for what it holds and what the query named. A
|
|
52
|
+
# purpose whose consent the policy requires can still load the records
|
|
53
|
+
# of the people who consented, by attributes it may use.
|
|
54
|
+
# - Everything else (aggregates, exists?, ids, pluck, pick, bulk writes)
|
|
55
|
+
# reads many people at once. Its subject is the model ("Registration$*")
|
|
56
|
+
# and it is checked whole: under a purpose that rests on consent it is
|
|
57
|
+
# refused, since no one person's consent can be checked; load the
|
|
58
|
+
# records instead.
|
|
59
|
+
#
|
|
60
|
+
# A joined model's declared attributes (where(users: { email: ... }) after
|
|
61
|
+
# joins(:user), a "users.email" inside a fragment, or a column only the
|
|
62
|
+
# joined model has) are checked against that model's own policy, subject
|
|
63
|
+
# "User$*". A subquery or CTE reads other people: what it names is checked
|
|
64
|
+
# whole, subject "Model$*", even on the queried model. A query is checked,
|
|
65
|
+
# and recorded, once: the cursor queries of find_each and in_batches, and
|
|
66
|
+
# the queries Active Record runs inside another, are not checked again (the
|
|
67
|
+
# application's own block inside the loop is, each time), and a refused
|
|
68
|
+
# check is never remembered. The rule is the same for Active Record's
|
|
69
|
+
# relations and for ES-NoProj's (Projections::CachedRelation), whose
|
|
70
|
+
# association readers, records, enumerators and inspect are checked as
|
|
71
|
+
# loads.
|
|
72
|
+
#
|
|
73
|
+
# The subject's rights (Arts. 18 and 21, with PAM DSL; from 1.0.0). Each
|
|
74
|
+
# record checked meets Condition 6: a restricted person's record raises, as
|
|
75
|
+
# a non-consenting one does, and a load that names no declared field is
|
|
76
|
+
# checked for a restriction of all of the subject's data or an objection
|
|
77
|
+
# to the purpose (validate_rights!). A model-wide read (aggregates, ids,
|
|
78
|
+
# pluck, pick, exists?, cache_version) leaves the restricted subjects out,
|
|
79
|
+
# in both read paths, and a join, subquery or CTE reaching one is refused
|
|
80
|
+
# (Lyra::Rights::Queries); Model.lyra_processable is the scope for batch
|
|
81
|
+
# jobs. Bulk writes are not filtered: writes are not checked under a
|
|
82
|
+
# restriction in 1.0.0. A purpose block, and each batch of a batch loop, is
|
|
83
|
+
# a checking scope (Lyra::Rights.scope): its first check reads the rights
|
|
84
|
+
# afresh. With nothing in force all this costs the store's any_active?, and
|
|
85
|
+
# with checking? false nothing at all.
|
|
86
|
+
#
|
|
87
|
+
# The purpose follows the code into the fibers it creates (an Enumerator's,
|
|
88
|
+
# relation.to_enum.next) and stays in its thread: see Context.
|
|
89
|
+
#
|
|
90
|
+
# Lyra's own reads (projections, bypass-event snapshots, Genesis, DualView's
|
|
91
|
+
# comparisons for Lyra, mode checks, repair, erasure, ES-NoProj's join
|
|
92
|
+
# partners) are not data processing for a purpose and are not checked.
|
|
93
|
+
# Neither is Rails' uniqueness validation, which is part of writing the
|
|
94
|
+
# record (so valid? alone can answer whether a value is taken), nor an
|
|
95
|
+
# association's dependent: handling when a record is destroyed (for
|
|
96
|
+
# :destroy, the lookup of the dependents), which is part of destroying it.
|
|
97
|
+
# find_or_create_by is the application's own lookup and is checked, as are
|
|
98
|
+
# its own where(fk: id).delete_all and collection writes
|
|
99
|
+
# (user.articles.delete(article)).
|
|
100
|
+
#
|
|
101
|
+
# Limits. Attributes are found by walking the relation's Arel, and an SQL
|
|
102
|
+
# string fragment by its "table.column" pairs and, for the rest, the
|
|
103
|
+
# queried model's declared names as words (quoted literals left out). So:
|
|
104
|
+
# - SQL written by hand (connection.select_*, execute, Arel handed to the
|
|
105
|
+
# connection) names no model and is not checked (find_by_sql's records
|
|
106
|
+
# are, as loads);
|
|
107
|
+
# - a raw JOIN string is not read, and an unqualified word in a fragment
|
|
108
|
+
# is taken for the queried model's (or a joined model's) column;
|
|
109
|
+
# - database views, functions and triggers are outside the ORM;
|
|
110
|
+
# - a form the walk does not reach (a custom Arel node) is not checked;
|
|
111
|
+
# - a load under a consent purpose that finds nothing tells that no
|
|
112
|
+
# matching record exists; the attributes it filtered on are ones the
|
|
113
|
+
# purpose may use.
|
|
50
114
|
module PurposeBoundReads
|
|
51
115
|
MODES = %i[allow audit deny].freeze
|
|
52
116
|
|
|
53
117
|
class PurposeRequiredError < StandardError; end
|
|
54
118
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
119
|
+
# Where the purpose and the check's own state are kept. Thread.current[]
|
|
120
|
+
# is fiber-local, so an Enumerator's fiber (relation.to_enum.next,
|
|
121
|
+
# find_each.next) ran without the purpose of the code that made it. Ruby's
|
|
122
|
+
# fiber storage (Fiber[]) is inherited by the fibers created under it, and
|
|
123
|
+
# each fiber's assignments are its own; it is also copied into a new
|
|
124
|
+
# thread, so each value carries the thread that set it and is ignored in
|
|
125
|
+
# any other: a pooled thread started inside a purpose does not keep it.
|
|
126
|
+
module Context
|
|
127
|
+
def self.[](key)
|
|
128
|
+
entry = Fiber[key]
|
|
129
|
+
entry[1] if entry && entry[0].equal?(Thread.current)
|
|
58
130
|
end
|
|
59
131
|
|
|
60
|
-
def
|
|
61
|
-
|
|
62
|
-
Thread.current[:lyra_purpose] = purpose&.to_sym
|
|
63
|
-
yield
|
|
64
|
-
ensure
|
|
65
|
-
Thread.current[:lyra_purpose] = previous
|
|
132
|
+
def self.[]=(key, value)
|
|
133
|
+
Fiber[key] = value.nil? ? nil : [Thread.current, value].freeze
|
|
66
134
|
end
|
|
67
135
|
|
|
68
|
-
#
|
|
69
|
-
def
|
|
70
|
-
previous =
|
|
71
|
-
|
|
136
|
+
# +value+ for +key+ while the block runs, restored after.
|
|
137
|
+
def self.with(key, value)
|
|
138
|
+
previous = self[key]
|
|
139
|
+
self[key] = value
|
|
72
140
|
yield
|
|
73
141
|
ensure
|
|
74
|
-
|
|
142
|
+
self[key] = previous
|
|
75
143
|
end
|
|
144
|
+
end
|
|
76
145
|
|
|
77
|
-
|
|
78
|
-
def
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
146
|
+
class << self
|
|
147
|
+
def current
|
|
148
|
+
Context[:lyra_purpose]
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
# A purpose block is a checking scope: the subject's rights (Arts. 18
|
|
152
|
+
# and 21) and consent are read afresh at its first check
|
|
153
|
+
# (Lyra::Rights.scope).
|
|
154
|
+
def with_purpose(purpose, &block)
|
|
155
|
+
return Context.with(:lyra_purpose, purpose&.to_sym, &block) unless defined?(Lyra::Rights.scope)
|
|
156
|
+
|
|
157
|
+
Lyra::Rights.scope { Context.with(:lyra_purpose, purpose&.to_sym, &block) }
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
# Lyra's own reads, not checked. Only Lyra's own call sites may call it
|
|
161
|
+
# (they pass Lyra::INTERNAL): application code that could would read a
|
|
162
|
+
# declared field under :deny.
|
|
163
|
+
# A refused call raises before it touches the flag, so it leaves the
|
|
164
|
+
# block it was called in as it was.
|
|
165
|
+
def internal(token = nil, &block)
|
|
166
|
+
raise InternalApiError, "PurposeBoundReads.internal is for Lyra's own reads" unless token.equal?(INTERNAL)
|
|
167
|
+
|
|
168
|
+
Context.with(:lyra_internal_read, true, &block)
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
# Whether Lyra's own reads are in progress (PurposeBoundReads.internal).
|
|
172
|
+
def internal?
|
|
173
|
+
Context[:lyra_internal_read] ? true : false
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
# Whether reads are checked here at all: a purpose is in scope or reads
|
|
177
|
+
# without one are not simply allowed, and the read is not Lyra's own.
|
|
178
|
+
# Every hook asks first and returns when it is false, before it looks
|
|
179
|
+
# at the query: with no purpose and the default :allow (every
|
|
180
|
+
# benchmark), a query costs this and nothing more.
|
|
181
|
+
def checking?
|
|
182
|
+
return false if Context[:lyra_purpose].nil? && Lyra.config.reads_without_purpose == :allow
|
|
183
|
+
return false if Context[:lyra_internal_read] || Thread.current[:lyra_projection_write]
|
|
184
|
+
|
|
185
|
+
!Lyra.config.disabled_mode?
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
# after_find on monitored models, and every record ES-NoProj returns:
|
|
189
|
+
# the declared attributes the record holds, and those the query that
|
|
190
|
+
# loaded it named (+named+, or the query being instantiated), checked
|
|
191
|
+
# with the record as the subject, consent included.
|
|
192
|
+
def check(record, named = nil)
|
|
193
|
+
return unless checking?
|
|
83
194
|
|
|
84
195
|
policy = Lyra::Privacy.policy_for(record.class)
|
|
85
196
|
return unless policy.loaded?
|
|
86
197
|
|
|
87
|
-
|
|
88
|
-
|
|
198
|
+
declared = policy.declared_fields.map(&:to_s)
|
|
199
|
+
fields = ((record.attribute_names & declared) | (query_names_for(record, named) & declared)).map(&:to_sym)
|
|
200
|
+
# A load that names no declared field reads nothing a field
|
|
201
|
+
# restriction covers, but it is still processing of the record: a
|
|
202
|
+
# restriction of all of its subjects' data, or an objection to the
|
|
203
|
+
# purpose, refuses it (Condition 6 alone; with nothing in force, the
|
|
204
|
+
# store's any_active? and no more).
|
|
205
|
+
if fields.empty?
|
|
206
|
+
policy.validate_rights!([], current, subject: record) if current
|
|
207
|
+
return
|
|
208
|
+
end
|
|
89
209
|
|
|
90
|
-
if
|
|
91
|
-
policy.validate_access!(fields,
|
|
210
|
+
if current
|
|
211
|
+
policy.validate_access!(fields, current, subject: record)
|
|
92
212
|
else
|
|
93
213
|
without_purpose("#{record.class.name} #{record.id}", record, policy, fields)
|
|
94
214
|
end
|
|
95
215
|
end
|
|
96
216
|
|
|
97
|
-
#
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
217
|
+
# Check what a query names, before it runs. +named+ is
|
|
218
|
+
# { model => [attribute names] } (named_fields_of). +kind+ is :load for
|
|
219
|
+
# a record load (the queried model's attributes checked without the
|
|
220
|
+
# consent step; each record returned is checked after) or :all for
|
|
221
|
+
# anything else (subject Model$*, consent included). +whole+ holds what
|
|
222
|
+
# the query's subqueries and CTEs name: they read other people, so they
|
|
223
|
+
# are checked as :all whatever the kind, as is a joined model (no
|
|
224
|
+
# record of it is returned).
|
|
225
|
+
#
|
|
226
|
+
# Then, with a right in force: a joined model (+joined+, or one whose
|
|
227
|
+
# attributes the query names), a subquery or a CTE that reaches a
|
|
228
|
+
# subject whose restriction or objection covers the access is refused
|
|
229
|
+
# (Lyra::Rights::Queries.refuse_reaching!): only the queried model's
|
|
230
|
+
# own rows can be left out of a query.
|
|
231
|
+
def check_query(model_class, named, kind, whole = {}, joined: [], opaque: false)
|
|
232
|
+
own_names = named
|
|
233
|
+
if kind == :all
|
|
234
|
+
named = named.merge(whole) { |_, mine, theirs| mine | theirs }
|
|
235
|
+
else
|
|
236
|
+
whole.each { |klass, names| check_fields(model_class, klass, names, :all) }
|
|
237
|
+
end
|
|
238
|
+
named.each { |klass, names| check_fields(model_class, klass, names, kind) }
|
|
239
|
+
refuse_reaching(model_class, own_names, whole, joined, opaque) if current
|
|
240
|
+
end
|
|
104
241
|
|
|
105
|
-
|
|
106
|
-
|
|
242
|
+
# Is a right (Arts. 18, 21) in force anywhere? A clock read once the
|
|
243
|
+
# scope has read the rights; false without PAM DSL.
|
|
244
|
+
def rights_in_force?
|
|
245
|
+
defined?(Lyra::Rights::Queries) ? Lyra::Rights::Queries.in_force? : false
|
|
246
|
+
end
|
|
107
247
|
|
|
108
|
-
|
|
109
|
-
|
|
248
|
+
# +relation+ (a model-wide read naming +names+) leaving out the
|
|
249
|
+
# subjects a right in force keeps out of it, or nil when it leaves out
|
|
250
|
+
# no one (Lyra::Rights::Queries).
|
|
251
|
+
def rights_filtered(relation, names)
|
|
252
|
+
return nil unless current && monitored?(relation.klass) && rights_in_force?
|
|
110
253
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
254
|
+
Lyra::Rights::Queries.filter(relation, names.result.fetch(relation.klass, []))
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
# The Lyra::Rights::Queries::Exclusion a model-wide read of +model+
|
|
258
|
+
# (naming +fields+) would apply under the purpose in scope, or nil.
|
|
259
|
+
def rights_exclusion(model, fields = [])
|
|
260
|
+
return nil unless current && monitored?(model) && rights_in_force?
|
|
261
|
+
|
|
262
|
+
Lyra::Rights::Queries.exclusion(model, fields)
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
# Run the filtered form of a query already checked, as Lyra's own: it
|
|
266
|
+
# is not checked, filtered or recorded again.
|
|
267
|
+
def run_filtered(&block)
|
|
268
|
+
internal(INTERNAL, &block)
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
# The +records+ of +model_class+ that a model-wide read naming +named+
|
|
272
|
+
# ({ model => names }) keeps, or nil when it keeps them all
|
|
273
|
+
# (ES-NoProj's relations).
|
|
274
|
+
def rights_kept_records(model_class, named, records)
|
|
275
|
+
return nil unless current && rights_in_force?
|
|
276
|
+
|
|
277
|
+
Lyra::Rights::Queries.kept_records(model_class, (named || {}).fetch(model_class, []), records)
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
# Model.lyra_processable (Lyra::Rights::Queries.processable).
|
|
281
|
+
def processable(relation, fields = nil)
|
|
282
|
+
return relation unless current && defined?(Lyra::Rights::Queries)
|
|
283
|
+
|
|
284
|
+
Lyra::Rights::Queries.processable(relation, fields)
|
|
285
|
+
end
|
|
286
|
+
|
|
287
|
+
NO_BATCH_SCOPE = -> {}
|
|
288
|
+
|
|
289
|
+
# A batch loop under checking: each batch is a checking scope of its
|
|
290
|
+
# own (Lyra::Rights.batches). The block gets the callable that ends a
|
|
291
|
+
# batch's scope and begins the next one's.
|
|
292
|
+
def batch_scopes(&block)
|
|
293
|
+
return yield(NO_BATCH_SCOPE) unless defined?(Lyra::Rights.batches)
|
|
294
|
+
|
|
295
|
+
Lyra::Rights.batches(&block)
|
|
296
|
+
end
|
|
297
|
+
|
|
298
|
+
# The attribute names an ActiveRecord relation's query names, by model:
|
|
299
|
+
# { ReadUser => ["name", ...], ... }. Every name is listed; the check
|
|
300
|
+
# keeps the declared ones. What its subqueries name is not included
|
|
301
|
+
# (query_names gives both).
|
|
302
|
+
def named_fields_of(relation, extra = nil)
|
|
303
|
+
query_names(relation, extra).result
|
|
304
|
+
end
|
|
305
|
+
|
|
306
|
+
# The NamedFields of +relation+ and of +extra+ (columns the call adds).
|
|
307
|
+
def query_names(relation, extra = nil)
|
|
308
|
+
NamedFields.new.relation(relation).value(extra, relation.klass)
|
|
309
|
+
end
|
|
310
|
+
|
|
311
|
+
# The names a hash of conditions (find_by, exists?, where) uses, by
|
|
312
|
+
# model, without building a relation.
|
|
313
|
+
def named_in_conditions(model_class, conditions)
|
|
314
|
+
NamedFields.new.conditions(conditions, model_class)
|
|
315
|
+
end
|
|
316
|
+
|
|
317
|
+
# Run a query hook: check what +relation+ names (and +extra+, columns
|
|
318
|
+
# or conditions the call adds) as +kind+, then the query. Identical
|
|
319
|
+
# checks of Active Record's own queries inside it are made once (with
|
|
320
|
+
# around: false, the check only: the query runs application callbacks).
|
|
321
|
+
def checked_query(relation, kind, extra = nil, around: true)
|
|
322
|
+
names = query_names(relation, extra)
|
|
323
|
+
unless around
|
|
324
|
+
once { check_query(relation.klass, names.result, kind, names.whole, joined: names.joined_classes, opaque: names.opaque?) }
|
|
325
|
+
return yield names
|
|
326
|
+
end
|
|
327
|
+
|
|
328
|
+
once do
|
|
329
|
+
check_query(relation.klass, names.result, kind, names.whole, joined: names.joined_classes, opaque: names.opaque?)
|
|
330
|
+
yield names
|
|
331
|
+
end
|
|
332
|
+
end
|
|
333
|
+
|
|
334
|
+
# Class-level find_by and find_by!: the condition keys, checked as a
|
|
335
|
+
# load before the query; the record found is checked with them.
|
|
336
|
+
def checked_finder(model_class, conditions)
|
|
337
|
+
return yield unless conditions.is_a?(Hash) && checking?
|
|
338
|
+
|
|
339
|
+
names = named_in_conditions(model_class, conditions)
|
|
340
|
+
named = names.result
|
|
341
|
+
once do
|
|
342
|
+
check_query(model_class, named, :load, names.whole)
|
|
343
|
+
with_query_names(model_class, named[model_class]) { yield }
|
|
116
344
|
end
|
|
117
345
|
end
|
|
118
346
|
|
|
347
|
+
# Identical checks inside the block that pass (or pass with a logged
|
|
348
|
+
# violation, in audit mode) are made, and recorded, once: for the
|
|
349
|
+
# queries Active Record runs inside one query and the cursor queries of
|
|
350
|
+
# one batch loop. A refused check is not remembered.
|
|
351
|
+
def once
|
|
352
|
+
return yield if Context[:lyra_checked_queries]
|
|
353
|
+
|
|
354
|
+
Context.with(:lyra_checked_queries, {}) { yield }
|
|
355
|
+
end
|
|
356
|
+
|
|
357
|
+
# The application's block inside a batch loop is its own code: its
|
|
358
|
+
# queries are checked and recorded each time.
|
|
359
|
+
def outside_once(&block)
|
|
360
|
+
Context.with(:lyra_checked_queries, nil, &block)
|
|
361
|
+
end
|
|
362
|
+
|
|
363
|
+
# The names of the query whose records are being instantiated, for
|
|
364
|
+
# check (after_find).
|
|
365
|
+
def with_query_names(model_class, names, &block)
|
|
366
|
+
return yield if names.blank?
|
|
367
|
+
|
|
368
|
+
Context.with(:lyra_query_names, (Context[:lyra_query_names] || []) + [[model_class, names]], &block)
|
|
369
|
+
end
|
|
370
|
+
|
|
119
371
|
def validate_mode!(mode)
|
|
120
372
|
mode = mode&.to_sym
|
|
121
373
|
return mode if MODES.include?(mode)
|
|
@@ -125,21 +377,75 @@ module Lyra
|
|
|
125
377
|
|
|
126
378
|
private
|
|
127
379
|
|
|
128
|
-
def
|
|
129
|
-
|
|
130
|
-
|
|
380
|
+
def check_fields(model_class, klass, names, kind)
|
|
381
|
+
return unless monitored?(klass)
|
|
382
|
+
|
|
383
|
+
policy = Lyra::Privacy.policy_for(klass)
|
|
384
|
+
return unless policy.loaded?
|
|
385
|
+
|
|
386
|
+
fields = (names & policy.declared_fields.map(&:to_s)).uniq.map(&:to_sym)
|
|
387
|
+
return if fields.empty?
|
|
388
|
+
|
|
389
|
+
purpose = current
|
|
390
|
+
split = kind == :load && (klass <= model_class || model_class <= klass)
|
|
391
|
+
key = [klass, split, purpose, fields.sort, policy.name]
|
|
392
|
+
return if already_checked?(key)
|
|
393
|
+
|
|
394
|
+
subject = "#{Lyra.stream_prefix(klass)}*"
|
|
395
|
+
if purpose.nil?
|
|
396
|
+
without_purpose("#{klass.name} (query)", subject, policy, fields)
|
|
397
|
+
elsif split
|
|
398
|
+
policy.validate_fields!(fields, purpose, subject: subject) unless policy.field_violations(fields, purpose).empty?
|
|
399
|
+
else
|
|
400
|
+
policy.validate_access!(fields, purpose, subject: subject)
|
|
401
|
+
end
|
|
402
|
+
remember(key)
|
|
403
|
+
end
|
|
404
|
+
|
|
405
|
+
def monitored?(klass)
|
|
406
|
+
klass.respond_to?(:lyra_monitored) && klass.lyra_monitored
|
|
407
|
+
end
|
|
408
|
+
|
|
409
|
+
def refuse_reaching(model_class, named, whole, joined, opaque = false)
|
|
410
|
+
related = ->(klass) { klass <= model_class || model_class <= klass }
|
|
411
|
+
reached = (named.keys.reject(&related) + whole.keys + Array(joined).reject(&related)).uniq.select { monitored?(_1) }
|
|
412
|
+
refuse_opaque(model_class) if opaque && rights_in_force?
|
|
413
|
+
return if reached.empty? || !rights_in_force?
|
|
414
|
+
|
|
415
|
+
reached.each do |klass|
|
|
416
|
+
Lyra::Rights::Queries.refuse_reaching!(klass, (named[klass] || []) | (whole[klass] || []))
|
|
417
|
+
end
|
|
418
|
+
end
|
|
419
|
+
|
|
420
|
+
# An SQL fragment reads FROM or JOIN something Lyra cannot name, while a
|
|
421
|
+
# right is in force for a model the purpose reads: what it reaches
|
|
422
|
+
# cannot be told, so it is refused (fail closed).
|
|
423
|
+
def refuse_opaque(model_class)
|
|
424
|
+
return unless Lyra.config.monitored_models.any? { |model| Lyra::Rights::Queries.exclusion(model, [], audit: false) }
|
|
425
|
+
|
|
426
|
+
raise PamDsl::RestrictedProcessingError,
|
|
427
|
+
"A query on #{model_class.name} reads, in SQL written as a string, a FROM or JOIN Lyra cannot read, " \
|
|
428
|
+
"while a restriction of processing or an objection is in force for purpose '#{current}': which " \
|
|
429
|
+
"subjects it reaches cannot be told, so it is refused. Name the tables it reads plainly " \
|
|
430
|
+
"(FROM table [alias], JOIN table [alias]) or use Active Record's joins and subqueries"
|
|
431
|
+
end
|
|
432
|
+
|
|
433
|
+
def already_checked?(key)
|
|
434
|
+
Context[:lyra_checked_queries]&.key?(key)
|
|
435
|
+
end
|
|
436
|
+
|
|
437
|
+
def remember(key)
|
|
438
|
+
checked = Context[:lyra_checked_queries]
|
|
439
|
+
checked[key] = true if checked
|
|
131
440
|
end
|
|
132
441
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
plain = name.split(".").last.delete('"')
|
|
139
|
-
next [plain] if declared.include?(plain)
|
|
442
|
+
def query_names_for(record, named)
|
|
443
|
+
return named.to_a if named
|
|
444
|
+
|
|
445
|
+
stack = Context[:lyra_query_names]
|
|
446
|
+
return [] if stack.blank?
|
|
140
447
|
|
|
141
|
-
|
|
142
|
-
end.uniq.map(&:to_sym)
|
|
448
|
+
stack.flat_map { |klass, names| record.is_a?(klass) ? names : [] }
|
|
143
449
|
end
|
|
144
450
|
|
|
145
451
|
def without_purpose(label, subject, policy, fields)
|
|
@@ -164,6 +470,351 @@ module Lyra
|
|
|
164
470
|
end
|
|
165
471
|
end
|
|
166
472
|
|
|
473
|
+
# The attribute names a query names, by model (PurposeBoundReads
|
|
474
|
+
# .query_names). Arel nodes are walked by compiling them with a visitor
|
|
475
|
+
# that notes every attribute (its table gives the model) and every SQL
|
|
476
|
+
# literal; bound values are not names. Symbols and "table.column" strings
|
|
477
|
+
# are columns; any other string is a fragment: its "table.column" pairs
|
|
478
|
+
# belong to that table's model, its other words to the queried model.
|
|
479
|
+
# A column named without its table that the queried model does not have
|
|
480
|
+
# belongs to the joined model that has it, as SQL resolves it. A
|
|
481
|
+
# condition on a belongs_to association is one on its foreign key (and
|
|
482
|
+
# type). What a subquery or a CTE names goes to #whole: it reads other
|
|
483
|
+
# people, whatever its model.
|
|
484
|
+
class NamedFields
|
|
485
|
+
QUOTED_LITERAL = /'(?:[^']|'')*'/
|
|
486
|
+
IDENTIFIER = /[A-Za-z_][A-Za-z0-9_]*/
|
|
487
|
+
COLUMN = /\A\s*(?:"?(\w+)"?\s*\.\s*)?"?(\w+)"?\s*\z/
|
|
488
|
+
QUALIFIED = /"?([A-Za-z_]\w*)"?\s*\.\s*"?([A-Za-z_]\w*)"?/
|
|
489
|
+
|
|
490
|
+
# A table an SQL fragment reads: FROM or JOIN, the table, and an alias.
|
|
491
|
+
TABLE_REF = /\b(FROM|JOIN)\s+"?([A-Za-z_][\w.]*)"?(?:\s+(?:AS\s+)?"?([A-Za-z_]\w*)"?)?/i
|
|
492
|
+
NOT_ALIASES = %w[on where inner left right full outer cross natural join using group order limit having
|
|
493
|
+
union select set and or lateral offset window for].freeze
|
|
494
|
+
|
|
495
|
+
def initialize
|
|
496
|
+
@names = Hash.new { |hash, model| hash[model] = [] }
|
|
497
|
+
@whole = Hash.new { |hash, model| hash[model] = [] }
|
|
498
|
+
@joined = {}
|
|
499
|
+
@aliases = {}
|
|
500
|
+
@reached = []
|
|
501
|
+
@opaque = false
|
|
502
|
+
@depth = 0
|
|
503
|
+
end
|
|
504
|
+
|
|
505
|
+
# An SQL fragment read FROM or JOIN something it could not name: the
|
|
506
|
+
# tables it reaches are not known (checks fail closed on it).
|
|
507
|
+
def opaque?
|
|
508
|
+
@opaque
|
|
509
|
+
end
|
|
510
|
+
|
|
511
|
+
def result
|
|
512
|
+
@names.reject { |_, names| names.empty? }.transform_values(&:uniq)
|
|
513
|
+
end
|
|
514
|
+
|
|
515
|
+
# The models joined into the query (joins, left_joins, eager_load, an
|
|
516
|
+
# SQL JOIN string) or read by a subquery written as SQL.
|
|
517
|
+
def joined_classes
|
|
518
|
+
(@joined.values.flatten + @reached).uniq
|
|
519
|
+
end
|
|
520
|
+
|
|
521
|
+
# What the query's subqueries and CTEs name.
|
|
522
|
+
def whole
|
|
523
|
+
@whole.reject { |_, names| names.empty? }.transform_values(&:uniq)
|
|
524
|
+
end
|
|
525
|
+
|
|
526
|
+
def relation(relation)
|
|
527
|
+
model = relation.klass
|
|
528
|
+
joined(model, joined_models(model, relation.joins_values + relation.left_outer_joins_values +
|
|
529
|
+
(relation.eager_loading? ? relation.includes_values + relation.eager_load_values : [])))
|
|
530
|
+
# JOINs written as SQL first: their tables and aliases name the
|
|
531
|
+
# columns the rest of the query uses.
|
|
532
|
+
(relation.joins_values + relation.left_outer_joins_values).each do |join|
|
|
533
|
+
sql = join.is_a?(Arel::Nodes::StringJoin) ? join.left : join
|
|
534
|
+
fragment(sql.to_s, model, join: true) if sql.is_a?(String) || sql.is_a?(Arel::Nodes::SqlLiteral)
|
|
535
|
+
end
|
|
536
|
+
relation.select_values.each { value(_1, model) }
|
|
537
|
+
relation.group_values.each { value(_1, model) }
|
|
538
|
+
relation.order_values.each { value(_1, model) }
|
|
539
|
+
arel(relation.where_clause.ast, model) unless relation.where_clause.empty?
|
|
540
|
+
arel(relation.having_clause.ast, model) unless relation.having_clause.empty?
|
|
541
|
+
nested { value(relation.from_clause.value, model) } unless relation.from_clause.empty?
|
|
542
|
+
(relation.joins_values + relation.left_outer_joins_values).each do |join|
|
|
543
|
+
arel(join, model) if Arel.arel_node?(join) && !join.is_a?(Arel::Nodes::StringJoin)
|
|
544
|
+
end
|
|
545
|
+
nested { relation.with_values.each { value(_1, model) } } if relation.respond_to?(:with_values)
|
|
546
|
+
self
|
|
547
|
+
end
|
|
548
|
+
|
|
549
|
+
# The models joined to +model+ in this query, for its unqualified
|
|
550
|
+
# columns.
|
|
551
|
+
def joined(model, models)
|
|
552
|
+
@joined[model] = ((@joined[model] || []) + models).uniq if models.any?
|
|
553
|
+
self
|
|
554
|
+
end
|
|
555
|
+
|
|
556
|
+
def value(value, model)
|
|
557
|
+
case value
|
|
558
|
+
when nil, true, false, Numeric then nil
|
|
559
|
+
when ActiveRecord::Relation then nested { relation(value) }
|
|
560
|
+
when Lyra::Projections::CachedRelation then nested { merge(value.lyra_query_names) }
|
|
561
|
+
when Symbol then column(model, value.to_s)
|
|
562
|
+
when String then string(value, model)
|
|
563
|
+
when Hash then value.each { |key, column| value(key, model).value(column, model) }
|
|
564
|
+
when Array then value.each { value(_1, model) }
|
|
565
|
+
else arel(value, model) if Arel.arel_node?(value)
|
|
566
|
+
end
|
|
567
|
+
self
|
|
568
|
+
end
|
|
569
|
+
|
|
570
|
+
# Hash conditions: a column, "table.column", a belongs_to association
|
|
571
|
+
# (its foreign key), or a table (or association) with a hash of its
|
|
572
|
+
# columns; a relation as a value is a subquery.
|
|
573
|
+
def conditions(conditions, model)
|
|
574
|
+
return value(conditions, model) unless conditions.is_a?(Hash)
|
|
575
|
+
|
|
576
|
+
conditions.each do |key, condition|
|
|
577
|
+
if condition.is_a?(Hash)
|
|
578
|
+
model_for(key.to_s, model).each { |other| condition.each_key { |column| add(other, column.to_s) } }
|
|
579
|
+
else
|
|
580
|
+
string(key.to_s, model)
|
|
581
|
+
association_keys(model, key)
|
|
582
|
+
end
|
|
583
|
+
value(condition, model) if condition.is_a?(ActiveRecord::Relation) || condition.is_a?(Lyra::Projections::CachedRelation)
|
|
584
|
+
end
|
|
585
|
+
self
|
|
586
|
+
end
|
|
587
|
+
|
|
588
|
+
# Another query's names, all of them (#value nests it: a subquery).
|
|
589
|
+
def merge(other)
|
|
590
|
+
other.result.each { |model, names| names.each { add(model, _1) } }
|
|
591
|
+
other.whole.each { |model, names| nested { names.each { add(model, _1) } } }
|
|
592
|
+
self
|
|
593
|
+
end
|
|
594
|
+
|
|
595
|
+
private
|
|
596
|
+
|
|
597
|
+
def add(model, name)
|
|
598
|
+
return if name.to_s == "*"
|
|
599
|
+
|
|
600
|
+
(@depth.positive? ? @whole : @names)[model] << name.to_s
|
|
601
|
+
end
|
|
602
|
+
|
|
603
|
+
def nested
|
|
604
|
+
@depth += 1
|
|
605
|
+
yield
|
|
606
|
+
ensure
|
|
607
|
+
@depth -= 1
|
|
608
|
+
end
|
|
609
|
+
|
|
610
|
+
# A column named without its table: the queried model's, else the
|
|
611
|
+
# joined model's that has it.
|
|
612
|
+
def column(model, name)
|
|
613
|
+
name = model_column(model, name)
|
|
614
|
+
columns = column_names(model)
|
|
615
|
+
others = @joined[model]
|
|
616
|
+
return add(model, name) if others.nil? || columns.nil? || columns.include?(name)
|
|
617
|
+
|
|
618
|
+
owners = others.select { |other| column_names(other)&.include?(name) }
|
|
619
|
+
owners.empty? ? add(model, name) : owners.each { add(_1, name) }
|
|
620
|
+
end
|
|
621
|
+
|
|
622
|
+
def string(string, model)
|
|
623
|
+
match = string.match(COLUMN)
|
|
624
|
+
return fragment(string, model) unless match
|
|
625
|
+
return column(model, match[2]) unless match[1]
|
|
626
|
+
|
|
627
|
+
qualified(match[1], match[2], model)
|
|
628
|
+
end
|
|
629
|
+
|
|
630
|
+
def qualified(table, name, model)
|
|
631
|
+
targets = model_for(table, model)
|
|
632
|
+
return column(model, name) if targets.empty?
|
|
633
|
+
|
|
634
|
+
targets.each { |target| add(target, model_column(target, name)) }
|
|
635
|
+
end
|
|
636
|
+
|
|
637
|
+
# The fragment's "table.column" pairs, then its other words, outside
|
|
638
|
+
# quoted literals (only declared names count). From 1.0.0 the tables it
|
|
639
|
+
# reads (FROM, JOIN, with their aliases) are read too: an alias names
|
|
640
|
+
# its table's model, a JOIN string joins that model, and the words of a
|
|
641
|
+
# subquery written as SQL that are columns of the tables it reads
|
|
642
|
+
# belong to those tables' models, read whole (other people). A FROM or
|
|
643
|
+
# JOIN whose table cannot be read makes the query opaque.
|
|
644
|
+
def fragment(sql, model, join: false)
|
|
645
|
+
text = sql.to_s.gsub(QUOTED_LITERAL, " ")
|
|
646
|
+
others = table_references(text, model)
|
|
647
|
+
subquery = !join && text.match?(/\bSELECT\b/i)
|
|
648
|
+
reached = subquery ? others : others.reject { |other| other <= model || model <= other }
|
|
649
|
+
if join
|
|
650
|
+
joined(model, reached)
|
|
651
|
+
elsif reached.any?
|
|
652
|
+
@reached |= reached
|
|
653
|
+
reached.each { |other| nested { add(other, Lyra.key_column(other)) } }
|
|
654
|
+
end
|
|
655
|
+
|
|
656
|
+
rest = text.gsub(QUALIFIED) do
|
|
657
|
+
table = ::Regexp.last_match(1)
|
|
658
|
+
name = ::Regexp.last_match(2).downcase
|
|
659
|
+
inner = subquery && model_for(table, model).intersect?(reached)
|
|
660
|
+
inner ? nested { qualified(table, name, model) } : qualified(table, name, model)
|
|
661
|
+
" "
|
|
662
|
+
end
|
|
663
|
+
rest.scan(IDENTIFIER).each do |word|
|
|
664
|
+
word = word.downcase
|
|
665
|
+
owners = subquery ? reached.select { |other| column_names(other)&.include?(word) } : []
|
|
666
|
+
owners.each { |other| nested { add(other, word) } }
|
|
667
|
+
column(model, word) if owners.empty? || column_names(model)&.include?(word)
|
|
668
|
+
end
|
|
669
|
+
end
|
|
670
|
+
|
|
671
|
+
# The monitored models of the tables +text+ reads FROM or JOIN,
|
|
672
|
+
# remembering their aliases; marks the query opaque when a FROM or JOIN
|
|
673
|
+
# names nothing it can read.
|
|
674
|
+
def table_references(text, model)
|
|
675
|
+
refs = text.scan(TABLE_REF)
|
|
676
|
+
@opaque = true if refs.empty? && text.match?(/\b(FROM|JOIN)\b/i)
|
|
677
|
+
refs.flat_map do |_keyword, table, alias_name|
|
|
678
|
+
table = table.split(".").last.downcase
|
|
679
|
+
models = table == model.table_name ? [model] : Lyra.config.monitored_models.select { |m| (m.table_name == table rescue false) }
|
|
680
|
+
if alias_name && !NOT_ALIASES.include?(alias_name.downcase) && models.any?
|
|
681
|
+
@aliases[alias_name.downcase] = models
|
|
682
|
+
end
|
|
683
|
+
models
|
|
684
|
+
end.uniq
|
|
685
|
+
end
|
|
686
|
+
|
|
687
|
+
def association_keys(model, key)
|
|
688
|
+
return unless model.respond_to?(:reflect_on_association)
|
|
689
|
+
|
|
690
|
+
reflection = model.reflect_on_association(key.to_s.to_sym)
|
|
691
|
+
return unless reflection&.belongs_to?
|
|
692
|
+
|
|
693
|
+
Array(reflection.foreign_key).each { add(model, _1.to_s) }
|
|
694
|
+
add(model, reflection.foreign_type.to_s) if reflection.polymorphic?
|
|
695
|
+
end
|
|
696
|
+
|
|
697
|
+
# The classes of the associations a joins/left_joins/eager_load spec
|
|
698
|
+
# names, nested ones included; polymorphic ones are skipped.
|
|
699
|
+
def joined_models(model, specs)
|
|
700
|
+
Array(specs).flat_map do |spec|
|
|
701
|
+
case spec
|
|
702
|
+
when Symbol, String
|
|
703
|
+
klass = association_class(model, spec)
|
|
704
|
+
klass ? [klass] : []
|
|
705
|
+
when Hash
|
|
706
|
+
spec.flat_map do |name, inner|
|
|
707
|
+
klass = association_class(model, name)
|
|
708
|
+
klass ? [klass, *joined_models(klass, inner)] : []
|
|
709
|
+
end
|
|
710
|
+
when Array then joined_models(model, spec)
|
|
711
|
+
else []
|
|
712
|
+
end
|
|
713
|
+
end
|
|
714
|
+
end
|
|
715
|
+
|
|
716
|
+
def association_class(model, name)
|
|
717
|
+
reflection = model.reflect_on_association(name.to_s.to_sym) if model.respond_to?(:reflect_on_association)
|
|
718
|
+
reflection.klass if reflection && !reflection.polymorphic?
|
|
719
|
+
rescue StandardError
|
|
720
|
+
nil
|
|
721
|
+
end
|
|
722
|
+
|
|
723
|
+
def column_names(model)
|
|
724
|
+
model.respond_to?(:column_names) ? model.column_names : nil
|
|
725
|
+
rescue StandardError
|
|
726
|
+
nil
|
|
727
|
+
end
|
|
728
|
+
|
|
729
|
+
def arel(node, model)
|
|
730
|
+
found = model.with_connection { |connection| Recorder.for(connection).found(node) }
|
|
731
|
+
found.each do |kind, item, inner|
|
|
732
|
+
handle = -> { kind == :fragment ? string(item, model) : attribute(item, model) }
|
|
733
|
+
inner ? nested(&handle) : handle.call
|
|
734
|
+
end
|
|
735
|
+
end
|
|
736
|
+
|
|
737
|
+
def attribute(attribute, model)
|
|
738
|
+
table = attribute.relation
|
|
739
|
+
table = table.relation while table.is_a?(Arel::Nodes::TableAlias) && table.relation.is_a?(Arel::Table)
|
|
740
|
+
klass = table.instance_variable_get(:@klass) if table.is_a?(Arel::Table)
|
|
741
|
+
targets = if klass then [klass]
|
|
742
|
+
elsif table.respond_to?(:name) then model_for(table.name.to_s, model)
|
|
743
|
+
else [model]
|
|
744
|
+
end
|
|
745
|
+
targets = [model] if targets.empty?
|
|
746
|
+
targets.each { |target| add(target, attribute.name) }
|
|
747
|
+
end
|
|
748
|
+
|
|
749
|
+
def model_column(model, name)
|
|
750
|
+
model.respond_to?(:attribute_aliases) ? (model.attribute_aliases[name] || name) : name
|
|
751
|
+
end
|
|
752
|
+
|
|
753
|
+
# The models a table name (or an association name) stands for in a
|
|
754
|
+
# query on +model+: the model itself, the class of its association, or
|
|
755
|
+
# the monitored models on that table.
|
|
756
|
+
def model_for(name, model)
|
|
757
|
+
aliased = @aliases[name.to_s.downcase]
|
|
758
|
+
return aliased if aliased
|
|
759
|
+
return [model] if name == model.table_name
|
|
760
|
+
|
|
761
|
+
reflection = model.reflect_on_association(name.to_sym) || model.reflect_on_association(name.singularize.to_sym)
|
|
762
|
+
reflection ||= model.reflect_on_all_associations.find { |r| !r.polymorphic? && (r.klass.table_name == name rescue false) }
|
|
763
|
+
return [reflection.klass] if reflection && !reflection.polymorphic?
|
|
764
|
+
|
|
765
|
+
Lyra.config.monitored_models.select { |monitored| monitored.table_name == name rescue false }
|
|
766
|
+
end
|
|
767
|
+
|
|
768
|
+
# Compiles an Arel node as the connection would, noting the attributes
|
|
769
|
+
# and SQL literals it meets.
|
|
770
|
+
module Recording
|
|
771
|
+
# [kind, item, inside a subquery?] for every attribute and literal.
|
|
772
|
+
def found(node)
|
|
773
|
+
@found = []
|
|
774
|
+
@depth = 0
|
|
775
|
+
accept(node, Arel::Collectors::SQLString.new)
|
|
776
|
+
@found
|
|
777
|
+
end
|
|
778
|
+
|
|
779
|
+
private
|
|
780
|
+
|
|
781
|
+
def visit_Arel_Nodes_SelectStatement(o, collector)
|
|
782
|
+
@depth += 1
|
|
783
|
+
super
|
|
784
|
+
ensure
|
|
785
|
+
@depth -= 1
|
|
786
|
+
end
|
|
787
|
+
|
|
788
|
+
def visit_Arel_Attributes_Attribute(o, collector)
|
|
789
|
+
@found << [:attribute, o, @depth.positive?]
|
|
790
|
+
super
|
|
791
|
+
end
|
|
792
|
+
|
|
793
|
+
def visit_Arel_Nodes_SqlLiteral(o, collector)
|
|
794
|
+
@found << [:fragment, o.to_s, @depth.positive?]
|
|
795
|
+
super
|
|
796
|
+
end
|
|
797
|
+
|
|
798
|
+
def visit_Arel_Nodes_BoundSqlLiteral(o, collector)
|
|
799
|
+
@found << [:fragment, o.sql_with_placeholders, @depth.positive?]
|
|
800
|
+
super
|
|
801
|
+
end
|
|
802
|
+
end
|
|
803
|
+
|
|
804
|
+
module Recorder
|
|
805
|
+
CLASSES = {}
|
|
806
|
+
MUTEX = Mutex.new
|
|
807
|
+
|
|
808
|
+
def self.for(connection)
|
|
809
|
+
visitor_class = connection.visitor.class
|
|
810
|
+
klass = CLASSES[visitor_class] || MUTEX.synchronize do
|
|
811
|
+
CLASSES[visitor_class] ||= Class.new(visitor_class) { include Recording }
|
|
812
|
+
end
|
|
813
|
+
klass.new(connection)
|
|
814
|
+
end
|
|
815
|
+
end
|
|
816
|
+
end
|
|
817
|
+
|
|
167
818
|
# lyra_purpose for controllers (around_action) and jobs (around_perform).
|
|
168
819
|
module ControllerMethods
|
|
169
820
|
def lyra_purpose(purpose, **options)
|
|
@@ -171,18 +822,200 @@ module Lyra
|
|
|
171
822
|
end
|
|
172
823
|
end
|
|
173
824
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
825
|
+
module JobMethods
|
|
826
|
+
def lyra_purpose(purpose)
|
|
827
|
+
around_perform { |_job, block| Lyra.with_purpose(purpose, &block) }
|
|
828
|
+
end
|
|
829
|
+
end
|
|
830
|
+
|
|
831
|
+
# The query hooks on ActiveRecord::Relation (Model.count and the other
|
|
832
|
+
# class-level forms are delegated to it). Installed above Lyra's bulk-write
|
|
833
|
+
# hooks (StrictDataAccessRelation), so a refused write is refused before
|
|
834
|
+
# anything is snapshotted or published.
|
|
835
|
+
module RelationCheck
|
|
836
|
+
def calculate(operation, column_name = nil, *rest, **options, &block)
|
|
837
|
+
return super unless PurposeBoundReads.checking?
|
|
838
|
+
|
|
839
|
+
column = column_name unless operation.to_s == "count" && [nil, :all, "*", "all"].include?(column_name)
|
|
840
|
+
PurposeBoundReads.checked_query(self, :all, column) do |names|
|
|
841
|
+
filtered = PurposeBoundReads.rights_filtered(self, names)
|
|
842
|
+
next super unless filtered
|
|
843
|
+
|
|
844
|
+
PurposeBoundReads.run_filtered { filtered.calculate(operation, column_name, *rest, **options, &block) }
|
|
845
|
+
end
|
|
846
|
+
end
|
|
847
|
+
|
|
848
|
+
def exists?(conditions = :none)
|
|
849
|
+
return super unless PurposeBoundReads.checking?
|
|
850
|
+
|
|
851
|
+
scope = conditions.is_a?(Hash) || conditions.is_a?(Array) ? where(conditions) : self
|
|
852
|
+
PurposeBoundReads.checked_query(scope, :all) do |names|
|
|
853
|
+
filtered = PurposeBoundReads.rights_filtered(self, names)
|
|
854
|
+
next super unless filtered
|
|
855
|
+
|
|
856
|
+
PurposeBoundReads.run_filtered { filtered.exists?(conditions) }
|
|
857
|
+
end
|
|
858
|
+
end
|
|
859
|
+
|
|
860
|
+
def ids
|
|
861
|
+
return super unless PurposeBoundReads.checking?
|
|
862
|
+
|
|
863
|
+
PurposeBoundReads.checked_query(self, :all) do |names|
|
|
864
|
+
filtered = PurposeBoundReads.rights_filtered(self, names)
|
|
865
|
+
next super unless filtered
|
|
866
|
+
|
|
867
|
+
PurposeBoundReads.run_filtered { filtered.ids }
|
|
868
|
+
end
|
|
869
|
+
end
|
|
870
|
+
|
|
177
871
|
def pluck(*column_names)
|
|
178
|
-
|
|
179
|
-
|
|
872
|
+
return super unless PurposeBoundReads.checking?
|
|
873
|
+
|
|
874
|
+
PurposeBoundReads.checked_query(self, :all, column_names) do |names|
|
|
875
|
+
filtered = PurposeBoundReads.rights_filtered(self, names)
|
|
876
|
+
next super unless filtered
|
|
877
|
+
|
|
878
|
+
PurposeBoundReads.run_filtered { filtered.pluck(*column_names) }
|
|
879
|
+
end
|
|
880
|
+
end
|
|
881
|
+
|
|
882
|
+
# A count and the latest timestamp of the relation's rows: an aggregate
|
|
883
|
+
# over its filter (cache_key_with_version asks it too).
|
|
884
|
+
def cache_version(timestamp_column = :updated_at)
|
|
885
|
+
return super unless PurposeBoundReads.checking?
|
|
886
|
+
|
|
887
|
+
PurposeBoundReads.checked_query(self, :all, timestamp_column) do |names|
|
|
888
|
+
filtered = PurposeBoundReads.rights_filtered(self, names)
|
|
889
|
+
next super unless filtered
|
|
890
|
+
|
|
891
|
+
PurposeBoundReads.run_filtered { filtered.cache_version(timestamp_column) }
|
|
892
|
+
end
|
|
893
|
+
end
|
|
894
|
+
|
|
895
|
+
# The filter is a use; so is every column the SET expressions read
|
|
896
|
+
# ("email = name", email: arel_table[:name]). The values written are not.
|
|
897
|
+
def update_all(updates)
|
|
898
|
+
return super unless PurposeBoundReads.checking?
|
|
899
|
+
|
|
900
|
+
PurposeBoundReads.checked_query(self, :all, RelationCheck.set_reads(updates)) { super }
|
|
901
|
+
end
|
|
902
|
+
|
|
903
|
+
def delete_all
|
|
904
|
+
return super unless PurposeBoundReads.checking?
|
|
905
|
+
|
|
906
|
+
PurposeBoundReads.checked_query(self, :all) { super }
|
|
907
|
+
end
|
|
908
|
+
|
|
909
|
+
# Checked as a filtered write; its records are then loaded (and checked)
|
|
910
|
+
# and destroyed with the application's callbacks, outside the check.
|
|
911
|
+
def destroy_all
|
|
912
|
+
return super unless PurposeBoundReads.checking?
|
|
913
|
+
|
|
914
|
+
PurposeBoundReads.checked_query(self, :all, around: false) { super }
|
|
915
|
+
end
|
|
916
|
+
|
|
917
|
+
# The columns a bulk insert returns are read back, from everyone it
|
|
918
|
+
# wrote (subject Model$*).
|
|
919
|
+
%i[insert_all insert_all! upsert_all].each do |name|
|
|
920
|
+
define_method(name) do |attributes, returning: nil, **options, &block|
|
|
921
|
+
if returning && PurposeBoundReads.checking?
|
|
922
|
+
names = PurposeBoundReads::NamedFields.new.value(returning, klass)
|
|
923
|
+
PurposeBoundReads.once { PurposeBoundReads.check_query(klass, names.result, :all) }
|
|
924
|
+
end
|
|
925
|
+
super(attributes, returning: returning, **options, &block)
|
|
926
|
+
end
|
|
927
|
+
end
|
|
928
|
+
|
|
929
|
+
# The batch loop's own queries (its cursor's plucks, each batch's load)
|
|
930
|
+
# are checked and recorded once; the application's block is its own code.
|
|
931
|
+
# Each batch is a checking scope (find_each and find_in_batches run
|
|
932
|
+
# through here): the rights are read afresh before the next batch loads.
|
|
933
|
+
def in_batches(*args, **options, &block)
|
|
934
|
+
return super unless block && PurposeBoundReads.checking?
|
|
935
|
+
|
|
936
|
+
PurposeBoundReads.batch_scopes do |next_batch|
|
|
937
|
+
PurposeBoundReads.once do
|
|
938
|
+
super(*args, **options) do |batch|
|
|
939
|
+
PurposeBoundReads.outside_once { block.call(batch) }
|
|
940
|
+
ensure
|
|
941
|
+
next_batch.call
|
|
942
|
+
end
|
|
943
|
+
end
|
|
944
|
+
end
|
|
945
|
+
end
|
|
946
|
+
|
|
947
|
+
# The columns an update_all's SET expressions read: Arel values, and the
|
|
948
|
+
# words of an SQL string other than the columns it assigns.
|
|
949
|
+
def self.set_reads(updates)
|
|
950
|
+
case updates
|
|
951
|
+
when Hash then updates.values.select { Arel.arel_node?(_1) }
|
|
952
|
+
when String then Arel.sql(updates.gsub(/"?\b\w+"?\s*=(?!=)/, " "))
|
|
953
|
+
when Array then updates.first.is_a?(String) ? set_reads(updates.first) : nil
|
|
954
|
+
end
|
|
955
|
+
end
|
|
956
|
+
|
|
957
|
+
private
|
|
958
|
+
|
|
959
|
+
# A record load (load, load_async, eager loads): what the query names,
|
|
960
|
+
# without the consent step, and what its subqueries name, whole. Each
|
|
961
|
+
# record is checked by after_find.
|
|
962
|
+
def exec_main_query(*args, **options)
|
|
963
|
+
return super unless PurposeBoundReads.checking?
|
|
964
|
+
|
|
965
|
+
names = PurposeBoundReads.query_names(self)
|
|
966
|
+
@lyra_query_names = names.result
|
|
967
|
+
PurposeBoundReads.once do
|
|
968
|
+
PurposeBoundReads.check_query(klass, @lyra_query_names, :load, names.whole, joined: names.joined_classes, opaque: names.opaque?)
|
|
969
|
+
super
|
|
970
|
+
end
|
|
971
|
+
end
|
|
972
|
+
|
|
973
|
+
# The rows of a grouped load are aggregates, not one person's records
|
|
974
|
+
# (Rights::SubjectKeys.aggregate_row?).
|
|
975
|
+
def instantiate_records(*args, **options, &block)
|
|
976
|
+
return super unless PurposeBoundReads.checking? && @lyra_query_names
|
|
977
|
+
|
|
978
|
+
PurposeBoundReads.with_query_names(klass, @lyra_query_names[klass]) do
|
|
979
|
+
next super if group_values.empty?
|
|
980
|
+
|
|
981
|
+
PurposeBoundReads::Context.with(:lyra_grouped_load, true) { super }
|
|
982
|
+
end
|
|
180
983
|
end
|
|
181
984
|
end
|
|
182
985
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
986
|
+
# Rails' uniqueness check runs exists? on the validated attribute as part
|
|
987
|
+
# of saving the record: a write, not a read for a purpose.
|
|
988
|
+
module UniquenessExemption
|
|
989
|
+
def validate_each(record, attribute, value)
|
|
990
|
+
Lyra::PurposeBoundReads.internal(INTERNAL) { super }
|
|
991
|
+
end
|
|
992
|
+
end
|
|
993
|
+
|
|
994
|
+
# What Rails does to a record's dependents when it is destroyed (an
|
|
995
|
+
# association's dependent: option) is part of destroying it, as a
|
|
996
|
+
# uniqueness check is part of saving it. :delete_all, :nullify, :delete
|
|
997
|
+
# and the restrict options run as Lyra's own work: their query on the
|
|
998
|
+
# dependents' key is not a read for a purpose. For :destroy, only the
|
|
999
|
+
# lookup of the dependents is: each is then destroyed as the application
|
|
1000
|
+
# destroys a record, with its own callbacks.
|
|
1001
|
+
module DependentExemption
|
|
1002
|
+
def handle_dependency
|
|
1003
|
+
if %i[destroy destroy_async].include?(options[:dependent])
|
|
1004
|
+
Lyra::PurposeBoundReads.internal(INTERNAL) { load_target }
|
|
1005
|
+
super
|
|
1006
|
+
else
|
|
1007
|
+
Lyra::PurposeBoundReads.internal(INTERNAL) { super }
|
|
1008
|
+
end
|
|
1009
|
+
end
|
|
1010
|
+
end
|
|
1011
|
+
|
|
1012
|
+
def self.install!
|
|
1013
|
+
ActiveRecord::Relation.prepend(RelationCheck) unless ActiveRecord::Relation.ancestors.include?(RelationCheck)
|
|
1014
|
+
validator = ActiveRecord::Validations::UniquenessValidator
|
|
1015
|
+
validator.prepend(UniquenessExemption) unless validator.ancestors.include?(UniquenessExemption)
|
|
1016
|
+
[ActiveRecord::Associations::HasManyAssociation, ActiveRecord::Associations::HasOneAssociation,
|
|
1017
|
+
ActiveRecord::Associations::BelongsToAssociation].each do |association|
|
|
1018
|
+
association.prepend(DependentExemption) unless association.ancestors.include?(DependentExemption)
|
|
186
1019
|
end
|
|
187
1020
|
end
|
|
188
1021
|
end
|
|
@@ -193,5 +1026,7 @@ end
|
|
|
193
1026
|
if defined?(ActiveSupport)
|
|
194
1027
|
ActiveSupport.on_load(:action_controller) { extend Lyra::PurposeBoundReads::ControllerMethods }
|
|
195
1028
|
ActiveSupport.on_load(:active_job) { extend Lyra::PurposeBoundReads::JobMethods }
|
|
196
|
-
|
|
1029
|
+
# Under Rails the engine installs the query hooks, after the bulk-write
|
|
1030
|
+
# hooks, so that they run first (Lyra::Engine).
|
|
1031
|
+
ActiveSupport.on_load(:active_record) { Lyra::PurposeBoundReads.install! } unless defined?(Lyra::Engine)
|
|
197
1032
|
end
|