blob: ae018deaab1c539dec843e0c7ea2f6212ff22384 [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']
DavidPurcell029d8c32017-01-06 15:27:41 -050058
Felipe Monteiro34a138c2017-03-02 17:01:37 -050059 def switch_role(cls, test_obj, switchToRbacRole=None):
DavidPurcell029d8c32017-01-06 15:27:41 -050060 LOG.debug('Switching role to: %s', switchToRbacRole)
Felipe Monteirob3b7bc82017-03-03 15:58:15 -050061 # Check if admin and rbac roles exist.
62 if not cls.admin_role_id or not cls.rbac_role_id:
63 msg = ("Defined 'rbac_role' or 'admin' role does not exist"
64 " in the system.")
65 raise rbac_exceptions.RbacResourceSetupFailed(msg)
66
Felipe Monteiro34a138c2017-03-02 17:01:37 -050067 if not isinstance(switchToRbacRole, bool):
68 msg = ("Wrong value for parameter 'switchToRbacRole' is passed."
69 " It should be either 'True' or 'False'.")
Felipe Monteirob3b7bc82017-03-03 15:58:15 -050070 raise rbac_exceptions.RbacResourceSetupFailed(msg)
DavidPurcell029d8c32017-01-06 15:27:41 -050071
72 try:
Felipe Monteiro34a138c2017-03-02 17:01:37 -050073 user_id = test_obj.auth_provider.credentials.user_id
74 project_id = test_obj.auth_provider.credentials.tenant_id
DavidPurcell029d8c32017-01-06 15:27:41 -050075
Felipe Monteirob3b7bc82017-03-03 15:58:15 -050076 cls._clear_user_roles(user_id, project_id)
DavidPurcell029d8c32017-01-06 15:27:41 -050077
78 if switchToRbacRole:
Felipe Monteiro34a138c2017-03-02 17:01:37 -050079 cls.creds_client.roles_client.create_user_role_on_project(
80 project_id, user_id, cls.rbac_role_id)
DavidPurcell029d8c32017-01-06 15:27:41 -050081 else:
Felipe Monteiro34a138c2017-03-02 17:01:37 -050082 cls.creds_client.roles_client.create_user_role_on_project(
83 project_id, user_id, cls.admin_role_id)
DavidPurcell029d8c32017-01-06 15:27:41 -050084
85 except Exception as exp:
86 LOG.error(exp)
87 raise
DavidPurcell029d8c32017-01-06 15:27:41 -050088
Felipe Monteiro34a138c2017-03-02 17:01:37 -050089 finally:
90 test_obj.auth_provider.clear_auth()
Felipe Monteiro613de662017-03-09 05:20:48 +000091 # Sleep to avoid 401 errors caused by rounding in timing of fernet
92 # token creation.
Felipe Monteiro34a138c2017-03-02 17:01:37 -050093 time.sleep(1)
94 test_obj.auth_provider.set_auth()
95
Felipe Monteirob3b7bc82017-03-03 15:58:15 -050096 def _clear_user_roles(cls, user_id, tenant_id):
97 roles = cls.creds_client.roles_client.list_user_roles_on_project(
98 tenant_id, user_id)['roles']
99
100 for role in roles:
101 cls.creds_client.roles_client.delete_role_from_user_on_project(
102 tenant_id, user_id, role['id'])
103
Felipe Monteiro34a138c2017-03-02 17:01:37 -0500104rbac_utils = RbacUtils