Skip to content

Commit 1e96c28

Browse files
authored
PYTHON-5191 Add key_expiration_ms option for DEK cache lifetime (#984)
1 parent 467f1fc commit 1e96c28

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

bindings/python/CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Changelog
22
=========
33

4+
Changes in Version 1.13.0
5+
-------------------------
6+
7+
- Add support for the key_expiration_ms option to MongoCryptOptions.
8+
49
Changes in Version 1.12.0
510
-------------------------
611

bindings/python/pymongocrypt/mongocrypt.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,15 @@ def __init(self):
149149
if any([on_demand_aws, on_demand_gcp, on_demand_azure]):
150150
lib.mongocrypt_setopt_use_need_kms_credentials_state(self.__crypt)
151151

152-
# Enable KMS retry when available, libmongocrypt >= 1.12.0,
152+
# Enable KMS retry and key_expiration_ms when available, libmongocrypt >= 1.12.0,
153153
try:
154154
if not lib.mongocrypt_setopt_retry_kms(self.__crypt, True):
155155
self.__raise_from_status()
156+
if self.__opts.key_expiration_ms is not None:
157+
if not lib.mongocrypt_setopt_key_expiration(
158+
self.__crypt, self.__opts.key_expiration_ms
159+
):
160+
self.__raise_from_status()
156161
except AttributeError:
157162
# libmongocrypt < 1.12
158163
pass

bindings/python/pymongocrypt/options.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ def __init__(
1111
crypt_shared_lib_path=None,
1212
crypt_shared_lib_required=False,
1313
bypass_encryption=False,
14+
key_expiration_ms=None,
1415
):
1516
"""Options for :class:`MongoCrypt`.
1617
@@ -53,6 +54,11 @@ def __init__(
5354
- `crypt_shared_lib_required`: Whether to require a crypt_shared
5455
library.
5556
- `bypass_encryption`: Whether to bypass encryption.
57+
- `key_expiration_ms` (int): The cache expiration time for data
58+
encryption keys. Defaults to 60000. 0 means keys never expire.
59+
60+
.. versionadded:: 1.13
61+
Added the ``key_expiration_ms`` parameter.
5662
5763
.. versionremoved:: 1.11
5864
Removed the ``enable_range_v2`` parameter.
@@ -136,6 +142,11 @@ def __init__(
136142
encrypted_fields_map, bytes
137143
):
138144
raise TypeError("encrypted_fields_map must be bytes or None")
145+
if key_expiration_ms is not None:
146+
if not isinstance(key_expiration_ms, int):
147+
raise TypeError("key_expiration_ms must be int or None")
148+
if key_expiration_ms < 0:
149+
raise ValueError("key_expiration_ms must be >=0 or None")
139150

140151
self.kms_providers = kms_providers
141152
self.schema_map = schema_map
@@ -144,6 +155,7 @@ def __init__(
144155
self.crypt_shared_lib_path = crypt_shared_lib_path
145156
self.crypt_shared_lib_required = crypt_shared_lib_required
146157
self.bypass_encryption = bypass_encryption
158+
self.key_expiration_ms = key_expiration_ms
147159

148160

149161
class ExplicitEncryptOpts:

bindings/python/test/test_mongocrypt.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ def test_mongocrypt_options(self):
143143
)
144144
self.assertEqual(opts.encrypted_fields_map, encrypted_fields_map)
145145
self.assertTrue(opts.bypass_query_analysis)
146+
for expiration in [0, 1, 1000000]:
147+
opts = MongoCryptOptions(
148+
valid[0][0], schema_map, key_expiration_ms=expiration
149+
)
150+
self.assertEqual(opts.key_expiration_ms, expiration)
146151

147152
def test_mongocrypt_options_validation(self):
148153
with self.assertRaisesRegex(
@@ -192,6 +197,12 @@ def test_mongocrypt_options_validation(self):
192197
TypeError, "encrypted_fields_map must be bytes or None"
193198
):
194199
MongoCryptOptions(valid_kms, encrypted_fields_map={})
200+
with self.assertRaisesRegex(TypeError, "key_expiration_ms must be int or None"):
201+
MongoCryptOptions(valid_kms, key_expiration_ms="123")
202+
with self.assertRaisesRegex(
203+
ValueError, "key_expiration_ms must be >=0 or None"
204+
):
205+
MongoCryptOptions(valid_kms, key_expiration_ms=-1)
195206

196207

197208
class TestMongoCrypt(unittest.TestCase):

0 commit comments

Comments
 (0)