blob: 4ef0f807241339020b1b011e5eed220631f9a53f [file] [log] [blame]
DavidPurcellb25f93d2017-01-27 12:46:27 -05001# Copyright 2017 AT&T Corporation.
DavidPurcell029d8c32017-01-06 15:27:41 -05002# 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 Bartraed950052017-06-29 17:20:33 -040016import abc
Felipe Monteiro10e82fd2017-11-21 01:47:20 +000017from contextlib import contextmanager
18import debtcollector.removals
Rick Bartraed950052017-06-29 17:20:33 -040019import six
DavidPurcell029d8c32017-01-06 15:27:41 -050020import time
Felipe Monteiro34a138c2017-03-02 17:01:37 -050021
Rajiv Kumar645dfc92017-01-19 13:48:27 +053022from oslo_log import log as logging
Felipe Monteiro2693bf72017-08-12 22:56:47 +010023from oslo_utils import excutils
Felipe Monteirofa01d5f2017-04-01 06:18:25 +010024
Felipe Monteiro3e14f472017-08-17 23:02:11 +010025from tempest import clients
Felipe Monteiroe7e552e2017-05-02 17:04:12 +010026from tempest.common import credentials_factory as credentials
Felipe Monteirofa01d5f2017-04-01 06:18:25 +010027from tempest import config
DavidPurcell029d8c32017-01-06 15:27:41 -050028
Felipe Monteiro34a138c2017-03-02 17:01:37 -050029from patrole_tempest_plugin import rbac_exceptions
DavidPurcell029d8c32017-01-06 15:27:41 -050030
DavidPurcell029d8c32017-01-06 15:27:41 -050031CONF = config.CONF
Felipe Monteiro34a138c2017-03-02 17:01:37 -050032LOG = logging.getLogger(__name__)
DavidPurcell029d8c32017-01-06 15:27:41 -050033
34
DavidPurcell029d8c32017-01-06 15:27:41 -050035class RbacUtils(object):
Felipe Monteiro10e82fd2017-11-21 01:47:20 +000036 """Utility class responsible for switching ``os_primary`` role.
Felipe Monteiroe8d93e02017-07-19 20:52:20 +010037
38 This class is responsible for overriding the value of the primary Tempest
Felipe Monteiro10e82fd2017-11-21 01:47:20 +000039 credential's role (i.e. ``os_primary`` role). By doing so, it is possible
40 to seamlessly swap between admin credentials, needed for setup and clean
41 up, and primary credentials, needed to perform the API call which does
Felipe Monteiroe8d93e02017-07-19 20:52:20 +010042 policy enforcement. The primary credentials always cycle between roles
Felipe Monteiro10e82fd2017-11-21 01:47:20 +000043 defined by ``CONF.identity.admin_role`` and
44 ``CONF.patrole.rbac_test_role``.
Felipe Monteiroe8d93e02017-07-19 20:52:20 +010045 """
DavidPurcell029d8c32017-01-06 15:27:41 -050046
Felipe Monteirob35de582017-05-05 00:16:53 +010047 def __init__(self, test_obj):
Felipe Monteiroe8d93e02017-07-19 20:52:20 +010048 """Constructor for ``RbacUtils``.
49
Felipe Monteiro2693bf72017-08-12 22:56:47 +010050 :param test_obj: An instance of `tempest.test.BaseTestCase`.
Felipe Monteiroe8d93e02017-07-19 20:52:20 +010051 """
Felipe Monteiroe8d93e02017-07-19 20:52:20 +010052 # Intialize the admin roles_client to perform role switching.
Felipe Monteiro3e14f472017-08-17 23:02:11 +010053 admin_mgr = clients.Manager(
54 credentials.get_configured_admin_credentials())
Felipe Monteiroe8d93e02017-07-19 20:52:20 +010055 if test_obj.get_identity_version() == 'v3':
Felipe Monteiro3e14f472017-08-17 23:02:11 +010056 admin_roles_client = admin_mgr.roles_v3_client
Felipe Monteiroe8d93e02017-07-19 20:52:20 +010057 else:
Felipe Monteiro3e14f472017-08-17 23:02:11 +010058 admin_roles_client = admin_mgr.roles_client
Felipe Monteiroe8d93e02017-07-19 20:52:20 +010059
60 self.admin_roles_client = admin_roles_client
Felipe Monteiro10e82fd2017-11-21 01:47:20 +000061 self._override_role(test_obj, False)
Felipe Monteirob35de582017-05-05 00:16:53 +010062
Felipe Monteirofa01d5f2017-04-01 06:18:25 +010063 admin_role_id = None
64 rbac_role_id = None
DavidPurcell029d8c32017-01-06 15:27:41 -050065
Felipe Monteiro10e82fd2017-11-21 01:47:20 +000066 @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:
71 * ``[patrole] rbac_test_role`` before test execution
72 * ``[identity] admin_role`` after test execution
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):
91 # The role for `os_primary` has now been overriden. Within
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
109 @debtcollector.removals.remove(removal_version='Rocky')
110 def switch_role(self, test_obj, toggle_rbac_role):
Felipe Monteiro7be94e82017-07-26 02:17:08 +0100111 """Switch the role used by `os_primary` Tempest credentials.
112
113 Switch the role used by `os_primary` credentials to:
114 * admin if `toggle_rbac_role` is False
Felipe Monteirof6eb8622017-08-06 06:08:02 +0100115 * `CONF.patrole.rbac_test_role` if `toggle_rbac_role` is True
Felipe Monteiro7be94e82017-07-26 02:17:08 +0100116
Felipe Monteirof6eb8622017-08-06 06:08:02 +0100117 :param test_obj: test object of type tempest.lib.base.BaseTestCase
118 :param toggle_rbac_role: role to switch `os_primary` Tempest creds to
Felipe Monteiro7be94e82017-07-26 02:17:08 +0100119 """
Felipe Monteiro10e82fd2017-11-21 01:47:20 +0000120 self._override_role(test_obj, toggle_rbac_role)
121
122 def _override_role(self, test_obj, toggle_rbac_role=False):
123 """Private helper for overriding ``os_primary`` Tempest credentials.
124
125 :param test_obj: test object of type tempest.lib.base.BaseTestCase
126 :param toggle_rbac_role: Boolean value that controls the role that
127 overrides default role of ``os_primary`` credentials.
128 * If True: role is set to ``[patrole] rbac_test_role``
129 * If False: role is set to ``[identity] admin_role``
130 """
Felipe Monteiroe8d93e02017-07-19 20:52:20 +0100131 self.user_id = test_obj.os_primary.credentials.user_id
132 self.project_id = test_obj.os_primary.credentials.tenant_id
133 self.token = test_obj.os_primary.auth_provider.get_token()
DavidPurcell029d8c32017-01-06 15:27:41 -0500134
Felipe Monteiro10e82fd2017-11-21 01:47:20 +0000135 LOG.debug('Overriding role to: %s.', toggle_rbac_role)
Felipe Monteiro2693bf72017-08-12 22:56:47 +0100136 role_already_present = False
137
DavidPurcell029d8c32017-01-06 15:27:41 -0500138 try:
Felipe Monteiro2693bf72017-08-12 22:56:47 +0100139 if not all([self.admin_role_id, self.rbac_role_id]):
140 self._get_roles_by_name()
DavidPurcell029d8c32017-01-06 15:27:41 -0500141
Felipe Monteiro2693bf72017-08-12 22:56:47 +0100142 target_role = (
143 self.rbac_role_id if toggle_rbac_role else self.admin_role_id)
144 role_already_present = self._list_and_clear_user_roles_on_project(
145 target_role)
146
Felipe Monteiro10e82fd2017-11-21 01:47:20 +0000147 # Do not override roles if `target_role` already exists.
Felipe Monteiro2693bf72017-08-12 22:56:47 +0100148 if not role_already_present:
149 self._create_user_role_on_project(target_role)
DavidPurcell029d8c32017-01-06 15:27:41 -0500150 except Exception as exp:
Felipe Monteiro2693bf72017-08-12 22:56:47 +0100151 with excutils.save_and_reraise_exception():
152 LOG.exception(exp)
Felipe Monteiro34a138c2017-03-02 17:01:37 -0500153 finally:
Felipe Monteiroe8d93e02017-07-19 20:52:20 +0100154 test_obj.os_primary.auth_provider.clear_auth()
Felipe Monteiro7be94e82017-07-26 02:17:08 +0100155 # Fernet tokens are not subsecond aware so sleep to ensure we are
156 # passing the second boundary before attempting to authenticate.
Felipe Monteiro2693bf72017-08-12 22:56:47 +0100157 # Only sleep if a token revocation occurred as a result of role
Felipe Monteiro10e82fd2017-11-21 01:47:20 +0000158 # overriding. This will optimize test runtime in the case where
Felipe Monteirob58c1192017-11-20 01:50:24 +0000159 # ``[identity] admin_role`` == ``[patrole] rbac_test_role``.
Felipe Monteiro2693bf72017-08-12 22:56:47 +0100160 if not role_already_present:
Rick Bartra89f498f2017-03-20 15:54:45 -0400161 time.sleep(1)
Felipe Monteiroe8d93e02017-07-19 20:52:20 +0100162 test_obj.os_primary.auth_provider.set_auth()
Felipe Monteiro34a138c2017-03-02 17:01:37 -0500163
Felipe Monteiro2693bf72017-08-12 22:56:47 +0100164 def _get_roles_by_name(self):
165 available_roles = self.admin_roles_client.list_roles()
166 admin_role_id = rbac_role_id = None
Felipe Monteirofa01d5f2017-04-01 06:18:25 +0100167
Felipe Monteiro2693bf72017-08-12 22:56:47 +0100168 for role in available_roles['roles']:
Felipe Monteirof6eb8622017-08-06 06:08:02 +0100169 if role['name'] == CONF.patrole.rbac_test_role:
Felipe Monteiro2693bf72017-08-12 22:56:47 +0100170 rbac_role_id = role['id']
171 if role['name'] == CONF.identity.admin_role:
172 admin_role_id = role['id']
173
174 if not all([admin_role_id, rbac_role_id]):
Felipe Monteirof6eb8622017-08-06 06:08:02 +0100175 msg = ("Roles defined by `[patrole] rbac_test_role` and "
176 "`[identity] admin_role` must be defined in the system.")
Felipe Monteiro2693bf72017-08-12 22:56:47 +0100177 raise rbac_exceptions.RbacResourceSetupFailed(msg)
178
179 self.admin_role_id = admin_role_id
180 self.rbac_role_id = rbac_role_id
181
182 def _create_user_role_on_project(self, role_id):
Felipe Monteiroe8d93e02017-07-19 20:52:20 +0100183 self.admin_roles_client.create_user_role_on_project(
Felipe Monteirofa01d5f2017-04-01 06:18:25 +0100184 self.project_id, self.user_id, role_id)
185
Felipe Monteiro2693bf72017-08-12 22:56:47 +0100186 def _list_and_clear_user_roles_on_project(self, role_id):
Felipe Monteiroe8d93e02017-07-19 20:52:20 +0100187 roles = self.admin_roles_client.list_user_roles_on_project(
Felipe Monteirofa01d5f2017-04-01 06:18:25 +0100188 self.project_id, self.user_id)['roles']
Felipe Monteirofa01d5f2017-04-01 06:18:25 +0100189 role_ids = [role['id'] for role in roles]
Felipe Monteiro2693bf72017-08-12 22:56:47 +0100190
191 # NOTE(felipemonteiro): We do not use ``role_id in role_ids`` here to
192 # avoid over-permission errors: if the current list of roles on the
193 # project includes "admin" and "Member", and we are switching to the
194 # "Member" role, then we must delete the "admin" role. Thus, we only
195 # return early if the user's roles on the project are an exact match.
196 if [role_id] == role_ids:
Felipe Monteirofa01d5f2017-04-01 06:18:25 +0100197 return True
Felipe Monteirob3b7bc82017-03-03 15:58:15 -0500198
199 for role in roles:
Felipe Monteiroe8d93e02017-07-19 20:52:20 +0100200 self.admin_roles_client.delete_role_from_user_on_project(
Felipe Monteirofa01d5f2017-04-01 06:18:25 +0100201 self.project_id, self.user_id, role['id'])
202
203 return False
204
Felipe Monteiro17e9b492017-05-27 05:45:20 +0100205
Felipe Monteiro8a043fb2017-08-06 06:29:05 +0100206def is_admin():
207 """Verifies whether the current test role equals the admin role.
208
209 :returns: True if ``rbac_test_role`` is the admin role.
210 """
Felipe Monteirof6eb8622017-08-06 06:08:02 +0100211 return CONF.patrole.rbac_test_role == CONF.identity.admin_role
Rick Bartraed950052017-06-29 17:20:33 -0400212
213
214@six.add_metaclass(abc.ABCMeta)
215class RbacAuthority(object):
Felipe Monteiro2a3b5132017-08-11 01:19:06 +0100216 """Class for validating whether a given role can perform a policy action.
217
218 Any class that extends ``RbacAuthority`` provides the logic for determining
219 whether a role has permissions to execute a policy action.
220 """
Rick Bartraed950052017-06-29 17:20:33 -0400221
222 @abc.abstractmethod
Felipe Monteiro2a3b5132017-08-11 01:19:06 +0100223 def allowed(self, rule, role):
224 """Determine whether the role should be able to perform the API.
225
226 :param rule: The name of the policy enforced by the API.
227 :param role: The role used to determine whether ``rule`` can be
228 executed.
229 :returns: True if the ``role`` has permissions to execute
230 ``rule``, else False.
231 """