@@ -445,6 +445,7 @@ def _get_internal_client(
445
445
bypass_encryption = opts ._bypass_auto_encryption ,
446
446
encrypted_fields_map = encrypted_fields_map ,
447
447
bypass_query_analysis = opts ._bypass_query_analysis ,
448
+ key_expiration_ms = opts ._key_expiration_ms ,
448
449
),
449
450
)
450
451
self ._closed = False
@@ -547,11 +548,10 @@ class QueryType(str, enum.Enum):
547
548
548
549
549
550
def _create_mongocrypt_options (** kwargs : Any ) -> MongoCryptOptions :
550
- opts = MongoCryptOptions (** kwargs )
551
- # Opt into range V2 encryption.
552
- if hasattr (opts , "enable_range_v2" ):
553
- opts .enable_range_v2 = True
554
- return opts
551
+ # For compat with pymongocrypt <1.13, avoid setting the default key_expiration_ms.
552
+ if kwargs .get ("key_expiration_ms" ) is None :
553
+ kwargs .pop ("key_expiration_ms" , None )
554
+ return MongoCryptOptions (** kwargs )
555
555
556
556
557
557
class AsyncClientEncryption (Generic [_DocumentType ]):
@@ -564,6 +564,7 @@ def __init__(
564
564
key_vault_client : AsyncMongoClient [_DocumentTypeArg ],
565
565
codec_options : CodecOptions [_DocumentTypeArg ],
566
566
kms_tls_options : Optional [Mapping [str , Any ]] = None ,
567
+ key_expiration_ms : Optional [int ] = None ,
567
568
) -> None :
568
569
"""Explicit client-side field level encryption.
569
570
@@ -630,7 +631,12 @@ def __init__(
630
631
Or to supply a client certificate::
631
632
632
633
kms_tls_options={'kmip': {'tlsCertificateKeyFile': 'client.pem'}}
634
+ :param key_expiration_ms: The cache expiration time for data encryption keys.
635
+ Defaults to ``None`` which defers to libmongocrypt's default which is currently 60000.
636
+ Set to 0 to disable key expiration.
633
637
638
+ .. versionchanged:: 4.12
639
+ Added the `key_expiration_ms` parameter.
634
640
.. versionchanged:: 4.0
635
641
Added the `kms_tls_options` parameter and the "kmip" KMS provider.
636
642
@@ -666,14 +672,19 @@ def __init__(
666
672
key_vault_coll = key_vault_client [db ][coll ]
667
673
668
674
opts = AutoEncryptionOpts (
669
- kms_providers , key_vault_namespace , kms_tls_options = kms_tls_options
675
+ kms_providers ,
676
+ key_vault_namespace ,
677
+ kms_tls_options = kms_tls_options ,
678
+ key_expiration_ms = key_expiration_ms ,
670
679
)
671
680
self ._io_callbacks : Optional [_EncryptionIO ] = _EncryptionIO (
672
681
None , key_vault_coll , None , opts
673
682
)
674
683
self ._encryption = AsyncExplicitEncrypter (
675
684
self ._io_callbacks ,
676
- _create_mongocrypt_options (kms_providers = kms_providers , schema_map = None ),
685
+ _create_mongocrypt_options (
686
+ kms_providers = kms_providers , schema_map = None , key_expiration_ms = key_expiration_ms
687
+ ),
677
688
)
678
689
# Use the same key vault collection as the callback.
679
690
assert self ._io_callbacks .key_vault_coll is not None
@@ -700,6 +711,7 @@ async def create_encrypted_collection(
700
711
creation. :class:`~pymongo.errors.EncryptionError` will be
701
712
raised if the collection already exists.
702
713
714
+ :param database: the database to create the collection
703
715
:param name: the name of the collection to create
704
716
:param encrypted_fields: Document that describes the encrypted fields for
705
717
Queryable Encryption. The "keyId" may be set to ``None`` to auto-generate the data keys. For example:
0 commit comments