Skip to content

Commit 6970cbf

Browse files
committed
Added docstrings to baseprovider
1 parent aacf0db commit 6970cbf

File tree

4 files changed

+73
-8
lines changed

4 files changed

+73
-8
lines changed

aws_lambda_powertools/utilities/_data_masking/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def lambda_handler(event, context):
3737
3838
data = {
3939
"project": "powertools",
40-
"sensitive": "xxxxxxxxxx"
40+
"sensitive": "password"
4141
}
4242
4343
masked = masker.mask(data,fields=["sensitive"])

aws_lambda_powertools/utilities/_data_masking/exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ class DataMaskingDecryptValueError(Exception):
2424

2525
class DataMaskingContextMismatchError(Exception):
2626
"""
27-
Decrypting an invalid field.
27+
Decrypting with the incorrect encryption context.
2828
"""

aws_lambda_powertools/utilities/_data_masking/provider/base.py

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,63 @@
66

77
class BaseProvider:
88
"""
9-
When you try to create an instance of a subclass that does not implement the encrypt method,
10-
you will get a NotImplementedError with a message that says the method is not implemented:
9+
The BaseProvider class serves as an abstract base class for data masking providers.
10+
11+
Attributes
12+
----------
13+
json_serializer : Callable
14+
A callable function responsible for JSON serialization.
15+
json_deserializer : Callable
16+
A callable function responsible for JSON deserialization.
17+
18+
Methods
19+
-------
20+
default_json_serializer(data)
21+
Default method for JSON serialization.
22+
default_json_deserializer(data)
23+
Default method for JSON deserialization.
24+
encrypt(data)
25+
Abstract method for encrypting data. Subclasses must implement this method.
26+
decrypt(data)
27+
Abstract method for decrypting data. Subclasses must implement this method.
28+
mask(data)
29+
Default method for masking data.
30+
31+
Examples
32+
--------
33+
```
34+
from aws_lambda_powertools.utilities._data_masking.provider import BaseProvider
35+
from aws_lambda_powertools.utilities.data_masking import DataMasking
36+
37+
class MyCustomProvider(BaseProvider):
38+
def encrypt(self, data) -> str:
39+
# Implementation logic for data encryption
40+
41+
def decrypt(self, data) -> Any:
42+
# Implementation logic for data decryption
43+
44+
def mask(self, data) -> Union[str, Iterable]:
45+
# Implementation logic for data masking
46+
pass
47+
48+
def lambda_handler(event, context):
49+
provider = MyCustomProvider(["secret-key"])
50+
data_masker = DataMasking(provider=provider)
51+
52+
data = {
53+
"project": "powertools",
54+
"sensitive": "password"
55+
}
56+
57+
encrypted = data_masker.encrypt(data, fields=["sensitive"])
58+
59+
return encrypted
60+
```
61+
62+
Raises
63+
-------
64+
NotImplementedError
65+
If `encrypt()` or `decrypt()` methods are not implemented.
1166
"""
1267

1368
def __init__(self, json_serializer=None, json_deserializer=None) -> None:
@@ -27,6 +82,16 @@ def decrypt(self, data) -> Any:
2782
raise NotImplementedError("Subclasses must implement decrypt()")
2883

2984
def mask(self, data) -> Union[str, Iterable]:
85+
"""
86+
This method irreversibly masks data.
87+
88+
If the data to be masked is of type `str`, `dict`, or `bytes`,
89+
this method will return a masked string, i.e. "*****".
90+
91+
If the data to be masked is of an iterable type like `list`, `tuple`,
92+
or `set`, this method will return a new object of the same type as the
93+
input data but with each element replaced by the string "*****".
94+
"""
3095
if isinstance(data, (str, dict, bytes)):
3196
return DATA_MASKING_STRING
3297
elif isinstance(data, (list, tuple, set)):

aws_lambda_powertools/utilities/_data_masking/provider/kms/aws_encryption_sdk.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,16 @@ class AwsEncryptionSdkProvider(BaseProvider):
5050
5151
def lambda_handler(event, context):
5252
provider = AwsEncryptionSdkProvider(["arn:aws:kms:us-east-1:0123456789012:key/key-id"])
53-
masker = DataMasking(provider=provider)
53+
data_masker = DataMasking(provider=provider)
5454
5555
data = {
5656
"project": "powertools",
57-
"sensitive": "xxxxxxxxxx"
57+
"sensitive": "password"
5858
}
5959
60-
masked = masker.encrypt(data,fields=["sensitive"])
60+
encrypted = data_masker.encrypt(data, fields=["sensitive"])
6161
62-
return masked
62+
return encrypted
6363
6464
```
6565
"""

0 commit comments

Comments
 (0)