This repository has been archived by the owner on Nov 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
closes #36- masks params provided in % str formatter in logs, unless …
…set otherwise
- Loading branch information
catherinesmith
committed
Nov 15, 2021
1 parent
dc72d96
commit afebf29
Showing
17 changed files
with
103 additions
and
53 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
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 |
---|---|---|
|
@@ -15,7 +15,6 @@ | |
Field, | ||
) | ||
|
||
logging.basicConfig(level=logging.INFO) | ||
logger = logging.getLogger(__name__) | ||
|
||
|
||
|
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
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
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
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
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
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,51 @@ | ||
import logging | ||
import os | ||
from typing import Any, Mapping, Union | ||
|
||
MASKED = "MASKED" | ||
|
||
|
||
class NotPii(str): | ||
"""whitelist non pii data""" | ||
|
||
|
||
def get_fides_log_record_factory() -> Any: | ||
"""intercepts default LogRecord for custom handling of params""" | ||
|
||
def factory( # pylint: disable=R0913 | ||
name: str, | ||
level: int, | ||
fn: str, | ||
lno: int, | ||
msg: str, | ||
args: Union[tuple[Any, ...], Mapping[str, Any]], | ||
exc_info: Any, | ||
func: str = None, | ||
sinfo: str = None, | ||
) -> logging.LogRecord: | ||
masked_args: tuple[Any, ...] = tuple(_mask_pii_for_logs(arg) for arg in args) | ||
return logging.LogRecord( | ||
name=name, | ||
level=level, | ||
pathname=fn, | ||
lineno=lno, | ||
msg=msg, | ||
args=masked_args, | ||
exc_info=exc_info, | ||
func=func, | ||
sinfo=sinfo, | ||
) | ||
|
||
return factory | ||
|
||
|
||
def _mask_pii_for_logs(pii_text: Any) -> Any: | ||
""" | ||
:param pii_text: param that contains possible pii | ||
:return: depending on ENV config, returns masked pii param | ||
""" | ||
if os.getenv("LOG_PII") == "True": | ||
return pii_text | ||
if isinstance(pii_text, NotPii): | ||
return pii_text | ||
return MASKED |
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,24 @@ | ||
import os | ||
|
||
from fidesops.util import logger | ||
from fidesops.util.logger import NotPii, MASKED | ||
|
||
|
||
def test_logger_masks_pii() -> None: | ||
some_data = "some_data" | ||
result = logger._mask_pii_for_logs(some_data) | ||
assert result == MASKED | ||
|
||
|
||
def test_logger_does_not_mask_pii_env() -> None: | ||
os.environ["LOG_PII"] = "True" | ||
some_data = "some_data" | ||
result = logger._mask_pii_for_logs(some_data) | ||
assert result == some_data | ||
os.environ["LOG_PII"] = "False" | ||
|
||
|
||
def test_logger_does_not_mask_whitelist() -> None: | ||
some_data = NotPii("some_data") | ||
result = logger._mask_pii_for_logs(some_data) | ||
assert result == some_data |