blob: c85376f176b264565b279f9d3e4b1318c268f276 [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
Felipe Monteiro2fe986d2018-03-20 21:53:51 +000016import functools
Felipe Monteirob0595652017-01-23 16:51:58 -050017import logging
Felipe Monteiro8eda8cc2017-03-22 14:15:14 +000018import sys
19
Felipe Monteiro38f344b2017-11-03 12:59:15 +000020from oslo_utils import excutils
Felipe Monteiro8eda8cc2017-03-22 14:15:14 +000021import six
Felipe Monteirob0595652017-01-23 16:51:58 -050022
DavidPurcell029d8c32017-01-06 15:27:41 -050023from tempest import config
Felipe Monteiro51299a12018-06-28 20:03:27 -040024from tempest.lib import exceptions as lib_exc
raiesmh088590c0c2017-03-14 18:06:52 +053025from tempest import test
DavidPurcell029d8c32017-01-06 15:27:41 -050026
Felipe Monteiro88a5bab2017-08-31 04:00:32 +010027from patrole_tempest_plugin import policy_authority
DavidPurcell029d8c32017-01-06 15:27:41 -050028from patrole_tempest_plugin import rbac_exceptions
Rick Bartraed950052017-06-29 17:20:33 -040029from patrole_tempest_plugin import requirements_authority
DavidPurcell029d8c32017-01-06 15:27:41 -050030
31CONF = config.CONF
32LOG = logging.getLogger(__name__)
33
Felipe Monteiro973a1bc2017-06-14 21:23:54 +010034_SUPPORTED_ERROR_CODES = [403, 404]
Cliff Parsons35a77112018-05-07 14:03:40 -050035_DEFAULT_ERROR_CODE = 403
Felipe Monteiro973a1bc2017-06-14 21:23:54 +010036
Sean Pryor7f8993f2017-08-14 12:53:17 -040037RBACLOG = logging.getLogger('rbac_reporting')
38
DavidPurcell029d8c32017-01-06 15:27:41 -050039
Chi Lo8c04bd82018-06-01 16:21:50 -050040def action(service,
Chi Lo8c04bd82018-06-01 16:21:50 -050041 rules=None,
Chi Lo8c04bd82018-06-01 16:21:50 -050042 expected_error_codes=None,
Felipe Monteiro44d77842018-03-21 02:42:59 +000043 extra_target_data=None):
Felipe Monteirof2b58d72017-08-31 22:40:36 +010044 """A decorator for verifying OpenStack policy enforcement.
Felipe Monteirod5d76b82017-03-20 23:18:50 +000045
Felipe Monteiro01d633b2017-08-16 20:17:26 +010046 A decorator which allows for positive and negative RBAC testing. Given:
Rick Bartraed950052017-06-29 17:20:33 -040047
Masayuki Igawa80b9aab2018-01-09 17:00:45 +090048 * an OpenStack service,
49 * a policy action (``rule``) enforced by that service, and
50 * the test role defined by ``[patrole] rbac_test_role``
Felipe Monteirod5d76b82017-03-20 23:18:50 +000051
Felipe Monteiro01d633b2017-08-16 20:17:26 +010052 determines whether the test role has sufficient permissions to perform an
53 API call that enforces the ``rule``.
Felipe Monteirod5d76b82017-03-20 23:18:50 +000054
Felipe Monteiro01d633b2017-08-16 20:17:26 +010055 This decorator should only be applied to an instance or subclass of
Masayuki Igawa80b9aab2018-01-09 17:00:45 +090056 ``tempest.test.BaseTestCase``.
Felipe Monteiro01d633b2017-08-16 20:17:26 +010057
58 The result from ``_is_authorized`` is used to determine the *expected*
59 test result. The *actual* test result is determined by running the
60 Tempest test this decorator applies to.
61
62 Below are the following possibilities from comparing the *expected* and
63 *actual* results:
64
65 1) If *expected* is True and the test passes (*actual*), this is a success.
66 2) If *expected* is True and the test fails (*actual*), this results in a
Felipe Monteirof16b6b32018-06-28 19:32:59 -040067 ``RbacUnderPermissionException`` exception failure.
Felipe Monteiro01d633b2017-08-16 20:17:26 +010068 3) If *expected* is False and the test passes (*actual*), this results in
Felipe Monteirof16b6b32018-06-28 19:32:59 -040069 an ``RbacOverPermissionException`` exception failure.
Felipe Monteiro01d633b2017-08-16 20:17:26 +010070 4) If *expected* is False and the test fails (*actual*), this is a success.
71
72 As such, negative and positive testing can be applied using this decorator.
73
Felipe Monteiro44d77842018-03-21 02:42:59 +000074 :param str service: An OpenStack service. Examples: "nova" or "neutron".
Felipe Monteiro59f538f2018-08-22 23:34:40 -040075 :param list rules: A list of policy actions defined in a policy file or in
76 code. The rules are logical-ANDed together to derive the expected
Chi Lo8c04bd82018-06-01 16:21:50 -050077 result. Also accepts list of callables that return a policy action.
Felipe Monteiro01d633b2017-08-16 20:17:26 +010078
79 .. note::
80
81 Patrole currently only supports custom JSON policy files.
82
Chi Lo8c04bd82018-06-01 16:21:50 -050083 :type rules: list[str] or list[callable]
Cliff Parsons35a77112018-05-07 14:03:40 -050084 :param list expected_error_codes: When the ``rules`` list parameter is
85 used, then this list indicates the expected error code to use if one
86 of the rules does not allow the role being tested. This list must
87 coincide with and its elements remain in the same order as the rules
88 in the rules list.
89
90 Example::
Felipe Monteiro318fa3b2018-06-19 16:53:33 -040091
Cliff Parsons35a77112018-05-07 14:03:40 -050092 rules=["api_action1", "api_action2"]
93 expected_error_codes=[404, 403]
94
95 a) If api_action1 fails and api_action2 passes, then the expected
96 error code is 404.
97 b) if api_action2 fails and api_action1 passes, then the expected
98 error code is 403.
99 c) if both api_action1 and api_action2 fail, then the expected error
100 code is the first error seen (404).
101
ghanshyam98437d42018-08-17 08:51:43 +0000102 If it is not passed, then it is defaulted to 403.
Cliff Parsons35a77112018-05-07 14:03:40 -0500103
Felipe Monteiro59f538f2018-08-22 23:34:40 -0400104 .. warning::
105
106 A 404 should not be provided *unless* the endpoint masks a
107 ``Forbidden`` exception as a ``NotFound`` exception.
108
109 :type expected_error_codes: list[int]
Felipe Monteiro44d77842018-03-21 02:42:59 +0000110 :param dict extra_target_data: Dictionary, keyed with ``oslo.policy``
111 generic check names, whose values are string literals that reference
112 nested ``tempest.test.BaseTestCase`` attributes. Used by
113 ``oslo.policy`` for performing matching against attributes that are
114 sent along with the API calls. Example::
Felipe Monteiro01d633b2017-08-16 20:17:26 +0100115
116 extra_target_data={
117 "target.token.user_id":
118 "os_alt.auth_provider.credentials.user_id"
119 })
120
Felipe Monteiro51299a12018-06-28 20:03:27 -0400121 :raises RbacInvalidServiceException: If ``service`` is invalid.
Felipe Monteirof16b6b32018-06-28 19:32:59 -0400122 :raises RbacUnderPermissionException: For item (2) above.
123 :raises RbacOverPermissionException: For item (3) above.
124 :raises RbacExpectedWrongException: When a 403 is expected but a 404
125 is raised instead or vice versa.
Felipe Monteiro01d633b2017-08-16 20:17:26 +0100126
127 Examples::
128
129 @rbac_rule_validation.action(
Felipe Monteiro59f538f2018-08-22 23:34:40 -0400130 service="nova",
131 rules=["os_compute_api:os-agents"])
Felipe Monteiro01d633b2017-08-16 20:17:26 +0100132 def test_list_agents_rbac(self):
Felipe Monteiro1c8620a2018-02-25 18:52:22 +0000133 # The call to `override_role` is mandatory.
134 with self.rbac_utils.override_role(self):
135 self.agents_client.list_agents()
Felipe Monteirod5d76b82017-03-20 23:18:50 +0000136 """
Felipe Monteiro0854ded2017-05-05 16:30:55 +0100137
138 if extra_target_data is None:
139 extra_target_data = {}
140
Felipe Monteiro59f538f2018-08-22 23:34:40 -0400141 rules, expected_error_codes = _prepare_multi_policy(rules,
Cliff Parsons35a77112018-05-07 14:03:40 -0500142 expected_error_codes)
Felipe Monteiro44d77842018-03-21 02:42:59 +0000143
Sean Pryor7f8993f2017-08-14 12:53:17 -0400144 def decorator(test_func):
Felipe Monteirof6eb8622017-08-06 06:08:02 +0100145 role = CONF.patrole.rbac_test_role
Felipe Monteiro78fc4892017-04-12 21:33:39 +0100146
Felipe Monteiro2fe986d2018-03-20 21:53:51 +0000147 @functools.wraps(test_func)
DavidPurcell029d8c32017-01-06 15:27:41 -0500148 def wrapper(*args, **kwargs):
Felipe Monteiro78fc4892017-04-12 21:33:39 +0100149 if args and isinstance(args[0], test.BaseTestCase):
150 test_obj = args[0]
151 else:
152 raise rbac_exceptions.RbacResourceSetupFailed(
153 '`rbac_rule_validation` decorator can only be applied to '
154 'an instance of `tempest.test.BaseTestCase`.')
raiesmh088590c0c2017-03-14 18:06:52 +0530155
Felipe Monteiro44d77842018-03-21 02:42:59 +0000156 allowed = True
157 disallowed_rules = []
158 for rule in rules:
159 _allowed = _is_authorized(
160 test_obj, service, rule, extra_target_data)
161 if not _allowed:
162 disallowed_rules.append(rule)
163 allowed = allowed and _allowed
Felipe Monteirod5d76b82017-03-20 23:18:50 +0000164
Cliff Parsons35a77112018-05-07 14:03:40 -0500165 if disallowed_rules:
166 # Choose the first disallowed rule and expect the error
167 # code corresponding to it.
168 first_error_index = rules.index(disallowed_rules[0])
169 exp_error_code = expected_error_codes[first_error_index]
170 LOG.debug("%s: Expecting %d to be raised for policy name: %s",
171 test_func.__name__, exp_error_code,
172 disallowed_rules[0])
Felipe Monteiro59f538f2018-08-22 23:34:40 -0400173 else:
174 exp_error_code = expected_error_codes[0]
Cliff Parsons35a77112018-05-07 14:03:40 -0500175
Rick Bartra12998942017-03-17 17:35:45 -0400176 expected_exception, irregular_msg = _get_exception_type(
Cliff Parsons35a77112018-05-07 14:03:40 -0500177 exp_error_code)
DavidPurcell029d8c32017-01-06 15:27:41 -0500178
Mykola Yakovliev11376ab2018-08-06 15:34:22 -0500179 caught_exception = None
Sean Pryor7f8993f2017-08-14 12:53:17 -0400180 test_status = 'Allowed'
181
DavidPurcell029d8c32017-01-06 15:27:41 -0500182 try:
Sean Pryor7f8993f2017-08-14 12:53:17 -0400183 test_func(*args, **kwargs)
Felipe Monteiro51299a12018-06-28 20:03:27 -0400184 except rbac_exceptions.RbacInvalidServiceException:
185 with excutils.save_and_reraise_exception():
186 msg = ("%s is not a valid service." % service)
187 # FIXME(felipemonteiro): This test_status is logged too
188 # late. Need a function to log it before re-raising.
189 test_status = ('Error, %s' % (msg))
190 LOG.error(msg)
Samantha Blanco36bea052017-07-19 12:01:59 -0400191 except (expected_exception,
Mykola Yakovliev11376ab2018-08-06 15:34:22 -0500192 rbac_exceptions.RbacMalformedResponse) as actual_exception:
193 caught_exception = actual_exception
Sean Pryor7f8993f2017-08-14 12:53:17 -0400194 test_status = 'Denied'
Mykola Yakovliev11376ab2018-08-06 15:34:22 -0500195
Felipe Monteiro8eda8cc2017-03-22 14:15:14 +0000196 if irregular_msg:
Felipe Monteiroc0cb7eb2018-06-19 19:50:36 -0400197 LOG.warning(irregular_msg,
198 test_func.__name__,
199 ', '.join(rules),
200 service)
Mykola Yakovliev11376ab2018-08-06 15:34:22 -0500201
DavidPurcell029d8c32017-01-06 15:27:41 -0500202 if allowed:
Felipe Monteiro44d77842018-03-21 02:42:59 +0000203 msg = ("Role %s was not allowed to perform the following "
204 "actions: %s. Expected allowed actions: %s. "
205 "Expected disallowed actions: %s." % (
206 role, sorted(rules),
207 sorted(set(rules) - set(disallowed_rules)),
208 sorted(disallowed_rules)))
DavidPurcell029d8c32017-01-06 15:27:41 -0500209 LOG.error(msg)
Felipe Monteirof16b6b32018-06-28 19:32:59 -0400210 raise rbac_exceptions.RbacUnderPermissionException(
Mykola Yakovliev11376ab2018-08-06 15:34:22 -0500211 "%s Exception was: %s" % (msg, actual_exception))
Felipe Monteirof16b6b32018-06-28 19:32:59 -0400212 except Exception as actual_exception:
Mykola Yakovliev11376ab2018-08-06 15:34:22 -0500213 caught_exception = actual_exception
214
Felipe Monteirof16b6b32018-06-28 19:32:59 -0400215 if _check_for_expected_mismatch_exception(expected_exception,
216 actual_exception):
217 LOG.error('Expected and actual exceptions do not match. '
218 'Expected: %s. Actual: %s.',
219 expected_exception,
220 actual_exception.__class__)
221 raise rbac_exceptions.RbacExpectedWrongException(
222 expected=expected_exception,
223 actual=actual_exception.__class__,
224 exception=actual_exception)
225 else:
226 with excutils.save_and_reraise_exception():
227 exc_info = sys.exc_info()
228 error_details = six.text_type(exc_info[1])
229 msg = ("An unexpected exception has occurred during "
230 "test: %s. Exception was: %s" % (
231 test_func.__name__, error_details))
232 test_status = 'Error, %s' % (error_details)
233 LOG.error(msg)
DavidPurcell029d8c32017-01-06 15:27:41 -0500234 else:
235 if not allowed:
Felipe Monteiro44d77842018-03-21 02:42:59 +0000236 msg = (
237 "OverPermission: Role %s was allowed to perform the "
238 "following disallowed actions: %s" % (
239 role, sorted(disallowed_rules)
240 )
241 )
242 LOG.error(msg)
Felipe Monteirof16b6b32018-06-28 19:32:59 -0400243 raise rbac_exceptions.RbacOverPermissionException(msg)
raiesmh088590c0c2017-03-14 18:06:52 +0530244 finally:
Sean Pryor7f8993f2017-08-14 12:53:17 -0400245 if CONF.patrole_log.enable_reporting:
246 RBACLOG.info(
Felipe Monteiroc0cb7eb2018-06-19 19:50:36 -0400247 "[Service]: %s, [Test]: %s, [Rules]: %s, "
Sean Pryor7f8993f2017-08-14 12:53:17 -0400248 "[Expected]: %s, [Actual]: %s",
Felipe Monteiroc0cb7eb2018-06-19 19:50:36 -0400249 service, test_func.__name__, ', '.join(rules),
Sean Pryor7f8993f2017-08-14 12:53:17 -0400250 "Allowed" if allowed else "Denied",
251 test_status)
Felipe Monteiro78fc4892017-04-12 21:33:39 +0100252
Mykola Yakovliev11376ab2018-08-06 15:34:22 -0500253 # Sanity-check that ``override_role`` was called to eliminate
254 # false-positives and bad test flows resulting from exceptions
255 # getting raised too early, too late or not at all, within
256 # the scope of an RBAC test.
257 _validate_override_role_called(
258 test_obj,
259 actual_exception=caught_exception)
260
Felipe Monteiro2fe986d2018-03-20 21:53:51 +0000261 return wrapper
DavidPurcell029d8c32017-01-06 15:27:41 -0500262 return decorator
Rick Bartra12998942017-03-17 17:35:45 -0400263
264
Felipe Monteiro59f538f2018-08-22 23:34:40 -0400265def _prepare_multi_policy(rules, exp_error_codes):
Cliff Parsons35a77112018-05-07 14:03:40 -0500266 if exp_error_codes:
267 if not rules:
268 msg = ("The `rules` list must be provided if using the "
269 "`expected_error_codes` list.")
270 raise ValueError(msg)
271 if len(rules) != len(exp_error_codes):
272 msg = ("The `expected_error_codes` list is not the same length "
273 "as the `rules` list.")
274 raise ValueError(msg)
Cliff Parsons35a77112018-05-07 14:03:40 -0500275 if not isinstance(exp_error_codes, (tuple, list)):
276 exp_error_codes = [exp_error_codes]
277 else:
278 exp_error_codes = []
Cliff Parsons35a77112018-05-07 14:03:40 -0500279
Felipe Monteiro44d77842018-03-21 02:42:59 +0000280 if rules is None:
281 rules = []
282 elif not isinstance(rules, (tuple, list)):
283 rules = [rules]
Cliff Parsons35a77112018-05-07 14:03:40 -0500284
285 # Fill in the exp_error_codes if needed. This is needed for the scenarios
286 # where no exp_error_codes array is provided, so the error codes must be
287 # set to the default error code value and there must be the same number
288 # of error codes as rules.
289 num_ecs = len(exp_error_codes)
290 num_rules = len(rules)
291 if (num_ecs < num_rules):
292 for i in range(num_rules - num_ecs):
293 exp_error_codes.append(_DEFAULT_ERROR_CODE)
294
Chi Lo8c04bd82018-06-01 16:21:50 -0500295 evaluated_rules = [
296 r() if callable(r) else r for r in rules
297 ]
298
299 return evaluated_rules, exp_error_codes
Felipe Monteiro44d77842018-03-21 02:42:59 +0000300
301
Felipe Monteiro318a0bf2018-02-27 06:57:10 -0500302def _is_authorized(test_obj, service, rule, extra_target_data):
Felipe Monteirodea13842017-07-05 04:11:18 +0100303 """Validates whether current RBAC role has permission to do policy action.
304
Felipe Monteirof2b58d72017-08-31 22:40:36 +0100305 :param test_obj: An instance or subclass of ``tempest.test.BaseTestCase``.
Felipe Monteiro01d633b2017-08-16 20:17:26 +0100306 :param service: The OpenStack service that enforces ``rule``.
307 :param rule: The name of the policy action. Examples include
308 "identity:create_user" or "os_compute_api:os-agents".
Felipe Monteirof2b58d72017-08-31 22:40:36 +0100309 :param extra_target_data: Dictionary, keyed with ``oslo.policy`` generic
Felipe Monteiro01d633b2017-08-16 20:17:26 +0100310 check names, whose values are string literals that reference nested
Felipe Monteirof2b58d72017-08-31 22:40:36 +0100311 ``tempest.test.BaseTestCase`` attributes. Used by ``oslo.policy`` for
Felipe Monteiro01d633b2017-08-16 20:17:26 +0100312 performing matching against attributes that are sent along with the API
313 calls.
Sean Pryor7f8993f2017-08-14 12:53:17 -0400314
Felipe Monteiro01d633b2017-08-16 20:17:26 +0100315 :returns: True if the current RBAC role can perform the policy action,
316 else False.
Sean Pryor7f8993f2017-08-14 12:53:17 -0400317
Felipe Monteiro01d633b2017-08-16 20:17:26 +0100318 :raises RbacResourceSetupFailed: If `project_id` or `user_id` are missing
319 from the `auth_provider` attribute in `test_obj`.
Felipe Monteirodea13842017-07-05 04:11:18 +0100320 """
Sean Pryor7f8993f2017-08-14 12:53:17 -0400321
Felipe Monteiro78fc4892017-04-12 21:33:39 +0100322 try:
Felipe Monteiroe8d93e02017-07-19 20:52:20 +0100323 project_id = test_obj.os_primary.credentials.project_id
324 user_id = test_obj.os_primary.credentials.user_id
Felipe Monteiro78fc4892017-04-12 21:33:39 +0100325 except AttributeError as e:
Felipe Monteiroe8d93e02017-07-19 20:52:20 +0100326 msg = ("{0}: project_id or user_id not found in os_primary.credentials"
327 .format(e))
Felipe Monteiro78fc4892017-04-12 21:33:39 +0100328 LOG.error(msg)
329 raise rbac_exceptions.RbacResourceSetupFailed(msg)
330
Felipe Monteiro4ef7e532018-03-11 07:17:11 -0400331 role = CONF.patrole.rbac_test_role
332 # Test RBAC against custom requirements. Otherwise use oslo.policy.
333 if CONF.patrole.test_custom_requirements:
334 authority = requirements_authority.RequirementsAuthority(
335 CONF.patrole.custom_requirements_file, service)
336 else:
337 formatted_target_data = _format_extra_target_data(
338 test_obj, extra_target_data)
339 authority = policy_authority.PolicyAuthority(
340 project_id, user_id, service,
341 extra_target_data=formatted_target_data)
342 is_allowed = authority.allowed(rule, role)
Felipe Monteiro78fc4892017-04-12 21:33:39 +0100343
Felipe Monteiro4ef7e532018-03-11 07:17:11 -0400344 if is_allowed:
Felipe Monteiroc0cb7eb2018-06-19 19:50:36 -0400345 LOG.debug("[Policy action]: %s, [Role]: %s is allowed!", rule,
Felipe Monteiro4ef7e532018-03-11 07:17:11 -0400346 role)
347 else:
Felipe Monteiroc0cb7eb2018-06-19 19:50:36 -0400348 LOG.debug("[Policy action]: %s, [Role]: %s is NOT allowed!",
Felipe Monteiro4ef7e532018-03-11 07:17:11 -0400349 rule, role)
350
351 return is_allowed
Felipe Monteiro78fc4892017-04-12 21:33:39 +0100352
353
Felipe Monteiroc0cb7eb2018-06-19 19:50:36 -0400354def _get_exception_type(expected_error_code=_DEFAULT_ERROR_CODE):
Felipe Monteiro973a1bc2017-06-14 21:23:54 +0100355 """Dynamically calculate the expected exception to be caught.
356
357 Dynamically calculate the expected exception to be caught by the test case.
Felipe Monteirof2b58d72017-08-31 22:40:36 +0100358 Only ``Forbidden`` and ``NotFound`` exceptions are permitted. ``NotFound``
359 is supported because Neutron, for security reasons, masks ``Forbidden``
360 exceptions as ``NotFound`` exceptions.
Felipe Monteiro973a1bc2017-06-14 21:23:54 +0100361
362 :param expected_error_code: the integer representation of the expected
Felipe Monteirof2b58d72017-08-31 22:40:36 +0100363 exception to be caught. Must be contained in
364 ``_SUPPORTED_ERROR_CODES``.
Felipe Monteiro973a1bc2017-06-14 21:23:54 +0100365 :returns: tuple of the exception type corresponding to
Felipe Monteirof2b58d72017-08-31 22:40:36 +0100366 ``expected_error_code`` and a message explaining that a non-Forbidden
Felipe Monteiro973a1bc2017-06-14 21:23:54 +0100367 exception was expected, if applicable.
368 """
Rick Bartra12998942017-03-17 17:35:45 -0400369 expected_exception = None
370 irregular_msg = None
Felipe Monteiro973a1bc2017-06-14 21:23:54 +0100371
372 if not isinstance(expected_error_code, six.integer_types) \
Sean Pryor7f8993f2017-08-14 12:53:17 -0400373 or expected_error_code not in _SUPPORTED_ERROR_CODES:
Felipe Monteiro973a1bc2017-06-14 21:23:54 +0100374 msg = ("Please pass an expected error code. Currently "
375 "supported codes: {0}".format(_SUPPORTED_ERROR_CODES))
376 LOG.error(msg)
377 raise rbac_exceptions.RbacInvalidErrorCode(msg)
Felipe Monteiro78fc4892017-04-12 21:33:39 +0100378
Rick Bartra12998942017-03-17 17:35:45 -0400379 if expected_error_code == 403:
Felipe Monteiro51299a12018-06-28 20:03:27 -0400380 expected_exception = lib_exc.Forbidden
Rick Bartra12998942017-03-17 17:35:45 -0400381 elif expected_error_code == 404:
Felipe Monteiro51299a12018-06-28 20:03:27 -0400382 expected_exception = lib_exc.NotFound
Felipe Monteiroc0cb7eb2018-06-19 19:50:36 -0400383 irregular_msg = ("NotFound exception was caught for test %s. Expected "
384 "policies which may have caused the error: %s. The "
385 "service %s throws a 404 instead of a 403, which is "
Mykola Yakovliev11376ab2018-08-06 15:34:22 -0500386 "irregular")
Rick Bartra12998942017-03-17 17:35:45 -0400387 return expected_exception, irregular_msg
Felipe Monteirofd1db982017-04-13 21:19:41 +0100388
389
390def _format_extra_target_data(test_obj, extra_target_data):
391 """Formats the "extra_target_data" dictionary with correct test data.
392
393 Before being formatted, "extra_target_data" is a dictionary that maps a
Felipe Monteiro01d633b2017-08-16 20:17:26 +0100394 policy string like "trust.trustor_user_id" to a nested list of
Felipe Monteirof2b58d72017-08-31 22:40:36 +0100395 ``tempest.test.BaseTestCase`` attributes. For example, the attribute list
Masayuki Igawa80b9aab2018-01-09 17:00:45 +0900396 in::
Felipe Monteirofd1db982017-04-13 21:19:41 +0100397
Masayuki Igawa80b9aab2018-01-09 17:00:45 +0900398 "trust.trustor_user_id": "os.auth_provider.credentials.user_id"
Felipe Monteirofd1db982017-04-13 21:19:41 +0100399
Felipe Monteiro01d633b2017-08-16 20:17:26 +0100400 is parsed by iteratively calling ``getattr`` until the value of "user_id"
Masayuki Igawa80b9aab2018-01-09 17:00:45 +0900401 is resolved. The resulting dictionary returns::
Felipe Monteirofd1db982017-04-13 21:19:41 +0100402
Masayuki Igawa80b9aab2018-01-09 17:00:45 +0900403 "trust.trustor_user_id": "the user_id of the `os_primary` credential"
Felipe Monteirofd1db982017-04-13 21:19:41 +0100404
Felipe Monteirof2b58d72017-08-31 22:40:36 +0100405 :param test_obj: An instance or subclass of ``tempest.test.BaseTestCase``.
406 :param extra_target_data: Dictionary, keyed with ``oslo.policy`` generic
Felipe Monteiro01d633b2017-08-16 20:17:26 +0100407 check names, whose values are string literals that reference nested
Felipe Monteirof2b58d72017-08-31 22:40:36 +0100408 ``tempest.test.BaseTestCase`` attributes. Used by ``oslo.policy`` for
Felipe Monteiro01d633b2017-08-16 20:17:26 +0100409 performing matching against attributes that are sent along with the API
410 calls.
411 :returns: Dictionary containing additional object data needed by
Felipe Monteirof2b58d72017-08-31 22:40:36 +0100412 ``oslo.policy`` to validate generic checks.
Felipe Monteirofd1db982017-04-13 21:19:41 +0100413 """
414 attr_value = test_obj
415 formatted_target_data = {}
416
417 for user_attribute, attr_string in extra_target_data.items():
418 attrs = attr_string.split('.')
419 for attr in attrs:
420 attr_value = getattr(attr_value, attr)
421 formatted_target_data[user_attribute] = attr_value
422
423 return formatted_target_data
Felipe Monteirof16b6b32018-06-28 19:32:59 -0400424
425
426def _check_for_expected_mismatch_exception(expected_exception,
427 actual_exception):
Mykola Yakovliev11376ab2018-08-06 15:34:22 -0500428 """Checks that ``expected_exception`` matches ``actual_exception``.
429
430 Since Patrole must handle 403/404 it is important that the expected and
431 actual error codes match.
432
433 :param excepted_exception: Expected exception for test.
434 :param actual_exception: Actual exception raised by test.
435 :returns: True if match, else False.
436 :rtype: boolean
437 """
Felipe Monteiro51299a12018-06-28 20:03:27 -0400438 permission_exceptions = (lib_exc.Forbidden, lib_exc.NotFound)
Felipe Monteirof16b6b32018-06-28 19:32:59 -0400439 if isinstance(actual_exception, permission_exceptions):
440 if not isinstance(actual_exception, expected_exception.__class__):
441 return True
442 return False
Mykola Yakovliev11376ab2018-08-06 15:34:22 -0500443
444
445def _validate_override_role_called(test_obj, actual_exception):
446 """Validates that :func:`rbac_utils.RbacUtils.override_role` is called
447 during each Patrole test.
448
449 Useful for validating that the expected exception isn't raised too early
450 (before ``override_role`` call) or too late (after ``override_call``) or
451 at all (which is a bad test).
452
453 :param test_obj: An instance or subclass of ``tempest.test.BaseTestCase``.
454 :param actual_exception: Actual exception raised by test.
455 :raises RbacOverrideRoleException: If ``override_role`` isn't called, is
456 called too early, or is called too late.
457 """
458 called = test_obj._validate_override_role_called()
459 base_msg = ('This error is unrelated to RBAC and is due to either '
460 'an API or override role failure. Exception: %s' %
461 actual_exception)
462
463 if not called:
464 if actual_exception is not None:
465 msg = ('Caught exception (%s) but it was raised before the '
466 '`override_role` context. ' % actual_exception.__class__)
467 else:
468 msg = 'Test missing required `override_role` call. '
469 msg += base_msg
470 LOG.error(msg)
471 raise rbac_exceptions.RbacOverrideRoleException(msg)
472 else:
473 exc_caught_in_ctx = test_obj._validate_override_role_caught_exc()
474 # This block is only executed if ``override_role`` is called. If
475 # an exception is raised and the exception wasn't raised in the
476 # ``override_role`` context and if the exception isn't a valid
477 # exception type (instance of ``BasePatroleException``), then this is
478 # a legitimate error.
479 if (not exc_caught_in_ctx and
480 actual_exception is not None and
481 not isinstance(actual_exception,
482 rbac_exceptions.BasePatroleException)):
483 msg = ('Caught exception (%s) but it was raised after the '
484 '`override_role` context. ' % actual_exception.__class__)
485 msg += base_msg
486 LOG.error(msg)
487 raise rbac_exceptions.RbacOverrideRoleException(msg)