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
|
@@ -163,6 +163,164 @@ describe Mongoid::Config::Encryption do
|
|
|
163
163
|
end
|
|
164
164
|
end
|
|
165
165
|
|
|
166
|
+
# A belongs_to association generates an Object field for its foreign key.
|
|
167
|
+
# When the field was declared with a type of its own, that declared type
|
|
168
|
+
# has to reach the schema: it is what libmongocrypt checks the value
|
|
169
|
+
# against before encrypting it.
|
|
170
|
+
context 'when an encrypted field is a belongs_to foreign key' do
|
|
171
|
+
let(:defined_models) { [] }
|
|
172
|
+
|
|
173
|
+
let(:encryption_options) { { deterministic: true } }
|
|
174
|
+
|
|
175
|
+
let(:models) do
|
|
176
|
+
owner = Class.new do
|
|
177
|
+
include Mongoid::Document
|
|
178
|
+
|
|
179
|
+
store_in collection: 'crypt_encrypted_foreign_key_owners'
|
|
180
|
+
end
|
|
181
|
+
defined_models << owner
|
|
182
|
+
stub_const('Crypt::EncryptedForeignKeyOwner', owner)
|
|
183
|
+
|
|
184
|
+
reference = Class.new do
|
|
185
|
+
include Mongoid::Document
|
|
186
|
+
|
|
187
|
+
store_in collection: 'crypt_encrypted_foreign_key_references'
|
|
188
|
+
end
|
|
189
|
+
defined_models << reference
|
|
190
|
+
stub_const('Crypt::EncryptedForeignKeyReference', reference)
|
|
191
|
+
reference.field(:owner_id, type: BSON::ObjectId, encrypt: encryption_options)
|
|
192
|
+
reference.belongs_to(:owner, class_name: 'Crypt::EncryptedForeignKeyOwner', inverse_of: nil)
|
|
193
|
+
|
|
194
|
+
[ reference ]
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
after do
|
|
198
|
+
defined_models.each { |model| Mongoid.deregister_model(model) }
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
it 'uses the declared type of the foreign key' do
|
|
202
|
+
expect(encryption_schema_map['mongoid_test.crypt_encrypted_foreign_key_references']).to eq(
|
|
203
|
+
'bsonType' => 'object',
|
|
204
|
+
'properties' => {
|
|
205
|
+
'owner_id' => {
|
|
206
|
+
'encrypt' => {
|
|
207
|
+
'bsonType' => 'objectId',
|
|
208
|
+
'algorithm' => 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic'
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
)
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
context 'when the field specifies a key alt name' do
|
|
216
|
+
let(:encryption_options) { { deterministic: false, key_name_field: :owner_key_name } }
|
|
217
|
+
|
|
218
|
+
it 'keeps the key alt name of the field' do
|
|
219
|
+
expect(
|
|
220
|
+
encryption_schema_map.dig(
|
|
221
|
+
'mongoid_test.crypt_encrypted_foreign_key_references',
|
|
222
|
+
'properties', 'owner_id', 'encrypt'
|
|
223
|
+
)
|
|
224
|
+
).to eq(
|
|
225
|
+
'keyId' => '/owner_key_name',
|
|
226
|
+
'bsonType' => 'objectId',
|
|
227
|
+
'algorithm' => 'AEAD_AES_256_CBC_HMAC_SHA_512-Random'
|
|
228
|
+
)
|
|
229
|
+
end
|
|
230
|
+
end
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
# A parent and its subclass write to one collection. When each declares the
|
|
234
|
+
# belongs_to foreign key with its own encryption options, the declaration of
|
|
235
|
+
# the subclass must not be replaced by the association declared on the
|
|
236
|
+
# parent: the schema merge reports the disagreement instead.
|
|
237
|
+
context 'when a belongs_to foreign key is encrypted differently on a parent and a subclass' do
|
|
238
|
+
let(:defined_models) { [] }
|
|
239
|
+
|
|
240
|
+
let(:parent_key_id) { 'grolrnFVSSW9Gq04Q87R9Q==' }
|
|
241
|
+
|
|
242
|
+
let(:child_key_id) { 'S34mE/HhSFSym3yErpER6Q==' }
|
|
243
|
+
|
|
244
|
+
let(:parent_options) { { deterministic: true, key_id: parent_key_id } }
|
|
245
|
+
|
|
246
|
+
let(:child_options) { { deterministic: false, key_id: child_key_id } }
|
|
247
|
+
|
|
248
|
+
let(:parent) do
|
|
249
|
+
klass = Class.new do
|
|
250
|
+
include Mongoid::Document
|
|
251
|
+
|
|
252
|
+
store_in collection: 'crypt_inherited_foreign_keys'
|
|
253
|
+
end
|
|
254
|
+
defined_models << klass
|
|
255
|
+
stub_const('Crypt::SchemaInheritedParent', klass)
|
|
256
|
+
klass
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
let(:child) do
|
|
260
|
+
parent
|
|
261
|
+
klass = Class.new(parent)
|
|
262
|
+
defined_models << klass
|
|
263
|
+
stub_const('Crypt::SchemaInheritedChild', klass)
|
|
264
|
+
klass
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
let(:models) do
|
|
268
|
+
[ parent, child ]
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
before do
|
|
272
|
+
parent.field(:owner_id, type: BSON::ObjectId, encrypt: parent_options)
|
|
273
|
+
child.field(:owner_id, type: BSON::ObjectId, encrypt: child_options)
|
|
274
|
+
parent.belongs_to(:owner, class_name: 'Person', inverse_of: nil)
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
after do
|
|
278
|
+
defined_models.each { |model| Mongoid.deregister_model(model) }
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
it 'keeps the options of each field' do
|
|
282
|
+
parent_field = parent.fields['owner_id']
|
|
283
|
+
child_field = child.fields['owner_id']
|
|
284
|
+
|
|
285
|
+
expect(parent_field).to be_a(Mongoid::Fields::EncryptedForeignKey)
|
|
286
|
+
expect(parent_field.type).to eq(BSON::ObjectId)
|
|
287
|
+
expect(parent_field.deterministic?).to be true
|
|
288
|
+
expect(parent_field.key_id).to eq(parent_key_id)
|
|
289
|
+
|
|
290
|
+
expect(child_field).to be_a(Mongoid::Fields::EncryptedForeignKey)
|
|
291
|
+
expect(child_field.type).to eq(BSON::ObjectId)
|
|
292
|
+
expect(child_field.deterministic?).to be false
|
|
293
|
+
expect(child_field.key_id).to eq(child_key_id)
|
|
294
|
+
end
|
|
295
|
+
|
|
296
|
+
it 'raises for the shared collection' do
|
|
297
|
+
expect { encryption_schema_map }.to raise_error(Mongoid::Errors::InvalidEncryptionSchema)
|
|
298
|
+
end
|
|
299
|
+
|
|
300
|
+
context 'when both fields use the same encryption options' do
|
|
301
|
+
let(:child_options) { parent_options }
|
|
302
|
+
|
|
303
|
+
it 'keeps the shared options on both fields' do
|
|
304
|
+
expect(parent.fields['owner_id'].deterministic?).to be true
|
|
305
|
+
expect(parent.fields['owner_id'].key_id).to eq(parent_key_id)
|
|
306
|
+
expect(child.fields['owner_id'].deterministic?).to be true
|
|
307
|
+
expect(child.fields['owner_id'].key_id).to eq(parent_key_id)
|
|
308
|
+
end
|
|
309
|
+
|
|
310
|
+
it 'builds the schema for the shared collection' do
|
|
311
|
+
expect(
|
|
312
|
+
encryption_schema_map.dig('mongoid_test.crypt_inherited_foreign_keys', 'properties', 'owner_id')
|
|
313
|
+
).to eq(
|
|
314
|
+
'encrypt' => {
|
|
315
|
+
'bsonType' => 'objectId',
|
|
316
|
+
'algorithm' => 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic',
|
|
317
|
+
'keyId' => [ BSON::Binary.new(Base64.decode64(parent_key_id), :uuid) ]
|
|
318
|
+
}
|
|
319
|
+
)
|
|
320
|
+
end
|
|
321
|
+
end
|
|
322
|
+
end
|
|
323
|
+
|
|
166
324
|
# A field left out of the schema map is written in plaintext, with no
|
|
167
325
|
# exception and nothing logged, so an omission here is silent data
|
|
168
326
|
# exposure rather than a broken feature.
|
|
@@ -290,6 +448,75 @@ describe Mongoid::Config::Encryption do
|
|
|
290
448
|
end
|
|
291
449
|
end
|
|
292
450
|
|
|
451
|
+
# The document is persisted under the store_as key, so that is the key
|
|
452
|
+
# libmongocrypt has to find. Keyed by the association name, the fields
|
|
453
|
+
# would be written in plaintext.
|
|
454
|
+
context 'when the association uses store_as' do
|
|
455
|
+
let(:token_properties) do
|
|
456
|
+
{
|
|
457
|
+
'bsonType' => 'object',
|
|
458
|
+
'properties' => {
|
|
459
|
+
'token' => {
|
|
460
|
+
'encrypt' => {
|
|
461
|
+
'bsonType' => 'string',
|
|
462
|
+
'algorithm' => 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic'
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
end
|
|
468
|
+
|
|
469
|
+
let(:models) do
|
|
470
|
+
[ Crypt::Dossier, Crypt::DossierSecret ]
|
|
471
|
+
end
|
|
472
|
+
|
|
473
|
+
let(:properties) do
|
|
474
|
+
encryption_schema_map.dig('mongoid_test.crypt_dossiers', 'properties')
|
|
475
|
+
end
|
|
476
|
+
|
|
477
|
+
it 'keys the nested schema by the stored name' do
|
|
478
|
+
expect(properties.keys).to eq([ 's' ])
|
|
479
|
+
expect(properties['s']).to eq(token_properties)
|
|
480
|
+
end
|
|
481
|
+
|
|
482
|
+
it 'reports the stored name as the encrypted path' do
|
|
483
|
+
expect(Mongoid.config.encrypted_paths_for(Crypt::Dossier)).to eq([ %w[s token] ])
|
|
484
|
+
end
|
|
485
|
+
end
|
|
486
|
+
|
|
487
|
+
context 'when a nested association uses store_as' do
|
|
488
|
+
let(:token_properties) do
|
|
489
|
+
{
|
|
490
|
+
'bsonType' => 'object',
|
|
491
|
+
'properties' => {
|
|
492
|
+
'token' => {
|
|
493
|
+
'encrypt' => {
|
|
494
|
+
'bsonType' => 'string',
|
|
495
|
+
'algorithm' => 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic'
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
end
|
|
501
|
+
|
|
502
|
+
let(:models) do
|
|
503
|
+
[ Crypt::Archive, Crypt::ArchiveEntry, Crypt::ArchivePayload ]
|
|
504
|
+
end
|
|
505
|
+
|
|
506
|
+
it 'keys every level by its stored name' do
|
|
507
|
+
expect(encryption_schema_map.dig('mongoid_test.crypt_archives', 'properties')).to eq(
|
|
508
|
+
'e' => {
|
|
509
|
+
'bsonType' => 'object',
|
|
510
|
+
'properties' => { 'p' => token_properties }
|
|
511
|
+
}
|
|
512
|
+
)
|
|
513
|
+
end
|
|
514
|
+
|
|
515
|
+
it 'reports the stored names as the encrypted path' do
|
|
516
|
+
expect(Mongoid.config.encrypted_paths_for(Crypt::Archive)).to eq([ %w[e p token] ])
|
|
517
|
+
end
|
|
518
|
+
end
|
|
519
|
+
|
|
293
520
|
# The walk has to keep stopping at a model it is already inside of,
|
|
294
521
|
# otherwise a self-embedding model recurses forever.
|
|
295
522
|
context 'when a model embeds itself' do
|
|
@@ -376,5 +603,228 @@ describe Mongoid::Config::Encryption do
|
|
|
376
603
|
)
|
|
377
604
|
end
|
|
378
605
|
end
|
|
606
|
+
|
|
607
|
+
# The map has one entry per namespace. Every model that writes to the
|
|
608
|
+
# namespace, including subclasses that are never registered in
|
|
609
|
+
# Mongoid.models, has to be covered by that entry, or its encrypted fields
|
|
610
|
+
# are written in plaintext.
|
|
611
|
+
context 'when more than one model writes to a namespace' do
|
|
612
|
+
let(:encrypt_metadata) do
|
|
613
|
+
{
|
|
614
|
+
'keyId' => [ BSON::Binary.new(Base64.decode64('grolrnFVSSW9Gq04Q87R9Q=='), :uuid) ],
|
|
615
|
+
'algorithm' => 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic'
|
|
616
|
+
}
|
|
617
|
+
end
|
|
618
|
+
|
|
619
|
+
let(:encrypted_string) do
|
|
620
|
+
{ 'encrypt' => { 'bsonType' => 'string' } }
|
|
621
|
+
end
|
|
622
|
+
|
|
623
|
+
context 'when two models are stored in one collection' do
|
|
624
|
+
let(:expected_entry) do
|
|
625
|
+
{
|
|
626
|
+
'bsonType' => 'object',
|
|
627
|
+
'encryptMetadata' => encrypt_metadata,
|
|
628
|
+
'properties' => {
|
|
629
|
+
'serial' => encrypted_string,
|
|
630
|
+
'holder' => encrypted_string
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
end
|
|
634
|
+
|
|
635
|
+
context 'when the models are registered in one order' do
|
|
636
|
+
let(:models) do
|
|
637
|
+
[ Crypt::Badge, Crypt::BadgeScan ]
|
|
638
|
+
end
|
|
639
|
+
|
|
640
|
+
it 'maps the fields of both models' do
|
|
641
|
+
expect(encryption_schema_map['mongoid_test.crypt_badges']).to eq(expected_entry)
|
|
642
|
+
end
|
|
643
|
+
end
|
|
644
|
+
|
|
645
|
+
context 'when the models are registered in the other order' do
|
|
646
|
+
let(:models) do
|
|
647
|
+
[ Crypt::BadgeScan, Crypt::Badge ]
|
|
648
|
+
end
|
|
649
|
+
|
|
650
|
+
it 'maps the fields of both models' do
|
|
651
|
+
expect(encryption_schema_map['mongoid_test.crypt_badges']).to eq(expected_entry)
|
|
652
|
+
end
|
|
653
|
+
end
|
|
654
|
+
end
|
|
655
|
+
|
|
656
|
+
context 'when a subclass adds an encrypted field to an encrypted parent' do
|
|
657
|
+
let(:expected_entry) do
|
|
658
|
+
{
|
|
659
|
+
'bsonType' => 'object',
|
|
660
|
+
'encryptMetadata' => encrypt_metadata,
|
|
661
|
+
'properties' => {
|
|
662
|
+
'ssn' => encrypted_string,
|
|
663
|
+
'salary' => encrypted_string,
|
|
664
|
+
'rate' => encrypted_string
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
end
|
|
668
|
+
|
|
669
|
+
# Subclasses are not registered in Mongoid.models, so only the parent
|
|
670
|
+
# is passed in.
|
|
671
|
+
context 'when only the parent is registered' do
|
|
672
|
+
let(:models) do
|
|
673
|
+
[ Crypt::Member ]
|
|
674
|
+
end
|
|
675
|
+
|
|
676
|
+
it 'maps the fields of the parent and of every subclass' do
|
|
677
|
+
expect(encryption_schema_map['mongoid_test.crypt_members']).to eq(expected_entry)
|
|
678
|
+
end
|
|
679
|
+
end
|
|
680
|
+
|
|
681
|
+
context 'when a subclass is registered as well' do
|
|
682
|
+
let(:models) do
|
|
683
|
+
[ Crypt::Member, Crypt::Employee ]
|
|
684
|
+
end
|
|
685
|
+
|
|
686
|
+
it 'maps the fields of the parent and of every subclass' do
|
|
687
|
+
expect(encryption_schema_map['mongoid_test.crypt_members']).to eq(expected_entry)
|
|
688
|
+
end
|
|
689
|
+
end
|
|
690
|
+
end
|
|
691
|
+
|
|
692
|
+
context 'when only a subclass has encrypted fields' do
|
|
693
|
+
let(:models) do
|
|
694
|
+
[ Crypt::Listing ]
|
|
695
|
+
end
|
|
696
|
+
|
|
697
|
+
it 'maps the fields of the subclass' do
|
|
698
|
+
expect(encryption_schema_map['mongoid_test.crypt_listings']).to eq(
|
|
699
|
+
'bsonType' => 'object',
|
|
700
|
+
'encryptMetadata' => encrypt_metadata,
|
|
701
|
+
'properties' => {
|
|
702
|
+
'owner_ssn' => encrypted_string
|
|
703
|
+
}
|
|
704
|
+
)
|
|
705
|
+
end
|
|
706
|
+
end
|
|
707
|
+
|
|
708
|
+
context 'when a subclass of an embeds_one target adds an encrypted field' do
|
|
709
|
+
let(:models) do
|
|
710
|
+
[ Crypt::Box ]
|
|
711
|
+
end
|
|
712
|
+
|
|
713
|
+
it 'maps the fields of the target and of its subclass' do
|
|
714
|
+
expect(encryption_schema_map.dig('mongoid_test.crypt_boxes', 'properties', 'item')).to eq(
|
|
715
|
+
'bsonType' => 'object',
|
|
716
|
+
'properties' => {
|
|
717
|
+
'label' => encrypted_string,
|
|
718
|
+
'seal' => encrypted_string
|
|
719
|
+
}
|
|
720
|
+
)
|
|
721
|
+
end
|
|
722
|
+
end
|
|
723
|
+
|
|
724
|
+
context 'when only a subclass of an embeds_one target has encrypted fields' do
|
|
725
|
+
let(:models) do
|
|
726
|
+
[ Crypt::Locker ]
|
|
727
|
+
end
|
|
728
|
+
|
|
729
|
+
it 'maps the fields of the subclass' do
|
|
730
|
+
expect(encryption_schema_map.dig('mongoid_test.crypt_lockers', 'properties', 'content')).to eq(
|
|
731
|
+
'bsonType' => 'object',
|
|
732
|
+
'encryptMetadata' => encrypt_metadata,
|
|
733
|
+
'properties' => {
|
|
734
|
+
'code' => encrypted_string
|
|
735
|
+
}
|
|
736
|
+
)
|
|
737
|
+
end
|
|
738
|
+
end
|
|
739
|
+
|
|
740
|
+
# These models are not added to the shared support file: every client
|
|
741
|
+
# with automatic encryption builds the map over all registered models,
|
|
742
|
+
# and a conflict would make every such client fail to build.
|
|
743
|
+
context 'when models on one collection disagree on encryption' do
|
|
744
|
+
let(:defined_models) { [] }
|
|
745
|
+
|
|
746
|
+
after do
|
|
747
|
+
defined_models.each { |model| Mongoid.deregister_model(model) }
|
|
748
|
+
end
|
|
749
|
+
|
|
750
|
+
def define_model(&block)
|
|
751
|
+
model = Class.new do
|
|
752
|
+
include Mongoid::Document
|
|
753
|
+
|
|
754
|
+
store_in collection: 'crypt_conflicts'
|
|
755
|
+
end
|
|
756
|
+
model.class_eval(&block)
|
|
757
|
+
defined_models << model
|
|
758
|
+
model
|
|
759
|
+
end
|
|
760
|
+
|
|
761
|
+
context 'when the same field is encrypted the same way' do
|
|
762
|
+
let(:models) do
|
|
763
|
+
[
|
|
764
|
+
define_model { field :code, type: String, encrypt: { deterministic: true } },
|
|
765
|
+
define_model { field :code, type: String, encrypt: { deterministic: true } }
|
|
766
|
+
]
|
|
767
|
+
end
|
|
768
|
+
|
|
769
|
+
it 'maps the field once' do
|
|
770
|
+
expect(encryption_schema_map['mongoid_test.crypt_conflicts']).to eq(
|
|
771
|
+
'bsonType' => 'object',
|
|
772
|
+
'properties' => {
|
|
773
|
+
'code' => {
|
|
774
|
+
'encrypt' => {
|
|
775
|
+
'bsonType' => 'string',
|
|
776
|
+
'algorithm' => 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic'
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
)
|
|
781
|
+
end
|
|
782
|
+
end
|
|
783
|
+
|
|
784
|
+
context 'when the same field is encrypted differently' do
|
|
785
|
+
let(:models) do
|
|
786
|
+
[
|
|
787
|
+
define_model { field :code, type: String, encrypt: { deterministic: true } },
|
|
788
|
+
define_model { field :code, type: String, encrypt: { deterministic: false } }
|
|
789
|
+
]
|
|
790
|
+
end
|
|
791
|
+
|
|
792
|
+
it 'raises' do
|
|
793
|
+
expect { encryption_schema_map }.to raise_error(Mongoid::Errors::InvalidEncryptionSchema)
|
|
794
|
+
end
|
|
795
|
+
end
|
|
796
|
+
|
|
797
|
+
context 'when the collection metadata differs' do
|
|
798
|
+
let(:models) do
|
|
799
|
+
[
|
|
800
|
+
define_model do
|
|
801
|
+
encrypt_with key_id: 'grolrnFVSSW9Gq04Q87R9Q==', deterministic: true
|
|
802
|
+
field :code, type: String, encrypt: true
|
|
803
|
+
end,
|
|
804
|
+
define_model do
|
|
805
|
+
encrypt_with key_id: 'grolrnFVSSW9Gq04Q87R9Q==', deterministic: false
|
|
806
|
+
field :name, type: String, encrypt: true
|
|
807
|
+
end
|
|
808
|
+
]
|
|
809
|
+
end
|
|
810
|
+
|
|
811
|
+
it 'raises' do
|
|
812
|
+
expect { encryption_schema_map }.to raise_error(Mongoid::Errors::InvalidEncryptionSchema)
|
|
813
|
+
end
|
|
814
|
+
end
|
|
815
|
+
|
|
816
|
+
context 'when a subclass encrypts an inherited field differently' do
|
|
817
|
+
let(:models) do
|
|
818
|
+
parent = define_model { field :code, type: String, encrypt: { deterministic: true } }
|
|
819
|
+
Class.new(parent) { field :code, type: String, encrypt: { deterministic: false } }
|
|
820
|
+
[ parent ]
|
|
821
|
+
end
|
|
822
|
+
|
|
823
|
+
it 'raises' do
|
|
824
|
+
expect { encryption_schema_map }.to raise_error(Mongoid::Errors::InvalidEncryptionSchema)
|
|
825
|
+
end
|
|
826
|
+
end
|
|
827
|
+
end
|
|
828
|
+
end
|
|
379
829
|
end
|
|
380
830
|
end
|
|
@@ -54,10 +54,17 @@ describe Mongoid::Contextual::None do
|
|
|
54
54
|
end
|
|
55
55
|
end
|
|
56
56
|
|
|
57
|
-
describe
|
|
57
|
+
describe '#destroy' do
|
|
58
|
+
it 'returns zero and destroys nothing' do
|
|
59
|
+
expect(context.destroy).to eq(0)
|
|
60
|
+
expect(context.destroy_all).to eq(0)
|
|
61
|
+
expect(Band.where(name: 'Depeche Mode').count).to eq(1)
|
|
62
|
+
end
|
|
63
|
+
end
|
|
58
64
|
|
|
59
|
-
|
|
60
|
-
|
|
65
|
+
describe '#pluck' do
|
|
66
|
+
context 'when plucking one field' do
|
|
67
|
+
it 'returns an empty array' do
|
|
61
68
|
expect(context.pluck(:id)).to eq([])
|
|
62
69
|
end
|
|
63
70
|
end
|
|
@@ -81,8 +88,18 @@ describe Mongoid::Contextual::None do
|
|
|
81
88
|
end
|
|
82
89
|
end
|
|
83
90
|
|
|
84
|
-
describe
|
|
85
|
-
it
|
|
91
|
+
describe '#update' do
|
|
92
|
+
it 'returns false without attributes and nil with them, updating nothing' do
|
|
93
|
+
expect(context.update).to be(false)
|
|
94
|
+
expect(context.update(name: 'The Cure')).to be_nil
|
|
95
|
+
expect(context.update_all).to be(false)
|
|
96
|
+
expect(context.update_all(name: 'The Cure')).to be_nil
|
|
97
|
+
expect(Band.where(name: 'Depeche Mode').first.name).to eq('Depeche Mode')
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
describe '#first' do
|
|
102
|
+
it 'returns nil' do
|
|
86
103
|
expect(context.first).to be_nil
|
|
87
104
|
end
|
|
88
105
|
|
|
@@ -1489,6 +1489,23 @@ describe Mongoid::Criteria do
|
|
|
1489
1489
|
expect(merged).to equal(criteria)
|
|
1490
1490
|
end
|
|
1491
1491
|
end
|
|
1492
|
+
|
|
1493
|
+
context 'when merging a none criteria into a criteria with a cached context' do
|
|
1494
|
+
let(:criteria) { Band.where(name: 'Depeche Mode') }
|
|
1495
|
+
let(:other) { Band.none }
|
|
1496
|
+
|
|
1497
|
+
before do
|
|
1498
|
+
Band.create!(name: 'Depeche Mode')
|
|
1499
|
+
end
|
|
1500
|
+
|
|
1501
|
+
it 'does not use the cached context' do
|
|
1502
|
+
expect(criteria.count).to eq(1)
|
|
1503
|
+
|
|
1504
|
+
merged
|
|
1505
|
+
|
|
1506
|
+
expect(criteria.count).to eq(0)
|
|
1507
|
+
end
|
|
1508
|
+
end
|
|
1492
1509
|
end
|
|
1493
1510
|
|
|
1494
1511
|
describe "#min" do
|
|
@@ -29,6 +29,14 @@ describe Mongoid::Encryptable do
|
|
|
29
29
|
end
|
|
30
30
|
end
|
|
31
31
|
|
|
32
|
+
# Any subclass of the declared class can be assigned to an embeds_one
|
|
33
|
+
# association, so the check has to look at the subclasses too.
|
|
34
|
+
context 'when only a subclass of an embedded model is encrypted' do
|
|
35
|
+
it 'returns true' do
|
|
36
|
+
expect(Crypt::Locker.requires_encryption_schema?).to be true
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
32
40
|
context 'when the model declares no encryption anywhere' do
|
|
33
41
|
it 'returns false' do
|
|
34
42
|
expect(Person.requires_encryption_schema?).to be false
|
|
@@ -57,5 +65,51 @@ describe Mongoid::Encryptable do
|
|
|
57
65
|
expect(Crypt::Drawer.requires_encryption_schema?).to be false
|
|
58
66
|
end
|
|
59
67
|
end
|
|
68
|
+
|
|
69
|
+
context 'when every embedded association resolves' do
|
|
70
|
+
it 'memoizes a false answer' do
|
|
71
|
+
expect(Truck.requires_encryption_schema?).to be false
|
|
72
|
+
expect(Truck.instance_variable_defined?(:@requires_encryption_schema)).to be true
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
context 'when an embedded association does not resolve' do
|
|
77
|
+
it 'does not memoize a false answer' do
|
|
78
|
+
expect(Crypt::Drawer.requires_encryption_schema?).to be false
|
|
79
|
+
expect(Crypt::Drawer.instance_variable_defined?(:@requires_encryption_schema)).to be false
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
context 'when an embedded encrypted class is defined after the first check' do
|
|
84
|
+
after do
|
|
85
|
+
if Crypt.const_defined?(:MissingNote, false)
|
|
86
|
+
Mongoid.deregister_model(Crypt::MissingNote)
|
|
87
|
+
Crypt.send(:remove_const, :MissingNote)
|
|
88
|
+
end
|
|
89
|
+
# While the class above existed, resolving it through the association
|
|
90
|
+
# memoized the class on the association, and a true answer on the
|
|
91
|
+
# model. Drop both, so the other examples see the model as it was
|
|
92
|
+
# before the class was defined.
|
|
93
|
+
relation = Crypt::Drawer.relations['missing_note']
|
|
94
|
+
relation.remove_instance_variable(:@klass) if relation.instance_variable_defined?(:@klass)
|
|
95
|
+
if Crypt::Drawer.instance_variable_defined?(:@requires_encryption_schema)
|
|
96
|
+
Crypt::Drawer.remove_instance_variable(:@requires_encryption_schema)
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
it 'returns true once the class is loaded' do
|
|
101
|
+
expect(Crypt::Drawer.requires_encryption_schema?).to be false
|
|
102
|
+
|
|
103
|
+
missing_note = Class.new do
|
|
104
|
+
include Mongoid::Document
|
|
105
|
+
|
|
106
|
+
embedded_in :drawer, class_name: 'Crypt::Drawer'
|
|
107
|
+
field :text, type: String, encrypt: true
|
|
108
|
+
end
|
|
109
|
+
Crypt.const_set(:MissingNote, missing_note)
|
|
110
|
+
|
|
111
|
+
expect(Crypt::Drawer.requires_encryption_schema?).to be true
|
|
112
|
+
end
|
|
113
|
+
end
|
|
60
114
|
end
|
|
61
115
|
end
|
data/spec/mongoid/fields_spec.rb
CHANGED
|
@@ -1281,6 +1281,35 @@ describe Mongoid::Fields do
|
|
|
1281
1281
|
end
|
|
1282
1282
|
end
|
|
1283
1283
|
|
|
1284
|
+
context 'when a subclass declares an encrypted field and the parent declares it as a plain field' do
|
|
1285
|
+
let(:parent) do
|
|
1286
|
+
klass = stub_const('PlainFieldParent', Class.new)
|
|
1287
|
+
klass.include(Mongoid::Document)
|
|
1288
|
+
klass
|
|
1289
|
+
end
|
|
1290
|
+
|
|
1291
|
+
let(:child) do
|
|
1292
|
+
parent
|
|
1293
|
+
klass = stub_const('PlainFieldChild', Class.new(parent))
|
|
1294
|
+
klass.field(:secret, type: String, encrypt: { deterministic: true })
|
|
1295
|
+
klass
|
|
1296
|
+
end
|
|
1297
|
+
|
|
1298
|
+
after do
|
|
1299
|
+
Mongoid.deregister_model(child)
|
|
1300
|
+
Mongoid.deregister_model(parent)
|
|
1301
|
+
end
|
|
1302
|
+
|
|
1303
|
+
it 'replaces the encrypted field on the subclass' do
|
|
1304
|
+
child
|
|
1305
|
+
|
|
1306
|
+
parent.field(:secret, type: String)
|
|
1307
|
+
|
|
1308
|
+
expect(parent.fields['secret']).not_to be_encrypted
|
|
1309
|
+
expect(child.fields['secret']).not_to be_encrypted
|
|
1310
|
+
end
|
|
1311
|
+
end
|
|
1312
|
+
|
|
1284
1313
|
context "custom options" do
|
|
1285
1314
|
|
|
1286
1315
|
let(:handler) do
|
|
@@ -818,6 +818,20 @@ describe Mongoid::Findable do
|
|
|
818
818
|
expect(criteria.size).to eq(0)
|
|
819
819
|
end
|
|
820
820
|
end
|
|
821
|
+
|
|
822
|
+
context 'when none is called on a criteria with a cached context' do
|
|
823
|
+
let(:criteria) do
|
|
824
|
+
Band.where(name: 'Depeche Mode')
|
|
825
|
+
end
|
|
826
|
+
|
|
827
|
+
it 'does not use the cached context' do
|
|
828
|
+
expect(criteria.count).to eq(1)
|
|
829
|
+
|
|
830
|
+
criteria.none
|
|
831
|
+
|
|
832
|
+
expect(criteria.count).to eq(0)
|
|
833
|
+
end
|
|
834
|
+
end
|
|
821
835
|
end
|
|
822
836
|
|
|
823
837
|
describe ".pluck" do
|
|
@@ -32,6 +32,12 @@ describe Mongoid::Matcher::RegexpBudget do
|
|
|
32
32
|
# exercising one has to hand .open something to bound.
|
|
33
33
|
let(:regexp_selector) { { 'name' => /\Aabc\z/ } }
|
|
34
34
|
|
|
35
|
+
describe 'REGEXP_BUDGET_KEY' do
|
|
36
|
+
it 'is namespaced so a foreign thread-local cannot collide with it' do
|
|
37
|
+
expect(Mongoid::Threaded::REGEXP_BUDGET_KEY).to start_with('[mongoid]:')
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
35
41
|
describe '.open' do
|
|
36
42
|
context 'when no limit is configured' do
|
|
37
43
|
config_override :in_memory_regexp_time_limit, nil
|