Skip to content

Commit b944d94

Browse files
committed
include initiator user id in metric for ks auth req
1 parent e218531 commit b944d94

File tree

3 files changed

+84
-7
lines changed

3 files changed

+84
-7
lines changed

watcher/common.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,11 @@ def string_to_bool(possible_bool_string):
222222
"""
223223
if isinstance(possible_bool_string, bool):
224224
return possible_bool_string
225-
if isinstance(possible_bool_string, six.text_type):
226-
return str(possible_bool_string).lower() == 'true'
225+
if isinstance(possible_bool_string, (str, six.text_type)):
226+
return str(possible_bool_string) \
227+
.replace("'", "", -1) \
228+
.replace('"', '', -1) \
229+
.lower() == 'true'
227230
return False
228231

229232

@@ -234,7 +237,7 @@ def load_json_dict(json_body):
234237
:param json_body: the json body of a request might be string, unicode or dict
235238
:return: the json body as a dictionary
236239
"""
237-
if isinstance(json_body, six.text_type):
240+
if isinstance(json_body, (str, six.text_type)):
238241
json_body = json.loads(json_body)
239242
if isinstance(json_body, dict):
240243
return json_body

watcher/tests/test_common.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,52 @@ def test_is_content_json(self):
117117
"should be '{0}' but got '{1}'".format(expected, actual)
118118
)
119119

120+
def test_string_to_bool(self):
121+
stimuli = [
122+
{
123+
'input': u'True',
124+
'expected': True
125+
},
126+
{
127+
'input': u'False',
128+
'expected': False
129+
},
130+
{
131+
'input': 'True',
132+
'expected': True
133+
},
134+
{
135+
'input': "True",
136+
'expected': True
137+
},
138+
{
139+
'input': 'False',
140+
'expected': False
141+
},
142+
{
143+
'input': True,
144+
'expected': True
145+
},
146+
{
147+
'input': False,
148+
'expected': False
149+
},
150+
{
151+
'input': "foobar",
152+
'expected': False
153+
}
154+
]
155+
156+
for s in stimuli:
157+
input = s.get('input')
158+
expected = s.get('expected')
159+
actual = common.string_to_bool(input)
160+
161+
self.assertEqual(
162+
actual,
163+
expected,
164+
"bool of '{0}' should be '{1}' but got '{2}'".format(repr(input), expected, actual)
165+
)
120166

121167

122168
if __name__ == '__main__':

watcher/watcher.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,26 @@ def __init__(self, app, config, logger=logging.getLogger(__name__)):
4949
self.cadf_service_name = self.wsgi_config.get('cadf_service_name', None)
5050
self.service_type = self.wsgi_config.get('service_type', taxonomy.UNKNOWN)
5151
# get the project uid from the request path or from the token (default)
52-
self.is_project_id_from_path = common.string_to_bool(self.wsgi_config.get('target_project_id_from_path', 'False'))
52+
self.is_project_id_from_path = common.string_to_bool(
53+
self.wsgi_config.get('target_project_id_from_path', 'False')
54+
)
55+
# get the project id from the service catalog (see documentation on keystone auth_token middleware)
5356
self.is_project_id_from_service_catalog = common.string_to_bool(
54-
self.wsgi_config.get('target_project_id_from_service_catalog', 'False'))
57+
self.wsgi_config.get('target_project_id_from_service_catalog', 'False')
58+
)
59+
60+
# whether to include the target project id in the metrics
61+
self.is_include_target_project_id_in_metric = common.string_to_bool(
62+
self.wsgi_config.get('include_target_project_id_in_metric', 'True')
63+
)
64+
# whether to include the target domain id in the metrics
65+
self.is_include_target_domain_id_in_metric = common.string_to_bool(
66+
self.wsgi_config.get('include_target_domain_id_in_metric', 'True')
67+
)
68+
# whether to include the initiator user id for authentication request in the metrics
69+
self.is_include_authentication_initiator_user_id_in_metric = common.string_to_bool(
70+
self.wsgi_config.get('include_authentication_initiator_user_id_in_metric', 'True')
71+
)
5572

5673
config_file_path = config.get('config_file', None)
5774
if config_file_path:
@@ -163,10 +180,21 @@ def __call__(self, environ, start_response):
163180
"action:{0}".format(cadf_action),
164181
"initiator_project_id:{0}".format(initiator_project_id),
165182
"initiator_domain_id:{0}".format(initiator_domain_id),
166-
"target_project_id:{0}".format(target_project_id),
167183
"target_type_uri:{0}".format(target_type_uri),
168184
]
169185

186+
# include the target project id in metric
187+
if self.is_include_target_project_id_in_metric:
188+
labels.append(
189+
"target_project_id:{0}".format(target_project_id)
190+
)
191+
192+
# if authentication request: include initiator user id
193+
if cadf_action == taxonomy.ACTION_AUTHENTICATE and self.is_include_authentication_initiator_user_id_in_metric:
194+
labels.append(
195+
"initiator_user_id:{0}".format(initiator_user_id)
196+
)
197+
170198
# if swift request: determine target.container_id based on request path
171199
if common.is_swift_request(req.path) or self.service_type == 'object-store':
172200
_, target_container_id = self.get_target_account_container_id_from_request(req)
@@ -330,7 +358,7 @@ def get_project_domain_and_user_id_from_keystone_authentication_request(self, re
330358
"""
331359
project_id = domain_id = user_id = taxonomy.UNKNOWN
332360
try:
333-
if not req.json_body:
361+
if not req.json:
334362
return
335363

336364
json_body_dict = common.load_json_dict(req.json)

0 commit comments

Comments
 (0)