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 | ed95005 | 2017-06-29 17:20:33 -0400 | [diff] [blame] | 16 | import abc |
| 17 | import six |
Felipe Monteiro | fa01d5f | 2017-04-01 06:18:25 +0100 | [diff] [blame] | 18 | import sys |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 19 | import time |
Felipe Monteiro | 34a138c | 2017-03-02 17:01:37 -0500 | [diff] [blame] | 20 | |
Rajiv Kumar | 645dfc9 | 2017-01-19 13:48:27 +0530 | [diff] [blame] | 21 | from oslo_log import log as logging |
Felipe Monteiro | 2693bf7 | 2017-08-12 22:56:47 +0100 | [diff] [blame] | 22 | from oslo_utils import excutils |
Felipe Monteiro | fa01d5f | 2017-04-01 06:18:25 +0100 | [diff] [blame] | 23 | |
Felipe Monteiro | 3e14f47 | 2017-08-17 23:02:11 +0100 | [diff] [blame] | 24 | from tempest import clients |
Felipe Monteiro | e7e552e | 2017-05-02 17:04:12 +0100 | [diff] [blame] | 25 | from tempest.common import credentials_factory as credentials |
Felipe Monteiro | fa01d5f | 2017-04-01 06:18:25 +0100 | [diff] [blame] | 26 | from tempest import config |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 27 | |
Felipe Monteiro | 34a138c | 2017-03-02 17:01:37 -0500 | [diff] [blame] | 28 | from patrole_tempest_plugin import rbac_exceptions |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 29 | |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 30 | CONF = config.CONF |
Felipe Monteiro | 34a138c | 2017-03-02 17:01:37 -0500 | [diff] [blame] | 31 | LOG = logging.getLogger(__name__) |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 32 | |
| 33 | |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 34 | class RbacUtils(object): |
Felipe Monteiro | e8d93e0 | 2017-07-19 20:52:20 +0100 | [diff] [blame] | 35 | """Utility class responsible for switching os_primary role. |
| 36 | |
| 37 | This class is responsible for overriding the value of the primary Tempest |
| 38 | credential's role (i.e. "os_primary" role). By doing so, it is possible to |
| 39 | seamlessly swap between admin credentials, needed for setup and clean up, |
| 40 | and primary credentials, needed to perform the API call which does |
| 41 | policy enforcement. The primary credentials always cycle between roles |
Felipe Monteiro | f6eb862 | 2017-08-06 06:08:02 +0100 | [diff] [blame] | 42 | defined by ``CONF.identity.admin_role`` and `CONF.patrole.rbac_test_role``. |
Felipe Monteiro | e8d93e0 | 2017-07-19 20:52:20 +0100 | [diff] [blame] | 43 | """ |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 44 | |
Felipe Monteiro | b35de58 | 2017-05-05 00:16:53 +0100 | [diff] [blame] | 45 | def __init__(self, test_obj): |
Felipe Monteiro | e8d93e0 | 2017-07-19 20:52:20 +0100 | [diff] [blame] | 46 | """Constructor for ``RbacUtils``. |
| 47 | |
Felipe Monteiro | 2693bf7 | 2017-08-12 22:56:47 +0100 | [diff] [blame] | 48 | :param test_obj: An instance of `tempest.test.BaseTestCase`. |
Felipe Monteiro | e8d93e0 | 2017-07-19 20:52:20 +0100 | [diff] [blame] | 49 | """ |
Felipe Monteiro | e8d93e0 | 2017-07-19 20:52:20 +0100 | [diff] [blame] | 50 | # Intialize the admin roles_client to perform role switching. |
Felipe Monteiro | 3e14f47 | 2017-08-17 23:02:11 +0100 | [diff] [blame] | 51 | admin_mgr = clients.Manager( |
| 52 | credentials.get_configured_admin_credentials()) |
Felipe Monteiro | e8d93e0 | 2017-07-19 20:52:20 +0100 | [diff] [blame] | 53 | if test_obj.get_identity_version() == 'v3': |
Felipe Monteiro | 3e14f47 | 2017-08-17 23:02:11 +0100 | [diff] [blame] | 54 | admin_roles_client = admin_mgr.roles_v3_client |
Felipe Monteiro | e8d93e0 | 2017-07-19 20:52:20 +0100 | [diff] [blame] | 55 | else: |
Felipe Monteiro | 3e14f47 | 2017-08-17 23:02:11 +0100 | [diff] [blame] | 56 | admin_roles_client = admin_mgr.roles_client |
Felipe Monteiro | e8d93e0 | 2017-07-19 20:52:20 +0100 | [diff] [blame] | 57 | |
| 58 | self.admin_roles_client = admin_roles_client |
Felipe Monteiro | b35de58 | 2017-05-05 00:16:53 +0100 | [diff] [blame] | 59 | self.switch_role(test_obj, toggle_rbac_role=False) |
| 60 | |
Felipe Monteiro | 75f2363 | 2017-04-07 15:56:26 +0100 | [diff] [blame] | 61 | # References the last value of `toggle_rbac_role` that was passed to |
Felipe Monteiro | fa01d5f | 2017-04-01 06:18:25 +0100 | [diff] [blame] | 62 | # `switch_role`. Used for ensuring that `switch_role` is correctly used |
| 63 | # in a test file, so that false positives are prevented. The key used |
Felipe Monteiro | 521e5c1 | 2017-04-05 22:59:57 +0100 | [diff] [blame] | 64 | # to index into the dictionary is the module path plus class name, which is |
| 65 | # unique. |
Felipe Monteiro | fa01d5f | 2017-04-01 06:18:25 +0100 | [diff] [blame] | 66 | switch_role_history = {} |
| 67 | admin_role_id = None |
| 68 | rbac_role_id = None |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 69 | |
Felipe Monteiro | 75f2363 | 2017-04-07 15:56:26 +0100 | [diff] [blame] | 70 | def switch_role(self, test_obj, toggle_rbac_role=False): |
Felipe Monteiro | 7be94e8 | 2017-07-26 02:17:08 +0100 | [diff] [blame] | 71 | """Switch the role used by `os_primary` Tempest credentials. |
| 72 | |
| 73 | Switch the role used by `os_primary` credentials to: |
| 74 | * admin if `toggle_rbac_role` is False |
Felipe Monteiro | f6eb862 | 2017-08-06 06:08:02 +0100 | [diff] [blame] | 75 | * `CONF.patrole.rbac_test_role` if `toggle_rbac_role` is True |
Felipe Monteiro | 7be94e8 | 2017-07-26 02:17:08 +0100 | [diff] [blame] | 76 | |
Felipe Monteiro | f6eb862 | 2017-08-06 06:08:02 +0100 | [diff] [blame] | 77 | :param test_obj: test object of type tempest.lib.base.BaseTestCase |
| 78 | :param toggle_rbac_role: role to switch `os_primary` Tempest creds to |
Felipe Monteiro | 7be94e8 | 2017-07-26 02:17:08 +0100 | [diff] [blame] | 79 | """ |
Felipe Monteiro | e8d93e0 | 2017-07-19 20:52:20 +0100 | [diff] [blame] | 80 | self.user_id = test_obj.os_primary.credentials.user_id |
| 81 | self.project_id = test_obj.os_primary.credentials.tenant_id |
| 82 | self.token = test_obj.os_primary.auth_provider.get_token() |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 83 | |
Felipe Monteiro | e8d93e0 | 2017-07-19 20:52:20 +0100 | [diff] [blame] | 84 | LOG.debug('Switching role to: %s.', toggle_rbac_role) |
Felipe Monteiro | 2693bf7 | 2017-08-12 22:56:47 +0100 | [diff] [blame] | 85 | role_already_present = False |
| 86 | |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 87 | try: |
Felipe Monteiro | 2693bf7 | 2017-08-12 22:56:47 +0100 | [diff] [blame] | 88 | if not all([self.admin_role_id, self.rbac_role_id]): |
| 89 | self._get_roles_by_name() |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 90 | |
Felipe Monteiro | b35de58 | 2017-05-05 00:16:53 +0100 | [diff] [blame] | 91 | self._validate_switch_role(test_obj, toggle_rbac_role) |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 92 | |
Felipe Monteiro | 2693bf7 | 2017-08-12 22:56:47 +0100 | [diff] [blame] | 93 | target_role = ( |
| 94 | self.rbac_role_id if toggle_rbac_role else self.admin_role_id) |
| 95 | role_already_present = self._list_and_clear_user_roles_on_project( |
| 96 | target_role) |
| 97 | |
| 98 | # Do not switch roles if `target_role` already exists. |
| 99 | if not role_already_present: |
| 100 | self._create_user_role_on_project(target_role) |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 101 | except Exception as exp: |
Felipe Monteiro | 2693bf7 | 2017-08-12 22:56:47 +0100 | [diff] [blame] | 102 | with excutils.save_and_reraise_exception(): |
| 103 | LOG.exception(exp) |
Felipe Monteiro | 34a138c | 2017-03-02 17:01:37 -0500 | [diff] [blame] | 104 | finally: |
Felipe Monteiro | e8d93e0 | 2017-07-19 20:52:20 +0100 | [diff] [blame] | 105 | test_obj.os_primary.auth_provider.clear_auth() |
Felipe Monteiro | 7be94e8 | 2017-07-26 02:17:08 +0100 | [diff] [blame] | 106 | # Fernet tokens are not subsecond aware so sleep to ensure we are |
| 107 | # passing the second boundary before attempting to authenticate. |
Felipe Monteiro | 2693bf7 | 2017-08-12 22:56:47 +0100 | [diff] [blame] | 108 | # Only sleep if a token revocation occurred as a result of role |
| 109 | # switching. This will optimize test runtime in the case where |
| 110 | # ``[identity] admin_role`` == ``[rbac] rbac_test_role``. |
| 111 | if not role_already_present: |
Rick Bartra | 89f498f | 2017-03-20 15:54:45 -0400 | [diff] [blame] | 112 | time.sleep(1) |
Felipe Monteiro | e8d93e0 | 2017-07-19 20:52:20 +0100 | [diff] [blame] | 113 | test_obj.os_primary.auth_provider.set_auth() |
Felipe Monteiro | 34a138c | 2017-03-02 17:01:37 -0500 | [diff] [blame] | 114 | |
Felipe Monteiro | 2693bf7 | 2017-08-12 22:56:47 +0100 | [diff] [blame] | 115 | def _get_roles_by_name(self): |
| 116 | available_roles = self.admin_roles_client.list_roles() |
| 117 | admin_role_id = rbac_role_id = None |
Felipe Monteiro | fa01d5f | 2017-04-01 06:18:25 +0100 | [diff] [blame] | 118 | |
Felipe Monteiro | 2693bf7 | 2017-08-12 22:56:47 +0100 | [diff] [blame] | 119 | for role in available_roles['roles']: |
Felipe Monteiro | f6eb862 | 2017-08-06 06:08:02 +0100 | [diff] [blame] | 120 | if role['name'] == CONF.patrole.rbac_test_role: |
Felipe Monteiro | 2693bf7 | 2017-08-12 22:56:47 +0100 | [diff] [blame] | 121 | rbac_role_id = role['id'] |
| 122 | if role['name'] == CONF.identity.admin_role: |
| 123 | admin_role_id = role['id'] |
| 124 | |
| 125 | if not all([admin_role_id, rbac_role_id]): |
Felipe Monteiro | f6eb862 | 2017-08-06 06:08:02 +0100 | [diff] [blame] | 126 | msg = ("Roles defined by `[patrole] rbac_test_role` and " |
| 127 | "`[identity] admin_role` must be defined in the system.") |
Felipe Monteiro | 2693bf7 | 2017-08-12 22:56:47 +0100 | [diff] [blame] | 128 | raise rbac_exceptions.RbacResourceSetupFailed(msg) |
| 129 | |
| 130 | self.admin_role_id = admin_role_id |
| 131 | self.rbac_role_id = rbac_role_id |
| 132 | |
| 133 | def _create_user_role_on_project(self, role_id): |
Felipe Monteiro | e8d93e0 | 2017-07-19 20:52:20 +0100 | [diff] [blame] | 134 | self.admin_roles_client.create_user_role_on_project( |
Felipe Monteiro | fa01d5f | 2017-04-01 06:18:25 +0100 | [diff] [blame] | 135 | self.project_id, self.user_id, role_id) |
| 136 | |
Felipe Monteiro | 2693bf7 | 2017-08-12 22:56:47 +0100 | [diff] [blame] | 137 | def _list_and_clear_user_roles_on_project(self, role_id): |
Felipe Monteiro | e8d93e0 | 2017-07-19 20:52:20 +0100 | [diff] [blame] | 138 | roles = self.admin_roles_client.list_user_roles_on_project( |
Felipe Monteiro | fa01d5f | 2017-04-01 06:18:25 +0100 | [diff] [blame] | 139 | self.project_id, self.user_id)['roles'] |
Felipe Monteiro | fa01d5f | 2017-04-01 06:18:25 +0100 | [diff] [blame] | 140 | role_ids = [role['id'] for role in roles] |
Felipe Monteiro | 2693bf7 | 2017-08-12 22:56:47 +0100 | [diff] [blame] | 141 | |
| 142 | # NOTE(felipemonteiro): We do not use ``role_id in role_ids`` here to |
| 143 | # avoid over-permission errors: if the current list of roles on the |
| 144 | # project includes "admin" and "Member", and we are switching to the |
| 145 | # "Member" role, then we must delete the "admin" role. Thus, we only |
| 146 | # return early if the user's roles on the project are an exact match. |
| 147 | if [role_id] == role_ids: |
Felipe Monteiro | fa01d5f | 2017-04-01 06:18:25 +0100 | [diff] [blame] | 148 | return True |
Felipe Monteiro | b3b7bc8 | 2017-03-03 15:58:15 -0500 | [diff] [blame] | 149 | |
| 150 | for role in roles: |
Felipe Monteiro | e8d93e0 | 2017-07-19 20:52:20 +0100 | [diff] [blame] | 151 | self.admin_roles_client.delete_role_from_user_on_project( |
Felipe Monteiro | fa01d5f | 2017-04-01 06:18:25 +0100 | [diff] [blame] | 152 | self.project_id, self.user_id, role['id']) |
| 153 | |
| 154 | return False |
| 155 | |
Felipe Monteiro | 75f2363 | 2017-04-07 15:56:26 +0100 | [diff] [blame] | 156 | def _validate_switch_role(self, test_obj, toggle_rbac_role): |
Felipe Monteiro | 2693bf7 | 2017-08-12 22:56:47 +0100 | [diff] [blame] | 157 | """Validates that the test role passed to `switch_role` is legal. |
Felipe Monteiro | fa01d5f | 2017-04-01 06:18:25 +0100 | [diff] [blame] | 158 | |
| 159 | Throws an error for the following improper usages of `switch_role`: |
| 160 | * `switch_role` is not called with a boolean value |
Felipe Monteiro | 2693bf7 | 2017-08-12 22:56:47 +0100 | [diff] [blame] | 161 | * `switch_role` is never called inside a test, except in tearDown |
Felipe Monteiro | fa01d5f | 2017-04-01 06:18:25 +0100 | [diff] [blame] | 162 | * `switch_role` is called with the same boolean value twice |
Felipe Monteiro | 7be94e8 | 2017-07-26 02:17:08 +0100 | [diff] [blame] | 163 | |
| 164 | If a `skipException` is thrown then this is a legitimate reason why |
| 165 | `switch_role` is not called. |
Felipe Monteiro | fa01d5f | 2017-04-01 06:18:25 +0100 | [diff] [blame] | 166 | """ |
Felipe Monteiro | 75f2363 | 2017-04-07 15:56:26 +0100 | [diff] [blame] | 167 | if not isinstance(toggle_rbac_role, bool): |
Felipe Monteiro | fa01d5f | 2017-04-01 06:18:25 +0100 | [diff] [blame] | 168 | raise rbac_exceptions.RbacResourceSetupFailed( |
Felipe Monteiro | 2693bf7 | 2017-08-12 22:56:47 +0100 | [diff] [blame] | 169 | '`toggle_rbac_role` must be a boolean value.') |
Felipe Monteiro | fa01d5f | 2017-04-01 06:18:25 +0100 | [diff] [blame] | 170 | |
Felipe Monteiro | 521e5c1 | 2017-04-05 22:59:57 +0100 | [diff] [blame] | 171 | # The unique key is the combination of module path plus class name. |
| 172 | class_name = test_obj.__name__ if isinstance(test_obj, type) else \ |
Felipe Monteiro | fa01d5f | 2017-04-01 06:18:25 +0100 | [diff] [blame] | 173 | test_obj.__class__.__name__ |
Felipe Monteiro | 521e5c1 | 2017-04-05 22:59:57 +0100 | [diff] [blame] | 174 | module_name = test_obj.__module__ |
| 175 | key = '%s.%s' % (module_name, class_name) |
| 176 | |
Felipe Monteiro | fa01d5f | 2017-04-01 06:18:25 +0100 | [diff] [blame] | 177 | self.switch_role_history.setdefault(key, None) |
| 178 | |
Felipe Monteiro | 75f2363 | 2017-04-07 15:56:26 +0100 | [diff] [blame] | 179 | if self.switch_role_history[key] == toggle_rbac_role: |
Felipe Monteiro | ba4881b | 2017-04-09 02:11:25 +0100 | [diff] [blame] | 180 | # If an exception was thrown, like a skipException or otherwise, |
| 181 | # then this is a legitimate reason why `switch_role` was not |
| 182 | # called, so only raise an exception if no current exception is |
| 183 | # being handled. |
| 184 | if sys.exc_info()[0] is None: |
Felipe Monteiro | fa01d5f | 2017-04-01 06:18:25 +0100 | [diff] [blame] | 185 | self.switch_role_history[key] = False |
Felipe Monteiro | 75f2363 | 2017-04-07 15:56:26 +0100 | [diff] [blame] | 186 | error_message = '`toggle_rbac_role` must not be called with '\ |
Felipe Monteiro | fa01d5f | 2017-04-01 06:18:25 +0100 | [diff] [blame] | 187 | 'the same bool value twice. Make sure that you included '\ |
| 188 | 'a rbac_utils.switch_role method call inside the test.' |
| 189 | LOG.error(error_message) |
| 190 | raise rbac_exceptions.RbacResourceSetupFailed(error_message) |
| 191 | else: |
Felipe Monteiro | 75f2363 | 2017-04-07 15:56:26 +0100 | [diff] [blame] | 192 | self.switch_role_history[key] = toggle_rbac_role |
Felipe Monteiro | fa01d5f | 2017-04-01 06:18:25 +0100 | [diff] [blame] | 193 | |
Felipe Monteiro | 17e9b49 | 2017-05-27 05:45:20 +0100 | [diff] [blame] | 194 | |
Felipe Monteiro | 8a043fb | 2017-08-06 06:29:05 +0100 | [diff] [blame] | 195 | def is_admin(): |
| 196 | """Verifies whether the current test role equals the admin role. |
| 197 | |
| 198 | :returns: True if ``rbac_test_role`` is the admin role. |
| 199 | """ |
Felipe Monteiro | f6eb862 | 2017-08-06 06:08:02 +0100 | [diff] [blame] | 200 | return CONF.patrole.rbac_test_role == CONF.identity.admin_role |
Rick Bartra | ed95005 | 2017-06-29 17:20:33 -0400 | [diff] [blame] | 201 | |
| 202 | |
| 203 | @six.add_metaclass(abc.ABCMeta) |
| 204 | class RbacAuthority(object): |
Felipe Monteiro | 2a3b513 | 2017-08-11 01:19:06 +0100 | [diff] [blame] | 205 | """Class for validating whether a given role can perform a policy action. |
| 206 | |
| 207 | Any class that extends ``RbacAuthority`` provides the logic for determining |
| 208 | whether a role has permissions to execute a policy action. |
| 209 | """ |
Rick Bartra | ed95005 | 2017-06-29 17:20:33 -0400 | [diff] [blame] | 210 | |
| 211 | @abc.abstractmethod |
Felipe Monteiro | 2a3b513 | 2017-08-11 01:19:06 +0100 | [diff] [blame] | 212 | def allowed(self, rule, role): |
| 213 | """Determine whether the role should be able to perform the API. |
| 214 | |
| 215 | :param rule: The name of the policy enforced by the API. |
| 216 | :param role: The role used to determine whether ``rule`` can be |
| 217 | executed. |
| 218 | :returns: True if the ``role`` has permissions to execute |
| 219 | ``rule``, else False. |
| 220 | """ |