Skip to content

Redact the URL query parameters from the urllib3.connectionpool logs #341

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

Merged
merged 3 commits into from
Feb 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/databricks/sql/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,39 @@

paramstyle = "named"

import re


class RedactUrlQueryParamsFilter(logging.Filter):
pattern = re.compile(r"(\?|&)([\w-]+)=([^&]+)")
mask = r"\1\2=<REDACTED>"

def __init__(self):
super().__init__()

def redact(self, string):
return re.sub(self.pattern, self.mask, str(string))

def filter(self, record):
record.msg = self.redact(str(record.msg))
if isinstance(record.args, dict):
for k in record.args.keys():
record.args[k] = (
self.redact(record.args[k])
if isinstance(record.arg[k], str)
else record.args[k]
)
else:
record.args = tuple(
(self.redact(arg) if isinstance(arg, str) else arg)
for arg in record.args
)

return True


logging.getLogger("urllib3.connectionpool").addFilter(RedactUrlQueryParamsFilter())


class DBAPITypeObject(object):
def __init__(self, *values):
Expand Down