Skip to content

Commit bbffca8

Browse files
committed
add tests for new masks
1 parent 30b2153 commit bbffca8

File tree

3 files changed

+53
-7
lines changed

3 files changed

+53
-7
lines changed

aws_lambda_powertools/utilities/data_masking/base.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,6 @@ def erase(
119119
mask_format: str | None = None,
120120
masking_rules: dict | None = None,
121121
) -> str | list[str] | tuple[str] | dict:
122-
if not data:
123-
return data
124122
if masking_rules:
125123
return self._apply_masking_rules(data, masking_rules)
126124
else:

aws_lambda_powertools/utilities/data_masking/provider/base.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,16 @@ def erase(
7777
masking_rules: dict | None = None,
7878
**kwargs,
7979
) -> Any:
80-
# Handle empty or None data
81-
if data is None or (isinstance(data, (str, list, dict)) and not data):
82-
return data
8380

84-
result = data # Default to returning the original data
81+
result = DATA_MASKING_STRING
82+
83+
if not any([dynamic_mask, custom_mask, regex_pattern, mask_format, masking_rules]):
84+
if isinstance(data, (str, int, float, dict, bytes)):
85+
return DATA_MASKING_STRING
86+
elif isinstance(data, (list, tuple, set)):
87+
return type(data)([DATA_MASKING_STRING] * len(data))
88+
else:
89+
return DATA_MASKING_STRING
8590

8691
if isinstance(data, (str, int, float)):
8792
result = self._mask_primitive(str(data), dynamic_mask, custom_mask, regex_pattern, mask_format, **kwargs)
@@ -194,7 +199,7 @@ def _regex_mask(self, data: str, regex_pattern: str, mask_format: str) -> str:
194199
_regex_cache[regex_pattern] = re.compile(regex_pattern)
195200
return _regex_cache[regex_pattern].sub(mask_format, data)
196201
except re.error:
197-
return DATA_MASKING_STRING
202+
return data
198203

199204
def _custom_erase(self, data: str, **kwargs) -> str:
200205
if not data:

tests/unit/data_masking/_aws_encryption_sdk/test_unit_data_masking.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ def test_erase_int(data_masker):
2525
assert erased_string == DATA_MASKING_STRING
2626

2727

28+
def test_erase_int_custom_mask(data_masker):
29+
# GIVEN an int data type
30+
31+
# WHEN erase is called with no fields argument
32+
erased_string = data_masker.erase(42, custom_mask="XX")
33+
34+
# THEN the result is the data masked
35+
assert erased_string == "XX"
36+
37+
2838
def test_erase_float(data_masker):
2939
# GIVEN a float data type
3040

@@ -205,3 +215,36 @@ def test_parsing_nonexistent_fields_warning_on_missing_field():
205215

206216
# THEN the "erased" payload is the same of the original
207217
assert masked_json_string == data
218+
219+
220+
def test_regex_mask(data_masker):
221+
data = "Hello! My name is Fulano Ciclano"
222+
regex_pattern = r"\b[A-Z][a-z]+ [A-Z][a-z]+\b"
223+
mask_format = "XXXX XXXX"
224+
225+
result = data_masker.erase(data, regex_pattern=regex_pattern, mask_format=mask_format)
226+
227+
assert result == "Hello! My name is XXXX XXXX"
228+
229+
230+
def test_erase_json_dict_with_fields_and_masks(data_masker):
231+
# GIVEN the data type is a json representation of a dictionary
232+
data = json.dumps(
233+
{
234+
"a": {
235+
"1": {"None": "hello", "four": "world"},
236+
"b": {"3": {"4": "goodbye", "e": "world"}},
237+
},
238+
},
239+
)
240+
241+
# WHEN erase is called with a list of fields specified
242+
masked_json_string = data_masker.erase(data, fields=["a.'1'.None", "a..'4'"], dynamic_mask=True)
243+
244+
# THEN the result is only the specified fields are erased
245+
assert masked_json_string == {
246+
"a": {
247+
"1": {"None": "*****", "four": "world"},
248+
"b": {"3": {"4": "*******", "e": "world"}},
249+
},
250+
}

0 commit comments

Comments
 (0)