Skip to content

Commit ee3dddc

Browse files
committed
Added some docstrings and typing
1 parent b15b866 commit ee3dddc

File tree

3 files changed

+66
-33
lines changed

3 files changed

+66
-33
lines changed

aws_lambda_powertools/utilities/data_masking/base.py

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import json
2-
from typing import Union
2+
from typing import Union, Optional
33

44
from aws_lambda_powertools.utilities.data_masking.provider import BaseProvider
55

66

77
class DataMasking:
8-
def __init__(self, provider=None):
8+
def __init__(self, provider: Optional[BaseProvider] = None):
99
self.provider = provider or BaseProvider()
1010

1111
def encrypt(self, data, fields=None, **provider_options):
@@ -18,37 +18,66 @@ def mask(self, data, fields=None, **provider_options) -> str:
1818
return self._apply_action(data, fields, self.provider.mask, **provider_options)
1919

2020
def _apply_action(self, data, fields, action, **provider_options):
21+
"""
22+
Helper method to determine whether to apply a given action to the entire input data
23+
or to specific fields if the 'fields' argument is specified.
24+
25+
Parameters
26+
----------
27+
data : any
28+
The input data to process.
29+
fields : Optional[List[any]] = None
30+
A list of fields to apply the action to. If 'None', the action is applied to the entire 'data'.
31+
action : Callable
32+
The action to apply to the data. It should be a callable that performs an operation on the data
33+
and returns the modified value.
34+
35+
Returns
36+
-------
37+
any
38+
The modified data after applying the action.
39+
"""
40+
2141
if fields is not None:
2242
return self._apply_action_to_fields(data, fields, action, **provider_options)
2343
else:
2444
return action(data, **provider_options)
2545

2646
def _apply_action_to_fields(self, data: Union[dict, str], fields, action, **provider_options) -> Union[dict, str]:
2747
"""
28-
Apply the specified action to the specified fields in the input data.
29-
30-
This method is takes the input data, which can be either a dictionary or a JSON string representation
48+
This method takes the input data, which can be either a dictionary or a JSON string representation
3149
of a dictionary, and applies a mask, an encryption, or a decryption to the specified fields.
3250
33-
Parameters:
34-
data (Union[dict, str]): The input data to process. It can be either a dictionary or a JSON string
51+
Parameters
52+
----------
53+
data : Union[dict, str])
54+
The input data to process. It can be either a dictionary or a JSON string
3555
representation of a dictionary.
36-
fields (list): A list of fields to apply the action to. Each field can be specified as a string or
56+
fields : List
57+
A list of fields to apply the action to. Each field can be specified as a string or
3758
a list of strings representing nested keys in the dictionary.
38-
action (callable): The action to apply to the fields. It should be a callable that takes the current
59+
action : Callable
60+
The action to apply to the fields. It should be a callable that takes the current
3961
value of the field as the first argument and any additional arguments that might be required
4062
for the action. It performs an operation on the current value using the provided arguments and
4163
returns the modified value.
42-
**provider_options: Additional keyword arguments to pass to the 'action' function.
64+
**provider_options:
65+
Additional keyword arguments to pass to the 'action' function.
4366
44-
Returns:
45-
str: A JSON string representation of the modified dictionary after applying the action to the
67+
Returns
68+
-------
69+
str
70+
A JSON string representation of the modified dictionary after applying the action to the
4671
specified fields.
4772
48-
Raises:
49-
ValueError: If 'fields' parameter is None.
50-
TypeError: If the 'data' parameter is not a dictionary or a JSON string representation of a dictionary.
51-
KeyError: If specified 'fields' do not exist in input data
73+
Raises
74+
-------
75+
ValueError
76+
If 'fields' parameter is None.
77+
TypeError
78+
If the 'data' parameter is not a dictionary or a JSON string representation of a dictionary.
79+
KeyError
80+
If specified 'fields' do not exist in input data
5281
"""
5382

5483
if fields is None:

aws_lambda_powertools/utilities/data_masking/provider.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
1-
from abc import abstractmethod
1+
from abc import ABC
22
from collections.abc import Iterable
33
from typing import Union
44

55
from aws_lambda_powertools.utilities.data_masking.constants import DATA_MASKING_STRING
66

77

8-
class BaseProvider:
8+
class BaseProvider(ABC):
99
"""
1010
When you try to create an instance of a subclass that does not implement the encrypt method,
1111
you will get a NotImplementedError with a message that says the method is not implemented:
1212
"""
1313

14-
@abstractmethod
1514
def encrypt(self, data) -> Union[bytes, str]:
1615
raise NotImplementedError("Subclasses must implement encrypt()")
1716

18-
@abstractmethod
19-
def decrypt(self, data) -> Union[bytes, str]:
17+
def decrypt(self, data) -> any:
2018
raise NotImplementedError("Subclasses must implement decrypt()")
2119

2220
def mask(self, data) -> str:

aws_lambda_powertools/utilities/data_masking/providers/aws_encryption_sdk.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ def __call__(cls, *args, **provider_options):
3939
class AwsEncryptionSdkProvider(BaseProvider):
4040
"""
4141
The AwsEncryptionSdkProvider is to be used as a Provider for the Datamasking class.
42-
Example:
42+
43+
Example
44+
-------
4345
>>> data_masker = DataMasking(provider=AwsEncryptionSdkProvider(keys=[keyARN1, keyARN2,...,]))
4446
>>> encrypted_data = data_masker.encrypt("a string")
4547
"encrptedBase64String"
@@ -73,14 +75,16 @@ def encrypt(self, data: Union[bytes, str], **provider_options) -> str:
7375
"""
7476
Encrypt data using the AwsEncryptionSdkProvider.
7577
76-
Parameters:
77-
- data (Union[bytes, str]):
78+
Parameters
79+
-------
80+
data : Union[bytes, str]
7881
The data to be encrypted.
79-
- provider_options:
82+
provider_options
8083
Additional options for the aws_encryption_sdk.EncryptionSDKClient
8184
82-
Returns:
83-
- ciphertext (str):
85+
Returns
86+
-------
87+
ciphertext : str
8488
The encrypted data, as a base64-encoded string.
8589
"""
8690
ciphertext, _ = self.client.encrypt(source=data, materials_manager=self.cache_cmm, **provider_options)
@@ -91,14 +95,16 @@ def decrypt(self, data: str, **provider_options) -> bytes:
9195
"""
9296
Decrypt data using AwsEncryptionSdkProvider.
9397
94-
Parameters:
95-
- data (Union[bytes, str]):
96-
The encrypted data, as a base64-encoded string.
97-
- provider_options:
98+
Parameters
99+
-------
100+
data : Union[bytes, str]
101+
The encrypted data, as a base64-encoded string
102+
provider_options
98103
Additional options for the aws_encryption_sdk.EncryptionSDKClient
99104
100-
Returns:
101-
- ciphertext (bytes):
105+
Returns
106+
-------
107+
ciphertext : bytes
102108
The decrypted data in bytes
103109
"""
104110
ciphertext_decoded = base64.b64decode(data)

0 commit comments

Comments
 (0)