-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
83 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from typing import Dict | ||
import re | ||
|
||
|
||
def redact_string(value: str) -> str: | ||
"""remove sensitive data from a string | ||
Args: | ||
value (str): a string that may contain sensitive data | ||
Returns: | ||
str: remove sensitive data from string | ||
""" | ||
sensitive_patterns = { | ||
"google_sheets": r"https://sheets\.googleapis\.com/v4/spreadsheets/[\w-]+" | ||
} | ||
_compiled_patterns = { | ||
name: re.compile(pattern) for name, pattern in sensitive_patterns.items() | ||
} | ||
redacted = value | ||
for pattern_name, pattern in _compiled_patterns.items(): | ||
redacted = pattern.sub(f"[REDACTED_{pattern_name.upper()}]", redacted) | ||
return redacted | ||
|
||
|
||
def redacted_sensitive_data_in_exception( | ||
exception_attributes: Dict[str, str] | ||
) -> Dict[str, str]: | ||
"""remove sensitive data in exception | ||
Args: | ||
exception_attributes (dict):a dictionary of exception attributes | ||
Returns: | ||
dict: a dictionary of exception attributes with sensitive data redacted | ||
""" | ||
redacted_exception_attributes = {} | ||
for key, value in exception_attributes.items(): | ||
# remove sensitive information from exception message and stacktrace | ||
if key == "exception.message" or key == "exception.stacktrace": | ||
redacted_exception_attributes[key] = redact_string(value) | ||
else: | ||
redacted_exception_attributes[key] = value | ||
return redacted_exception_attributes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from schematic.utils.remove_sensitive_data_utils import ( | ||
redact_string, | ||
redacted_sensitive_data_in_exception, | ||
) | ||
|
||
|
||
class TestFilterSensitiveData: | ||
def test_redact_string(self) -> None: | ||
# given a string with sensitive data, make sure that they are redacted | ||
sensitive_data = "googleapiclient.errors.HttpError: <HttpError 400 when requesting https://sheets.googleapis.com/v4/spreadsheets/11234budyhf:batchUpdate?fields=%2A&alt=json returned abc>" | ||
redacted_data = redact_string(sensitive_data) | ||
assert ( | ||
redacted_data | ||
== "googleapiclient.errors.HttpError: <HttpError 400 when requesting [REDACTED_GOOGLE_SHEETS]:batchUpdate?fields=%2A&alt=json returned abc>" | ||
) | ||
|
||
def test_redacted_sensitive_data_in_exception(self) -> None: | ||
# given a dictionary of exception attributes, make sure that sensitive data is redacted | ||
exception_attributes = { | ||
"exception.message": "googleapiclient.errors.HttpError: <HttpError 400 when requesting https://sheets.googleapis.com/v4/spreadsheets/11234budyhf:batchUpdate?fields=%2A&alt=json returned>", | ||
"exception.stacktrace": 'Traceback (most recent call last):\n File "<stdin>", line 1, in <module>\n File "<string>", line 1, in <module>\n File "/usr/local/lib/python3.7/dist-packages/googleapiclient/_helpers.py", line 134, in positional_wrapper\n return wrapped(*args, **kwargs)\n File "/usr/local/lib/python3.7/dist-packages/googleapiclient/http.py", line 905, in execute\n raise HttpError(resp, content, uri=self.uri)\ngoogleapiclient.errors.HttpError: <HttpError 400 when requesting https://sheets.googleapis.com/v4/spreadsheets/11234budyhf:batchUpdate?fields=%2A&alt=json returned>', | ||
} | ||
redacted_exception_attributes = redacted_sensitive_data_in_exception( | ||
exception_attributes | ||
) | ||
assert ( | ||
redacted_exception_attributes["exception.message"] | ||
== "googleapiclient.errors.HttpError: <HttpError 400 when requesting [REDACTED_GOOGLE_SHEETS]:batchUpdate?fields=%2A&alt=json returned>" | ||
) | ||
assert ( | ||
redacted_exception_attributes["exception.stacktrace"] | ||
== 'Traceback (most recent call last):\n File "<stdin>", line 1, in <module>\n File "<string>", line 1, in <module>\n File "/usr/local/lib/python3.7/dist-packages/googleapiclient/_helpers.py", line 134, in positional_wrapper\n return wrapped(*args, **kwargs)\n File "/usr/local/lib/python3.7/dist-packages/googleapiclient/http.py", line 905, in execute\n raise HttpError(resp, content, uri=self.uri)\ngoogleapiclient.errors.HttpError: <HttpError 400 when requesting [REDACTED_GOOGLE_SHEETS]:batchUpdate?fields=%2A&alt=json returned>' | ||
) |