Skip to content

Commit 09d2c83

Browse files
committed
change doc examples
1 parent 7b4aa7c commit 09d2c83

File tree

3 files changed

+34
-32
lines changed

3 files changed

+34
-32
lines changed

aws_lambda_powertools/utilities/data_masking/base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ def erase(self, data: tuple, fields: list[str]) -> tuple[str]: ...
9595
@overload
9696
def erase(self, data: dict, fields: list[str]) -> dict: ...
9797

98+
@overload
99+
def erase(self, data: dict[Any, Any], *, masking_rules: dict[str, object]) -> dict[Any, Any]: ...
100+
98101
@overload
99102
def erase(
100103
self,

docs/utilities/data_masking.md

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -119,22 +119,37 @@ Erasing will remove the original data and replace it with a `*****`. This means
119119

120120
The `erase` method also supports additional flags for more advanced and flexible masking:
121121

122-
| Flag | Behavior |
123-
| ---------------- | ----------------------------------------------------------|
124-
| `dynamic_mask`(bool) | When set to `True`, this flag enables custom masking behavior. It activates the use of advanced masking techniques such as pattern-based or regex-based masking.|
125-
| `custom_mask`(str) | Specifies a simple pattern for masking data. This pattern is applied directly to the input string, replacing all the original characters. For example, with a `custom_mask` of "XX-XX" applied to "12345", the result would be "XX-XX".|
126-
| `regex_pattern`(str) | Defines a regular expression pattern used to identify parts of the input string that should be masked. This allows for more complex and flexible masking rules. It's used in conjunction with `mask_format`.|
127-
| `mask_format`(str) | Specifies the format to use when replacing parts of the string matched by `regex_pattern`. It can include placeholders (like \1, \2) to refer to captured groups in the regex pattern, allowing some parts of the original string to be preserved.|
128-
| `masking_rules`(dict) | Allows you to apply different masking rules (flags) for each data field.|
129-
130-
=== "custom_data_masking.py"
131-
```python hl_lines="13 17 21 25 36"
122+
=== "dynamic_mask"
123+
124+
(bool) When set to `True`, this flag enables custom masking behavior. It activates the use of advanced masking techniques such as pattern-based or regex-based masking.
125+
126+
> Expression: `data_masker.erase(data, fields=["address.zip"], dynamic_mask=True)`
127+
128+
> Field result: `'street': '*** **** **'`
129+
130+
=== "custom_mask"
131+
132+
(str) Specifies a simple pattern for masking data. This pattern is applied directly to the input string, replacing all the original characters. For example, with a `custom_mask` of "XX-XX" applied to "12345", the result would be "XX-XX".
133+
134+
> Expression: `data_masker.erase(data, fields=["address.zip"], custom_mask="XX")`
135+
136+
> Field result: `'zip': 'XX'`
137+
138+
=== "regex_pattern & mask_format"
139+
140+
(str) `regex_pattern` defines a regular expression pattern used to identify parts of the input string that should be masked. This allows for more complex and flexible masking rules. It's used in conjunction with `mask_format`.
141+
`mask_format` specifies the format to use when replacing parts of the string matched by `regex_pattern`. It can include placeholders (like \1, \2) to refer to captured groups in the regex pattern, allowing some parts of the original string to be preserved.
142+
143+
> Expression: `data_masker.erase(data, fields=["email"], regex_pattern=r"(.)(.*)(@.*)", mask_format=r"\1****\3")`
144+
145+
> Field result: `'email': 'j****@example.com'`
146+
147+
=== "masking_rules"
148+
149+
(dict) Allows you to apply different masking rules (flags) for each data field.
150+
```python hl_lines="20"
132151
--8<-- "examples/data_masking/src/custom_data_masking.py"
133152
```
134-
=== "generic_data_input.json"
135-
```json hl_lines="6 7 9 12"
136-
--8<-- "examples/data_masking/src/generic_data_input.json"
137-
```
138153

139154
### Encrypting data
140155

examples/data_masking/src/custom_data_masking.py

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,6 @@
99
def lambda_handler(event: dict, context: LambdaContext) -> dict:
1010
data: dict = event.get("body", {})
1111

12-
# Default erase (*****)
13-
default_erased = data_masker.erase(data, fields=["address.zip"])
14-
# 'street': '*****'
15-
16-
# dynamic_mask
17-
dynamic_mask = data_masker.erase(data, fields=["address.zip"], dynamic_mask=True)
18-
#'street': '*** **** **'
19-
20-
# custom_mask
21-
custom_mask = data_masker.erase(data, fields=["address.zip"], custom_mask="XX")
22-
#'zip': 'XX'
23-
24-
# regex_pattern and mask_format
25-
regex_pattern = data_masker.erase(data, fields=["email"], regex_pattern=r"(.)(.*)(@.*)", mask_format=r"\1****\3")
26-
#'email': 'j****@example.com'
27-
2812
# Masking rules for each field
2913
masking_rules = {
3014
"email": {"regex_pattern": "(.)(.*)(@.*)", "mask_format": r"\1****\3"},
@@ -33,6 +17,6 @@ def lambda_handler(event: dict, context: LambdaContext) -> dict:
3317
"address.street": {"dynamic_mask": False},
3418
}
3519

36-
masking_rules_erase = data_masker.erase(data, masking_rules=masking_rules)
20+
result = data_masker.erase(data, masking_rules=masking_rules)
3721

38-
return default_erased, dynamic_mask, custom_mask, regex_pattern, masking_rules_erase
22+
return result

0 commit comments

Comments
 (0)