Skip to content
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

Adds logger filter for TOKENS when logging in DEBUG MODE #539

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
532: moves the logger warning to the requests initializer, updates tests
  • Loading branch information
Jacques Troussard committed Mar 23, 2024
commit a77b392c852177141efd209884cfc53842e5dc39
7 changes: 7 additions & 0 deletions requests_oauthlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,10 @@

logging.getLogger("requests_oauthlib").addHandler(logging.NullHandler())
logging.getLogger("requests_oauthlib").addFilter(DebugModeTokenFilter())

for filter_ in logging.getLogger("requests_oauthlib").filters:
if isinstance(filter_, DebugModeTokenFilter):
if filter_.mode == 'DEFAULT':
msg = "Your logger, when in DEBUG mode, will log TOKENS"
logging.warning(msg)
break
7 changes: 2 additions & 5 deletions requests_oauthlib/log_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(self):
environment variable.
"""
super().__init__()
self.mode = os.getenv('DEBUG_MODE_TOKEN_FILTER', 'DEFAULT').upper()
self.mode = os.getenv('DEBUG_MODE_TOKEN_FILTER', 'DEFAULT').upper()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest prefixing the envvar with the library name to reduce likelihood of collisions with other environment variables.

Suggested change
self.mode = os.getenv('DEBUG_MODE_TOKEN_FILTER', 'DEFAULT').upper()
self.mode = os.getenv('REQUESTS_OAUTHLIB_DEBUG_MODE_TOKEN_FILTER', 'DEFAULT').upper()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love this comment. In fact I was running into this issue in another project. This project then variable name pattern is clean. I'm going to borrow and apply this pattern there as well. 🙏 Thank you!


def filter(self, record):
"""
Expand All @@ -37,7 +37,4 @@ def filter(self, record):
record.msg = re.sub(r'Bearer (\w+)', '[MASKED]', record.getMessage())
elif self.mode == "SUPPRESS":
return False

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe only suppress log messages which match the above regex? Otherwise this equivalent to disabling the logger entirely.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am hoping a single space would suffice as a replacement? Or maybe just pass None to the msg?

elif self.mode == "DEFAULT":
msg = "Your logger, when in DEBUG mode, will log TOKENS"
raise Warning(msg)
return True
return True # if mode is not MASKED then DEFAULT is implied
5 changes: 2 additions & 3 deletions tests/test_log_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ def test_suppress_mode(self):
@patch.dict('os.environ', {'DEBUG_MODE_TOKEN_FILTER': 'DEFAULT'})
def test_default_mode_raises_warning(self):
filter = DebugModeTokenFilter()
with self.assertRaises(Warning) as context:
filter.filter(self.record)
self.assertTrue("Your logger, when in DEBUG mode, will log TOKENS" in str(context.exception))
result = filter.filter(self.record)
self.assertTrue(result)

if __name__ == '__main__':
unittest.main()
Loading