-
Notifications
You must be signed in to change notification settings - Fork 0
/
accesslog.py
124 lines (107 loc) · 4.85 KB
/
accesslog.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import webob.dec
import base64
import hashlib
import log
import json
from cinder import wsgi
from cinder.openstack.common import timeutils
from cinder import HWExtend
from oslo.config import cfg
CONF = cfg.CONF
log.init('cinder-api')
APACHE_TIME_FORMAT = '%d/%b/%Y:%H:%M:%S'
APACHE_LOG_FORMAT = (
'%(remote_addr)s - %(remote_user)s [%(datetime)s] "%(method)s %(url)s '
'%(http_version)s" %(status)s %(content_length)s')
DRM_LOG_FORMAT = ('%(remote_addr)s - %(remote_user)s - %(token_id)s '
'[%(request_datetime)s][%(response_datetime)s]'
' %(method)s %(url)s %(http_version)s %(status)s'
' %(content_length)s %(request_body)s %(instance_id)s')
class AccessLogMiddleware(wsgi.Middleware):
"""Writes an access log to INFO."""
@webob.dec.wsgify
def __call__(self, request):
now = timeutils.utcnow()
heartBeatLog = False
reqBody = "-"
if 'xml' in str(request.content_type) or 'json' in str(request.content_type):
if request.content_length is not None and request.content_length < 10240:
reqBody = str(request.body) or '-'
if HWExtend.hasSensitiveStr(reqBody):
reqBody = '-'
data = {
'remote_addr': request.remote_addr,
'remote_user': request.remote_user or '-',
'token_id':"None",
'request_datetime':'%s' % now.strftime(APACHE_TIME_FORMAT),
'response_datetime':'%s' % now.strftime(APACHE_TIME_FORMAT),
'method': request.method,
'url': request.url,
'http_version': request.http_version,
'status': 500,
'content_length': '-',
'request_body':reqBody,
'instance_id':'-'}
token = ''
try:
token = request.headers['X-Auth-Token']
token = HWExtend.b64encodeToken(token)
except:
token = "-"
try:
response = request.get_response(self.application)
data['status'] = response.status_int
data['content_length'] = response.content_length or '-'
finally:
# must be calculated *after* the application has been called
now = timeutils.utcnow()
data['token_id'] = token
if "GET" in data['method'] and "/tokens/" in data['url']:
Pos = data['url'].find("tokens") + 7
logToken = data['url'][Pos:Pos+32]
encodedToken = HWExtend.b64encodeToken(logToken)
data['url'] = data['url'].replace(logToken,encodedToken)
elif ("POST" in data['method']) and (data['url'].endswith("/volumes")):
if int(data['status']) < 400:
try:
resp_body = json.loads(response.body)
volume = resp_body.get('volume', None)
if volume is not None:
instance_id = volume.get('id', None)
if instance_id is not None:
data['instance_id'] = instance_id
except Exception:
pass
#'''---------snapshots ID -----------'''
elif "POST" in data['method'] and data['url'].endswith("/snapshots"):
if int(data['status']) < 400:
try:
resp_body = json.loads(response.body)
snapshots = resp_body.get('snapshot', None)
if snapshots is not None:
instance_id = snapshots.get('id', None)
if instance_id is not None:
data['instance_id'] = instance_id
except Exception:
pass
#'''---------backups ID ----------'''
elif "POST" in data['method'] and data['url'].endswith("/backups"):
if int(data['status']) < 400:
try:
resp_body = json.loads(response.body)
backups = resp_body.get('backup', None)
if backups is not None:
instance_id = backups.get('id', None)
if instance_id is not None:
data['instance_id'] = instance_id
except Exception:
pass
elif "OPTIONS" in data['method'] and data['url'].endswith(":"+str(CONF.osapi_volume_listen_port)+"/"):
heartBeatLog = True
if heartBeatLog != True:
data['response_datetime'] = '%s' % (now.strftime(APACHE_TIME_FORMAT))
log.info(DRM_LOG_FORMAT % data, extra={"type":"operate"})
return response