Skip to content

PYTHON-2267: Allow UUID key_id to be passed to ClientEncryption.encrypt #1494

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions pymongo/encryption.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import contextlib
import enum
import socket
import uuid
import weakref
from copy import deepcopy
from typing import (
Expand All @@ -30,6 +31,7 @@
MutableMapping,
Optional,
Sequence,
Union,
cast,
)

Expand Down Expand Up @@ -755,14 +757,16 @@ def _encrypt_helper(
self,
value: Any,
algorithm: str,
key_id: Optional[Binary] = None,
key_id: Optional[Union[Binary, uuid.UUID]] = None,
key_alt_name: Optional[str] = None,
query_type: Optional[str] = None,
contention_factor: Optional[int] = None,
range_opts: Optional[RangeOpts] = None,
is_expression: bool = False,
) -> Any:
self._check_closed()
if isinstance(key_id, uuid.UUID):
key_id = Binary.from_uuid(key_id)
if key_id is not None and not (
isinstance(key_id, Binary) and key_id.subtype == UUID_SUBTYPE
):
Expand Down Expand Up @@ -795,7 +799,7 @@ def encrypt(
self,
value: Any,
algorithm: str,
key_id: Optional[Binary] = None,
key_id: Optional[Union[Binary, uuid.UUID]] = None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the docstring below can you add this?

.. versionchanged:: 4.7
   ``key_id`` can now be passed in as a :class:`uuid.UUID`.

.. versionchanged:: 4.2
   ...

And same for encrypt_expression.

key_alt_name: Optional[str] = None,
query_type: Optional[str] = None,
contention_factor: Optional[int] = None,
Expand All @@ -822,6 +826,9 @@ def encrypt(

:return: The encrypted value, a :class:`~bson.binary.Binary` with subtype 6.

.. versionchanged:: 4.7
``key_id`` can now be passed in as a :class:`uuid.UUID`.

.. versionchanged:: 4.2
Added the `query_type` and `contention_factor` parameters.
"""
Expand All @@ -843,7 +850,7 @@ def encrypt_expression(
self,
expression: Mapping[str, Any],
algorithm: str,
key_id: Optional[Binary] = None,
key_id: Optional[Union[Binary, uuid.UUID]] = None,
key_alt_name: Optional[str] = None,
query_type: Optional[str] = None,
contention_factor: Optional[int] = None,
Expand Down Expand Up @@ -871,6 +878,9 @@ def encrypt_expression(

:return: The encrypted expression, a :class:`~bson.RawBSONDocument`.

.. versionchanged:: 4.7
``key_id`` can now be passed in as a :class:`uuid.UUID`.

.. versionadded:: 4.4
"""
return cast(
Expand Down
11 changes: 8 additions & 3 deletions test/test_encryption.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,14 @@ def test_encrypt_decrypt(self):
)
self.assertEqual(encrypted_ssn, encrypted_ssn2)

# Test encryption via UUID
encrypted_ssn3 = client_encryption.encrypt(
doc["ssn"],
Algorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic,
key_id=key_id.as_uuid(),
)
self.assertEqual(encrypted_ssn, encrypted_ssn3)

# Test decryption.
decrypted_ssn = client_encryption.decrypt(encrypted_ssn)
self.assertEqual(decrypted_ssn, doc["ssn"])
Expand All @@ -479,9 +487,6 @@ def test_validation(self):

msg = "key_id must be a bson.binary.Binary with subtype 4"
algo = Algorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic
uid = uuid.uuid4()
with self.assertRaisesRegex(TypeError, msg):
client_encryption.encrypt("str", algo, key_id=uid) # type: ignore[arg-type]
with self.assertRaisesRegex(TypeError, msg):
client_encryption.encrypt("str", algo, key_id=Binary(b"123"))

Expand Down