-
Notifications
You must be signed in to change notification settings - Fork 0
/
wsgi.py
63 lines (53 loc) · 1.87 KB
/
wsgi.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
"""Application entry point."""
import os
import re
from logging import Filter
from logging.config import dictConfig
from hashlib import sha512
from smrtuncrndsh import create_app
class RedactingFilter(Filter):
def filter(self, record):
# print(record.msg)
# print(self.redact(record.msg))
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 replace_pwd(self, match_object):
# print(match_object.group("pwd"))
if match_object.group("pwd"):
hashed_pwd = sha512(match_object.group("pwd").encode()).hexdigest()
return match_object[0].replace(match_object.group('pwd'), hashed_pwd)
return match_object[0]
def redact(self, msg):
msg = str(msg)
return re.sub(r'(?:(\w+):\/\/(.*?):)(?P<pwd>.*?)(?:\@(.*?):(.*?)\/(\w+))', self.replace_pwd, msg)
dictConfig({
'version': 1,
'filters': {
'myfilter': {
'()': RedactingFilter,
}
},
'formatters': {'default': {
'format': 'File "%(pathname)-75s", line %(lineno)-3d, in %(funcName)-20s: %(levelname)-8s : %(message)s',
# 'funcName: %(funcName)s line: %(lineno)d path: %(pathname)s filename: %(filename)s module: %(module)15s -
# %(levelname)-8s : %(message)s',
}},
'handlers': {'wsgi': {
'class': 'logging.StreamHandler',
'stream': 'ext://flask.logging.wsgi_errors_stream',
'formatter': 'default',
'filters': ['myfilter']
}},
'root': {
'level': os.environ.get('LOG_LEVEL', 'WARNING'),
'handlers': ['wsgi']
}
})
app = create_app()
if __name__ == "__main__":
app.run(host='0.0.0.0', debug=True)