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 | |
Rick Bartra | 89f498f | 2017-03-20 15:54:45 -0400 | [diff] [blame] | 16 | import oslo_utils.uuidutils as uuid_utils |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 17 | import six |
| 18 | import time |
Felipe Monteiro | 34a138c | 2017-03-02 17:01:37 -0500 | [diff] [blame] | 19 | |
| 20 | from tempest.common import credentials_factory |
| 21 | from tempest import config |
| 22 | from tempest.test import BaseTestCase |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 23 | |
Rajiv Kumar | 645dfc9 | 2017-01-19 13:48:27 +0530 | [diff] [blame] | 24 | from oslo_log import log as logging |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 25 | |
Felipe Monteiro | 34a138c | 2017-03-02 17:01:37 -0500 | [diff] [blame] | 26 | from patrole_tempest_plugin import rbac_exceptions |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 27 | |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 28 | CONF = config.CONF |
Felipe Monteiro | 34a138c | 2017-03-02 17:01:37 -0500 | [diff] [blame] | 29 | LOG = logging.getLogger(__name__) |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 30 | |
| 31 | |
| 32 | class Singleton(type): |
| 33 | _instances = {} |
| 34 | |
| 35 | def __call__(cls, *args, **kwargs): |
| 36 | if cls not in cls._instances: |
| 37 | cls._instances[cls] = super(Singleton, cls).__call__(*args, |
| 38 | **kwargs) |
| 39 | return cls._instances[cls] |
| 40 | |
| 41 | |
| 42 | @six.add_metaclass(Singleton) |
| 43 | class RbacUtils(object): |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 44 | |
Felipe Monteiro | 34a138c | 2017-03-02 17:01:37 -0500 | [diff] [blame] | 45 | def __init__(cls): |
| 46 | creds_provider = credentials_factory.get_credentials_provider( |
| 47 | name=__name__, |
| 48 | force_tenant_isolation=True, |
| 49 | identity_version=BaseTestCase.get_identity_version()) |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 50 | |
Felipe Monteiro | 34a138c | 2017-03-02 17:01:37 -0500 | [diff] [blame] | 51 | cls.creds_client = creds_provider.creds_client |
| 52 | cls.available_roles = cls.creds_client.roles_client.list_roles() |
| 53 | cls.admin_role_id = cls.rbac_role_id = None |
| 54 | for item in cls.available_roles['roles']: |
| 55 | if item['name'] == CONF.rbac.rbac_test_role: |
| 56 | cls.rbac_role_id = item['id'] |
| 57 | if item['name'] == 'admin': |
| 58 | cls.admin_role_id = item['id'] |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 59 | |
Felipe Monteiro | 34a138c | 2017-03-02 17:01:37 -0500 | [diff] [blame] | 60 | def switch_role(cls, test_obj, switchToRbacRole=None): |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 61 | LOG.debug('Switching role to: %s', switchToRbacRole) |
Felipe Monteiro | b3b7bc8 | 2017-03-03 15:58:15 -0500 | [diff] [blame] | 62 | # Check if admin and rbac roles exist. |
| 63 | if not cls.admin_role_id or not cls.rbac_role_id: |
| 64 | msg = ("Defined 'rbac_role' or 'admin' role does not exist" |
| 65 | " in the system.") |
| 66 | raise rbac_exceptions.RbacResourceSetupFailed(msg) |
| 67 | |
Felipe Monteiro | 34a138c | 2017-03-02 17:01:37 -0500 | [diff] [blame] | 68 | if not isinstance(switchToRbacRole, bool): |
| 69 | msg = ("Wrong value for parameter 'switchToRbacRole' is passed." |
| 70 | " It should be either 'True' or 'False'.") |
Felipe Monteiro | b3b7bc8 | 2017-03-03 15:58:15 -0500 | [diff] [blame] | 71 | raise rbac_exceptions.RbacResourceSetupFailed(msg) |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 72 | |
| 73 | try: |
Felipe Monteiro | 34a138c | 2017-03-02 17:01:37 -0500 | [diff] [blame] | 74 | user_id = test_obj.auth_provider.credentials.user_id |
| 75 | project_id = test_obj.auth_provider.credentials.tenant_id |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 76 | |
Felipe Monteiro | b3b7bc8 | 2017-03-03 15:58:15 -0500 | [diff] [blame] | 77 | cls._clear_user_roles(user_id, project_id) |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 78 | |
| 79 | if switchToRbacRole: |
Felipe Monteiro | 34a138c | 2017-03-02 17:01:37 -0500 | [diff] [blame] | 80 | cls.creds_client.roles_client.create_user_role_on_project( |
| 81 | project_id, user_id, cls.rbac_role_id) |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 82 | else: |
Felipe Monteiro | 34a138c | 2017-03-02 17:01:37 -0500 | [diff] [blame] | 83 | cls.creds_client.roles_client.create_user_role_on_project( |
| 84 | project_id, user_id, cls.admin_role_id) |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 85 | |
| 86 | except Exception as exp: |
| 87 | LOG.error(exp) |
| 88 | raise |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 89 | |
Felipe Monteiro | 34a138c | 2017-03-02 17:01:37 -0500 | [diff] [blame] | 90 | finally: |
Felipe Monteiro | 23923f0 | 2017-03-17 02:15:07 +0000 | [diff] [blame] | 91 | # NOTE(felipemonteiro): These two comments below are copied from |
| 92 | # tempest.api.identity.v2/v3.test_users. |
| 93 | # |
| 94 | # Reset auth again to verify the password restore does work. |
| 95 | # Clear auth restores the original credentials and deletes |
| 96 | # cached auth data. |
| 97 | test_obj.auth_provider.clear_auth() |
| 98 | # Fernet tokens are not subsecond aware and Keystone should only be |
| 99 | # precise to the second. Sleep to ensure we are passing the second |
Rick Bartra | 89f498f | 2017-03-20 15:54:45 -0400 | [diff] [blame] | 100 | # boundary before attempting to authenticate. If token is of type |
| 101 | # uuid, then do not sleep. |
| 102 | if not uuid_utils.is_uuid_like(cls.creds_client. |
| 103 | identity_client.token): |
| 104 | time.sleep(1) |
Felipe Monteiro | 23923f0 | 2017-03-17 02:15:07 +0000 | [diff] [blame] | 105 | test_obj.auth_provider.set_auth() |
Felipe Monteiro | 34a138c | 2017-03-02 17:01:37 -0500 | [diff] [blame] | 106 | |
Felipe Monteiro | b3b7bc8 | 2017-03-03 15:58:15 -0500 | [diff] [blame] | 107 | def _clear_user_roles(cls, user_id, tenant_id): |
| 108 | roles = cls.creds_client.roles_client.list_user_roles_on_project( |
| 109 | tenant_id, user_id)['roles'] |
| 110 | |
| 111 | for role in roles: |
| 112 | cls.creds_client.roles_client.delete_role_from_user_on_project( |
| 113 | tenant_id, user_id, role['id']) |
| 114 | |
Felipe Monteiro | 34a138c | 2017-03-02 17:01:37 -0500 | [diff] [blame] | 115 | rbac_utils = RbacUtils |