25
25
sys .path [0 :0 ] = ["" ]
26
26
27
27
from bson import BSON , json_util
28
- from bson .binary import STANDARD , Binary , UUID_SUBTYPE
28
+ from bson .binary import (Binary ,
29
+ JAVA_LEGACY ,
30
+ STANDARD ,
31
+ UUID_SUBTYPE )
29
32
from bson .codec_options import CodecOptions
30
33
from bson .errors import BSONError
31
34
from bson .json_util import JSONOptions
35
38
from pymongo .errors import (ConfigurationError ,
36
39
EncryptionError ,
37
40
OperationFailure )
41
+ from pymongo .encryption import (Algorithm ,
42
+ ClientEncryption )
38
43
from pymongo .encryption_options import AutoEncryptionOpts , _HAVE_PYMONGOCRYPT
39
44
from pymongo .mongo_client import MongoClient
40
45
from pymongo .write_concern import WriteConcern
47
52
from test .utils_spec_runner import SpecRunner
48
53
49
54
50
- if _HAVE_PYMONGOCRYPT :
51
- # Load the mongocrypt library.
52
- from pymongocrypt .binding import init
53
- init (os .environ .get ('MONGOCRYPT_LIB' , 'mongocrypt' ))
54
-
55
- # This has to be imported after calling init().
56
- from pymongo .encryption import (Algorithm ,
57
- ClientEncryption )
58
-
59
-
60
55
def get_client_opts (client ):
61
56
return client ._MongoClient__options
62
57
@@ -261,7 +256,7 @@ class TestExplicitSimple(EncryptionIntegrationTest):
261
256
262
257
def test_encrypt_decrypt (self ):
263
258
client_encryption = ClientEncryption (
264
- KMS_PROVIDERS , 'admin.datakeys' , client_context .client )
259
+ KMS_PROVIDERS , 'admin.datakeys' , client_context .client , OPTS )
265
260
self .addCleanup (client_encryption .close )
266
261
# Use standard UUID representation.
267
262
key_vault = client_context .client .admin .get_collection (
@@ -296,7 +291,7 @@ def test_encrypt_decrypt(self):
296
291
297
292
def test_validation (self ):
298
293
client_encryption = ClientEncryption (
299
- KMS_PROVIDERS , 'admin.datakeys' , client_context .client )
294
+ KMS_PROVIDERS , 'admin.datakeys' , client_context .client , OPTS )
300
295
self .addCleanup (client_encryption .close )
301
296
302
297
msg = 'value to decrypt must be a bson.binary.Binary with subtype 6'
@@ -307,7 +302,7 @@ def test_validation(self):
307
302
308
303
def test_bson_errors (self ):
309
304
client_encryption = ClientEncryption (
310
- KMS_PROVIDERS , 'admin.datakeys' , client_context .client )
305
+ KMS_PROVIDERS , 'admin.datakeys' , client_context .client , OPTS )
311
306
self .addCleanup (client_encryption .close )
312
307
313
308
# Attempt to encrypt an unencodable object.
@@ -317,6 +312,43 @@ def test_bson_errors(self):
317
312
unencodable_value , Algorithm .Deterministic ,
318
313
key_id = Binary (uuid .uuid4 ().bytes , UUID_SUBTYPE ))
319
314
315
+ def test_codec_options (self ):
316
+ with self .assertRaisesRegex (TypeError , 'codec_options must be' ):
317
+ ClientEncryption (
318
+ KMS_PROVIDERS , 'admin.datakeys' , client_context .client , None )
319
+
320
+ opts = CodecOptions (uuid_representation = JAVA_LEGACY )
321
+ client_encryption_legacy = ClientEncryption (
322
+ KMS_PROVIDERS , 'admin.datakeys' , client_context .client , opts )
323
+ self .addCleanup (client_encryption_legacy .close )
324
+
325
+ # Create the encrypted field's data key.
326
+ key_id = client_encryption_legacy .create_data_key ('local' )
327
+
328
+ # Encrypt a UUID with JAVA_LEGACY codec options.
329
+ value = uuid .uuid4 ()
330
+ encrypted_legacy = client_encryption_legacy .encrypt (
331
+ value , Algorithm .Deterministic , key_id = key_id )
332
+ decrypted_value_legacy = client_encryption_legacy .decrypt (
333
+ encrypted_legacy )
334
+ self .assertEqual (decrypted_value_legacy , value )
335
+
336
+ # Encrypt the same UUID with STANDARD codec options.
337
+ client_encryption = ClientEncryption (
338
+ KMS_PROVIDERS , 'admin.datakeys' , client_context .client , OPTS )
339
+ self .addCleanup (client_encryption .close )
340
+ encrypted_standard = client_encryption .encrypt (
341
+ value , Algorithm .Deterministic , key_id = key_id )
342
+ decrypted_standard = client_encryption .decrypt (encrypted_standard )
343
+ self .assertEqual (decrypted_standard , value )
344
+
345
+ # Test that codec_options is applied during encryption.
346
+ self .assertNotEqual (encrypted_standard , encrypted_legacy )
347
+ # Test that codec_options is applied during decryption.
348
+ self .assertEqual (
349
+ client_encryption_legacy .decrypt (encrypted_standard ), value )
350
+ self .assertNotEqual (
351
+ client_encryption .decrypt (encrypted_legacy ), value )
320
352
321
353
# Spec tests
322
354
@@ -485,7 +517,8 @@ def test_data_key(self):
485
517
self .addCleanup (client_encrypted .close )
486
518
487
519
client_encryption = ClientEncryption (
488
- self .kms_providers (), 'admin.datakeys' , client_context .client )
520
+ self .kms_providers (), 'admin.datakeys' , client_context .client ,
521
+ OPTS )
489
522
self .addCleanup (client_encryption .close )
490
523
491
524
# Local create data key.
@@ -577,7 +610,7 @@ def _test_external_key_vault(self, with_external_key_vault):
577
610
self .addCleanup (client_encrypted .close )
578
611
579
612
client_encryption = ClientEncryption (
580
- self .kms_providers (), 'admin.datakeys' , key_vault_client )
613
+ self .kms_providers (), 'admin.datakeys' , key_vault_client , OPTS )
581
614
self .addCleanup (client_encryption .close )
582
615
583
616
if with_external_key_vault :
@@ -684,7 +717,8 @@ def _test_corpus(self, opts):
684
717
self .addCleanup (client_encrypted .close )
685
718
686
719
client_encryption = ClientEncryption (
687
- self .kms_providers (), 'admin.datakeys' , client_context .client )
720
+ self .kms_providers (), 'admin.datakeys' , client_context .client ,
721
+ OPTS )
688
722
self .addCleanup (client_encryption .close )
689
723
690
724
corpus = self .fix_up_curpus (json_data ('corpus' , 'corpus.json' ))
0 commit comments