1515"""Support for explicit client-side field level encryption."""
1616
1717import contextlib
18+ import enum
1819import uuid
1920import weakref
2021from typing import Any , Mapping , Optional , Sequence
@@ -303,6 +304,7 @@ def _get_internal_client(encrypter, mongo_client):
303304 crypt_shared_lib_path = opts ._crypt_shared_lib_path ,
304305 crypt_shared_lib_required = opts ._crypt_shared_lib_required ,
305306 bypass_encryption = opts ._bypass_auto_encryption ,
307+ bypass_query_analysis = opts ._bypass_query_analysis ,
306308 ),
307309 )
308310 self ._closed = False
@@ -352,11 +354,33 @@ def close(self):
352354 self ._internal_client = None
353355
354356
355- class Algorithm (object ):
357+ class Algorithm (str , enum . Enum ):
356358 """An enum that defines the supported encryption algorithms."""
357359
358360 AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic = "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic"
361+ """AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic."""
359362 AEAD_AES_256_CBC_HMAC_SHA_512_Random = "AEAD_AES_256_CBC_HMAC_SHA_512-Random"
363+ """AEAD_AES_256_CBC_HMAC_SHA_512_Random."""
364+ INDEXED = "Indexed"
365+ """Indexed.
366+
367+ .. versionadded:: 4.2
368+ """
369+ UNINDEXED = "Unindexed"
370+ """Unindexed.
371+
372+ .. versionadded:: 4.2
373+ """
374+
375+
376+ class QueryType (enum .IntEnum ):
377+ """An enum that defines the supported values for explicit encryption query_type.
378+
379+ .. versionadded:: 4.2
380+ """
381+
382+ EQUALITY = 1
383+ """Used to encrypt a value for an equality query."""
360384
361385
362386class ClientEncryption (object ):
@@ -550,6 +574,9 @@ def encrypt(
550574 algorithm : str ,
551575 key_id : Optional [Binary ] = None ,
552576 key_alt_name : Optional [str ] = None ,
577+ index_key_id : Optional [Binary ] = None ,
578+ query_type : Optional [int ] = None ,
579+ contention_factor : Optional [int ] = None ,
553580 ) -> Binary :
554581 """Encrypt a BSON value with a given key and algorithm.
555582
@@ -564,20 +591,38 @@ def encrypt(
564591 :class:`~bson.binary.Binary` with subtype 4 (
565592 :attr:`~bson.binary.UUID_SUBTYPE`).
566593 - `key_alt_name`: Identifies a key vault document by 'keyAltName'.
594+ - `index_key_id` (bytes): the index key id to use for Queryable Encryption.
595+ - `query_type` (int): The query type to execute. See
596+ :class:`QueryType` for valid options.
597+ - `contention_factor` (int): The contention factor to use
598+ when the algorithm is "Indexed".
567599
568600 :Returns:
569601 The encrypted value, a :class:`~bson.binary.Binary` with subtype 6.
602+
603+ .. versionchanged:: 4.2
604+ Added the `index_key_id`, `query_type`, and `contention_factor` parameters.
570605 """
571606 self ._check_closed ()
572607 if key_id is not None and not (
573608 isinstance (key_id , Binary ) and key_id .subtype == UUID_SUBTYPE
574609 ):
575610 raise TypeError ("key_id must be a bson.binary.Binary with subtype 4" )
611+ if index_key_id is not None and not (
612+ isinstance (index_key_id , Binary ) and index_key_id .subtype == UUID_SUBTYPE
613+ ):
614+ raise TypeError ("index_key_id must be a bson.binary.Binary with subtype 4" )
576615
577616 doc = encode ({"v" : value }, codec_options = self ._codec_options )
578617 with _wrap_encryption_errors ():
579618 encrypted_doc = self ._encryption .encrypt (
580- doc , algorithm , key_id = key_id , key_alt_name = key_alt_name
619+ doc ,
620+ algorithm ,
621+ key_id = key_id ,
622+ key_alt_name = key_alt_name ,
623+ index_key_id = index_key_id ,
624+ query_type = query_type ,
625+ contention_factor = contention_factor ,
581626 )
582627 return decode (encrypted_doc )["v" ] # type: ignore[index]
583628
0 commit comments