Skip to content

Commit 01885a5

Browse files
committed
Added serializer for aws enc sdk
1 parent 7127c9c commit 01885a5

File tree

5 files changed

+31
-23
lines changed

5 files changed

+31
-23
lines changed

aws_lambda_powertools/utilities/data_masking/providers/aws_encryption_sdk.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ def __init__(
7575
max_messages_encrypted=max_messages_encrypted,
7676
)
7777

78+
def _serialize(self, data: Any):
79+
return bytes(str(data), "utf-8")
80+
81+
def _deserialize(self, data: bytes):
82+
return data.decode("utf-8")
83+
7884
def encrypt(self, data: Union[bytes, str], **provider_options) -> str:
7985
"""
8086
Encrypt data using the AwsEncryptionSdkProvider.
@@ -91,6 +97,7 @@ def encrypt(self, data: Union[bytes, str], **provider_options) -> str:
9197
ciphertext : str
9298
The encrypted data, as a base64-encoded string.
9399
"""
100+
data = self._serialize(data)
94101
ciphertext, _ = self.client.encrypt(source=data, materials_manager=self.cache_cmm, **provider_options)
95102
ciphertext = base64.b64encode(ciphertext).decode()
96103
return ciphertext
@@ -125,4 +132,5 @@ def decrypt(self, data: str, **provider_options) -> bytes:
125132
if decryptor_header.encryption_context.get(key) != value:
126133
raise ContextMismatchError(key)
127134

135+
ciphertext = self._deserialize(ciphertext)
128136
return ciphertext

tests/e2e/data_masking/handlers/basic_handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def lambda_handler(event, context):
1414

1515
kms_key = event.get("kms_key")
1616
data_masker = DataMasking(provider=AwsEncryptionSdkProvider(keys=[kms_key]))
17-
value = bytes(str([1, 2, "string", 4.5]), "utf-8")
17+
value = [1, 2, "string", 4.5]
1818
encrypted_data = data_masker.encrypt(value)
1919
response = {}
2020
response["encrypted_data"] = encrypted_data

tests/e2e/data_masking/test_data_masking.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,36 +42,36 @@ def test_encryption(data_masker):
4242
# GIVEN an instantiation of DataMasking with the AWS encryption provider
4343

4444
# AWS Encryption SDK encrypt method only takes in bytes or strings
45-
value = bytes(str([1, 2, "string", 4.5]), "utf-8")
45+
value = [1, 2, "string", 4.5]
4646

4747
# WHEN encrypting and then decrypting the encrypted data
4848
encrypted_data = data_masker.encrypt(value)
4949
decrypted_data = data_masker.decrypt(encrypted_data)
5050

5151
# THEN the result is the original input data
52-
assert decrypted_data == value
52+
assert decrypted_data == str(value)
5353

5454

5555
@pytest.mark.xdist_group(name="data_masking")
5656
def test_encryption_context(data_masker):
5757
# GIVEN an instantiation of DataMasking with the AWS encryption provider
5858

59-
value = bytes(str([1, 2, "string", 4.5]), "utf-8")
59+
value = [1, 2, "string", 4.5]
6060
context = {"this": "is_secure"}
6161

6262
# WHEN encrypting and then decrypting the encrypted data with an encryption_context
6363
encrypted_data = data_masker.encrypt(value, encryption_context=context)
6464
decrypted_data = data_masker.decrypt(encrypted_data, encryption_context=context)
6565

6666
# THEN the result is the original input data
67-
assert decrypted_data == value
67+
assert decrypted_data == str(value)
6868

6969

7070
@pytest.mark.xdist_group(name="data_masking")
7171
def test_encryption_context_mismatch(data_masker):
7272
# GIVEN an instantiation of DataMasking with the AWS encryption provider
7373

74-
value = bytes(str([1, 2, "string", 4.5]), "utf-8")
74+
value = [1, 2, "string", 4.5]
7575

7676
# WHEN encrypting with a encryption_context
7777
encrypted_data = data_masker.encrypt(value, encryption_context={"this": "is_secure"})
@@ -85,7 +85,7 @@ def test_encryption_context_mismatch(data_masker):
8585
def test_encryption_no_context_fail(data_masker):
8686
# GIVEN an instantiation of DataMasking with the AWS encryption provider
8787

88-
value = bytes(str([1, 2, "string", 4.5]), "utf-8")
88+
value = [1, 2, "string", 4.5]
8989

9090
# WHEN encrypting with no encryption_context
9191
encrypted_data = data_masker.encrypt(value)
@@ -100,7 +100,7 @@ def test_encryption_decryption_key_mismatch(data_masker, kms_key2_arn):
100100
# GIVEN an instantiation of DataMasking with the AWS encryption provider with a certain key
101101

102102
# WHEN encrypting and then decrypting the encrypted data
103-
value = bytes(str([1, 2, "string", 4.5]), "utf-8")
103+
value = [1, 2, "string", 4.5]
104104
encrypted_data = data_masker.encrypt(value)
105105

106106
# THEN when decrypting with a different key it should fail
@@ -114,12 +114,14 @@ def test_encryption_provider_singleton(data_masker, kms_key1_arn, kms_key2_arn):
114114
data_masker_2 = DataMasking(provider=AwsEncryptionSdkProvider(keys=[kms_key1_arn]))
115115
assert data_masker.provider is data_masker_2.provider
116116

117+
value = [1, 2, "string", 4.5]
118+
117119
# WHEN encrypting and then decrypting the encrypted data
118-
encrypted_data = data_masker.encrypt("string")
120+
encrypted_data = data_masker.encrypt(value)
119121
decrypted_data = data_masker_2.decrypt(encrypted_data)
120122

121123
# THEN the result is the original input data
122-
assert decrypted_data == bytes("string", "utf-8")
124+
assert decrypted_data == str(value)
123125

124126
data_masker_3 = DataMasking(provider=AwsEncryptionSdkProvider(keys=[kms_key2_arn]))
125127
assert data_masker_2.provider is not data_masker_3.provider
@@ -130,7 +132,7 @@ def test_encryption_in_logs(data_masker, basic_handler_fn, basic_handler_fn_arn)
130132
# GIVEN an instantiation of DataMasking with the AWS encryption provider
131133

132134
# WHEN encrypting a value and logging it
133-
value = bytes(str([1, 2, "string", 4.5]), "utf-8")
135+
value = [1, 2, "string", 4.5]
134136
encrypted_data = data_masker.encrypt(value)
135137
message = encrypted_data
136138
custom_key = "order_id"
@@ -146,7 +148,7 @@ def test_encryption_in_logs(data_masker, basic_handler_fn, basic_handler_fn_arn)
146148
for log in logs.get_log(key=custom_key):
147149
encrypted_data = log.message
148150
decrypted_data = data_masker.decrypt(encrypted_data)
149-
assert decrypted_data == value
151+
assert decrypted_data == str(value)
150152

151153

152154
# NOTE: This test is failing currently, need to find a fix for building correct dependencies
@@ -162,4 +164,4 @@ def test_encryption_in_handler(basic_handler_fn_arn, kms_key1_arn):
162164
decrypted_data = data_masker.decrypt(encrypted_data)
163165

164166
# THEN decrypting the encrypted data from the response should result in the original value
165-
assert decrypted_data == bytes(str([1, 2, "string", 4.5]), "utf-8")
167+
assert decrypted_data == str([1, 2, "string", 4.5])

tests/functional/data_masking/test_aws_encryption_sdk.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,12 @@ def test_mask_with_fields(data_masker):
3939
def test_encrypt_decrypt(value, data_masker):
4040
# GIVEN an instantiation of DataMasking with the AWS encryption provider
4141

42-
# AWS Encryption SDK encrypt method only takes in bytes or strings
43-
value = bytes(str(value), "utf-8")
44-
4542
# WHEN encrypting and then decrypting the encrypted data
4643
encrypted_data = data_masker.encrypt(value)
4744
decrypted_data = data_masker.decrypt(encrypted_data)
4845

4946
# THEN the result is the original input data
50-
assert decrypted_data == value
47+
assert decrypted_data == str(value)
5148

5249

5350
@pytest.mark.parametrize("value, fields", zip(dictionaries, fields_to_mask))
@@ -60,7 +57,12 @@ def test_encrypt_decrypt_with_fields(value, fields, data_masker):
6057

6158
# THEN the result is the original input data
6259
# AWS Encryption SDK decrypt method only returns bytes
60+
print("value:", value)
6361
if value == json_blob:
64-
assert decrypted_data == aws_encrypted_json_blob
62+
print("json blob!!!!")
63+
assert decrypted_data == value
6564
else:
66-
assert decrypted_data == aws_encrypted_with_fields
65+
print("json_blob_fields!!!!")
66+
assert decrypted_data == str(value)
67+
print("decrypted_data:", decrypted_data)
68+
print("aws_encrypted_with_fields:", aws_encrypted_with_fields)

tests/unit/data_masking/setup.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
import copy
22
import json
33

4-
from aws_lambda_powertools.utilities.data_masking.base import DataMasking
54
from aws_lambda_powertools.utilities.data_masking.constants import DATA_MASKING_STRING
65

7-
data_maskers = [DataMasking()]
8-
9-
106
python_dict = {
117
"a": {
128
"1": {"None": "hello", "four": "world"}, # None type key doesn't work

0 commit comments

Comments
 (0)