-
Notifications
You must be signed in to change notification settings - Fork 28
BB2-3486: Mask mbi in logs #1252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import re | ||
| import logging | ||
| import logging.config | ||
|
|
||
| MBI_WITH_HYPHEN_PATTERN = r"""\b | ||
| [1-9](?![SLOIBZsloibz])[A-Za-z](?![SLOIBZsloibz)])[A-Za-z\d]\d | ||
| -(?![SLOIBZsloibz])[A-Za-z](?![SLOIBZsloibz])[A-Za-z\d]\d | ||
| -((?![SLOIBZsloibz])[A-Za-z]){2}\d{2} | ||
| \b | ||
| """ | ||
|
|
||
| MBI_WITHOUT_HYPHEN_PATTERN = r"""\b | ||
| [1-9](?![SLOIBZsloibz])[A-Za-z](?![SLOIBZsloibz)])[A-Za-z\d]\d | ||
| (?![SLOIBZsloibz])[A-Za-z](?![SLOIBZsloibz])[A-Za-z\d]\d | ||
| ((?![SLOIBZsloibzd])[A-Za-z]){2}\d{2} | ||
| \b""" | ||
|
|
||
| MBI_PATTERN = f'({MBI_WITH_HYPHEN_PATTERN}|{MBI_WITHOUT_HYPHEN_PATTERN})' | ||
| SENSITIVE_DATA_FILTER = "sensitive_data_filter" | ||
|
|
||
|
|
||
| def mask_if_has_mbi(text): | ||
| return re.sub(MBI_PATTERN, '***MBI***', str(text), flags=re.VERBOSE) | ||
|
|
||
|
|
||
| def is_not_primitive(value): | ||
| primitive_types = (int, float, bool, str, bytes) | ||
| return not isinstance(value, primitive_types) | ||
|
|
||
|
|
||
| def mask_mbi(value_to_mask): | ||
| if isinstance(value_to_mask, str): | ||
| return mask_if_has_mbi(value_to_mask) | ||
|
|
||
| if isinstance(value_to_mask, tuple): | ||
| return tuple([mask_if_has_mbi(arg) for arg in value_to_mask]) | ||
|
|
||
| if isinstance(value_to_mask, list): | ||
| return [mask_if_has_mbi(arg) for arg in value_to_mask] | ||
|
|
||
| if isinstance(value_to_mask, dict): | ||
| for key, value in value_to_mask.items(): | ||
| if is_not_primitive(value): | ||
| value_to_mask[key] = mask_mbi(value) | ||
| else: | ||
| value_to_mask[key] = mask_if_has_mbi(value) | ||
|
|
||
| return value_to_mask | ||
|
|
||
|
|
||
| class SensitiveDataFilter(logging.Filter): | ||
|
|
||
| def filter(self, record): | ||
| try: | ||
| record.args = mask_mbi(record.args) | ||
| record.msg = mask_mbi(record.msg) | ||
| return True | ||
| except Exception: | ||
| pass | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would you mind explaining some of these changes? I'm not too familiar with how some of this is set up, but curious for instance about why the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you check these logs Here these logs are coming from Adding django to list of loggers will make sure we are capturing all loggers in filter to hide sensitive information. MBI will be exposed if we don't have that entry in list of loggers |
Uh oh!
There was an error while loading. Please reload this page.