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 |
Felipe Monteiro | 10e82fd | 2017-11-21 01:47:20 +0000 | [diff] [blame] | 17 | from contextlib import contextmanager |
Rick Bartra | ed95005 | 2017-06-29 17:20:33 -0400 | [diff] [blame] | 18 | import six |
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 | 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 | e8d93e0 | 2017-07-19 20:52:20 +0100 | [diff] [blame] | 54 | if test_obj.get_identity_version() == '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 | 3e14f47 | 2017-08-17 23:02:11 +0100 | [diff] [blame] | 57 | admin_roles_client = admin_mgr.roles_client |
Felipe Monteiro | e8d93e0 | 2017-07-19 20:52:20 +0100 | [diff] [blame] | 58 | |
| 59 | self.admin_roles_client = admin_roles_client |
Felipe Monteiro | 10e82fd | 2017-11-21 01:47:20 +0000 | [diff] [blame] | 60 | self._override_role(test_obj, False) |
Felipe Monteiro | b35de58 | 2017-05-05 00:16:53 +0100 | [diff] [blame] | 61 | |
Felipe Monteiro | fa01d5f | 2017-04-01 06:18:25 +0100 | [diff] [blame] | 62 | admin_role_id = None |
| 63 | rbac_role_id = None |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 64 | |
Felipe Monteiro | 10e82fd | 2017-11-21 01:47:20 +0000 | [diff] [blame] | 65 | @contextmanager |
| 66 | def override_role(self, test_obj): |
| 67 | """Override the role used by ``os_primary`` Tempest credentials. |
| 68 | |
| 69 | Temporarily change the role used by ``os_primary`` credentials to: |
Masayuki Igawa | 80b9aab | 2018-01-09 17:00:45 +0900 | [diff] [blame] | 70 | |
| 71 | * ``[patrole] rbac_test_role`` before test execution |
| 72 | * ``[identity] admin_role`` after test execution |
Felipe Monteiro | 10e82fd | 2017-11-21 01:47:20 +0000 | [diff] [blame] | 73 | |
| 74 | Automatically switches to admin role after test execution. |
| 75 | |
| 76 | :param test_obj: Instance of ``tempest.test.BaseTestCase``. |
| 77 | :returns: None |
| 78 | |
| 79 | .. warning:: |
| 80 | |
| 81 | This function can alter user roles for pre-provisioned credentials. |
| 82 | Work is underway to safely clean up after this function. |
| 83 | |
| 84 | Example:: |
| 85 | |
| 86 | @rbac_rule_validation.action(service='test', |
| 87 | rule='a:test:rule') |
| 88 | def test_foo(self): |
| 89 | # Allocate test-level resources here. |
| 90 | with self.rbac_utils.override_role(self): |
melissaml | 7cd2161 | 2018-05-23 21:00:50 +0800 | [diff] [blame] | 91 | # The role for `os_primary` has now been overridden. Within |
Felipe Monteiro | 10e82fd | 2017-11-21 01:47:20 +0000 | [diff] [blame] | 92 | # this block, call the API endpoint that enforces the |
| 93 | # expected policy specified by "rule" in the decorator. |
| 94 | self.foo_service.bar_api_call() |
| 95 | # The role is switched back to admin automatically. Note that |
| 96 | # if the API call above threw an exception, any code below this |
| 97 | # point in the test is not executed. |
| 98 | """ |
| 99 | self._override_role(test_obj, True) |
| 100 | try: |
| 101 | # Execute the test. |
| 102 | yield |
| 103 | finally: |
| 104 | # This code block is always executed, no matter the result of the |
| 105 | # test. Automatically switch back to the admin role for test clean |
| 106 | # up. |
| 107 | self._override_role(test_obj, False) |
| 108 | |
Felipe Monteiro | 10e82fd | 2017-11-21 01:47:20 +0000 | [diff] [blame] | 109 | def _override_role(self, test_obj, toggle_rbac_role=False): |
| 110 | """Private helper for overriding ``os_primary`` Tempest credentials. |
| 111 | |
Felipe Monteiro | 07a1c17 | 2017-12-10 04:26:08 +0000 | [diff] [blame] | 112 | :param test_obj: instance of :py:class:`tempest.test.BaseTestCase` |
Felipe Monteiro | 10e82fd | 2017-11-21 01:47:20 +0000 | [diff] [blame] | 113 | :param toggle_rbac_role: Boolean value that controls the role that |
| 114 | overrides default role of ``os_primary`` credentials. |
| 115 | * If True: role is set to ``[patrole] rbac_test_role`` |
| 116 | * If False: role is set to ``[identity] admin_role`` |
| 117 | """ |
Felipe Monteiro | e8d93e0 | 2017-07-19 20:52:20 +0100 | [diff] [blame] | 118 | self.user_id = test_obj.os_primary.credentials.user_id |
| 119 | self.project_id = test_obj.os_primary.credentials.tenant_id |
| 120 | self.token = test_obj.os_primary.auth_provider.get_token() |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 121 | |
Felipe Monteiro | 10e82fd | 2017-11-21 01:47:20 +0000 | [diff] [blame] | 122 | LOG.debug('Overriding role to: %s.', toggle_rbac_role) |
Felipe Monteiro | 2693bf7 | 2017-08-12 22:56:47 +0100 | [diff] [blame] | 123 | role_already_present = False |
| 124 | |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 125 | try: |
Felipe Monteiro | 2693bf7 | 2017-08-12 22:56:47 +0100 | [diff] [blame] | 126 | if not all([self.admin_role_id, self.rbac_role_id]): |
| 127 | self._get_roles_by_name() |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 128 | |
Felipe Monteiro | 2693bf7 | 2017-08-12 22:56:47 +0100 | [diff] [blame] | 129 | target_role = ( |
| 130 | self.rbac_role_id if toggle_rbac_role else self.admin_role_id) |
| 131 | role_already_present = self._list_and_clear_user_roles_on_project( |
| 132 | target_role) |
| 133 | |
Felipe Monteiro | 10e82fd | 2017-11-21 01:47:20 +0000 | [diff] [blame] | 134 | # Do not override roles if `target_role` already exists. |
Felipe Monteiro | 2693bf7 | 2017-08-12 22:56:47 +0100 | [diff] [blame] | 135 | if not role_already_present: |
| 136 | self._create_user_role_on_project(target_role) |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 137 | except Exception as exp: |
Felipe Monteiro | 2693bf7 | 2017-08-12 22:56:47 +0100 | [diff] [blame] | 138 | with excutils.save_and_reraise_exception(): |
| 139 | LOG.exception(exp) |
Felipe Monteiro | 34a138c | 2017-03-02 17:01:37 -0500 | [diff] [blame] | 140 | finally: |
Felipe Monteiro | e8d93e0 | 2017-07-19 20:52:20 +0100 | [diff] [blame] | 141 | test_obj.os_primary.auth_provider.clear_auth() |
Felipe Monteiro | 7be94e8 | 2017-07-26 02:17:08 +0100 | [diff] [blame] | 142 | # Fernet tokens are not subsecond aware so sleep to ensure we are |
| 143 | # passing the second boundary before attempting to authenticate. |
Felipe Monteiro | 2693bf7 | 2017-08-12 22:56:47 +0100 | [diff] [blame] | 144 | # Only sleep if a token revocation occurred as a result of role |
Felipe Monteiro | 10e82fd | 2017-11-21 01:47:20 +0000 | [diff] [blame] | 145 | # overriding. This will optimize test runtime in the case where |
Felipe Monteiro | b58c119 | 2017-11-20 01:50:24 +0000 | [diff] [blame] | 146 | # ``[identity] admin_role`` == ``[patrole] rbac_test_role``. |
Felipe Monteiro | 2693bf7 | 2017-08-12 22:56:47 +0100 | [diff] [blame] | 147 | if not role_already_present: |
Rick Bartra | 89f498f | 2017-03-20 15:54:45 -0400 | [diff] [blame] | 148 | time.sleep(1) |
Felipe Monteiro | e8d93e0 | 2017-07-19 20:52:20 +0100 | [diff] [blame] | 149 | test_obj.os_primary.auth_provider.set_auth() |
Felipe Monteiro | 34a138c | 2017-03-02 17:01:37 -0500 | [diff] [blame] | 150 | |
Felipe Monteiro | 2693bf7 | 2017-08-12 22:56:47 +0100 | [diff] [blame] | 151 | def _get_roles_by_name(self): |
| 152 | available_roles = self.admin_roles_client.list_roles() |
| 153 | admin_role_id = rbac_role_id = None |
Felipe Monteiro | fa01d5f | 2017-04-01 06:18:25 +0100 | [diff] [blame] | 154 | |
Felipe Monteiro | 2693bf7 | 2017-08-12 22:56:47 +0100 | [diff] [blame] | 155 | for role in available_roles['roles']: |
Felipe Monteiro | f6eb862 | 2017-08-06 06:08:02 +0100 | [diff] [blame] | 156 | if role['name'] == CONF.patrole.rbac_test_role: |
Felipe Monteiro | 2693bf7 | 2017-08-12 22:56:47 +0100 | [diff] [blame] | 157 | rbac_role_id = role['id'] |
| 158 | if role['name'] == CONF.identity.admin_role: |
| 159 | admin_role_id = role['id'] |
| 160 | |
| 161 | if not all([admin_role_id, rbac_role_id]): |
Felipe Monteiro | f6eb862 | 2017-08-06 06:08:02 +0100 | [diff] [blame] | 162 | msg = ("Roles defined by `[patrole] rbac_test_role` and " |
| 163 | "`[identity] admin_role` must be defined in the system.") |
Felipe Monteiro | 2693bf7 | 2017-08-12 22:56:47 +0100 | [diff] [blame] | 164 | raise rbac_exceptions.RbacResourceSetupFailed(msg) |
| 165 | |
| 166 | self.admin_role_id = admin_role_id |
| 167 | self.rbac_role_id = rbac_role_id |
| 168 | |
| 169 | def _create_user_role_on_project(self, role_id): |
Felipe Monteiro | e8d93e0 | 2017-07-19 20:52:20 +0100 | [diff] [blame] | 170 | self.admin_roles_client.create_user_role_on_project( |
Felipe Monteiro | fa01d5f | 2017-04-01 06:18:25 +0100 | [diff] [blame] | 171 | self.project_id, self.user_id, role_id) |
| 172 | |
Felipe Monteiro | 2693bf7 | 2017-08-12 22:56:47 +0100 | [diff] [blame] | 173 | def _list_and_clear_user_roles_on_project(self, role_id): |
Felipe Monteiro | e8d93e0 | 2017-07-19 20:52:20 +0100 | [diff] [blame] | 174 | roles = self.admin_roles_client.list_user_roles_on_project( |
Felipe Monteiro | fa01d5f | 2017-04-01 06:18:25 +0100 | [diff] [blame] | 175 | self.project_id, self.user_id)['roles'] |
Felipe Monteiro | fa01d5f | 2017-04-01 06:18:25 +0100 | [diff] [blame] | 176 | role_ids = [role['id'] for role in roles] |
Felipe Monteiro | 2693bf7 | 2017-08-12 22:56:47 +0100 | [diff] [blame] | 177 | |
| 178 | # NOTE(felipemonteiro): We do not use ``role_id in role_ids`` here to |
| 179 | # avoid over-permission errors: if the current list of roles on the |
| 180 | # project includes "admin" and "Member", and we are switching to the |
| 181 | # "Member" role, then we must delete the "admin" role. Thus, we only |
| 182 | # return early if the user's roles on the project are an exact match. |
| 183 | if [role_id] == role_ids: |
Felipe Monteiro | fa01d5f | 2017-04-01 06:18:25 +0100 | [diff] [blame] | 184 | return True |
Felipe Monteiro | b3b7bc8 | 2017-03-03 15:58:15 -0500 | [diff] [blame] | 185 | |
| 186 | for role in roles: |
Felipe Monteiro | e8d93e0 | 2017-07-19 20:52:20 +0100 | [diff] [blame] | 187 | self.admin_roles_client.delete_role_from_user_on_project( |
Felipe Monteiro | fa01d5f | 2017-04-01 06:18:25 +0100 | [diff] [blame] | 188 | self.project_id, self.user_id, role['id']) |
| 189 | |
| 190 | return False |
| 191 | |
Felipe Monteiro | 17e9b49 | 2017-05-27 05:45:20 +0100 | [diff] [blame] | 192 | |
Felipe Monteiro | 07a1c17 | 2017-12-10 04:26:08 +0000 | [diff] [blame] | 193 | class RbacUtilsMixin(object): |
| 194 | """Mixin class to be used alongside an instance of |
| 195 | :py:class:`tempest.test.BaseTestCase`. |
| 196 | |
| 197 | Should be used to perform Patrole class setup for a base RBAC class. Child |
| 198 | classes should not use this mixin. |
| 199 | |
| 200 | Example:: |
| 201 | |
| 202 | class BaseRbacTest(rbac_utils.RbacUtilsMixin, base.BaseV2ComputeTest): |
| 203 | |
| 204 | @classmethod |
| 205 | def skip_checks(cls): |
| 206 | super(BaseRbacTest, cls).skip_checks() |
| 207 | cls.skip_rbac_checks() |
| 208 | |
| 209 | @classmethod |
| 210 | def setup_clients(cls): |
| 211 | super(BaseRbacTest, cls).setup_clients() |
| 212 | cls.setup_rbac_utils() |
| 213 | """ |
| 214 | |
| 215 | @classmethod |
| 216 | def skip_rbac_checks(cls): |
| 217 | if not CONF.patrole.enable_rbac: |
| 218 | raise cls.skipException( |
Felipe Monteiro | d737199 | 2018-04-25 16:57:09 +0100 | [diff] [blame] | 219 | 'Patrole testing not enabled so skipping %s.' % cls.__name__) |
Felipe Monteiro | 07a1c17 | 2017-12-10 04:26:08 +0000 | [diff] [blame] | 220 | |
| 221 | @classmethod |
| 222 | def setup_rbac_utils(cls): |
| 223 | cls.rbac_utils = RbacUtils(cls) |
| 224 | |
| 225 | |
Felipe Monteiro | 8a043fb | 2017-08-06 06:29:05 +0100 | [diff] [blame] | 226 | def is_admin(): |
| 227 | """Verifies whether the current test role equals the admin role. |
| 228 | |
| 229 | :returns: True if ``rbac_test_role`` is the admin role. |
| 230 | """ |
Felipe Monteiro | f6eb862 | 2017-08-06 06:08:02 +0100 | [diff] [blame] | 231 | return CONF.patrole.rbac_test_role == CONF.identity.admin_role |
Rick Bartra | ed95005 | 2017-06-29 17:20:33 -0400 | [diff] [blame] | 232 | |
| 233 | |
| 234 | @six.add_metaclass(abc.ABCMeta) |
| 235 | class RbacAuthority(object): |
Felipe Monteiro | 2a3b513 | 2017-08-11 01:19:06 +0100 | [diff] [blame] | 236 | """Class for validating whether a given role can perform a policy action. |
| 237 | |
| 238 | Any class that extends ``RbacAuthority`` provides the logic for determining |
| 239 | whether a role has permissions to execute a policy action. |
| 240 | """ |
Rick Bartra | ed95005 | 2017-06-29 17:20:33 -0400 | [diff] [blame] | 241 | |
| 242 | @abc.abstractmethod |
Felipe Monteiro | 2a3b513 | 2017-08-11 01:19:06 +0100 | [diff] [blame] | 243 | def allowed(self, rule, role): |
| 244 | """Determine whether the role should be able to perform the API. |
| 245 | |
| 246 | :param rule: The name of the policy enforced by the API. |
| 247 | :param role: The role used to determine whether ``rule`` can be |
| 248 | executed. |
| 249 | :returns: True if the ``role`` has permissions to execute |
| 250 | ``rule``, else False. |
| 251 | """ |