Skip to content

Commit 11ffb7c

Browse files
authored
Redact the URL query parameters from the urllib3.connectionpool logs (#341)
* Redact the URL query parameters from the urllib3.connectionpool logs Signed-off-by: Mubashir Kazia <[email protected]> * Fix code formatting Signed-off-by: Mubashir Kazia <[email protected]> * Add str check for the log record message arg dict values Signed-off-by: Mubashir Kazia <[email protected]> --------- Signed-off-by: Mubashir Kazia <[email protected]>
1 parent 0552990 commit 11ffb7c

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/databricks/sql/__init__.py

+33
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,39 @@
88

99
paramstyle = "named"
1010

11+
import re
12+
13+
14+
class RedactUrlQueryParamsFilter(logging.Filter):
15+
pattern = re.compile(r"(\?|&)([\w-]+)=([^&]+)")
16+
mask = r"\1\2=<REDACTED>"
17+
18+
def __init__(self):
19+
super().__init__()
20+
21+
def redact(self, string):
22+
return re.sub(self.pattern, self.mask, str(string))
23+
24+
def filter(self, record):
25+
record.msg = self.redact(str(record.msg))
26+
if isinstance(record.args, dict):
27+
for k in record.args.keys():
28+
record.args[k] = (
29+
self.redact(record.args[k])
30+
if isinstance(record.arg[k], str)
31+
else record.args[k]
32+
)
33+
else:
34+
record.args = tuple(
35+
(self.redact(arg) if isinstance(arg, str) else arg)
36+
for arg in record.args
37+
)
38+
39+
return True
40+
41+
42+
logging.getLogger("urllib3.connectionpool").addFilter(RedactUrlQueryParamsFilter())
43+
1144

1245
class DBAPITypeObject(object):
1346
def __init__(self, *values):

0 commit comments

Comments
 (0)