DavidPurcell | b25f93d | 2017-01-27 12:46:27 -0500 | [diff] [blame] | 1 | # Copyright 2017 AT&T Corporation. |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 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 | |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 16 | import six |
| 17 | import time |
Felipe Monteiro | 34a138c | 2017-03-02 17:01:37 -0500 | [diff] [blame] | 18 | |
| 19 | from tempest.common import credentials_factory |
| 20 | from tempest import config |
| 21 | from tempest.test import BaseTestCase |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 22 | |
Rajiv Kumar | 645dfc9 | 2017-01-19 13:48:27 +0530 | [diff] [blame] | 23 | from oslo_log import log as logging |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 24 | |
Felipe Monteiro | 34a138c | 2017-03-02 17:01:37 -0500 | [diff] [blame] | 25 | from patrole_tempest_plugin import rbac_exceptions |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 26 | |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 27 | CONF = config.CONF |
Felipe Monteiro | 34a138c | 2017-03-02 17:01:37 -0500 | [diff] [blame] | 28 | LOG = logging.getLogger(__name__) |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 29 | |
| 30 | |
| 31 | class 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) |
| 42 | class RbacUtils(object): |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 43 | |
Felipe Monteiro | 34a138c | 2017-03-02 17:01:37 -0500 | [diff] [blame] | 44 | 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()) |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 49 | |
Felipe Monteiro | 34a138c | 2017-03-02 17:01:37 -0500 | [diff] [blame] | 50 | 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'] |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 58 | |
Felipe Monteiro | 34a138c | 2017-03-02 17:01:37 -0500 | [diff] [blame] | 59 | def switch_role(cls, test_obj, switchToRbacRole=None): |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 60 | LOG.debug('Switching role to: %s', switchToRbacRole) |
Felipe Monteiro | b3b7bc8 | 2017-03-03 15:58:15 -0500 | [diff] [blame] | 61 | # 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 Monteiro | 34a138c | 2017-03-02 17:01:37 -0500 | [diff] [blame] | 67 | if not isinstance(switchToRbacRole, bool): |
| 68 | msg = ("Wrong value for parameter 'switchToRbacRole' is passed." |
| 69 | " It should be either 'True' or 'False'.") |
Felipe Monteiro | b3b7bc8 | 2017-03-03 15:58:15 -0500 | [diff] [blame] | 70 | raise rbac_exceptions.RbacResourceSetupFailed(msg) |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 71 | |
| 72 | try: |
Felipe Monteiro | 34a138c | 2017-03-02 17:01:37 -0500 | [diff] [blame] | 73 | user_id = test_obj.auth_provider.credentials.user_id |
| 74 | project_id = test_obj.auth_provider.credentials.tenant_id |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 75 | |
Felipe Monteiro | b3b7bc8 | 2017-03-03 15:58:15 -0500 | [diff] [blame] | 76 | cls._clear_user_roles(user_id, project_id) |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 77 | |
| 78 | if switchToRbacRole: |
Felipe Monteiro | 34a138c | 2017-03-02 17:01:37 -0500 | [diff] [blame] | 79 | cls.creds_client.roles_client.create_user_role_on_project( |
| 80 | project_id, user_id, cls.rbac_role_id) |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 81 | else: |
Felipe Monteiro | 34a138c | 2017-03-02 17:01:37 -0500 | [diff] [blame] | 82 | cls.creds_client.roles_client.create_user_role_on_project( |
| 83 | project_id, user_id, cls.admin_role_id) |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 84 | |
| 85 | except Exception as exp: |
| 86 | LOG.error(exp) |
| 87 | raise |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 88 | |
Felipe Monteiro | 34a138c | 2017-03-02 17:01:37 -0500 | [diff] [blame] | 89 | finally: |
| 90 | test_obj.auth_provider.clear_auth() |
Felipe Monteiro | 613de66 | 2017-03-09 05:20:48 +0000 | [diff] [blame^] | 91 | # Sleep to avoid 401 errors caused by rounding in timing of fernet |
| 92 | # token creation. |
Felipe Monteiro | 34a138c | 2017-03-02 17:01:37 -0500 | [diff] [blame] | 93 | time.sleep(1) |
| 94 | test_obj.auth_provider.set_auth() |
| 95 | |
Felipe Monteiro | b3b7bc8 | 2017-03-03 15:58:15 -0500 | [diff] [blame] | 96 | 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 Monteiro | 34a138c | 2017-03-02 17:01:37 -0500 | [diff] [blame] | 104 | rbac_utils = RbacUtils |