mongoid 9.1.1 → 9.1.3
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/accessors.rb +21 -4
- data/lib/mongoid/association/eager_load/discriminated_inclusion.rb +10 -10
- data/lib/mongoid/association/eager_load/embedded_distributor.rb +6 -6
- data/lib/mongoid/association/eager_load/lookup_pipeline.rb +1 -1
- data/lib/mongoid/association/eager_loadable.rb +8 -1
- data/lib/mongoid/association/embedded/batchable.rb +17 -1
- data/lib/mongoid/association/nested/many.rb +1 -1
- data/lib/mongoid/association/referenced/belongs_to/buildable.rb +19 -15
- data/lib/mongoid/association/referenced/belongs_to.rb +11 -3
- data/lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb +25 -5
- data/lib/mongoid/association/referenced/has_and_belongs_to_many.rb +12 -0
- data/lib/mongoid/association/referenced/has_many/buildable.rb +17 -7
- data/lib/mongoid/association/referenced/has_many/proxy.rb +4 -2
- data/lib/mongoid/association/referenced/has_one/buildable.rb +20 -6
- data/lib/mongoid/association/relatable.rb +18 -0
- data/lib/mongoid/association.rb +23 -0
- data/lib/mongoid/atomic/modifiers.rb +100 -16
- data/lib/mongoid/attributes/readonly.rb +19 -1
- data/lib/mongoid/changeable.rb +19 -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 +5 -0
- data/lib/mongoid/criteria.rb +45 -1
- data/lib/mongoid/document.rb +13 -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/factory.rb +1 -1
- 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 +49 -2
- data/lib/mongoid/persistable/creatable.rb +120 -14
- data/lib/mongoid/persistable.rb +15 -15
- 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/touchable.rb +28 -1
- data/lib/mongoid/version.rb +1 -1
- data/lib/mongoid/warnings.rb +4 -0
- data/spec/integration/associations/embedded_delayed_atomic_sets_spec.rb +216 -0
- data/spec/integration/associations/has_and_belongs_to_many_spec.rb +14 -0
- data/spec/integration/atomic/modifiers_spec.rb +283 -0
- data/spec/integration/encryption_spec.rb +418 -0
- data/spec/mongoid/association/accessors_spec.rb +35 -0
- data/spec/mongoid/association/referenced/belongs_to/buildable_spec.rb +35 -6
- data/spec/mongoid/association/referenced/encrypted_foreign_key_spec.rb +142 -0
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/buildable_spec.rb +36 -5
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +48 -0
- data/spec/mongoid/association/referenced/has_many/buildable_spec.rb +24 -6
- data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +81 -0
- data/spec/mongoid/association/referenced/has_one/buildable_spec.rb +18 -3
- data/spec/mongoid/atomic/modifiers_spec.rb +347 -0
- data/spec/mongoid/attributes/nested_spec.rb +29 -0
- data/spec/mongoid/attributes/readonly_dotted_spec.rb +334 -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 +18 -0
- data/spec/mongoid/criteria/includable_spec.rb +346 -0
- data/spec/mongoid/criteria_spec.rb +17 -0
- data/spec/mongoid/encryptable_spec.rb +54 -0
- data/spec/mongoid/factory_spec.rb +8 -0
- data/spec/mongoid/fields_spec.rb +37 -0
- data/spec/mongoid/findable_spec.rb +14 -0
- data/spec/mongoid/scopable_spec.rb +102 -0
- data/spec/mongoid/search_indexable_spec.rb +26 -0
- data/spec/mongoid/tasks/database_spec.rb +13 -0
- data/spec/mongoid/touchable_spec.rb +217 -0
- data/spec/mongoid/touchable_spec_models.rb +37 -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 +65 -0
- metadata +2045 -2008
|
@@ -160,6 +160,164 @@ describe Mongoid::Config::Encryption do
|
|
|
160
160
|
end
|
|
161
161
|
end
|
|
162
162
|
|
|
163
|
+
# A belongs_to association generates an Object field for its foreign key.
|
|
164
|
+
# When the field was declared with a type of its own, that declared type
|
|
165
|
+
# has to reach the schema: it is what libmongocrypt checks the value
|
|
166
|
+
# against before encrypting it.
|
|
167
|
+
context 'when an encrypted field is a belongs_to foreign key' do
|
|
168
|
+
let(:defined_models) { [] }
|
|
169
|
+
|
|
170
|
+
let(:encryption_options) { { deterministic: true } }
|
|
171
|
+
|
|
172
|
+
let(:models) do
|
|
173
|
+
owner = Class.new do
|
|
174
|
+
include Mongoid::Document
|
|
175
|
+
|
|
176
|
+
store_in collection: 'crypt_encrypted_foreign_key_owners'
|
|
177
|
+
end
|
|
178
|
+
defined_models << owner
|
|
179
|
+
stub_const('Crypt::EncryptedForeignKeyOwner', owner)
|
|
180
|
+
|
|
181
|
+
reference = Class.new do
|
|
182
|
+
include Mongoid::Document
|
|
183
|
+
|
|
184
|
+
store_in collection: 'crypt_encrypted_foreign_key_references'
|
|
185
|
+
end
|
|
186
|
+
defined_models << reference
|
|
187
|
+
stub_const('Crypt::EncryptedForeignKeyReference', reference)
|
|
188
|
+
reference.field(:owner_id, type: BSON::ObjectId, encrypt: encryption_options)
|
|
189
|
+
reference.belongs_to(:owner, class_name: 'Crypt::EncryptedForeignKeyOwner', inverse_of: nil)
|
|
190
|
+
|
|
191
|
+
[ reference ]
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
after do
|
|
195
|
+
defined_models.each { |model| Mongoid.deregister_model(model) }
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
it 'uses the declared type of the foreign key' do
|
|
199
|
+
expect(encryption_schema_map['mongoid_test.crypt_encrypted_foreign_key_references']).to eq(
|
|
200
|
+
'bsonType' => 'object',
|
|
201
|
+
'properties' => {
|
|
202
|
+
'owner_id' => {
|
|
203
|
+
'encrypt' => {
|
|
204
|
+
'bsonType' => 'objectId',
|
|
205
|
+
'algorithm' => 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic'
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
)
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
context 'when the field specifies a key alt name' do
|
|
213
|
+
let(:encryption_options) { { deterministic: false, key_name_field: :owner_key_name } }
|
|
214
|
+
|
|
215
|
+
it 'keeps the key alt name of the field' do
|
|
216
|
+
expect(
|
|
217
|
+
encryption_schema_map.dig(
|
|
218
|
+
'mongoid_test.crypt_encrypted_foreign_key_references',
|
|
219
|
+
'properties', 'owner_id', 'encrypt'
|
|
220
|
+
)
|
|
221
|
+
).to eq(
|
|
222
|
+
'keyId' => '/owner_key_name',
|
|
223
|
+
'bsonType' => 'objectId',
|
|
224
|
+
'algorithm' => 'AEAD_AES_256_CBC_HMAC_SHA_512-Random'
|
|
225
|
+
)
|
|
226
|
+
end
|
|
227
|
+
end
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
# A parent and its subclass write to one collection. When each declares the
|
|
231
|
+
# belongs_to foreign key with its own encryption options, the declaration of
|
|
232
|
+
# the subclass must not be replaced by the association declared on the
|
|
233
|
+
# parent: the schema merge reports the disagreement instead.
|
|
234
|
+
context 'when a belongs_to foreign key is encrypted differently on a parent and a subclass' do
|
|
235
|
+
let(:defined_models) { [] }
|
|
236
|
+
|
|
237
|
+
let(:parent_key_id) { 'grolrnFVSSW9Gq04Q87R9Q==' }
|
|
238
|
+
|
|
239
|
+
let(:child_key_id) { 'S34mE/HhSFSym3yErpER6Q==' }
|
|
240
|
+
|
|
241
|
+
let(:parent_options) { { deterministic: true, key_id: parent_key_id } }
|
|
242
|
+
|
|
243
|
+
let(:child_options) { { deterministic: false, key_id: child_key_id } }
|
|
244
|
+
|
|
245
|
+
let(:parent) do
|
|
246
|
+
klass = Class.new do
|
|
247
|
+
include Mongoid::Document
|
|
248
|
+
|
|
249
|
+
store_in collection: 'crypt_inherited_foreign_keys'
|
|
250
|
+
end
|
|
251
|
+
defined_models << klass
|
|
252
|
+
stub_const('Crypt::SchemaInheritedParent', klass)
|
|
253
|
+
klass
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
let(:child) do
|
|
257
|
+
parent
|
|
258
|
+
klass = Class.new(parent)
|
|
259
|
+
defined_models << klass
|
|
260
|
+
stub_const('Crypt::SchemaInheritedChild', klass)
|
|
261
|
+
klass
|
|
262
|
+
end
|
|
263
|
+
|
|
264
|
+
let(:models) do
|
|
265
|
+
[ parent, child ]
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
before do
|
|
269
|
+
parent.field(:owner_id, type: BSON::ObjectId, encrypt: parent_options)
|
|
270
|
+
child.field(:owner_id, type: BSON::ObjectId, encrypt: child_options)
|
|
271
|
+
parent.belongs_to(:owner, class_name: 'Person', inverse_of: nil)
|
|
272
|
+
end
|
|
273
|
+
|
|
274
|
+
after do
|
|
275
|
+
defined_models.each { |model| Mongoid.deregister_model(model) }
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
it 'keeps the options of each field' do
|
|
279
|
+
parent_field = parent.fields['owner_id']
|
|
280
|
+
child_field = child.fields['owner_id']
|
|
281
|
+
|
|
282
|
+
expect(parent_field).to be_a(Mongoid::Fields::EncryptedForeignKey)
|
|
283
|
+
expect(parent_field.type).to eq(BSON::ObjectId)
|
|
284
|
+
expect(parent_field.deterministic?).to be true
|
|
285
|
+
expect(parent_field.key_id).to eq(parent_key_id)
|
|
286
|
+
|
|
287
|
+
expect(child_field).to be_a(Mongoid::Fields::EncryptedForeignKey)
|
|
288
|
+
expect(child_field.type).to eq(BSON::ObjectId)
|
|
289
|
+
expect(child_field.deterministic?).to be false
|
|
290
|
+
expect(child_field.key_id).to eq(child_key_id)
|
|
291
|
+
end
|
|
292
|
+
|
|
293
|
+
it 'raises for the shared collection' do
|
|
294
|
+
expect { encryption_schema_map }.to raise_error(Mongoid::Errors::InvalidEncryptionSchema)
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
context 'when both fields use the same encryption options' do
|
|
298
|
+
let(:child_options) { parent_options }
|
|
299
|
+
|
|
300
|
+
it 'keeps the shared options on both fields' do
|
|
301
|
+
expect(parent.fields['owner_id'].deterministic?).to be true
|
|
302
|
+
expect(parent.fields['owner_id'].key_id).to eq(parent_key_id)
|
|
303
|
+
expect(child.fields['owner_id'].deterministic?).to be true
|
|
304
|
+
expect(child.fields['owner_id'].key_id).to eq(parent_key_id)
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
it 'builds the schema for the shared collection' do
|
|
308
|
+
expect(
|
|
309
|
+
encryption_schema_map.dig('mongoid_test.crypt_inherited_foreign_keys', 'properties', 'owner_id')
|
|
310
|
+
).to eq(
|
|
311
|
+
'encrypt' => {
|
|
312
|
+
'bsonType' => 'objectId',
|
|
313
|
+
'algorithm' => 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic',
|
|
314
|
+
'keyId' => [ BSON::Binary.new(Base64.decode64(parent_key_id), :uuid) ]
|
|
315
|
+
}
|
|
316
|
+
)
|
|
317
|
+
end
|
|
318
|
+
end
|
|
319
|
+
end
|
|
320
|
+
|
|
163
321
|
# A field left out of the schema map is written in plaintext, with no
|
|
164
322
|
# exception and nothing logged, so an omission here is silent data
|
|
165
323
|
# exposure rather than a broken feature.
|
|
@@ -287,6 +445,75 @@ describe Mongoid::Config::Encryption do
|
|
|
287
445
|
end
|
|
288
446
|
end
|
|
289
447
|
|
|
448
|
+
# The document is persisted under the store_as key, so that is the key
|
|
449
|
+
# libmongocrypt has to find. Keyed by the association name, the fields
|
|
450
|
+
# would be written in plaintext.
|
|
451
|
+
context 'when the association uses store_as' do
|
|
452
|
+
let(:token_properties) do
|
|
453
|
+
{
|
|
454
|
+
'bsonType' => 'object',
|
|
455
|
+
'properties' => {
|
|
456
|
+
'token' => {
|
|
457
|
+
'encrypt' => {
|
|
458
|
+
'bsonType' => 'string',
|
|
459
|
+
'algorithm' => 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic'
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
end
|
|
465
|
+
|
|
466
|
+
let(:models) do
|
|
467
|
+
[ Crypt::Dossier, Crypt::DossierSecret ]
|
|
468
|
+
end
|
|
469
|
+
|
|
470
|
+
let(:properties) do
|
|
471
|
+
encryption_schema_map.dig('mongoid_test.crypt_dossiers', 'properties')
|
|
472
|
+
end
|
|
473
|
+
|
|
474
|
+
it 'keys the nested schema by the stored name' do
|
|
475
|
+
expect(properties.keys).to eq([ 's' ])
|
|
476
|
+
expect(properties['s']).to eq(token_properties)
|
|
477
|
+
end
|
|
478
|
+
|
|
479
|
+
it 'reports the stored name as the encrypted path' do
|
|
480
|
+
expect(Mongoid.config.encrypted_paths_for(Crypt::Dossier)).to eq([ %w[s token] ])
|
|
481
|
+
end
|
|
482
|
+
end
|
|
483
|
+
|
|
484
|
+
context 'when a nested association uses store_as' do
|
|
485
|
+
let(:token_properties) do
|
|
486
|
+
{
|
|
487
|
+
'bsonType' => 'object',
|
|
488
|
+
'properties' => {
|
|
489
|
+
'token' => {
|
|
490
|
+
'encrypt' => {
|
|
491
|
+
'bsonType' => 'string',
|
|
492
|
+
'algorithm' => 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic'
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
end
|
|
498
|
+
|
|
499
|
+
let(:models) do
|
|
500
|
+
[ Crypt::Archive, Crypt::ArchiveEntry, Crypt::ArchivePayload ]
|
|
501
|
+
end
|
|
502
|
+
|
|
503
|
+
it 'keys every level by its stored name' do
|
|
504
|
+
expect(encryption_schema_map.dig('mongoid_test.crypt_archives', 'properties')).to eq(
|
|
505
|
+
'e' => {
|
|
506
|
+
'bsonType' => 'object',
|
|
507
|
+
'properties' => { 'p' => token_properties }
|
|
508
|
+
}
|
|
509
|
+
)
|
|
510
|
+
end
|
|
511
|
+
|
|
512
|
+
it 'reports the stored names as the encrypted path' do
|
|
513
|
+
expect(Mongoid.config.encrypted_paths_for(Crypt::Archive)).to eq([ %w[e p token] ])
|
|
514
|
+
end
|
|
515
|
+
end
|
|
516
|
+
|
|
290
517
|
# The walk has to keep stopping at a model it is already inside of,
|
|
291
518
|
# otherwise a self-embedding model recurses forever.
|
|
292
519
|
context 'when a model embeds itself' do
|
|
@@ -373,5 +600,228 @@ describe Mongoid::Config::Encryption do
|
|
|
373
600
|
)
|
|
374
601
|
end
|
|
375
602
|
end
|
|
603
|
+
|
|
604
|
+
# The map has one entry per namespace. Every model that writes to the
|
|
605
|
+
# namespace, including subclasses that are never registered in
|
|
606
|
+
# Mongoid.models, has to be covered by that entry, or its encrypted fields
|
|
607
|
+
# are written in plaintext.
|
|
608
|
+
context 'when more than one model writes to a namespace' do
|
|
609
|
+
let(:encrypt_metadata) do
|
|
610
|
+
{
|
|
611
|
+
'keyId' => [ BSON::Binary.new(Base64.decode64('grolrnFVSSW9Gq04Q87R9Q=='), :uuid) ],
|
|
612
|
+
'algorithm' => 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic'
|
|
613
|
+
}
|
|
614
|
+
end
|
|
615
|
+
|
|
616
|
+
let(:encrypted_string) do
|
|
617
|
+
{ 'encrypt' => { 'bsonType' => 'string' } }
|
|
618
|
+
end
|
|
619
|
+
|
|
620
|
+
context 'when two models are stored in one collection' do
|
|
621
|
+
let(:expected_entry) do
|
|
622
|
+
{
|
|
623
|
+
'bsonType' => 'object',
|
|
624
|
+
'encryptMetadata' => encrypt_metadata,
|
|
625
|
+
'properties' => {
|
|
626
|
+
'serial' => encrypted_string,
|
|
627
|
+
'holder' => encrypted_string
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
end
|
|
631
|
+
|
|
632
|
+
context 'when the models are registered in one order' do
|
|
633
|
+
let(:models) do
|
|
634
|
+
[ Crypt::Badge, Crypt::BadgeScan ]
|
|
635
|
+
end
|
|
636
|
+
|
|
637
|
+
it 'maps the fields of both models' do
|
|
638
|
+
expect(encryption_schema_map['mongoid_test.crypt_badges']).to eq(expected_entry)
|
|
639
|
+
end
|
|
640
|
+
end
|
|
641
|
+
|
|
642
|
+
context 'when the models are registered in the other order' do
|
|
643
|
+
let(:models) do
|
|
644
|
+
[ Crypt::BadgeScan, Crypt::Badge ]
|
|
645
|
+
end
|
|
646
|
+
|
|
647
|
+
it 'maps the fields of both models' do
|
|
648
|
+
expect(encryption_schema_map['mongoid_test.crypt_badges']).to eq(expected_entry)
|
|
649
|
+
end
|
|
650
|
+
end
|
|
651
|
+
end
|
|
652
|
+
|
|
653
|
+
context 'when a subclass adds an encrypted field to an encrypted parent' do
|
|
654
|
+
let(:expected_entry) do
|
|
655
|
+
{
|
|
656
|
+
'bsonType' => 'object',
|
|
657
|
+
'encryptMetadata' => encrypt_metadata,
|
|
658
|
+
'properties' => {
|
|
659
|
+
'ssn' => encrypted_string,
|
|
660
|
+
'salary' => encrypted_string,
|
|
661
|
+
'rate' => encrypted_string
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
end
|
|
665
|
+
|
|
666
|
+
# Subclasses are not registered in Mongoid.models, so only the parent
|
|
667
|
+
# is passed in.
|
|
668
|
+
context 'when only the parent is registered' do
|
|
669
|
+
let(:models) do
|
|
670
|
+
[ Crypt::Member ]
|
|
671
|
+
end
|
|
672
|
+
|
|
673
|
+
it 'maps the fields of the parent and of every subclass' do
|
|
674
|
+
expect(encryption_schema_map['mongoid_test.crypt_members']).to eq(expected_entry)
|
|
675
|
+
end
|
|
676
|
+
end
|
|
677
|
+
|
|
678
|
+
context 'when a subclass is registered as well' do
|
|
679
|
+
let(:models) do
|
|
680
|
+
[ Crypt::Member, Crypt::Employee ]
|
|
681
|
+
end
|
|
682
|
+
|
|
683
|
+
it 'maps the fields of the parent and of every subclass' do
|
|
684
|
+
expect(encryption_schema_map['mongoid_test.crypt_members']).to eq(expected_entry)
|
|
685
|
+
end
|
|
686
|
+
end
|
|
687
|
+
end
|
|
688
|
+
|
|
689
|
+
context 'when only a subclass has encrypted fields' do
|
|
690
|
+
let(:models) do
|
|
691
|
+
[ Crypt::Listing ]
|
|
692
|
+
end
|
|
693
|
+
|
|
694
|
+
it 'maps the fields of the subclass' do
|
|
695
|
+
expect(encryption_schema_map['mongoid_test.crypt_listings']).to eq(
|
|
696
|
+
'bsonType' => 'object',
|
|
697
|
+
'encryptMetadata' => encrypt_metadata,
|
|
698
|
+
'properties' => {
|
|
699
|
+
'owner_ssn' => encrypted_string
|
|
700
|
+
}
|
|
701
|
+
)
|
|
702
|
+
end
|
|
703
|
+
end
|
|
704
|
+
|
|
705
|
+
context 'when a subclass of an embeds_one target adds an encrypted field' do
|
|
706
|
+
let(:models) do
|
|
707
|
+
[ Crypt::Box ]
|
|
708
|
+
end
|
|
709
|
+
|
|
710
|
+
it 'maps the fields of the target and of its subclass' do
|
|
711
|
+
expect(encryption_schema_map.dig('mongoid_test.crypt_boxes', 'properties', 'item')).to eq(
|
|
712
|
+
'bsonType' => 'object',
|
|
713
|
+
'properties' => {
|
|
714
|
+
'label' => encrypted_string,
|
|
715
|
+
'seal' => encrypted_string
|
|
716
|
+
}
|
|
717
|
+
)
|
|
718
|
+
end
|
|
719
|
+
end
|
|
720
|
+
|
|
721
|
+
context 'when only a subclass of an embeds_one target has encrypted fields' do
|
|
722
|
+
let(:models) do
|
|
723
|
+
[ Crypt::Locker ]
|
|
724
|
+
end
|
|
725
|
+
|
|
726
|
+
it 'maps the fields of the subclass' do
|
|
727
|
+
expect(encryption_schema_map.dig('mongoid_test.crypt_lockers', 'properties', 'content')).to eq(
|
|
728
|
+
'bsonType' => 'object',
|
|
729
|
+
'encryptMetadata' => encrypt_metadata,
|
|
730
|
+
'properties' => {
|
|
731
|
+
'code' => encrypted_string
|
|
732
|
+
}
|
|
733
|
+
)
|
|
734
|
+
end
|
|
735
|
+
end
|
|
736
|
+
|
|
737
|
+
# These models are not added to the shared support file: every client
|
|
738
|
+
# with automatic encryption builds the map over all registered models,
|
|
739
|
+
# and a conflict would make every such client fail to build.
|
|
740
|
+
context 'when models on one collection disagree on encryption' do
|
|
741
|
+
let(:defined_models) { [] }
|
|
742
|
+
|
|
743
|
+
after do
|
|
744
|
+
defined_models.each { |model| Mongoid.deregister_model(model) }
|
|
745
|
+
end
|
|
746
|
+
|
|
747
|
+
def define_model(&block)
|
|
748
|
+
model = Class.new do
|
|
749
|
+
include Mongoid::Document
|
|
750
|
+
|
|
751
|
+
store_in collection: 'crypt_conflicts'
|
|
752
|
+
end
|
|
753
|
+
model.class_eval(&block)
|
|
754
|
+
defined_models << model
|
|
755
|
+
model
|
|
756
|
+
end
|
|
757
|
+
|
|
758
|
+
context 'when the same field is encrypted the same way' do
|
|
759
|
+
let(:models) do
|
|
760
|
+
[
|
|
761
|
+
define_model { field :code, type: String, encrypt: { deterministic: true } },
|
|
762
|
+
define_model { field :code, type: String, encrypt: { deterministic: true } }
|
|
763
|
+
]
|
|
764
|
+
end
|
|
765
|
+
|
|
766
|
+
it 'maps the field once' do
|
|
767
|
+
expect(encryption_schema_map['mongoid_test.crypt_conflicts']).to eq(
|
|
768
|
+
'bsonType' => 'object',
|
|
769
|
+
'properties' => {
|
|
770
|
+
'code' => {
|
|
771
|
+
'encrypt' => {
|
|
772
|
+
'bsonType' => 'string',
|
|
773
|
+
'algorithm' => 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic'
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
)
|
|
778
|
+
end
|
|
779
|
+
end
|
|
780
|
+
|
|
781
|
+
context 'when the same field is encrypted differently' do
|
|
782
|
+
let(:models) do
|
|
783
|
+
[
|
|
784
|
+
define_model { field :code, type: String, encrypt: { deterministic: true } },
|
|
785
|
+
define_model { field :code, type: String, encrypt: { deterministic: false } }
|
|
786
|
+
]
|
|
787
|
+
end
|
|
788
|
+
|
|
789
|
+
it 'raises' do
|
|
790
|
+
expect { encryption_schema_map }.to raise_error(Mongoid::Errors::InvalidEncryptionSchema)
|
|
791
|
+
end
|
|
792
|
+
end
|
|
793
|
+
|
|
794
|
+
context 'when the collection metadata differs' do
|
|
795
|
+
let(:models) do
|
|
796
|
+
[
|
|
797
|
+
define_model do
|
|
798
|
+
encrypt_with key_id: 'grolrnFVSSW9Gq04Q87R9Q==', deterministic: true
|
|
799
|
+
field :code, type: String, encrypt: true
|
|
800
|
+
end,
|
|
801
|
+
define_model do
|
|
802
|
+
encrypt_with key_id: 'grolrnFVSSW9Gq04Q87R9Q==', deterministic: false
|
|
803
|
+
field :name, type: String, encrypt: true
|
|
804
|
+
end
|
|
805
|
+
]
|
|
806
|
+
end
|
|
807
|
+
|
|
808
|
+
it 'raises' do
|
|
809
|
+
expect { encryption_schema_map }.to raise_error(Mongoid::Errors::InvalidEncryptionSchema)
|
|
810
|
+
end
|
|
811
|
+
end
|
|
812
|
+
|
|
813
|
+
context 'when a subclass encrypts an inherited field differently' do
|
|
814
|
+
let(:models) do
|
|
815
|
+
parent = define_model { field :code, type: String, encrypt: { deterministic: true } }
|
|
816
|
+
Class.new(parent) { field :code, type: String, encrypt: { deterministic: false } }
|
|
817
|
+
[ parent ]
|
|
818
|
+
end
|
|
819
|
+
|
|
820
|
+
it 'raises' do
|
|
821
|
+
expect { encryption_schema_map }.to raise_error(Mongoid::Errors::InvalidEncryptionSchema)
|
|
822
|
+
end
|
|
823
|
+
end
|
|
824
|
+
end
|
|
825
|
+
end
|
|
376
826
|
end
|
|
377
827
|
end
|
|
@@ -51,6 +51,14 @@ describe Mongoid::Contextual::None do
|
|
|
51
51
|
end
|
|
52
52
|
end
|
|
53
53
|
|
|
54
|
+
describe '#destroy' do
|
|
55
|
+
it 'returns zero and destroys nothing' do
|
|
56
|
+
expect(context.destroy).to eq(0)
|
|
57
|
+
expect(context.destroy_all).to eq(0)
|
|
58
|
+
expect(Band.where(name: 'Depeche Mode').count).to eq(1)
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
54
62
|
describe '#pluck' do
|
|
55
63
|
context 'when plucking one field' do
|
|
56
64
|
it 'returns an empty array' do
|
|
@@ -77,6 +85,16 @@ describe Mongoid::Contextual::None do
|
|
|
77
85
|
end
|
|
78
86
|
end
|
|
79
87
|
|
|
88
|
+
describe '#update' do
|
|
89
|
+
it 'returns false without attributes and nil with them, updating nothing' do
|
|
90
|
+
expect(context.update).to be(false)
|
|
91
|
+
expect(context.update(name: 'The Cure')).to be_nil
|
|
92
|
+
expect(context.update_all).to be(false)
|
|
93
|
+
expect(context.update_all(name: 'The Cure')).to be_nil
|
|
94
|
+
expect(Band.where(name: 'Depeche Mode').first.name).to eq('Depeche Mode')
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
80
98
|
describe '#first' do
|
|
81
99
|
it 'returns nil' do
|
|
82
100
|
expect(context.first).to be_nil
|