|
1 | 1 | import json |
2 | | -from typing import Any, Callable, Dict, Optional, Union |
| 2 | +from typing import Any, Callable, Dict, Optional, Union, List |
3 | 3 | from opentelemetry.baggage import get_baggage |
4 | 4 | from opentelemetry import context |
5 | 5 | from opentelemetry.context import Context |
@@ -185,19 +185,30 @@ def can_convert_to_dict(s: str) -> bool: |
185 | 185 | def recursive_key_operation( |
186 | 186 | data: Optional[Union[Dict[str, Any], List[Any], str]], |
187 | 187 | operation: Callable[[str], str], |
188 | | - keys_to_match: List[str] = ["key", "token"], |
| 188 | + keys_to_match: List[str] = ["key", "token", "password"], |
189 | 189 | ) -> Optional[Union[Dict[str, Any], List[Any], str]]: |
190 | | - """Recursively checks if any key in the dictionary or JSON object is |
191 | | - present in keys_to_match and applies the operation on the corresponding |
192 | | - value. |
| 190 | + """Recursively traverses a dictionary, list, or JSON string and applies a |
| 191 | + specified operation to the values of keys that match any in the |
| 192 | + `keys_to_match` list. This function is useful for masking sensitive data |
| 193 | + (e.g., keys, tokens, passwords) in nested structures. |
193 | 194 |
|
194 | 195 | Args: |
195 | | - data (dict or list or str): The dictionary or JSON object to traverse. |
196 | | - operation (function): The operation to perform on the matched values. |
197 | | - keys_to_match (list): List of keys to match. |
| 196 | + data (Optional[Union[Dict[str, Any], List[Any], str]]): The input data |
| 197 | + to traverse. This can bea dictionary, list, or JSON string. If a |
| 198 | + JSON string is provided, it will be parsed into a dictionary before |
| 199 | + processing. |
| 200 | +
|
| 201 | + operation (Callable[[str], str]): A function that takes a string value |
| 202 | + and returns a modified string. This operation is applied to the values |
| 203 | + of keys that match any in `keys_to_match`. |
| 204 | + keys_to_match (List[str]): A list of keys to search for in the data. If |
| 205 | + a key matche any in this list, the corresponding value will be processed |
| 206 | + by the `operation`. Defaults to ["key", "token", "password"]. |
198 | 207 |
|
199 | 208 | Returns: |
200 | | - dict or list or str: the modified dictionary, list or string. |
| 209 | + Optional[Union[Dict[str, Any], List[Any], str]]: The modified data structure |
| 210 | + with the operation applied to the values of matched keys. The return type |
| 211 | + matches the input type (dict, list, or str). |
201 | 212 | """ |
202 | 213 | if isinstance(data, str) and can_convert_to_dict(data): |
203 | 214 | data_dict = json.loads(data) |
|
0 commit comments