|
1 |
| -import copy |
2 | 1 | import json
|
3 | 2 |
|
4 | 3 | import pytest
|
5 |
| -from itsdangerous.url_safe import URLSafeSerializer |
6 |
| - |
7 | 4 | from aws_lambda_powertools.shared.constants import DATA_MASKING_STRING
|
8 | 5 | from aws_lambda_powertools.utilities.data_masking.base import DataMasking
|
9 |
| -from aws_lambda_powertools.utilities.data_masking.provider import Provider |
10 |
| - |
11 |
| - |
12 |
| -class MyEncryptionProvider(Provider): |
13 |
| - """Custom encryption provider class""" |
14 |
| - |
15 |
| - def __init__(self, keys, salt=None): |
16 |
| - self.keys = keys |
17 |
| - self.salt = salt |
18 |
| - |
19 |
| - def encrypt(self, data: str) -> str: |
20 |
| - if data is None: |
21 |
| - return data |
22 |
| - serialize = URLSafeSerializer(self.keys) |
23 |
| - return serialize.dumps(data) |
24 |
| - |
25 |
| - def decrypt(self, data: str) -> str: |
26 |
| - if data is None: |
27 |
| - return data |
28 |
| - serialize = URLSafeSerializer(self.keys) |
29 |
| - return serialize.loads(data) |
| 6 | +from tests.unit.data_masking.setup import * |
30 | 7 |
|
31 |
| - |
32 |
| -data_maskers = [ |
33 |
| - DataMasking(), |
34 |
| - DataMasking(provider=MyEncryptionProvider(keys="secret-key")), |
35 |
| -] |
36 |
| - |
37 |
| - |
38 |
| -python_dict = { |
39 |
| - "a": { |
40 |
| - "1": {"None": "hello", "four": "world"}, # None type key doesn't work |
41 |
| - "b": {"3": {"4": "goodbye", "e": "world"}}, # key "4.5" doesn't work |
42 |
| - } |
43 |
| -} |
44 |
| -json_dict = json.dumps(python_dict) |
45 |
| -dict_fields = ["a.1.None", "a.b.3.4"] |
46 |
| -masked_with_fields = { |
47 |
| - "a": {"1": {"None": DATA_MASKING_STRING, "four": "world"}, "b": {"3": {"4": DATA_MASKING_STRING, "e": "world"}}} |
48 |
| -} |
49 |
| -aws_encrypted_with_fields = { |
50 |
| - "a": { |
51 |
| - "1": {"None": bytes("hello", "utf-8"), "four": "world"}, |
52 |
| - "b": {"3": {"4": bytes("goodbye", "utf-8"), "e": "world"}}, |
53 |
| - } |
54 |
| -} |
55 |
| - |
56 |
| -# 10kb JSON blob for latency testing |
57 |
| -json_blob = { |
58 |
| - "id": 1, |
59 |
| - "name": "John Doe", |
60 |
| - "age": 30, |
61 |
| - |
62 |
| - "address": {"street": "123 Main St", "city": "Anytown", "state": "CA", "zip": "12345"}, |
63 |
| - "phone_numbers": ["+1-555-555-1234", "+1-555-555-5678"], |
64 |
| - "interests": ["Hiking", "Traveling", "Photography", "Reading"], |
65 |
| - "job_history": { |
66 |
| - "company": "Acme Inc.", |
67 |
| - "position": "Software Engineer", |
68 |
| - "start_date": "2015-01-01", |
69 |
| - "end_date": "2017-12-31", |
70 |
| - }, |
71 |
| - "about_me": """ |
72 |
| - Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla tincidunt velit quis |
73 |
| - sapien mollis, at egestas massa tincidunt. Suspendisse ultrices arcu a dolor dapibus, |
74 |
| - ut pretium turpis volutpat. Vestibulum at sapien quis sapien dignissim volutpat ut a enim. |
75 |
| - Praesent fringilla sem eu dui convallis luctus. Donec ullamcorper, sapien ut convallis congue, |
76 |
| - risus mauris pretium tortor, nec dignissim arcu urna a nisl. Vivamus non fermentum ex. Proin |
77 |
| - interdum nisi id sagittis egestas. Nam sit amet nisi nec quam pharetra sagittis. Aliquam erat |
78 |
| - volutpat. Donec nec luctus sem, nec ornare lorem. Vivamus vitae orci quis enim faucibus placerat. |
79 |
| - Nulla facilisi. Proin in turpis orci. Donec imperdiet velit ac tellus gravida, eget laoreet tellus |
80 |
| - malesuada. Praesent venenatis tellus ac urna blandit, at varius felis posuere. Integer a commodo nunc. |
81 |
| - """, |
82 |
| -} |
83 |
| -json_blob_fields = ["address.street", "job_history.company"] |
84 |
| -aws_encrypted_json_blob = copy.deepcopy(json_blob) |
85 |
| -aws_encrypted_json_blob["address"]["street"] = bytes("123 Main St", "utf-8") |
86 |
| -aws_encrypted_json_blob["job_history"]["company"] = bytes("Acme Inc.", "utf-8") |
87 |
| - |
88 |
| -dictionaries = [python_dict, json_dict, json_blob] |
89 |
| -fields_to_mask = [dict_fields, dict_fields, json_blob_fields] |
90 |
| - |
91 |
| -data_types_and_masks = [ |
92 |
| - # simple data types |
93 |
| - [42, DATA_MASKING_STRING], |
94 |
| - [4.22, DATA_MASKING_STRING], |
95 |
| - [True, DATA_MASKING_STRING], |
96 |
| - [None, DATA_MASKING_STRING], |
97 |
| - ["this is a string", DATA_MASKING_STRING], |
98 |
| - # iterables |
99 |
| - [[1, 2, 3, 4], [DATA_MASKING_STRING, DATA_MASKING_STRING, DATA_MASKING_STRING, DATA_MASKING_STRING]], |
100 |
| - [ |
101 |
| - ["hello", 1, 2, 3, "world"], |
102 |
| - [DATA_MASKING_STRING, DATA_MASKING_STRING, DATA_MASKING_STRING, DATA_MASKING_STRING, DATA_MASKING_STRING], |
103 |
| - ], |
104 |
| - # dictionaries |
105 |
| - [python_dict, DATA_MASKING_STRING], |
106 |
| - [json_dict, DATA_MASKING_STRING], |
107 |
| -] |
108 |
| -data_types = [item[0] for item in data_types_and_masks] |
| 8 | +# should be conftest? no other conftest in unit tests |
| 9 | +# didn't work when i made them all pytest.fixtures |
109 | 10 |
|
110 | 11 |
|
111 | 12 | @pytest.mark.parametrize("data_masker", data_maskers)
|
|
0 commit comments