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 | |
Felipe Monteiro | 10e82fd | 2017-11-21 01:47:20 +0000 | [diff] [blame] | 16 | from contextlib import contextmanager |
Mykola Yakovliev | 11376ab | 2018-08-06 15:34:22 -0500 | [diff] [blame] | 17 | import sys |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 18 | import time |
Felipe Monteiro | 34a138c | 2017-03-02 17:01:37 -0500 | [diff] [blame] | 19 | |
Rajiv Kumar | 645dfc9 | 2017-01-19 13:48:27 +0530 | [diff] [blame] | 20 | from oslo_log import log as logging |
Felipe Monteiro | 2693bf7 | 2017-08-12 22:56:47 +0100 | [diff] [blame] | 21 | from oslo_utils import excutils |
Felipe Monteiro | fa01d5f | 2017-04-01 06:18:25 +0100 | [diff] [blame] | 22 | |
Felipe Monteiro | 3e14f47 | 2017-08-17 23:02:11 +0100 | [diff] [blame] | 23 | from tempest import clients |
Felipe Monteiro | e7e552e | 2017-05-02 17:04:12 +0100 | [diff] [blame] | 24 | from tempest.common import credentials_factory as credentials |
Felipe Monteiro | fa01d5f | 2017-04-01 06:18:25 +0100 | [diff] [blame] | 25 | from tempest import config |
Felipe Monteiro | bf524fb | 2018-10-03 09:03:35 -0500 | [diff] [blame] | 26 | from tempest.lib import exceptions as lib_exc |
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 | 10e82fd | 2017-11-21 01:47:20 +0000 | [diff] [blame] | 35 | """Utility class responsible for switching ``os_primary`` role. |
Felipe Monteiro | e8d93e0 | 2017-07-19 20:52:20 +0100 | [diff] [blame] | 36 | |
| 37 | This class is responsible for overriding the value of the primary Tempest |
Felipe Monteiro | 10e82fd | 2017-11-21 01:47:20 +0000 | [diff] [blame] | 38 | credential's role (i.e. ``os_primary`` role). By doing so, it is possible |
| 39 | to seamlessly swap between admin credentials, needed for setup and clean |
| 40 | up, and primary credentials, needed to perform the API call which does |
Felipe Monteiro | e8d93e0 | 2017-07-19 20:52:20 +0100 | [diff] [blame] | 41 | policy enforcement. The primary credentials always cycle between roles |
Felipe Monteiro | 10e82fd | 2017-11-21 01:47:20 +0000 | [diff] [blame] | 42 | defined by ``CONF.identity.admin_role`` and |
| 43 | ``CONF.patrole.rbac_test_role``. |
Felipe Monteiro | e8d93e0 | 2017-07-19 20:52:20 +0100 | [diff] [blame] | 44 | """ |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 45 | |
Felipe Monteiro | b35de58 | 2017-05-05 00:16:53 +0100 | [diff] [blame] | 46 | def __init__(self, test_obj): |
Felipe Monteiro | e8d93e0 | 2017-07-19 20:52:20 +0100 | [diff] [blame] | 47 | """Constructor for ``RbacUtils``. |
| 48 | |
Felipe Monteiro | 2693bf7 | 2017-08-12 22:56:47 +0100 | [diff] [blame] | 49 | :param test_obj: An instance of `tempest.test.BaseTestCase`. |
Felipe Monteiro | e8d93e0 | 2017-07-19 20:52:20 +0100 | [diff] [blame] | 50 | """ |
Felipe Monteiro | e8d93e0 | 2017-07-19 20:52:20 +0100 | [diff] [blame] | 51 | # Intialize the admin roles_client to perform role switching. |
Felipe Monteiro | 3e14f47 | 2017-08-17 23:02:11 +0100 | [diff] [blame] | 52 | admin_mgr = clients.Manager( |
| 53 | credentials.get_configured_admin_credentials()) |
Felipe Monteiro | bf524fb | 2018-10-03 09:03:35 -0500 | [diff] [blame] | 54 | if CONF.identity_feature_enabled.api_v3: |
Felipe Monteiro | 3e14f47 | 2017-08-17 23:02:11 +0100 | [diff] [blame] | 55 | admin_roles_client = admin_mgr.roles_v3_client |
Felipe Monteiro | e8d93e0 | 2017-07-19 20:52:20 +0100 | [diff] [blame] | 56 | else: |
Felipe Monteiro | bf524fb | 2018-10-03 09:03:35 -0500 | [diff] [blame] | 57 | raise lib_exc.InvalidConfiguration( |
| 58 | "Patrole role overriding only supports v3 identity API.") |
Felipe Monteiro | e8d93e0 | 2017-07-19 20:52:20 +0100 | [diff] [blame] | 59 | |
| 60 | self.admin_roles_client = admin_roles_client |
Felipe Monteiro | 10e82fd | 2017-11-21 01:47:20 +0000 | [diff] [blame] | 61 | self._override_role(test_obj, False) |
Felipe Monteiro | b35de58 | 2017-05-05 00:16:53 +0100 | [diff] [blame] | 62 | |
Felipe Monteiro | fa01d5f | 2017-04-01 06:18:25 +0100 | [diff] [blame] | 63 | admin_role_id = None |
| 64 | rbac_role_id = None |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 65 | |
Felipe Monteiro | 10e82fd | 2017-11-21 01:47:20 +0000 | [diff] [blame] | 66 | @contextmanager |
| 67 | def override_role(self, test_obj): |
| 68 | """Override the role used by ``os_primary`` Tempest credentials. |
| 69 | |
| 70 | Temporarily change the role used by ``os_primary`` credentials to: |
Masayuki Igawa | 80b9aab | 2018-01-09 17:00:45 +0900 | [diff] [blame] | 71 | |
| 72 | * ``[patrole] rbac_test_role`` before test execution |
| 73 | * ``[identity] admin_role`` after test execution |
Felipe Monteiro | 10e82fd | 2017-11-21 01:47:20 +0000 | [diff] [blame] | 74 | |
| 75 | Automatically switches to admin role after test execution. |
| 76 | |
| 77 | :param test_obj: Instance of ``tempest.test.BaseTestCase``. |
| 78 | :returns: None |
| 79 | |
| 80 | .. warning:: |
| 81 | |
| 82 | This function can alter user roles for pre-provisioned credentials. |
| 83 | Work is underway to safely clean up after this function. |
| 84 | |
| 85 | Example:: |
| 86 | |
| 87 | @rbac_rule_validation.action(service='test', |
Felipe Monteiro | 59f538f | 2018-08-22 23:34:40 -0400 | [diff] [blame] | 88 | rules=['a:test:rule']) |
Felipe Monteiro | 10e82fd | 2017-11-21 01:47:20 +0000 | [diff] [blame] | 89 | def test_foo(self): |
| 90 | # Allocate test-level resources here. |
| 91 | with self.rbac_utils.override_role(self): |
melissaml | 7cd2161 | 2018-05-23 21:00:50 +0800 | [diff] [blame] | 92 | # The role for `os_primary` has now been overridden. Within |
Felipe Monteiro | 10e82fd | 2017-11-21 01:47:20 +0000 | [diff] [blame] | 93 | # this block, call the API endpoint that enforces the |
| 94 | # expected policy specified by "rule" in the decorator. |
| 95 | self.foo_service.bar_api_call() |
| 96 | # The role is switched back to admin automatically. Note that |
| 97 | # if the API call above threw an exception, any code below this |
| 98 | # point in the test is not executed. |
| 99 | """ |
Mykola Yakovliev | 11376ab | 2018-08-06 15:34:22 -0500 | [diff] [blame] | 100 | test_obj._set_override_role_called() |
Felipe Monteiro | 10e82fd | 2017-11-21 01:47:20 +0000 | [diff] [blame] | 101 | self._override_role(test_obj, True) |
| 102 | try: |
| 103 | # Execute the test. |
| 104 | yield |
| 105 | finally: |
Mykola Yakovliev | 11376ab | 2018-08-06 15:34:22 -0500 | [diff] [blame] | 106 | # Check whether an exception was raised. If so, remember that |
| 107 | # for future validation. |
| 108 | exc = sys.exc_info()[0] |
| 109 | if exc is not None: |
| 110 | test_obj._set_override_role_caught_exc() |
Felipe Monteiro | 10e82fd | 2017-11-21 01:47:20 +0000 | [diff] [blame] | 111 | # This code block is always executed, no matter the result of the |
| 112 | # test. Automatically switch back to the admin role for test clean |
| 113 | # up. |
| 114 | self._override_role(test_obj, False) |
| 115 | |
Felipe Monteiro | 10e82fd | 2017-11-21 01:47:20 +0000 | [diff] [blame] | 116 | def _override_role(self, test_obj, toggle_rbac_role=False): |
| 117 | """Private helper for overriding ``os_primary`` Tempest credentials. |
| 118 | |
Felipe Monteiro | 07a1c17 | 2017-12-10 04:26:08 +0000 | [diff] [blame] | 119 | :param test_obj: instance of :py:class:`tempest.test.BaseTestCase` |
Felipe Monteiro | 10e82fd | 2017-11-21 01:47:20 +0000 | [diff] [blame] | 120 | :param toggle_rbac_role: Boolean value that controls the role that |
| 121 | overrides default role of ``os_primary`` credentials. |
| 122 | * If True: role is set to ``[patrole] rbac_test_role`` |
| 123 | * If False: role is set to ``[identity] admin_role`` |
| 124 | """ |
Felipe Monteiro | e8d93e0 | 2017-07-19 20:52:20 +0100 | [diff] [blame] | 125 | self.user_id = test_obj.os_primary.credentials.user_id |
| 126 | self.project_id = test_obj.os_primary.credentials.tenant_id |
| 127 | self.token = test_obj.os_primary.auth_provider.get_token() |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 128 | |
Felipe Monteiro | 10e82fd | 2017-11-21 01:47:20 +0000 | [diff] [blame] | 129 | LOG.debug('Overriding role to: %s.', toggle_rbac_role) |
Felipe Monteiro | 2693bf7 | 2017-08-12 22:56:47 +0100 | [diff] [blame] | 130 | role_already_present = False |
| 131 | |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 132 | try: |
Felipe Monteiro | 2693bf7 | 2017-08-12 22:56:47 +0100 | [diff] [blame] | 133 | if not all([self.admin_role_id, self.rbac_role_id]): |
| 134 | self._get_roles_by_name() |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 135 | |
Felipe Monteiro | 2693bf7 | 2017-08-12 22:56:47 +0100 | [diff] [blame] | 136 | target_role = ( |
| 137 | self.rbac_role_id if toggle_rbac_role else self.admin_role_id) |
| 138 | role_already_present = self._list_and_clear_user_roles_on_project( |
| 139 | target_role) |
| 140 | |
Felipe Monteiro | 10e82fd | 2017-11-21 01:47:20 +0000 | [diff] [blame] | 141 | # Do not override roles if `target_role` already exists. |
Felipe Monteiro | 2693bf7 | 2017-08-12 22:56:47 +0100 | [diff] [blame] | 142 | if not role_already_present: |
| 143 | self._create_user_role_on_project(target_role) |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 144 | except Exception as exp: |
Felipe Monteiro | 2693bf7 | 2017-08-12 22:56:47 +0100 | [diff] [blame] | 145 | with excutils.save_and_reraise_exception(): |
| 146 | LOG.exception(exp) |
Felipe Monteiro | 34a138c | 2017-03-02 17:01:37 -0500 | [diff] [blame] | 147 | finally: |
Mykola Yakovliev | 1d82978 | 2018-08-03 14:37:37 -0500 | [diff] [blame] | 148 | auth_providers = test_obj.get_auth_providers() |
| 149 | for provider in auth_providers: |
| 150 | provider.clear_auth() |
Felipe Monteiro | 7be94e8 | 2017-07-26 02:17:08 +0100 | [diff] [blame] | 151 | # Fernet tokens are not subsecond aware so sleep to ensure we are |
| 152 | # passing the second boundary before attempting to authenticate. |
Felipe Monteiro | 2693bf7 | 2017-08-12 22:56:47 +0100 | [diff] [blame] | 153 | # Only sleep if a token revocation occurred as a result of role |
Felipe Monteiro | 10e82fd | 2017-11-21 01:47:20 +0000 | [diff] [blame] | 154 | # overriding. This will optimize test runtime in the case where |
Felipe Monteiro | b58c119 | 2017-11-20 01:50:24 +0000 | [diff] [blame] | 155 | # ``[identity] admin_role`` == ``[patrole] rbac_test_role``. |
Felipe Monteiro | 2693bf7 | 2017-08-12 22:56:47 +0100 | [diff] [blame] | 156 | if not role_already_present: |
Rick Bartra | 89f498f | 2017-03-20 15:54:45 -0400 | [diff] [blame] | 157 | time.sleep(1) |
Mykola Yakovliev | 1d82978 | 2018-08-03 14:37:37 -0500 | [diff] [blame] | 158 | |
| 159 | for provider in auth_providers: |
| 160 | provider.set_auth() |
Felipe Monteiro | 34a138c | 2017-03-02 17:01:37 -0500 | [diff] [blame] | 161 | |
Felipe Monteiro | 2693bf7 | 2017-08-12 22:56:47 +0100 | [diff] [blame] | 162 | def _get_roles_by_name(self): |
Felipe Monteiro | 2fc2929 | 2018-06-15 18:26:27 -0400 | [diff] [blame] | 163 | available_roles = self.admin_roles_client.list_roles()['roles'] |
| 164 | role_map = {r['name']: r['id'] for r in available_roles} |
| 165 | LOG.debug('Available roles: %s', list(role_map.keys())) |
Felipe Monteiro | fa01d5f | 2017-04-01 06:18:25 +0100 | [diff] [blame] | 166 | |
Felipe Monteiro | 2fc2929 | 2018-06-15 18:26:27 -0400 | [diff] [blame] | 167 | admin_role_id = role_map.get(CONF.identity.admin_role) |
| 168 | rbac_role_id = role_map.get(CONF.patrole.rbac_test_role) |
Felipe Monteiro | 2693bf7 | 2017-08-12 22:56:47 +0100 | [diff] [blame] | 169 | |
| 170 | if not all([admin_role_id, rbac_role_id]): |
Felipe Monteiro | 2fc2929 | 2018-06-15 18:26:27 -0400 | [diff] [blame] | 171 | missing_roles = [] |
| 172 | msg = ("Could not find `[patrole] rbac_test_role` or " |
| 173 | "`[identity] admin_role`, both of which are required for " |
| 174 | "RBAC testing.") |
| 175 | if not admin_role_id: |
| 176 | missing_roles.append(CONF.identity.admin_role) |
| 177 | if not rbac_role_id: |
| 178 | missing_roles.append(CONF.patrole.rbac_test_role) |
| 179 | msg += " Following roles were not found: %s." % ( |
| 180 | ", ".join(missing_roles)) |
| 181 | msg += " Available roles: %s." % ", ".join(list(role_map.keys())) |
Felipe Monteiro | 2693bf7 | 2017-08-12 22:56:47 +0100 | [diff] [blame] | 182 | raise rbac_exceptions.RbacResourceSetupFailed(msg) |
| 183 | |
| 184 | self.admin_role_id = admin_role_id |
| 185 | self.rbac_role_id = rbac_role_id |
| 186 | |
| 187 | def _create_user_role_on_project(self, role_id): |
Felipe Monteiro | e8d93e0 | 2017-07-19 20:52:20 +0100 | [diff] [blame] | 188 | self.admin_roles_client.create_user_role_on_project( |
Felipe Monteiro | fa01d5f | 2017-04-01 06:18:25 +0100 | [diff] [blame] | 189 | self.project_id, self.user_id, role_id) |
| 190 | |
Felipe Monteiro | 2693bf7 | 2017-08-12 22:56:47 +0100 | [diff] [blame] | 191 | def _list_and_clear_user_roles_on_project(self, role_id): |
Felipe Monteiro | e8d93e0 | 2017-07-19 20:52:20 +0100 | [diff] [blame] | 192 | roles = self.admin_roles_client.list_user_roles_on_project( |
Felipe Monteiro | fa01d5f | 2017-04-01 06:18:25 +0100 | [diff] [blame] | 193 | self.project_id, self.user_id)['roles'] |
Felipe Monteiro | fa01d5f | 2017-04-01 06:18:25 +0100 | [diff] [blame] | 194 | role_ids = [role['id'] for role in roles] |
Felipe Monteiro | 2693bf7 | 2017-08-12 22:56:47 +0100 | [diff] [blame] | 195 | |
| 196 | # NOTE(felipemonteiro): We do not use ``role_id in role_ids`` here to |
| 197 | # avoid over-permission errors: if the current list of roles on the |
| 198 | # project includes "admin" and "Member", and we are switching to the |
| 199 | # "Member" role, then we must delete the "admin" role. Thus, we only |
| 200 | # return early if the user's roles on the project are an exact match. |
| 201 | if [role_id] == role_ids: |
Felipe Monteiro | fa01d5f | 2017-04-01 06:18:25 +0100 | [diff] [blame] | 202 | return True |
Felipe Monteiro | b3b7bc8 | 2017-03-03 15:58:15 -0500 | [diff] [blame] | 203 | |
| 204 | for role in roles: |
Felipe Monteiro | e8d93e0 | 2017-07-19 20:52:20 +0100 | [diff] [blame] | 205 | self.admin_roles_client.delete_role_from_user_on_project( |
Felipe Monteiro | fa01d5f | 2017-04-01 06:18:25 +0100 | [diff] [blame] | 206 | self.project_id, self.user_id, role['id']) |
| 207 | |
| 208 | return False |
| 209 | |
Felipe Monteiro | 17e9b49 | 2017-05-27 05:45:20 +0100 | [diff] [blame] | 210 | |
Felipe Monteiro | 07a1c17 | 2017-12-10 04:26:08 +0000 | [diff] [blame] | 211 | class RbacUtilsMixin(object): |
| 212 | """Mixin class to be used alongside an instance of |
| 213 | :py:class:`tempest.test.BaseTestCase`. |
| 214 | |
| 215 | Should be used to perform Patrole class setup for a base RBAC class. Child |
| 216 | classes should not use this mixin. |
| 217 | |
| 218 | Example:: |
| 219 | |
| 220 | class BaseRbacTest(rbac_utils.RbacUtilsMixin, base.BaseV2ComputeTest): |
| 221 | |
| 222 | @classmethod |
| 223 | def skip_checks(cls): |
| 224 | super(BaseRbacTest, cls).skip_checks() |
| 225 | cls.skip_rbac_checks() |
| 226 | |
| 227 | @classmethod |
| 228 | def setup_clients(cls): |
| 229 | super(BaseRbacTest, cls).setup_clients() |
| 230 | cls.setup_rbac_utils() |
| 231 | """ |
| 232 | |
Mykola Yakovliev | 11376ab | 2018-08-06 15:34:22 -0500 | [diff] [blame] | 233 | # Shows if override_role was called. |
| 234 | __override_role_called = False |
| 235 | # Shows if exception raised during override_role. |
| 236 | __override_role_caught_exc = False |
| 237 | |
Felipe Monteiro | 07a1c17 | 2017-12-10 04:26:08 +0000 | [diff] [blame] | 238 | @classmethod |
Mykola Yakovliev | 1d82978 | 2018-08-03 14:37:37 -0500 | [diff] [blame] | 239 | def get_auth_providers(cls): |
| 240 | """Returns list of auth_providers used within test. |
| 241 | |
| 242 | Tests may redefine this method to include their own or third party |
| 243 | client auth_providers. |
| 244 | """ |
| 245 | return [cls.os_primary.auth_provider] |
| 246 | |
| 247 | @classmethod |
Felipe Monteiro | 07a1c17 | 2017-12-10 04:26:08 +0000 | [diff] [blame] | 248 | def setup_rbac_utils(cls): |
| 249 | cls.rbac_utils = RbacUtils(cls) |
| 250 | |
Mykola Yakovliev | 11376ab | 2018-08-06 15:34:22 -0500 | [diff] [blame] | 251 | def _set_override_role_called(self): |
| 252 | """Helper for tracking whether ``override_role`` was called.""" |
| 253 | self.__override_role_called = True |
| 254 | |
| 255 | def _set_override_role_caught_exc(self): |
| 256 | """Helper for tracking whether exception was thrown inside |
| 257 | ``override_role``. |
| 258 | """ |
| 259 | self.__override_role_caught_exc = True |
| 260 | |
| 261 | def _validate_override_role_called(self): |
| 262 | """Idempotently validate that ``override_role`` is called and reset |
| 263 | its value to False for sequential tests. |
| 264 | """ |
| 265 | was_called = self.__override_role_called |
| 266 | self.__override_role_called = False |
| 267 | return was_called |
| 268 | |
| 269 | def _validate_override_role_caught_exc(self): |
| 270 | """Idempotently validate that exception was caught inside |
| 271 | ``override_role``, so that, by process of elimination, it can be |
| 272 | determined whether one was thrown outside (which is invalid). |
| 273 | """ |
| 274 | caught_exception = self.__override_role_caught_exc |
| 275 | self.__override_role_caught_exc = False |
| 276 | return caught_exception |
| 277 | |
Felipe Monteiro | 07a1c17 | 2017-12-10 04:26:08 +0000 | [diff] [blame] | 278 | |
Felipe Monteiro | 8a043fb | 2017-08-06 06:29:05 +0100 | [diff] [blame] | 279 | def is_admin(): |
| 280 | """Verifies whether the current test role equals the admin role. |
| 281 | |
| 282 | :returns: True if ``rbac_test_role`` is the admin role. |
| 283 | """ |
Felipe Monteiro | 2fc2929 | 2018-06-15 18:26:27 -0400 | [diff] [blame] | 284 | # TODO(felipemonteiro): Make this more robust via a context is admin |
| 285 | # lookup. |
Felipe Monteiro | f6eb862 | 2017-08-06 06:08:02 +0100 | [diff] [blame] | 286 | return CONF.patrole.rbac_test_role == CONF.identity.admin_role |