Skip to content
Closed
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
2 changes: 1 addition & 1 deletion mongoengine_plus/types/encrypted_string/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import codecs

import boto3
from pymongo.encryption import _EncryptionIO
from pymongo.synchronous.encryption import _EncryptionIO

from .fields import EncryptedStringField

Expand Down
4 changes: 2 additions & 2 deletions mongoengine_plus/types/encrypted_string/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def get_data_key_binary(key_namespace: str, key_name: str) -> Binary:
# Buscamos el data key
data_key = get_data_key(key_namespace, key_name)
uuid_data_key = data_key['_id']
return Binary(uuid_data_key.bytes, UUID_SUBTYPE)
Copy link
Author

Choose a reason for hiding this comment

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

@alexviquez el Lint me marcaba que uuid_data_key no tiene el parametro bytes, y por lo que me meti aver efectivamente no lo tiene.
Quite el key y los test pasaron. Aun asi no estoy seguro que sea eso y como se use.
Dejo este comentario porque no se si eso realmente esta funcionando, pero tampoco se como probarlo.

return Binary(uuid_data_key, UUID_SUBTYPE)


def create_data_key(
Expand All @@ -57,7 +57,7 @@ def create_data_key(
key_namespace,
connection,
CodecOptions(uuid_representation=STANDARD),
) as client_encryption:
) as client_encryption: # type: ClientEncryption
client_encryption.create_data_key(
'aws',
key_alt_names=[key_name],
Expand Down
6 changes: 3 additions & 3 deletions mongoengine_plus/types/encrypted_string/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from .base import get_data_key_binary

CODEC_OPTION = CodecOptions(uuid_representation=STANDARD)
CODEC_OPTION: CodecOptions = CodecOptions(uuid_representation=STANDARD)


class EncryptedStringField(BaseField):
Expand Down Expand Up @@ -59,7 +59,7 @@ def to_python(self, value: Any) -> Any:

with ClientEncryption(
self.kms_provider, self.key_namespace, connection, CODEC_OPTION
) as client_encryption:
) as client_encryption: # type: ClientEncryption
return client_encryption.decrypt(value)

def to_mongo(self, value: Any) -> Any:
Expand All @@ -68,7 +68,7 @@ def to_mongo(self, value: Any) -> Any:

with ClientEncryption(
self.kms_provider, self.key_namespace, connection, CODEC_OPTION
) as client_encryption:
) as client_encryption: # type: ClientEncryption
return client_encryption.encrypt(value, self.algorithm, data_key)

def prepare_query_value(self, op, value):
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
blinker==1.9.0
dnspython==2.7.0
mongoengine==0.29.1
pymongo==3.13.0
pymongo==4.11.0
pymongocrypt==1.12.2
boto3==1.35.95
9 changes: 5 additions & 4 deletions tests/types/test_encrypted_string.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from functools import partial
from typing import Generator
from typing import Generator, cast
from unittest.mock import patch

import pytest
from bson import Binary
from mongoengine import Document, StringField
from pymongo import MongoClient
from pymongo.encryption import Algorithm, ClientEncryption, _EncryptionIO
from pymongo.encryption import Algorithm, ClientEncryption
from pymongo.synchronous.encryption import _EncryptionIO

from mongoengine_plus.models import uuid_field
from mongoengine_plus.types import EncryptedStringField
Expand Down Expand Up @@ -84,7 +85,7 @@ def test_create_data_key(
data_key = db_connection[db_name][collection_name].find_one(
({"keyAltNames": key_name})
)

data_key = cast(dict, data_key)
assert data_key['keyAltNames'] == [key_name]
assert type(data_key['keyMaterial']) is bytes
assert data_key['masterKey'] == dict(
Expand Down Expand Up @@ -118,7 +119,7 @@ def test_encrypted_string_on_saving_and_reading(
EncryptedStringField.key_namespace,
client,
CODEC_OPTION,
) as client_encryption:
) as client_encryption: # type: ClientEncryption
# The ClientEncryption object should be able to decrypt the encrypted
# value stored in MongoDB
assert client_encryption.decrypt(user_dict['ssn']) == user.ssn
Expand Down
Loading