blob: 65f335516a284749f5113cf874032237cdf19d3b [file] [log] [blame]
rajalakshmi-ganesan7312bb52013-01-29 20:03:42 +05301# Copyright 2013 OpenStack Foundation
2# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
16import json
rajalakshmi-ganesan7312bb52013-01-29 20:03:42 +053017
Haiwei Xuaad85db2014-03-05 05:17:39 +090018from tempest.common import rest_client
Matthew Treinish684d8992014-01-30 16:27:40 +000019from tempest import config
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000020from tempest import exceptions
Matthew Treinish684d8992014-01-30 16:27:40 +000021
22CONF = config.CONF
rajalakshmi-ganesan7312bb52013-01-29 20:03:42 +053023
24
Haiwei Xuaad85db2014-03-05 05:17:39 +090025class IdentityV3ClientJSON(rest_client.RestClient):
rajalakshmi-ganesan7312bb52013-01-29 20:03:42 +053026
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000027 def __init__(self, auth_provider):
28 super(IdentityV3ClientJSON, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000029 self.service = CONF.identity.catalog_type
rajalakshmi-ganesan7312bb52013-01-29 20:03:42 +053030 self.endpoint_url = 'adminURL'
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000031 self.api_version = "v3"
rajalakshmi-ganesan7312bb52013-01-29 20:03:42 +053032
33 def create_user(self, user_name, **kwargs):
34 """Creates a user."""
35 password = kwargs.get('password', None)
36 email = kwargs.get('email', None)
37 en = kwargs.get('enabled', True)
38 project_id = kwargs.get('project_id', None)
39 description = kwargs.get('description', None)
40 domain_id = kwargs.get('domain_id', 'default')
41 post_body = {
42 'project_id': project_id,
43 'description': description,
44 'domain_id': domain_id,
45 'email': email,
46 'enabled': en,
47 'name': user_name,
48 'password': password
49 }
50 post_body = json.dumps({'user': post_body})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020051 resp, body = self.post('users', post_body)
rajalakshmi-ganesan7312bb52013-01-29 20:03:42 +053052 body = json.loads(body)
53 return resp, body['user']
54
55 def update_user(self, user_id, name, **kwargs):
56 """Updates a user."""
nayna-patel755d8142013-07-16 06:45:34 +000057 resp, body = self.get_user(user_id)
58 email = kwargs.get('email', body['email'])
59 en = kwargs.get('enabled', body['enabled'])
60 project_id = kwargs.get('project_id', body['project_id'])
61 description = kwargs.get('description', body['description'])
62 domain_id = kwargs.get('domain_id', body['domain_id'])
rajalakshmi-ganesan7312bb52013-01-29 20:03:42 +053063 post_body = {
64 'name': name,
65 'email': email,
66 'enabled': en,
67 'project_id': project_id,
68 'id': user_id,
69 'domain_id': domain_id,
70 'description': description
71 }
72 post_body = json.dumps({'user': post_body})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020073 resp, body = self.patch('users/%s' % user_id, post_body)
rajalakshmi-ganesan7312bb52013-01-29 20:03:42 +053074 body = json.loads(body)
75 return resp, body['user']
76
77 def list_user_projects(self, user_id):
78 """Lists the projects on which a user has roles assigned."""
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020079 resp, body = self.get('users/%s/projects' % user_id)
rajalakshmi-ganesan7312bb52013-01-29 20:03:42 +053080 body = json.loads(body)
81 return resp, body['projects']
82
83 def get_users(self):
84 """Get the list of users."""
85 resp, body = self.get("users")
86 body = json.loads(body)
87 return resp, body['users']
88
89 def get_user(self, user_id):
90 """GET a user."""
91 resp, body = self.get("users/%s" % user_id)
92 body = json.loads(body)
93 return resp, body['user']
94
95 def delete_user(self, user_id):
96 """Deletes a User."""
97 resp, body = self.delete("users/%s" % user_id)
98 return resp, body
99
100 def create_project(self, name, **kwargs):
101 """Creates a project."""
102 description = kwargs.get('description', None)
103 en = kwargs.get('enabled', True)
104 domain_id = kwargs.get('domain_id', 'default')
105 post_body = {
106 'description': description,
107 'domain_id': domain_id,
108 'enabled': en,
109 'name': name
110 }
111 post_body = json.dumps({'project': post_body})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200112 resp, body = self.post('projects', post_body)
rajalakshmi-ganesan7312bb52013-01-29 20:03:42 +0530113 body = json.loads(body)
114 return resp, body['project']
115
Nayna Patele6331362013-08-12 06:59:48 +0000116 def list_projects(self):
117 resp, body = self.get("projects")
118 body = json.loads(body)
119 return resp, body['projects']
120
121 def update_project(self, project_id, **kwargs):
122 resp, body = self.get_project(project_id)
123 name = kwargs.get('name', body['name'])
124 desc = kwargs.get('description', body['description'])
125 en = kwargs.get('enabled', body['enabled'])
126 domain_id = kwargs.get('domain_id', body['domain_id'])
127 post_body = {
128 'id': project_id,
129 'name': name,
130 'description': desc,
131 'enabled': en,
132 'domain_id': domain_id,
133 }
134 post_body = json.dumps({'project': post_body})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200135 resp, body = self.patch('projects/%s' % project_id, post_body)
Nayna Patele6331362013-08-12 06:59:48 +0000136 body = json.loads(body)
137 return resp, body['project']
138
rajalakshmi-ganesan7312bb52013-01-29 20:03:42 +0530139 def get_project(self, project_id):
140 """GET a Project."""
141 resp, body = self.get("projects/%s" % project_id)
142 body = json.loads(body)
143 return resp, body['project']
144
145 def delete_project(self, project_id):
146 """Delete a project."""
147 resp, body = self.delete('projects/%s' % str(project_id))
148 return resp, body
149
150 def create_role(self, name):
151 """Create a Role."""
152 post_body = {
153 'name': name
154 }
155 post_body = json.dumps({'role': post_body})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200156 resp, body = self.post('roles', post_body)
rajalakshmi-ganesan7312bb52013-01-29 20:03:42 +0530157 body = json.loads(body)
158 return resp, body['role']
159
160 def get_role(self, role_id):
161 """GET a Role."""
162 resp, body = self.get('roles/%s' % str(role_id))
163 body = json.loads(body)
164 return resp, body['role']
165
nayna-patel755d8142013-07-16 06:45:34 +0000166 def update_role(self, name, role_id):
167 """Create a Role."""
168 post_body = {
169 'name': name
170 }
171 post_body = json.dumps({'role': post_body})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200172 resp, body = self.patch('roles/%s' % str(role_id), post_body)
nayna-patel755d8142013-07-16 06:45:34 +0000173 body = json.loads(body)
174 return resp, body['role']
175
rajalakshmi-ganesan7312bb52013-01-29 20:03:42 +0530176 def delete_role(self, role_id):
177 """Delete a role."""
178 resp, body = self.delete('roles/%s' % str(role_id))
179 return resp, body
180
181 def assign_user_role(self, project_id, user_id, role_id):
182 """Add roles to a user on a project."""
183 resp, body = self.put('projects/%s/users/%s/roles/%s' %
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200184 (project_id, user_id, role_id), None)
rajalakshmi-ganesan7312bb52013-01-29 20:03:42 +0530185 return resp, body
nayna-patel4df72dc2013-05-29 10:27:24 +0000186
187 def create_domain(self, name, **kwargs):
188 """Creates a domain."""
189 description = kwargs.get('description', None)
190 en = kwargs.get('enabled', True)
191 post_body = {
192 'description': description,
193 'enabled': en,
194 'name': name
195 }
196 post_body = json.dumps({'domain': post_body})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200197 resp, body = self.post('domains', post_body)
nayna-patel4df72dc2013-05-29 10:27:24 +0000198 body = json.loads(body)
199 return resp, body['domain']
200
201 def delete_domain(self, domain_id):
202 """Delete a domain."""
203 resp, body = self.delete('domains/%s' % str(domain_id))
204 return resp, body
205
206 def list_domains(self):
207 """List Domains."""
208 resp, body = self.get('domains')
209 body = json.loads(body)
210 return resp, body['domains']
211
212 def update_domain(self, domain_id, **kwargs):
213 """Updates a domain."""
214 resp, body = self.get_domain(domain_id)
215 description = kwargs.get('description', body['description'])
216 en = kwargs.get('enabled', body['enabled'])
217 name = kwargs.get('name', body['name'])
218 post_body = {
219 'description': description,
220 'enabled': en,
221 'name': name
222 }
223 post_body = json.dumps({'domain': post_body})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200224 resp, body = self.patch('domains/%s' % domain_id, post_body)
nayna-patel4df72dc2013-05-29 10:27:24 +0000225 body = json.loads(body)
226 return resp, body['domain']
227
228 def get_domain(self, domain_id):
229 """Get Domain details."""
230 resp, body = self.get('domains/%s' % domain_id)
231 body = json.loads(body)
232 return resp, body['domain']
nayna-patelb35f7232013-06-28 07:08:44 +0000233
234 def get_token(self, resp_token):
235 """Get token details."""
236 headers = {'X-Subject-Token': resp_token}
237 resp, body = self.get("auth/tokens", headers=headers)
238 body = json.loads(body)
239 return resp, body['token']
240
241 def delete_token(self, resp_token):
242 """Deletes token."""
243 headers = {'X-Subject-Token': resp_token}
244 resp, body = self.delete("auth/tokens", headers=headers)
245 return resp, body
246
nayna-patel755d8142013-07-16 06:45:34 +0000247 def create_group(self, name, **kwargs):
248 """Creates a group."""
249 description = kwargs.get('description', None)
250 domain_id = kwargs.get('domain_id', 'default')
251 project_id = kwargs.get('project_id', None)
252 post_body = {
253 'description': description,
254 'domain_id': domain_id,
255 'project_id': project_id,
256 'name': name
257 }
258 post_body = json.dumps({'group': post_body})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200259 resp, body = self.post('groups', post_body)
nayna-patel755d8142013-07-16 06:45:34 +0000260 body = json.loads(body)
261 return resp, body['group']
262
Zhi Kun Liue8136f02014-01-07 18:56:28 +0800263 def get_group(self, group_id):
264 """Get group details."""
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200265 resp, body = self.get('groups/%s' % group_id)
Zhi Kun Liue8136f02014-01-07 18:56:28 +0800266 body = json.loads(body)
267 return resp, body['group']
268
269 def update_group(self, group_id, **kwargs):
270 """Updates a group."""
271 resp, body = self.get_group(group_id)
272 name = kwargs.get('name', body['name'])
273 description = kwargs.get('description', body['description'])
274 post_body = {
275 'name': name,
276 'description': description
277 }
278 post_body = json.dumps({'group': post_body})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200279 resp, body = self.patch('groups/%s' % group_id, post_body)
Zhi Kun Liue8136f02014-01-07 18:56:28 +0800280 body = json.loads(body)
281 return resp, body['group']
282
nayna-patel755d8142013-07-16 06:45:34 +0000283 def delete_group(self, group_id):
284 """Delete a group."""
285 resp, body = self.delete('groups/%s' % str(group_id))
286 return resp, body
287
Zhi Kun Liue8136f02014-01-07 18:56:28 +0800288 def add_group_user(self, group_id, user_id):
289 """Add user into group."""
290 resp, body = self.put('groups/%s/users/%s' % (group_id, user_id),
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200291 None)
Zhi Kun Liue8136f02014-01-07 18:56:28 +0800292 return resp, body
293
294 def list_group_users(self, group_id):
295 """List users in group."""
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200296 resp, body = self.get('groups/%s/users' % group_id)
Zhi Kun Liue8136f02014-01-07 18:56:28 +0800297 body = json.loads(body)
298 return resp, body['users']
299
300 def delete_group_user(self, group_id, user_id):
301 """Delete user in group."""
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200302 resp, body = self.delete('groups/%s/users/%s' % (group_id, user_id))
Zhi Kun Liue8136f02014-01-07 18:56:28 +0800303 return resp, body
304
nayna-patel755d8142013-07-16 06:45:34 +0000305 def assign_user_role_on_project(self, project_id, user_id, role_id):
306 """Add roles to a user on a project."""
307 resp, body = self.put('projects/%s/users/%s/roles/%s' %
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200308 (project_id, user_id, role_id), None)
nayna-patel755d8142013-07-16 06:45:34 +0000309 return resp, body
310
311 def assign_user_role_on_domain(self, domain_id, user_id, role_id):
312 """Add roles to a user on a domain."""
313 resp, body = self.put('domains/%s/users/%s/roles/%s' %
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200314 (domain_id, user_id, role_id), None)
nayna-patel755d8142013-07-16 06:45:34 +0000315 return resp, body
316
317 def list_user_roles_on_project(self, project_id, user_id):
318 """list roles of a user on a project."""
319 resp, body = self.get('projects/%s/users/%s/roles' %
320 (project_id, user_id))
321 body = json.loads(body)
322 return resp, body['roles']
323
324 def list_user_roles_on_domain(self, domain_id, user_id):
325 """list roles of a user on a domain."""
326 resp, body = self.get('domains/%s/users/%s/roles' %
327 (domain_id, user_id))
328 body = json.loads(body)
329 return resp, body['roles']
330
331 def revoke_role_from_user_on_project(self, project_id, user_id, role_id):
332 """Delete role of a user on a project."""
333 resp, body = self.delete('projects/%s/users/%s/roles/%s' %
334 (project_id, user_id, role_id))
335 return resp, body
336
337 def revoke_role_from_user_on_domain(self, domain_id, user_id, role_id):
338 """Delete role of a user on a domain."""
339 resp, body = self.delete('domains/%s/users/%s/roles/%s' %
340 (domain_id, user_id, role_id))
341 return resp, body
342
343 def assign_group_role_on_project(self, project_id, group_id, role_id):
344 """Add roles to a user on a project."""
345 resp, body = self.put('projects/%s/groups/%s/roles/%s' %
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200346 (project_id, group_id, role_id), None)
nayna-patel755d8142013-07-16 06:45:34 +0000347 return resp, body
348
349 def assign_group_role_on_domain(self, domain_id, group_id, role_id):
350 """Add roles to a user on a domain."""
351 resp, body = self.put('domains/%s/groups/%s/roles/%s' %
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200352 (domain_id, group_id, role_id), None)
nayna-patel755d8142013-07-16 06:45:34 +0000353 return resp, body
354
355 def list_group_roles_on_project(self, project_id, group_id):
356 """list roles of a user on a project."""
357 resp, body = self.get('projects/%s/groups/%s/roles' %
358 (project_id, group_id))
359 body = json.loads(body)
360 return resp, body['roles']
361
362 def list_group_roles_on_domain(self, domain_id, group_id):
363 """list roles of a user on a domain."""
364 resp, body = self.get('domains/%s/groups/%s/roles' %
365 (domain_id, group_id))
366 body = json.loads(body)
367 return resp, body['roles']
368
369 def revoke_role_from_group_on_project(self, project_id, group_id, role_id):
370 """Delete role of a user on a project."""
371 resp, body = self.delete('projects/%s/groups/%s/roles/%s' %
372 (project_id, group_id, role_id))
373 return resp, body
374
375 def revoke_role_from_group_on_domain(self, domain_id, group_id, role_id):
376 """Delete role of a user on a domain."""
377 resp, body = self.delete('domains/%s/groups/%s/roles/%s' %
378 (domain_id, group_id, role_id))
379 return resp, body
380
Steven Hardybf70c5c2013-10-30 21:55:16 +0000381 def create_trust(self, trustor_user_id, trustee_user_id, project_id,
382 role_names, impersonation, expires_at):
383 """Creates a trust."""
384 roles = [{'name': n} for n in role_names]
385 post_body = {
386 'trustor_user_id': trustor_user_id,
387 'trustee_user_id': trustee_user_id,
388 'project_id': project_id,
389 'impersonation': impersonation,
390 'roles': roles,
391 'expires_at': expires_at
392 }
393 post_body = json.dumps({'trust': post_body})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200394 resp, body = self.post('OS-TRUST/trusts', post_body)
Steven Hardybf70c5c2013-10-30 21:55:16 +0000395 body = json.loads(body)
396 return resp, body['trust']
397
398 def delete_trust(self, trust_id):
399 """Deletes a trust."""
400 resp, body = self.delete("OS-TRUST/trusts/%s" % trust_id)
401 return resp, body
402
403 def get_trusts(self, trustor_user_id=None, trustee_user_id=None):
404 """GET trusts."""
405 if trustor_user_id:
406 resp, body = self.get("OS-TRUST/trusts?trustor_user_id=%s"
407 % trustor_user_id)
408 elif trustee_user_id:
409 resp, body = self.get("OS-TRUST/trusts?trustee_user_id=%s"
410 % trustee_user_id)
411 else:
412 resp, body = self.get("OS-TRUST/trusts")
413 body = json.loads(body)
414 return resp, body['trusts']
415
416 def get_trust(self, trust_id):
417 """GET trust."""
418 resp, body = self.get("OS-TRUST/trusts/%s" % trust_id)
419 body = json.loads(body)
420 return resp, body['trust']
421
422 def get_trust_roles(self, trust_id):
423 """GET roles delegated by a trust."""
424 resp, body = self.get("OS-TRUST/trusts/%s/roles" % trust_id)
425 body = json.loads(body)
426 return resp, body['roles']
427
428 def get_trust_role(self, trust_id, role_id):
429 """GET role delegated by a trust."""
430 resp, body = self.get("OS-TRUST/trusts/%s/roles/%s"
431 % (trust_id, role_id))
432 body = json.loads(body)
433 return resp, body['role']
434
435 def check_trust_role(self, trust_id, role_id):
436 """HEAD Check if role is delegated by a trust."""
437 resp, body = self.head("OS-TRUST/trusts/%s/roles/%s"
438 % (trust_id, role_id))
439 return resp, body
440
nayna-patelb35f7232013-06-28 07:08:44 +0000441
Haiwei Xuaad85db2014-03-05 05:17:39 +0900442class V3TokenClientJSON(rest_client.RestClient):
nayna-patelb35f7232013-06-28 07:08:44 +0000443
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000444 def __init__(self):
445 super(V3TokenClientJSON, self).__init__(None)
446 auth_url = CONF.identity.uri_v3
Matthew Treinishdb2c5972014-01-31 22:18:59 +0000447 if not auth_url and CONF.identity_feature_enabled.api_v3:
448 raise exceptions.InvalidConfiguration('you must specify a v3 uri '
449 'if using the v3 identity '
450 'api')
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000451 if 'auth/tokens' not in auth_url:
452 auth_url = auth_url.rstrip('/') + '/auth/tokens'
nayna-patelb35f7232013-06-28 07:08:44 +0000453
454 self.auth_url = auth_url
nayna-patelb35f7232013-06-28 07:08:44 +0000455
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000456 def auth(self, user, password, tenant=None, user_type='id', domain=None):
457 """
458 :param user: user id or name, as specified in user_type
459 :param domain: the user and tenant domain
460
461 Accepts different combinations of credentials. Restrictions:
462 - tenant and domain are only name (no id)
463 - user domain and tenant domain are assumed identical
464 - domain scope is not supported here
465 Sample sample valid combinations:
466 - user_id, password
467 - username, password, domain
468 - username, password, tenant, domain
469 Validation is left to the server side.
470 """
nayna-patelb35f7232013-06-28 07:08:44 +0000471 creds = {
472 'auth': {
473 'identity': {
474 'methods': ['password'],
475 'password': {
476 'user': {
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000477 'password': password,
nayna-patelb35f7232013-06-28 07:08:44 +0000478 }
479 }
480 }
481 }
482 }
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000483 if user_type == 'id':
484 creds['auth']['identity']['password']['user']['id'] = user
485 else:
486 creds['auth']['identity']['password']['user']['name'] = user
487 if domain is not None:
488 _domain = dict(name=domain)
489 creds['auth']['identity']['password']['user']['domain'] = _domain
490 if tenant is not None:
491 project = dict(name=tenant, domain=_domain)
492 scope = dict(project=project)
493 creds['auth']['scope'] = scope
494
nayna-patelb35f7232013-06-28 07:08:44 +0000495 body = json.dumps(creds)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200496 resp, body = self.post(self.auth_url, body=body)
nayna-patelb35f7232013-06-28 07:08:44 +0000497 return resp, body
498
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000499 def request(self, method, url, headers=None, body=None):
500 """A simple HTTP request interface."""
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200501 if headers is None:
502 # Always accept 'json', for xml token client too.
503 # Because XML response is not easily
504 # converted to the corresponding JSON one
505 headers = self.get_headers(accept_type="json")
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000506 self._log_request(method, url, headers, body)
507 resp, resp_body = self.http_obj.request(url, method,
508 headers=headers, body=body)
509 self._log_response(resp, resp_body)
510
511 if resp.status in [401, 403]:
512 resp_body = json.loads(resp_body)
513 raise exceptions.Unauthorized(resp_body['error']['message'])
514 elif resp.status not in [200, 201, 204]:
515 raise exceptions.IdentityError(
516 'Unexpected status code {0}'.format(resp.status))
517
518 return resp, json.loads(resp_body)
519
520 def get_token(self, user, password, tenant, domain='Default',
521 auth_data=False):
522 """
523 :param user: username
524 Returns (token id, token data) for supplied credentials
525 """
526 resp, body = self.auth(user, password, tenant, user_type='name',
527 domain=domain)
528
529 token = resp.get('x-subject-token')
530 if auth_data:
531 return token, body['token']
532 else:
533 return token