-
Notifications
You must be signed in to change notification settings - Fork 7
/
filters.py
108 lines (86 loc) · 3.38 KB
/
filters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#
# Licensed Materials - Property of IBM
#
# 5737-I09
#
# Copyright IBM Corp. 2019 All Rights Reserved.
# US Government Users Restricted Rights - Use, duplication or
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp
#
import logging, re
class JsonFilter(logging.Filter):
def __init__(self, json_keys):
super(JsonFilter, self).__init__()
self._json_keys = json_keys
def filter(self, record):
record.msg = self.redact(record.msg)
if isinstance(record.args, dict):
for k in record.args.keys():
record.args[k] = self.redact(record.args[k])
else:
record.args = tuple(self.redact(arg) for arg in record.args)
return True
def redact(self, msg):
if not isinstance(msg, str):
msg = str(msg)
for key in self._json_keys:
pattern = '"' + key + '":\s*"[^"]+"'
output = '"' + key + '": "<' + key + '>"'
msg = re.sub(pattern, output, msg)
pattern = '"' + key + '":\s*""'
output = '"' + key + '": "<null>"'
msg = re.sub(pattern, output, msg)
pattern = '"' + key + '=[^"]+"'
output = '"' + key + '=<' + key + '>"'
msg = re.sub(pattern, output, msg)
pattern = '"' + key + '="'
output = '"' + key + '=<null>"'
msg = re.sub(pattern, output, msg)
pattern = '"' + key.lower() + '":\s*"[^"]+"'
output = '"' + key.lower() + '": "<' + key.lower() + '>"'
msg = re.sub(pattern, output, msg)
pattern = '"' + key.lower() + '":\s*""'
output = '"' + key.lower() + '": "<null>"'
msg = re.sub(pattern, output, msg)
pattern = '"' + key.lower() + '=[^"]+"'
output = '"' + key.lower() + '=<' + key.lower() + '>"'
msg = re.sub(pattern, output, msg)
pattern = '"' + key.lower() + '="'
output = '"' + key.lower() + '=<null>"'
msg = re.sub(pattern, output, msg)
return msg
class TokenFilter(logging.Filter):
def __init__(self):
super(TokenFilter, self).__init__()
def filter(self, record):
record.msg = self.redact(record.msg)
if isinstance(record.args, dict):
for k in record.args.keys():
record.args[k] = self.redact(record.args[k])
else:
record.args = tuple(self.redact(arg) for arg in record.args)
return True
def redact(self, msg):
if not isinstance(msg, str):
msg = str(msg)
pattern = 'authorization: Bearer [\S]+'
output = 'authorization: Bearer <TOKEN>'
msg = re.sub(pattern, output, msg)
return msg
class StringFilter(logging.Filter):
def __init__(self, stringstobefiltered):
super(StringFilter, self).__init__()
self._stringstobefiltered = stringstobefiltered
def filter(self, record):
record.msg = self.redact(record.msg)
if isinstance(record.args, dict):
for k in record.args.keys():
record.args[k] = self.redact(record.args[k])
else:
record.args = tuple(self.redact(arg) for arg in record.args)
return True
def redact(self, msg):
for key in self._stringstobefiltered:
if len(key) != 0:
msg = re.sub(re.escape(key), "*******", msg)
return msg