mongoid 9.0.12 → 9.0.14
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/lib/config/locales/en.yml +44 -11
- data/lib/mongoid/association/embedded/batchable.rb +8 -0
- data/lib/mongoid/association/referenced/belongs_to.rb +16 -8
- data/lib/mongoid/association/referenced/has_and_belongs_to_many.rb +12 -0
- data/lib/mongoid/association/referenced/has_many/proxy.rb +4 -2
- data/lib/mongoid/attributes/readonly.rb +19 -1
- data/lib/mongoid/changeable.rb +21 -1
- data/lib/mongoid/clients/factory.rb +2 -0
- data/lib/mongoid/config/encryption.rb +152 -19
- data/lib/mongoid/contextual/none.rb +51 -0
- data/lib/mongoid/criteria/scopable.rb +7 -1
- data/lib/mongoid/criteria.rb +45 -1
- data/lib/mongoid/document.rb +10 -0
- data/lib/mongoid/encryptable.rb +43 -16
- data/lib/mongoid/errors/invalid_encrypted_association.rb +28 -0
- data/lib/mongoid/errors/invalid_encryption_schema.rb +27 -0
- data/lib/mongoid/errors/no_encryption_schema.rb +5 -3
- data/lib/mongoid/errors.rb +2 -0
- data/lib/mongoid/fields/encrypted.rb +14 -3
- data/lib/mongoid/fields/foreign_key.rb +9 -0
- data/lib/mongoid/fields/standard.rb +10 -0
- data/lib/mongoid/fields/validators/macro.rb +29 -0
- data/lib/mongoid/fields.rb +42 -2
- data/lib/mongoid/persistence_context.rb +46 -4
- data/lib/mongoid/reloadable.rb +1 -0
- data/lib/mongoid/search_indexable.rb +5 -1
- data/lib/mongoid/tasks/database.rb +10 -7
- data/lib/mongoid/threaded.rb +1 -1
- data/lib/mongoid/version.rb +1 -1
- data/lib/mongoid/warnings.rb +4 -0
- data/spec/integration/atomic/modifiers_spec.rb +57 -0
- data/spec/integration/encryption_spec.rb +418 -0
- data/spec/mongoid/association/referenced/encrypted_foreign_key_spec.rb +144 -0
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +48 -0
- data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +83 -0
- data/spec/mongoid/attributes/readonly_dotted_spec.rb +332 -0
- data/spec/mongoid/attributes/readonly_spec.rb +389 -0
- data/spec/mongoid/changeable_spec.rb +18 -0
- data/spec/mongoid/clients/factory_spec.rb +36 -0
- data/spec/mongoid/config/encryption_spec.rb +450 -0
- data/spec/mongoid/contextual/none_spec.rb +22 -5
- data/spec/mongoid/criteria_spec.rb +17 -0
- data/spec/mongoid/encryptable_spec.rb +54 -0
- data/spec/mongoid/fields_spec.rb +29 -0
- data/spec/mongoid/findable_spec.rb +14 -0
- data/spec/mongoid/matcher/regexp_budget_spec.rb +6 -0
- data/spec/mongoid/scopable_spec.rb +103 -2
- data/spec/mongoid/search_indexable_spec.rb +34 -1
- data/spec/mongoid/tasks/database_spec.rb +13 -0
- data/spec/spec_helper.rb +8 -0
- data/spec/support/crypt/models.rb +138 -0
- data/spec/support/models/delete_all_account.rb +10 -0
- data/spec/support/models/delete_all_item.rb +9 -0
- data/spec/support/models/delete_all_scoped_item.rb +11 -0
- data/spec/support/models/delete_all_tag.rb +7 -0
- data/spec/support/models/modifier_models.rb +32 -0
- metadata +1995 -1958
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 63461fddbbd4f1544e96e291c730597139e28fdc92631d4c43499cbcddb89d6a
|
|
4
|
+
data.tar.gz: 5cba7e07269662025db60f0f89a3f5b80d4a2e1d020bd368186c79a1b1cdb11f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e2b1c80725b0807a30c7a238a2007be6ea2ed851563db4a0dfed748f94a40143045eb48a6d3662af8c9a23021419841464595cad3bcb67563e02fb2ae3950580
|
|
7
|
+
data.tar.gz: 04a57c874d0f94569cbc579cdef32b4211eb65d2cd047a741b53b2e89a9f98f79ca5563cdc8c61959072e058a5c3ebc9d4342366489fb777f2caee583816b27e
|
data/lib/config/locales/en.yml
CHANGED
|
@@ -239,6 +239,26 @@ en:
|
|
|
239
239
|
Mongoid does not support."
|
|
240
240
|
resolution: "Valid field operators are: %{valid_operators}.
|
|
241
241
|
Ensure you are using one of these operators."
|
|
242
|
+
invalid_encrypted_association:
|
|
243
|
+
message: "The %{association} association of %{klass} uses the encrypted
|
|
244
|
+
field %{field} as its foreign key."
|
|
245
|
+
summary: "Client-side field level encryption can encrypt an array
|
|
246
|
+
only as a whole, using the random algorithm; the deterministic
|
|
247
|
+
algorithm does not support arrays at all. A
|
|
248
|
+
has_and_belongs_to_many association stores its foreign key in an
|
|
249
|
+
array and queries and atomically updates individual elements of
|
|
250
|
+
it, which encryption does not support. Mongoid raises this error
|
|
251
|
+
when the association or the field is defined, whichever comes
|
|
252
|
+
first, rather than defining a key the association cannot manage."
|
|
253
|
+
resolution: "Remove the encryption from %{field}, or store a single
|
|
254
|
+
encrypted value with a belongs_to association instead."
|
|
255
|
+
invalid_encryption_schema:
|
|
256
|
+
message: "The encryption schema for %{namespace} is invalid."
|
|
257
|
+
summary: "Two models writing to %{namespace} describe %{path}
|
|
258
|
+
differently. The encryption schema has one entry per namespace,
|
|
259
|
+
so the two descriptions cannot both be used."
|
|
260
|
+
resolution: "Make the models agree on the encryption options of the
|
|
261
|
+
conflicting field, or store them in different collections."
|
|
242
262
|
invalid_estimated_count_criteria:
|
|
243
263
|
message: "Invalid criteria for estimated_count."
|
|
244
264
|
summary: "Estimated count is strictly a collection-level operation and cannot be called
|
|
@@ -544,22 +564,35 @@ en:
|
|
|
544
564
|
no_encryption_schema:
|
|
545
565
|
message: "The encryption schema of client %{client} does not cover
|
|
546
566
|
%{namespace}."
|
|
547
|
-
summary: "%{klass} declares encrypted fields,
|
|
548
|
-
resolved to, %{namespace},
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
567
|
+
summary: "%{klass} declares encrypted fields, directly or through
|
|
568
|
+
an embedded model, but the namespace it resolved to, %{namespace},
|
|
569
|
+
is not part of the automatic encryption schema of client
|
|
570
|
+
%{client}. The encryption schema is built once, when the client
|
|
571
|
+
is created, and is keyed by namespace, so a database name that is
|
|
572
|
+
only known later - a callable :database option,
|
|
573
|
+
Model.with(database:), Mongoid.override_database - is not in it.
|
|
574
|
+
The same happens when an embedded model with encrypted fields was
|
|
575
|
+
not yet loaded when the client was created. Routing the model to a
|
|
576
|
+
client that has no auto_encryption_options has the same effect.
|
|
577
|
+
Mongoid refuses the operation because the driver would store the
|
|
578
|
+
declared fields in plaintext without reporting an error."
|
|
557
579
|
resolution: "Give the model a namespace that is known when the
|
|
558
580
|
client is created, and a client configured with
|
|
559
|
-
auto_encryption_options.
|
|
581
|
+
auto_encryption_options. Load all models, including embedded
|
|
582
|
+
ones, before the first client is created, for example by
|
|
583
|
+
preloading them at boot. If the model has to live in a database
|
|
560
584
|
chosen at runtime, configure one client per database, each with
|
|
561
585
|
its own auto_encryption_options, and select between them with
|
|
562
586
|
Model.with(client:) instead of switching the database."
|
|
587
|
+
no_encryption_schema_field:
|
|
588
|
+
message: "The encryption schema of client %{client} does not cover
|
|
589
|
+
the field %{field} of %{namespace}."
|
|
590
|
+
summary: "The namespace %{namespace} is in the automatic encryption
|
|
591
|
+
schema of client %{client}, but its entry does not encrypt
|
|
592
|
+
%{field}, which %{klass} declares as encrypted. Mongoid refuses
|
|
593
|
+
the operation rather than storing the field in plaintext."
|
|
594
|
+
resolution: "Regenerate the schema map of the client, or add
|
|
595
|
+
%{field} to the schema entry for %{namespace}."
|
|
563
596
|
no_map_reduce_output:
|
|
564
597
|
message: "No output location was specified for the map/reduce
|
|
565
598
|
operation."
|
|
@@ -138,6 +138,14 @@ module Mongoid
|
|
|
138
138
|
_base.collect_children.each do |child|
|
|
139
139
|
child.delayed_atomic_sets.clear
|
|
140
140
|
end
|
|
141
|
+
|
|
142
|
+
# If the base document is itself inserted wholesale via a $set of
|
|
143
|
+
# its atomic path, that $set already contains these documents, and
|
|
144
|
+
# a nested $set under the same path would conflict with it. The
|
|
145
|
+
# cleanup above has already cleared the delayed sets the assigned
|
|
146
|
+
# children recorded, so skipping the recording here is safe.
|
|
147
|
+
return if _base.settable?
|
|
148
|
+
|
|
141
149
|
_base.delayed_atomic_sets[path] = sets
|
|
142
150
|
end
|
|
143
151
|
end
|
|
@@ -227,14 +227,22 @@ module Mongoid
|
|
|
227
227
|
end
|
|
228
228
|
|
|
229
229
|
def create_foreign_key_field!
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
230
|
+
options = {
|
|
231
|
+
type: FOREIGN_KEY_FIELD_TYPE,
|
|
232
|
+
identity: true,
|
|
233
|
+
overwrite: true,
|
|
234
|
+
association: self,
|
|
235
|
+
default: nil
|
|
236
|
+
}
|
|
237
|
+
# The association must not replace an encrypted field with a plain
|
|
238
|
+
# foreign key: the key would then be stored in the clear. Keep the
|
|
239
|
+
# declared type as well, so the encryption schema describes it
|
|
240
|
+
# correctly.
|
|
241
|
+
if (existing = @owner_class.fields[foreign_key])&.encrypted?
|
|
242
|
+
options[:type] = existing.type
|
|
243
|
+
options[:encrypt] = existing.options[:encrypt]
|
|
244
|
+
end
|
|
245
|
+
@owner_class.field(foreign_key, options)
|
|
238
246
|
end
|
|
239
247
|
end
|
|
240
248
|
end
|
|
@@ -231,6 +231,18 @@ module Mongoid
|
|
|
231
231
|
end
|
|
232
232
|
|
|
233
233
|
def create_foreign_key_field!
|
|
234
|
+
# An encrypted array field cannot be used as the foreign key:
|
|
235
|
+
# encryption protects the array only as a whole, while this
|
|
236
|
+
# association queries and atomically updates its elements. Reject
|
|
237
|
+
# the association rather than replace the field and store the key in
|
|
238
|
+
# plaintext. The field may already be declared on a parent, whose
|
|
239
|
+
# declaration is propagated to the descendants.
|
|
240
|
+
([ @owner_class ] + @owner_class.descendants).each do |candidate|
|
|
241
|
+
next unless candidate.fields[foreign_key]&.encrypted?
|
|
242
|
+
|
|
243
|
+
raise Errors::InvalidEncryptedAssociation.new(candidate, name, foreign_key)
|
|
244
|
+
end
|
|
245
|
+
|
|
234
246
|
inverse_class.aliased_associations[foreign_key] = name.to_s
|
|
235
247
|
@owner_class.field(
|
|
236
248
|
foreign_key,
|
|
@@ -533,8 +533,10 @@ module Mongoid
|
|
|
533
533
|
#
|
|
534
534
|
# @return [ Integer ] The number of documents deleted.
|
|
535
535
|
def remove_all(conditions = nil, method = :delete_all)
|
|
536
|
-
|
|
537
|
-
|
|
536
|
+
# Conjoin the conditions with the association selector so that a
|
|
537
|
+
# key present in both is intersected rather than overwritten. This
|
|
538
|
+
# also leaves the caller's Hash alone.
|
|
539
|
+
selector = criteria.where(conditions || {}).selector
|
|
538
540
|
|
|
539
541
|
# Scanning before the delete means scanning only what is already in
|
|
540
542
|
# memory, so it is only done where there is a pattern whose cost has
|
|
@@ -23,7 +23,25 @@ module Mongoid
|
|
|
23
23
|
# @return [ true | false ] If the document is new, or if the field is not
|
|
24
24
|
# readonly.
|
|
25
25
|
def attribute_writable?(name)
|
|
26
|
-
|
|
26
|
+
return true if new_record?
|
|
27
|
+
|
|
28
|
+
readonly = self.class.readonly_attributes
|
|
29
|
+
return _loaded?(name) if readonly.empty?
|
|
30
|
+
|
|
31
|
+
path = name.to_s
|
|
32
|
+
return false if readonly.include?(path)
|
|
33
|
+
|
|
34
|
+
readonly.each do |field|
|
|
35
|
+
# A readonly attribute that is a declared field with a dotted name
|
|
36
|
+
# is that literal field, not a path prefix.
|
|
37
|
+
next if field.include?('.') && self.class.fields.key?(field)
|
|
38
|
+
|
|
39
|
+
# A path that is a declared field with a dotted name addresses that
|
|
40
|
+
# literal field, not a descendant of the readonly attribute.
|
|
41
|
+
return false if path.start_with?("#{field}.") && !self.class.fields.key?(path)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
_loaded?(name)
|
|
27
45
|
end
|
|
28
46
|
|
|
29
47
|
private
|
data/lib/mongoid/changeable.rb
CHANGED
|
@@ -80,6 +80,7 @@ module Mongoid
|
|
|
80
80
|
@children_may_have_changed = false
|
|
81
81
|
reset_atomic_updates!
|
|
82
82
|
changed_attributes.clear
|
|
83
|
+
@pre_create_changed_attributes = nil
|
|
83
84
|
end
|
|
84
85
|
|
|
85
86
|
# Things that need to execute after a document has been persisted.
|
|
@@ -263,15 +264,24 @@ module Mongoid
|
|
|
263
264
|
# @param [ Object ] to The object the attribute was changed to (optional).
|
|
264
265
|
#
|
|
265
266
|
# @return [ true | false ] Whether the attribute has changed.
|
|
267
|
+
# rubocop:disable Metrics/AbcSize
|
|
266
268
|
def attribute_changed?(attr, from: ATTRIBUTE_UNCHANGED, to: ATTRIBUTE_UNCHANGED)
|
|
267
269
|
attr = database_field_name(attr)
|
|
268
270
|
return false unless changed_attributes.key?(attr)
|
|
271
|
+
# An in-place change to a readonly attribute of a persisted document
|
|
272
|
+
# is not part of the dirty tracking API and will not be persisted. A
|
|
273
|
+
# change tracked while the document was new initializes the document,
|
|
274
|
+
# and new documents may set readonly attributes, so it is still
|
|
275
|
+
# reported as a saved change.
|
|
276
|
+
return false if !new_record? && self.class.readonly_attributes.include?(attr) &&
|
|
277
|
+
!@pre_create_changed_attributes&.include?(attr)
|
|
269
278
|
return false if changed_attributes[attr] == attributes[attr]
|
|
270
279
|
return false if from != changed_attributes[attr]
|
|
271
280
|
return false if to != attributes[attr]
|
|
272
281
|
|
|
273
282
|
true
|
|
274
283
|
end
|
|
284
|
+
# rubocop:enable Metrics/AbcSize
|
|
275
285
|
|
|
276
286
|
# Get whether or not the field has a different value from the default.
|
|
277
287
|
#
|
|
@@ -328,6 +338,13 @@ module Mongoid
|
|
|
328
338
|
def attribute_will_change!(attr)
|
|
329
339
|
return if changed_attributes.key?(attr)
|
|
330
340
|
|
|
341
|
+
# The snapshot is taken for readonly attributes as well. Their changes
|
|
342
|
+
# are hidden from the dirty tracking API (see #attribute_changed?), but
|
|
343
|
+
# the snapshot is needed to reset an in-place change. Changes tracked
|
|
344
|
+
# while the document is new initialize it, and new documents may set
|
|
345
|
+
# readonly attributes, so they are remembered here and still reported
|
|
346
|
+
# when the document is created.
|
|
347
|
+
(@pre_create_changed_attributes ||= ::Set.new).add(attr) if new_record?
|
|
331
348
|
changed_attributes[attr] = read_raw_attribute(attr).__deep_copy__
|
|
332
349
|
end
|
|
333
350
|
|
|
@@ -341,7 +358,10 @@ module Mongoid
|
|
|
341
358
|
# @return [ Object ] The old value.
|
|
342
359
|
def reset_attribute!(attr)
|
|
343
360
|
attr = database_field_name(attr)
|
|
344
|
-
|
|
361
|
+
return unless changed_attributes.key?(attr)
|
|
362
|
+
return if changed_attributes[attr] == attributes[attr]
|
|
363
|
+
|
|
364
|
+
attributes[attr] = changed_attributes.delete(attr)
|
|
345
365
|
end
|
|
346
366
|
|
|
347
367
|
def reset_attribute_to_default!(attr)
|
|
@@ -91,6 +91,8 @@ module Mongoid
|
|
|
91
91
|
return nil unless opts[:auto_encryption_options]
|
|
92
92
|
|
|
93
93
|
opts[:auto_encryption_options].dup.tap do |auto_encryption_options|
|
|
94
|
+
Mongoid::Warnings.warn_bypass_auto_encryption if auto_encryption_options[:bypass_auto_encryption]
|
|
95
|
+
|
|
94
96
|
if auto_encryption_options.key?(:schema_map)
|
|
95
97
|
default_logger.warn(
|
|
96
98
|
'The :schema_map is configured in the :auto_encryption_options for the client;' +
|
|
@@ -21,7 +21,11 @@ module Mongoid
|
|
|
21
21
|
#
|
|
22
22
|
# @return [ Hash ] The encryption schema map.
|
|
23
23
|
def encryption_schema_map(default_database, models = ::Mongoid.models)
|
|
24
|
-
|
|
24
|
+
# Subclasses share their parent's collection but are not registered in
|
|
25
|
+
# Mongoid.models, so they have to be added to the walk explicitly.
|
|
26
|
+
classes = models.flat_map { |model| [ model ] + model.descendants }.uniq
|
|
27
|
+
|
|
28
|
+
classes.each_with_object({}) do |model, map|
|
|
25
29
|
next if model.embedded?
|
|
26
30
|
next unless model.requires_encryption_schema?
|
|
27
31
|
|
|
@@ -35,14 +39,33 @@ module Mongoid
|
|
|
35
39
|
next if database.respond_to?(:call)
|
|
36
40
|
|
|
37
41
|
key = "#{database}.#{model.collection_name}"
|
|
38
|
-
props = metadata_for(model).merge(properties_for(model, [ model ]))
|
|
42
|
+
props = metadata_for(model).merge(properties_for(model, [ model ], key))
|
|
43
|
+
next if props.empty?
|
|
44
|
+
|
|
39
45
|
# The root of a collection schema describes the document, so it is
|
|
40
46
|
# always an object. Saying so matters when a nested schema carries
|
|
41
47
|
# encryptMetadata: mongocryptd rejects the schema otherwise.
|
|
42
|
-
|
|
48
|
+
entry = { 'bsonType' => 'object' }.merge(props)
|
|
49
|
+
map[key] = map.key?(key) ? merge_schema_entries(map[key], entry, key) : entry
|
|
43
50
|
end
|
|
44
51
|
end
|
|
45
52
|
|
|
53
|
+
# Collect the paths of the fields encrypted by the schema generated for
|
|
54
|
+
# a single model.
|
|
55
|
+
#
|
|
56
|
+
# @param [ Mongoid::Document ] model The model.
|
|
57
|
+
#
|
|
58
|
+
# @return [ Array<Array<String>> ] The property paths, outermost first.
|
|
59
|
+
#
|
|
60
|
+
# @api private
|
|
61
|
+
def encrypted_paths_for(model)
|
|
62
|
+
return model.instance_variable_get(:@encrypted_paths_for) if
|
|
63
|
+
model.instance_variable_defined?(:@encrypted_paths_for)
|
|
64
|
+
|
|
65
|
+
properties = properties_for(model, [ model ])['properties'] || {}
|
|
66
|
+
model.instance_variable_set(:@encrypted_paths_for, collect_encrypted_paths(properties, []))
|
|
67
|
+
end
|
|
68
|
+
|
|
46
69
|
private
|
|
47
70
|
|
|
48
71
|
# The algorithm to use for the deterministic encryption.
|
|
@@ -110,10 +133,12 @@ module Mongoid
|
|
|
110
133
|
# @param [ Mongoid::Document ] model The model to generate the properties for.
|
|
111
134
|
# @param [ Array<Mongoid::Document> ] path The models the walk is already
|
|
112
135
|
# inside of, outermost first.
|
|
136
|
+
# @param [ String | nil ] namespace The namespace the schema is built
|
|
137
|
+
# for, used for error reporting.
|
|
113
138
|
#
|
|
114
139
|
# @return [ Hash ] The encryption properties.
|
|
115
|
-
def properties_for(model, path)
|
|
116
|
-
result = properties_for_fields(model).merge(properties_for_relations(model, path))
|
|
140
|
+
def properties_for(model, path, namespace = nil)
|
|
141
|
+
result = properties_for_fields(model).merge(properties_for_relations(model, path, namespace))
|
|
117
142
|
if result.empty?
|
|
118
143
|
{}
|
|
119
144
|
else
|
|
@@ -128,7 +153,7 @@ module Mongoid
|
|
|
128
153
|
# @return [ Hash ] The encryption properties.
|
|
129
154
|
def properties_for_fields(model)
|
|
130
155
|
model.fields.each_with_object({}) do |(name, field), props|
|
|
131
|
-
next unless field.
|
|
156
|
+
next unless field.encrypted?
|
|
132
157
|
|
|
133
158
|
props[name] = {
|
|
134
159
|
'encrypt' => {
|
|
@@ -152,10 +177,12 @@ module Mongoid
|
|
|
152
177
|
# @param [ Mongoid::Document ] model The model to generate the properties for.
|
|
153
178
|
# @param [ Array<Mongoid::Document> ] path The models the walk is already
|
|
154
179
|
# inside of, outermost first.
|
|
180
|
+
# @param [ String | nil ] namespace The namespace the schema is built
|
|
181
|
+
# for, used for error reporting.
|
|
155
182
|
#
|
|
156
183
|
# @return [ Hash ] The encryption properties.
|
|
157
|
-
def properties_for_relations(model, path)
|
|
158
|
-
model.relations.each_with_object({}) do |(
|
|
184
|
+
def properties_for_relations(model, path, namespace = nil)
|
|
185
|
+
model.relations.each_with_object({}) do |(_name, relation), props|
|
|
159
186
|
# relation_class constantizes, and a polymorphic embedded_in has no
|
|
160
187
|
# class to resolve, so the relation type has to be checked first.
|
|
161
188
|
next unless relation.is_a?(Association::Embedded::EmbedsOne)
|
|
@@ -164,21 +191,127 @@ module Mongoid
|
|
|
164
191
|
# An association target does not have to be a Mongoid document, and
|
|
165
192
|
# the class it names does not have to exist.
|
|
166
193
|
next unless klass.respond_to?(:requires_encryption_schema?)
|
|
167
|
-
|
|
168
|
-
#
|
|
169
|
-
#
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
194
|
+
|
|
195
|
+
# Any subclass of the declared class can be assigned to the
|
|
196
|
+
# association, so its encrypted fields have to be mapped here too.
|
|
197
|
+
properties = ([ klass ] + klass.descendants).uniq.inject({}) do |merged, candidate|
|
|
198
|
+
# Stop at a model the walk is already inside of, or a self-embedding
|
|
199
|
+
# model never terminates. The path covers the current branch only:
|
|
200
|
+
# a model embedded by two parents, or twice by one parent, has to be
|
|
201
|
+
# emitted at every place it appears.
|
|
202
|
+
next merged if path.include?(candidate)
|
|
203
|
+
next merged unless candidate.respond_to?(:requires_encryption_schema?)
|
|
204
|
+
next merged unless candidate.requires_encryption_schema?
|
|
205
|
+
|
|
206
|
+
merge_schema_entries(
|
|
207
|
+
merged,
|
|
208
|
+
metadata_for(candidate).merge(
|
|
209
|
+
properties_for(candidate, path + [ candidate ], namespace)
|
|
210
|
+
),
|
|
211
|
+
namespace
|
|
212
|
+
)
|
|
178
213
|
end
|
|
214
|
+
# The document is persisted under the store_as key, not under the
|
|
215
|
+
# name of the association, so that is the key the schema needs.
|
|
216
|
+
props[relation.store_as] = { 'bsonType' => 'object' }.merge(properties) unless properties.empty?
|
|
179
217
|
end
|
|
180
218
|
end
|
|
181
219
|
|
|
220
|
+
# Walk the schema properties and collect the path of every encrypted
|
|
221
|
+
# leaf.
|
|
222
|
+
#
|
|
223
|
+
# @param [ Hash ] properties The properties to walk.
|
|
224
|
+
# @param [ Array<String> ] path The properties the walk is already
|
|
225
|
+
# inside of.
|
|
226
|
+
#
|
|
227
|
+
# @return [ Array<Array<String>> ] The property paths.
|
|
228
|
+
def collect_encrypted_paths(properties, path)
|
|
229
|
+
properties.each_with_object([]) do |(name, schema), paths|
|
|
230
|
+
if schema.key?('encrypt')
|
|
231
|
+
paths << (path + [ name ])
|
|
232
|
+
elsif schema['properties']
|
|
233
|
+
paths.concat(collect_encrypted_paths(schema['properties'], path + [ name ]))
|
|
234
|
+
end
|
|
235
|
+
end
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
# Merge two schema objects covering the same model or namespace.
|
|
239
|
+
#
|
|
240
|
+
# Every model writing to one namespace contributes an entry to the
|
|
241
|
+
# schema map. Those entries have to agree on everything they have in
|
|
242
|
+
# common, or the schema would silently encrypt a field differently for
|
|
243
|
+
# one model than for another.
|
|
244
|
+
#
|
|
245
|
+
# @param [ Hash ] left The first schema.
|
|
246
|
+
# @param [ Hash ] right The second schema.
|
|
247
|
+
# @param [ String | nil ] namespace The namespace the schema belongs to.
|
|
248
|
+
#
|
|
249
|
+
# @return [ Hash ] The merged schema.
|
|
250
|
+
def merge_schema_entries(left, right, namespace = nil)
|
|
251
|
+
left.merge(right) do |key, left_value, right_value|
|
|
252
|
+
case key
|
|
253
|
+
when 'bsonType'
|
|
254
|
+
left_value
|
|
255
|
+
when 'encryptMetadata'
|
|
256
|
+
merge_encrypt_metadata(left_value, right_value, namespace)
|
|
257
|
+
when 'properties'
|
|
258
|
+
merge_properties(left_value, right_value, namespace)
|
|
259
|
+
else
|
|
260
|
+
raise_invalid_encryption_schema(namespace, key)
|
|
261
|
+
end
|
|
262
|
+
end
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
# Merge two encryptMetadata objects.
|
|
266
|
+
#
|
|
267
|
+
# @param [ Hash ] left The first encryptMetadata object.
|
|
268
|
+
# @param [ Hash ] right The second encryptMetadata object.
|
|
269
|
+
# @param [ String | nil ] namespace The namespace the schema belongs to.
|
|
270
|
+
#
|
|
271
|
+
# @return [ Hash ] The merged encryptMetadata object.
|
|
272
|
+
def merge_encrypt_metadata(left, right, namespace)
|
|
273
|
+
left.merge(right) do |key, left_value, right_value|
|
|
274
|
+
unless left_value == right_value
|
|
275
|
+
raise_invalid_encryption_schema(namespace, "encryptMetadata.#{key}")
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
left_value
|
|
279
|
+
end
|
|
280
|
+
end
|
|
281
|
+
|
|
282
|
+
# Merge two properties objects.
|
|
283
|
+
#
|
|
284
|
+
# @param [ Hash ] left The first properties object.
|
|
285
|
+
# @param [ Hash ] right The second properties object.
|
|
286
|
+
# @param [ String | nil ] namespace The namespace the schema belongs to.
|
|
287
|
+
#
|
|
288
|
+
# @return [ Hash ] The merged properties object.
|
|
289
|
+
def merge_properties(left, right, namespace)
|
|
290
|
+
left.merge(right) do |name, left_value, right_value|
|
|
291
|
+
if left_value.key?('encrypt') && right_value.key?('encrypt')
|
|
292
|
+
unless left_value['encrypt'] == right_value['encrypt']
|
|
293
|
+
raise_invalid_encryption_schema(namespace, "properties.#{name}")
|
|
294
|
+
end
|
|
295
|
+
|
|
296
|
+
left_value
|
|
297
|
+
elsif left_value.key?('encrypt') || right_value.key?('encrypt')
|
|
298
|
+
raise_invalid_encryption_schema(namespace, "properties.#{name}")
|
|
299
|
+
else
|
|
300
|
+
merge_schema_entries(left_value, right_value, namespace)
|
|
301
|
+
end
|
|
302
|
+
end
|
|
303
|
+
end
|
|
304
|
+
|
|
305
|
+
# Raise an error for a schema conflicting with another one.
|
|
306
|
+
#
|
|
307
|
+
# @param [ String | nil ] namespace The namespace the schema belongs to.
|
|
308
|
+
# @param [ String ] path The conflicting path in the schema.
|
|
309
|
+
#
|
|
310
|
+
# @raise [ Errors::InvalidEncryptionSchema ] Always raised.
|
|
311
|
+
def raise_invalid_encryption_schema(namespace, path)
|
|
312
|
+
raise Errors::InvalidEncryptionSchema.new(namespace, path)
|
|
313
|
+
end
|
|
314
|
+
|
|
182
315
|
# Get the BSON type identifier for the provided field according to the
|
|
183
316
|
# https://www.mongodb.com/docs/manual/reference/bson-types/#std-label-bson-types
|
|
184
317
|
#
|
|
@@ -28,6 +28,31 @@ module Mongoid
|
|
|
28
28
|
other.is_a?(None)
|
|
29
29
|
end
|
|
30
30
|
|
|
31
|
+
# Delete all documents in the database that match the selector. There
|
|
32
|
+
# are no documents in the null context, so nothing is deleted.
|
|
33
|
+
#
|
|
34
|
+
# @example Delete all the documents.
|
|
35
|
+
# context.delete
|
|
36
|
+
#
|
|
37
|
+
# @return [ Integer ] Always zero.
|
|
38
|
+
def delete
|
|
39
|
+
0
|
|
40
|
+
end
|
|
41
|
+
alias delete_all delete
|
|
42
|
+
|
|
43
|
+
# Destroy all documents in the database that match the selector. There
|
|
44
|
+
# are no documents in the null context, so nothing is destroyed and no
|
|
45
|
+
# callbacks are run.
|
|
46
|
+
#
|
|
47
|
+
# @example Destroy all the documents.
|
|
48
|
+
# context.destroy
|
|
49
|
+
#
|
|
50
|
+
# @return [ Integer ] Always zero.
|
|
51
|
+
def destroy
|
|
52
|
+
0
|
|
53
|
+
end
|
|
54
|
+
alias destroy_all destroy
|
|
55
|
+
|
|
31
56
|
# Get the distinct field values in null context.
|
|
32
57
|
#
|
|
33
58
|
# @example Get the distinct values in null context.
|
|
@@ -111,6 +136,32 @@ module Mongoid
|
|
|
111
136
|
{}
|
|
112
137
|
end
|
|
113
138
|
|
|
139
|
+
# Update the first matching document atomically. There are no documents
|
|
140
|
+
# in the null context, so nothing is updated.
|
|
141
|
+
#
|
|
142
|
+
# @example Update the matching document.
|
|
143
|
+
# context.update(name: "Smiths")
|
|
144
|
+
#
|
|
145
|
+
# @param [ Hash ] attributes The new attributes for the document.
|
|
146
|
+
#
|
|
147
|
+
# @return [ nil | false ] False if no attributes were provided.
|
|
148
|
+
def update(attributes = nil, _opts = {})
|
|
149
|
+
attributes ? nil : false
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
# Update all the matching documents atomically. There are no documents
|
|
153
|
+
# in the null context, so nothing is updated.
|
|
154
|
+
#
|
|
155
|
+
# @example Update all the matching documents.
|
|
156
|
+
# context.update_all(name: "Smiths")
|
|
157
|
+
#
|
|
158
|
+
# @param [ Hash ] attributes The new attributes for each document.
|
|
159
|
+
#
|
|
160
|
+
# @return [ nil | false ] False if no attributes were provided.
|
|
161
|
+
def update_all(attributes = nil, _opts = {})
|
|
162
|
+
attributes ? nil : false
|
|
163
|
+
end
|
|
164
|
+
|
|
114
165
|
# Create the new null context.
|
|
115
166
|
#
|
|
116
167
|
# @example Create the new context.
|
|
@@ -16,9 +16,13 @@ module Mongoid
|
|
|
16
16
|
#
|
|
17
17
|
# @return [ Criteria ] The criteria.
|
|
18
18
|
def apply_default_scope
|
|
19
|
+
explicit_none = none_state == true
|
|
19
20
|
klass.without_default_scope do
|
|
20
21
|
merge!(klass.default_scoping.call)
|
|
21
22
|
end
|
|
23
|
+
# A none coming from the default scope is not an explicit none, so it
|
|
24
|
+
# may be removed by unscoping.
|
|
25
|
+
@none = :default if !explicit_none && @none
|
|
22
26
|
self.scoping_options = true, false
|
|
23
27
|
end
|
|
24
28
|
|
|
@@ -103,7 +107,9 @@ module Mongoid
|
|
|
103
107
|
crit = clone
|
|
104
108
|
unless unscoped?
|
|
105
109
|
crit.scoping_options = false, true
|
|
106
|
-
crit.selector.clear
|
|
110
|
+
crit.selector.clear
|
|
111
|
+
crit.options.clear
|
|
112
|
+
crit.send(:clear_default_scope_none)
|
|
107
113
|
end
|
|
108
114
|
crit
|
|
109
115
|
end
|
data/lib/mongoid/criteria.rb
CHANGED
|
@@ -312,6 +312,20 @@ module Mongoid
|
|
|
312
312
|
self.documents = other.documents.dup unless other.documents.empty?
|
|
313
313
|
self.scoping_options = other.scoping_options
|
|
314
314
|
self.inclusions = (inclusions + other.inclusions).uniq
|
|
315
|
+
if other.empty_and_chainable?
|
|
316
|
+
# An explicit none wins over a none that only came from a default
|
|
317
|
+
# scope. Association proxies forward is_a? to their target, so the
|
|
318
|
+
# runtime class is checked instead; anything that is not a criteria is
|
|
319
|
+
# treated as an explicit none.
|
|
320
|
+
explicit = if Criteria === other # rubocop:disable Style/CaseEquality
|
|
321
|
+
other.send(:none_state) == true
|
|
322
|
+
else
|
|
323
|
+
true
|
|
324
|
+
end
|
|
325
|
+
@none = true if @none == true || explicit
|
|
326
|
+
@none ||= :default
|
|
327
|
+
@context = nil
|
|
328
|
+
end
|
|
315
329
|
self
|
|
316
330
|
end
|
|
317
331
|
|
|
@@ -323,7 +337,9 @@ module Mongoid
|
|
|
323
337
|
#
|
|
324
338
|
# @return [ Criteria ] The none criteria.
|
|
325
339
|
def none
|
|
326
|
-
@none = true
|
|
340
|
+
@none = true
|
|
341
|
+
@context = nil
|
|
342
|
+
self
|
|
327
343
|
end
|
|
328
344
|
|
|
329
345
|
# Is the criteria an empty but chainable criteria?
|
|
@@ -510,6 +526,34 @@ module Mongoid
|
|
|
510
526
|
|
|
511
527
|
private
|
|
512
528
|
|
|
529
|
+
# The kind of none on this criteria, if any. An explicit none is +true+,
|
|
530
|
+
# a none coming only from the default scope is +:default+, and a criteria
|
|
531
|
+
# that is not none returns +nil+.
|
|
532
|
+
#
|
|
533
|
+
# @api private
|
|
534
|
+
#
|
|
535
|
+
# @example Get the none state.
|
|
536
|
+
# criteria.send(:none_state)
|
|
537
|
+
#
|
|
538
|
+
# @return [ true | :default | nil ] The none state.
|
|
539
|
+
def none_state
|
|
540
|
+
@none
|
|
541
|
+
end
|
|
542
|
+
|
|
543
|
+
# Removes the none flag if it was set only by the default scope. An
|
|
544
|
+
# explicitly requested none is never removed.
|
|
545
|
+
#
|
|
546
|
+
# @api private
|
|
547
|
+
#
|
|
548
|
+
# @example Clear a default scope none.
|
|
549
|
+
# criteria.send(:clear_default_scope_none)
|
|
550
|
+
#
|
|
551
|
+
# @return [ Criteria ] The criteria.
|
|
552
|
+
def clear_default_scope_none
|
|
553
|
+
@none = nil if @none == :default
|
|
554
|
+
self
|
|
555
|
+
end
|
|
556
|
+
|
|
513
557
|
# Are documents in the query missing, and are we configured to raise an
|
|
514
558
|
# error?
|
|
515
559
|
#
|