66
77class 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 )):
0 commit comments