blob: d5f1c2bc8756e0d592c5229ab9b866167e7e87a9 [file] [log] [blame]
DavidPurcellb25f93d2017-01-27 12:46:27 -05001# Copyright 2017 AT&T Corporation.
DavidPurcell029d8c32017-01-06 15:27:41 -05002# 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
DavidPurcell029d8c32017-01-06 15:27:41 -050016import six
17import time
Felipe Monteiro34a138c2017-03-02 17:01:37 -050018
19from tempest.common import credentials_factory
20from tempest import config
21from tempest.test import BaseTestCase
DavidPurcell029d8c32017-01-06 15:27:41 -050022
Rajiv Kumar645dfc92017-01-19 13:48:27 +053023from oslo_log import log as logging
DavidPurcell029d8c32017-01-06 15:27:41 -050024
Felipe Monteiro34a138c2017-03-02 17:01:37 -050025from patrole_tempest_plugin import rbac_exceptions
DavidPurcell029d8c32017-01-06 15:27:41 -050026
DavidPurcell029d8c32017-01-06 15:27:41 -050027CONF = config.CONF
Felipe Monteiro34a138c2017-03-02 17:01:37 -050028LOG = logging.getLogger(__name__)
DavidPurcell029d8c32017-01-06 15:27:41 -050029
30
31class Singleton(type):
32 _instances = {}
33
34 def __call__(cls, *args, **kwargs):
35 if cls not in cls._instances:
36 cls._instances[cls] = super(Singleton, cls).__call__(*args,
37 **kwargs)
38 return cls._instances[cls]
39
40
41@six.add_metaclass(Singleton)
42class RbacUtils(object):
DavidPurcell029d8c32017-01-06 15:27:41 -050043
Felipe Monteiro34a138c2017-03-02 17:01:37 -050044 def __init__(cls):
45 creds_provider = credentials_factory.get_credentials_provider(
46 name=__name__,
47 force_tenant_isolation=True,
48 identity_version=BaseTestCase.get_identity_version())
DavidPurcell029d8c32017-01-06 15:27:41 -050049
Felipe Monteiro34a138c2017-03-02 17:01:37 -050050 cls.creds_client = creds_provider.creds_client
51 cls.available_roles = cls.creds_client.roles_client.list_roles()
52 cls.admin_role_id = cls.rbac_role_id = None
53 for item in cls.available_roles['roles']:
54 if item['name'] == CONF.rbac.rbac_test_role:
55 cls.rbac_role_id = item['id']
56 if item['name'] == 'admin':
57 cls.admin_role_id = item['id']
58 # Check if admin and rbac role exits
59 if not cls.admin_role_id or not cls.rbac_role_id:
60 msg = ("defined 'rbac_role' or 'admin' role does not exist"
61 " in the system.")
62 raise rbac_exceptions.RbacResourceSetupFailed(msg)
DavidPurcell029d8c32017-01-06 15:27:41 -050063
Felipe Monteiro34a138c2017-03-02 17:01:37 -050064 def clear_user_roles(cls, user_id, tenant_id):
65 roles = cls.creds_client.roles_client.list_user_roles_on_project(
66 tenant_id, user_id)['roles']
DavidPurcell029d8c32017-01-06 15:27:41 -050067
Felipe Monteiro34a138c2017-03-02 17:01:37 -050068 for role in roles:
69 cls.creds_client.roles_client.delete_role_from_user_on_project(
70 tenant_id, user_id, role['id'])
DavidPurcell029d8c32017-01-06 15:27:41 -050071
Felipe Monteiro34a138c2017-03-02 17:01:37 -050072 def switch_role(cls, test_obj, switchToRbacRole=None):
DavidPurcell029d8c32017-01-06 15:27:41 -050073 LOG.debug('Switching role to: %s', switchToRbacRole)
Felipe Monteiro34a138c2017-03-02 17:01:37 -050074 if not isinstance(switchToRbacRole, bool):
75 msg = ("Wrong value for parameter 'switchToRbacRole' is passed."
76 " It should be either 'True' or 'False'.")
77 raise rbac_exceptions.RbacActionFailed(msg)
DavidPurcell029d8c32017-01-06 15:27:41 -050078
79 try:
Felipe Monteiro34a138c2017-03-02 17:01:37 -050080 user_id = test_obj.auth_provider.credentials.user_id
81 project_id = test_obj.auth_provider.credentials.tenant_id
DavidPurcell029d8c32017-01-06 15:27:41 -050082
Felipe Monteiro34a138c2017-03-02 17:01:37 -050083 cls.clear_user_roles(user_id, project_id)
DavidPurcell029d8c32017-01-06 15:27:41 -050084
85 if switchToRbacRole:
Felipe Monteiro34a138c2017-03-02 17:01:37 -050086 cls.creds_client.roles_client.create_user_role_on_project(
87 project_id, user_id, cls.rbac_role_id)
DavidPurcell029d8c32017-01-06 15:27:41 -050088 else:
Felipe Monteiro34a138c2017-03-02 17:01:37 -050089 cls.creds_client.roles_client.create_user_role_on_project(
90 project_id, user_id, cls.admin_role_id)
DavidPurcell029d8c32017-01-06 15:27:41 -050091
92 except Exception as exp:
93 LOG.error(exp)
94 raise
DavidPurcell029d8c32017-01-06 15:27:41 -050095
Felipe Monteiro34a138c2017-03-02 17:01:37 -050096 finally:
97 test_obj.auth_provider.clear_auth()
98 # Sleep to avoid 401 errors caused by rounding
99 # In timing of fernet token creation
100 time.sleep(1)
101 test_obj.auth_provider.set_auth()
102
103rbac_utils = RbacUtils