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 | b059565 | 2017-01-23 16:51:58 -0500 | [diff] [blame] | 16 | import logging |
Felipe Monteiro | 8eda8cc | 2017-03-22 14:15:14 +0000 | [diff] [blame] | 17 | import sys |
Felipe Monteiro | 78fc489 | 2017-04-12 21:33:39 +0100 | [diff] [blame] | 18 | import testtools |
Felipe Monteiro | 8eda8cc | 2017-03-22 14:15:14 +0000 | [diff] [blame] | 19 | |
Felipe Monteiro | 38f344b | 2017-11-03 12:59:15 +0000 | [diff] [blame] | 20 | from oslo_utils import excutils |
Felipe Monteiro | 8eda8cc | 2017-03-22 14:15:14 +0000 | [diff] [blame] | 21 | import six |
Felipe Monteiro | b059565 | 2017-01-23 16:51:58 -0500 | [diff] [blame] | 22 | |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 23 | from tempest import config |
| 24 | from tempest.lib import exceptions |
raiesmh08 | 8590c0c | 2017-03-14 18:06:52 +0530 | [diff] [blame] | 25 | from tempest import test |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 26 | |
Felipe Monteiro | 88a5bab | 2017-08-31 04:00:32 +0100 | [diff] [blame] | 27 | from patrole_tempest_plugin import policy_authority |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 28 | from patrole_tempest_plugin import rbac_exceptions |
Rick Bartra | ed95005 | 2017-06-29 17:20:33 -0400 | [diff] [blame] | 29 | from patrole_tempest_plugin import requirements_authority |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 30 | |
| 31 | CONF = config.CONF |
| 32 | LOG = logging.getLogger(__name__) |
| 33 | |
Felipe Monteiro | 973a1bc | 2017-06-14 21:23:54 +0100 | [diff] [blame] | 34 | _SUPPORTED_ERROR_CODES = [403, 404] |
| 35 | |
Sean Pryor | 7f8993f | 2017-08-14 12:53:17 -0400 | [diff] [blame] | 36 | RBACLOG = logging.getLogger('rbac_reporting') |
| 37 | |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 38 | |
Felipe Monteiro | 318a0bf | 2018-02-27 06:57:10 -0500 | [diff] [blame] | 39 | def action(service, rule='', expected_error_code=403, extra_target_data=None): |
Felipe Monteiro | f2b58d7 | 2017-08-31 22:40:36 +0100 | [diff] [blame] | 40 | """A decorator for verifying OpenStack policy enforcement. |
Felipe Monteiro | d5d76b8 | 2017-03-20 23:18:50 +0000 | [diff] [blame] | 41 | |
Felipe Monteiro | 01d633b | 2017-08-16 20:17:26 +0100 | [diff] [blame] | 42 | A decorator which allows for positive and negative RBAC testing. Given: |
Rick Bartra | ed95005 | 2017-06-29 17:20:33 -0400 | [diff] [blame] | 43 | |
Masayuki Igawa | 80b9aab | 2018-01-09 17:00:45 +0900 | [diff] [blame] | 44 | * an OpenStack service, |
| 45 | * a policy action (``rule``) enforced by that service, and |
| 46 | * the test role defined by ``[patrole] rbac_test_role`` |
Felipe Monteiro | d5d76b8 | 2017-03-20 23:18:50 +0000 | [diff] [blame] | 47 | |
Felipe Monteiro | 01d633b | 2017-08-16 20:17:26 +0100 | [diff] [blame] | 48 | determines whether the test role has sufficient permissions to perform an |
| 49 | API call that enforces the ``rule``. |
Felipe Monteiro | d5d76b8 | 2017-03-20 23:18:50 +0000 | [diff] [blame] | 50 | |
Felipe Monteiro | 01d633b | 2017-08-16 20:17:26 +0100 | [diff] [blame] | 51 | This decorator should only be applied to an instance or subclass of |
Masayuki Igawa | 80b9aab | 2018-01-09 17:00:45 +0900 | [diff] [blame] | 52 | ``tempest.test.BaseTestCase``. |
Felipe Monteiro | 01d633b | 2017-08-16 20:17:26 +0100 | [diff] [blame] | 53 | |
| 54 | The result from ``_is_authorized`` is used to determine the *expected* |
| 55 | test result. The *actual* test result is determined by running the |
| 56 | Tempest test this decorator applies to. |
| 57 | |
| 58 | Below are the following possibilities from comparing the *expected* and |
| 59 | *actual* results: |
| 60 | |
| 61 | 1) If *expected* is True and the test passes (*actual*), this is a success. |
| 62 | 2) If *expected* is True and the test fails (*actual*), this results in a |
| 63 | `Forbidden` exception failure. |
| 64 | 3) If *expected* is False and the test passes (*actual*), this results in |
| 65 | an `OverPermission` exception failure. |
| 66 | 4) If *expected* is False and the test fails (*actual*), this is a success. |
| 67 | |
| 68 | As such, negative and positive testing can be applied using this decorator. |
| 69 | |
Felipe Monteiro | f2b58d7 | 2017-08-31 22:40:36 +0100 | [diff] [blame] | 70 | :param service: An OpenStack service. Examples: "nova" or "neutron". |
Felipe Monteiro | 01d633b | 2017-08-16 20:17:26 +0100 | [diff] [blame] | 71 | :param rule: A policy action defined in a policy.json file (or in |
| 72 | code). |
| 73 | |
| 74 | .. note:: |
| 75 | |
| 76 | Patrole currently only supports custom JSON policy files. |
| 77 | |
Felipe Monteiro | d5d76b8 | 2017-03-20 23:18:50 +0000 | [diff] [blame] | 78 | :param expected_error_code: Overrides default value of 403 (Forbidden) |
Felipe Monteiro | 973a1bc | 2017-06-14 21:23:54 +0100 | [diff] [blame] | 79 | with endpoint-specific error code. Currently only supports 403 and 404. |
| 80 | Support for 404 is needed because some services, like Neutron, |
| 81 | intentionally throw a 404 for security reasons. |
Felipe Monteiro | d5d76b8 | 2017-03-20 23:18:50 +0000 | [diff] [blame] | 82 | |
Felipe Monteiro | 01d633b | 2017-08-16 20:17:26 +0100 | [diff] [blame] | 83 | .. warning:: |
| 84 | |
| 85 | A 404 should not be provided *unless* the endpoint masks a |
Felipe Monteiro | f2b58d7 | 2017-08-31 22:40:36 +0100 | [diff] [blame] | 86 | ``Forbidden`` exception as a ``NotFound`` exception. |
Felipe Monteiro | 01d633b | 2017-08-16 20:17:26 +0100 | [diff] [blame] | 87 | |
Felipe Monteiro | f2b58d7 | 2017-08-31 22:40:36 +0100 | [diff] [blame] | 88 | :param extra_target_data: Dictionary, keyed with ``oslo.policy`` generic |
Felipe Monteiro | 01d633b | 2017-08-16 20:17:26 +0100 | [diff] [blame] | 89 | check names, whose values are string literals that reference nested |
Felipe Monteiro | f2b58d7 | 2017-08-31 22:40:36 +0100 | [diff] [blame] | 90 | ``tempest.test.BaseTestCase`` attributes. Used by ``oslo.policy`` for |
Felipe Monteiro | 01d633b | 2017-08-16 20:17:26 +0100 | [diff] [blame] | 91 | performing matching against attributes that are sent along with the API |
| 92 | calls. Example:: |
| 93 | |
| 94 | extra_target_data={ |
| 95 | "target.token.user_id": |
| 96 | "os_alt.auth_provider.credentials.user_id" |
| 97 | }) |
| 98 | |
Felipe Monteiro | f2b58d7 | 2017-08-31 22:40:36 +0100 | [diff] [blame] | 99 | :raises NotFound: If ``service`` is invalid. |
Felipe Monteiro | 01d633b | 2017-08-16 20:17:26 +0100 | [diff] [blame] | 100 | :raises Forbidden: For item (2) above. |
| 101 | :raises RbacOverPermission: For item (3) above. |
| 102 | |
| 103 | Examples:: |
| 104 | |
| 105 | @rbac_rule_validation.action( |
| 106 | service="nova", rule="os_compute_api:os-agents") |
| 107 | def test_list_agents_rbac(self): |
Felipe Monteiro | 1c8620a | 2018-02-25 18:52:22 +0000 | [diff] [blame] | 108 | # The call to `override_role` is mandatory. |
| 109 | with self.rbac_utils.override_role(self): |
| 110 | self.agents_client.list_agents() |
Felipe Monteiro | d5d76b8 | 2017-03-20 23:18:50 +0000 | [diff] [blame] | 111 | """ |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 112 | |
| 113 | if extra_target_data is None: |
| 114 | extra_target_data = {} |
| 115 | |
Sean Pryor | 7f8993f | 2017-08-14 12:53:17 -0400 | [diff] [blame] | 116 | def decorator(test_func): |
Felipe Monteiro | f6eb862 | 2017-08-06 06:08:02 +0100 | [diff] [blame] | 117 | role = CONF.patrole.rbac_test_role |
Felipe Monteiro | 78fc489 | 2017-04-12 21:33:39 +0100 | [diff] [blame] | 118 | |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 119 | def wrapper(*args, **kwargs): |
Felipe Monteiro | 78fc489 | 2017-04-12 21:33:39 +0100 | [diff] [blame] | 120 | if args and isinstance(args[0], test.BaseTestCase): |
| 121 | test_obj = args[0] |
| 122 | else: |
| 123 | raise rbac_exceptions.RbacResourceSetupFailed( |
| 124 | '`rbac_rule_validation` decorator can only be applied to ' |
| 125 | 'an instance of `tempest.test.BaseTestCase`.') |
raiesmh08 | 8590c0c | 2017-03-14 18:06:52 +0530 | [diff] [blame] | 126 | |
Sean Pryor | 7f8993f | 2017-08-14 12:53:17 -0400 | [diff] [blame] | 127 | allowed = _is_authorized(test_obj, service, rule, |
Felipe Monteiro | 318a0bf | 2018-02-27 06:57:10 -0500 | [diff] [blame] | 128 | extra_target_data) |
Felipe Monteiro | d5d76b8 | 2017-03-20 23:18:50 +0000 | [diff] [blame] | 129 | |
Rick Bartra | 1299894 | 2017-03-17 17:35:45 -0400 | [diff] [blame] | 130 | expected_exception, irregular_msg = _get_exception_type( |
| 131 | expected_error_code) |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 132 | |
Sean Pryor | 7f8993f | 2017-08-14 12:53:17 -0400 | [diff] [blame] | 133 | test_status = 'Allowed' |
| 134 | |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 135 | try: |
Sean Pryor | 7f8993f | 2017-08-14 12:53:17 -0400 | [diff] [blame] | 136 | test_func(*args, **kwargs) |
Rick Bartra | 503c557 | 2017-03-09 13:49:58 -0500 | [diff] [blame] | 137 | except rbac_exceptions.RbacInvalidService as e: |
Felipe Monteiro | 48c913d | 2017-03-15 12:07:48 -0400 | [diff] [blame] | 138 | msg = ("%s is not a valid service." % service) |
Sean Pryor | 7f8993f | 2017-08-14 12:53:17 -0400 | [diff] [blame] | 139 | test_status = ('Error, %s' % (msg)) |
Felipe Monteiro | 48c913d | 2017-03-15 12:07:48 -0400 | [diff] [blame] | 140 | LOG.error(msg) |
| 141 | raise exceptions.NotFound( |
Felipe Monteiro | 78fc489 | 2017-04-12 21:33:39 +0100 | [diff] [blame] | 142 | "%s RbacInvalidService was: %s" % (msg, e)) |
Samantha Blanco | 36bea05 | 2017-07-19 12:01:59 -0400 | [diff] [blame] | 143 | except (expected_exception, |
| 144 | rbac_exceptions.RbacConflictingPolicies, |
| 145 | rbac_exceptions.RbacMalformedResponse) as e: |
Sean Pryor | 7f8993f | 2017-08-14 12:53:17 -0400 | [diff] [blame] | 146 | test_status = 'Denied' |
Felipe Monteiro | 8eda8cc | 2017-03-22 14:15:14 +0000 | [diff] [blame] | 147 | if irregular_msg: |
| 148 | LOG.warning(irregular_msg.format(rule, service)) |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 149 | if allowed: |
| 150 | msg = ("Role %s was not allowed to perform %s." % |
Felipe Monteiro | 78fc489 | 2017-04-12 21:33:39 +0100 | [diff] [blame] | 151 | (role, rule)) |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 152 | LOG.error(msg) |
| 153 | raise exceptions.Forbidden( |
Felipe Monteiro | 4bf66a2 | 2017-05-07 14:44:21 +0100 | [diff] [blame] | 154 | "%s Exception was: %s" % (msg, e)) |
Felipe Monteiro | 8eda8cc | 2017-03-22 14:15:14 +0000 | [diff] [blame] | 155 | except Exception as e: |
Felipe Monteiro | 38f344b | 2017-11-03 12:59:15 +0000 | [diff] [blame] | 156 | with excutils.save_and_reraise_exception(): |
| 157 | exc_info = sys.exc_info() |
| 158 | error_details = six.text_type(exc_info[1]) |
| 159 | msg = ("An unexpected exception has occurred during test: " |
| 160 | "%s. Exception was: %s" % (test_func.__name__, |
| 161 | error_details)) |
| 162 | test_status = 'Error, %s' % (error_details) |
| 163 | LOG.error(msg) |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 164 | else: |
| 165 | if not allowed: |
Felipe Monteiro | 4ef7e53 | 2018-03-11 07:17:11 -0400 | [diff] [blame] | 166 | LOG.error("Role %s was allowed to perform %s", role, rule) |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 167 | raise rbac_exceptions.RbacOverPermission( |
| 168 | "OverPermission: Role %s was allowed to perform %s" % |
Felipe Monteiro | 78fc489 | 2017-04-12 21:33:39 +0100 | [diff] [blame] | 169 | (role, rule)) |
raiesmh08 | 8590c0c | 2017-03-14 18:06:52 +0530 | [diff] [blame] | 170 | finally: |
Sean Pryor | 7f8993f | 2017-08-14 12:53:17 -0400 | [diff] [blame] | 171 | if CONF.patrole_log.enable_reporting: |
| 172 | RBACLOG.info( |
| 173 | "[Service]: %s, [Test]: %s, [Rule]: %s, " |
| 174 | "[Expected]: %s, [Actual]: %s", |
| 175 | service, test_func.__name__, rule, |
| 176 | "Allowed" if allowed else "Denied", |
| 177 | test_status) |
Felipe Monteiro | 78fc489 | 2017-04-12 21:33:39 +0100 | [diff] [blame] | 178 | |
| 179 | _wrapper = testtools.testcase.attr(role)(wrapper) |
| 180 | return _wrapper |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 181 | return decorator |
Rick Bartra | 1299894 | 2017-03-17 17:35:45 -0400 | [diff] [blame] | 182 | |
| 183 | |
Felipe Monteiro | 318a0bf | 2018-02-27 06:57:10 -0500 | [diff] [blame] | 184 | def _is_authorized(test_obj, service, rule, extra_target_data): |
Felipe Monteiro | dea1384 | 2017-07-05 04:11:18 +0100 | [diff] [blame] | 185 | """Validates whether current RBAC role has permission to do policy action. |
| 186 | |
Felipe Monteiro | f2b58d7 | 2017-08-31 22:40:36 +0100 | [diff] [blame] | 187 | :param test_obj: An instance or subclass of ``tempest.test.BaseTestCase``. |
Felipe Monteiro | 01d633b | 2017-08-16 20:17:26 +0100 | [diff] [blame] | 188 | :param service: The OpenStack service that enforces ``rule``. |
| 189 | :param rule: The name of the policy action. Examples include |
| 190 | "identity:create_user" or "os_compute_api:os-agents". |
Felipe Monteiro | f2b58d7 | 2017-08-31 22:40:36 +0100 | [diff] [blame] | 191 | :param extra_target_data: Dictionary, keyed with ``oslo.policy`` generic |
Felipe Monteiro | 01d633b | 2017-08-16 20:17:26 +0100 | [diff] [blame] | 192 | check names, whose values are string literals that reference nested |
Felipe Monteiro | f2b58d7 | 2017-08-31 22:40:36 +0100 | [diff] [blame] | 193 | ``tempest.test.BaseTestCase`` attributes. Used by ``oslo.policy`` for |
Felipe Monteiro | 01d633b | 2017-08-16 20:17:26 +0100 | [diff] [blame] | 194 | performing matching against attributes that are sent along with the API |
| 195 | calls. |
Sean Pryor | 7f8993f | 2017-08-14 12:53:17 -0400 | [diff] [blame] | 196 | |
Felipe Monteiro | 01d633b | 2017-08-16 20:17:26 +0100 | [diff] [blame] | 197 | :returns: True if the current RBAC role can perform the policy action, |
| 198 | else False. |
Sean Pryor | 7f8993f | 2017-08-14 12:53:17 -0400 | [diff] [blame] | 199 | |
Felipe Monteiro | 01d633b | 2017-08-16 20:17:26 +0100 | [diff] [blame] | 200 | :raises RbacResourceSetupFailed: If `project_id` or `user_id` are missing |
| 201 | from the `auth_provider` attribute in `test_obj`. |
Felipe Monteiro | dea1384 | 2017-07-05 04:11:18 +0100 | [diff] [blame] | 202 | """ |
Sean Pryor | 7f8993f | 2017-08-14 12:53:17 -0400 | [diff] [blame] | 203 | |
Felipe Monteiro | 78fc489 | 2017-04-12 21:33:39 +0100 | [diff] [blame] | 204 | try: |
Felipe Monteiro | e8d93e0 | 2017-07-19 20:52:20 +0100 | [diff] [blame] | 205 | project_id = test_obj.os_primary.credentials.project_id |
| 206 | user_id = test_obj.os_primary.credentials.user_id |
Felipe Monteiro | 78fc489 | 2017-04-12 21:33:39 +0100 | [diff] [blame] | 207 | except AttributeError as e: |
Felipe Monteiro | e8d93e0 | 2017-07-19 20:52:20 +0100 | [diff] [blame] | 208 | msg = ("{0}: project_id or user_id not found in os_primary.credentials" |
| 209 | .format(e)) |
Felipe Monteiro | 78fc489 | 2017-04-12 21:33:39 +0100 | [diff] [blame] | 210 | LOG.error(msg) |
| 211 | raise rbac_exceptions.RbacResourceSetupFailed(msg) |
| 212 | |
Felipe Monteiro | 4ef7e53 | 2018-03-11 07:17:11 -0400 | [diff] [blame] | 213 | role = CONF.patrole.rbac_test_role |
| 214 | # Test RBAC against custom requirements. Otherwise use oslo.policy. |
| 215 | if CONF.patrole.test_custom_requirements: |
| 216 | authority = requirements_authority.RequirementsAuthority( |
| 217 | CONF.patrole.custom_requirements_file, service) |
| 218 | else: |
| 219 | formatted_target_data = _format_extra_target_data( |
| 220 | test_obj, extra_target_data) |
| 221 | authority = policy_authority.PolicyAuthority( |
| 222 | project_id, user_id, service, |
| 223 | extra_target_data=formatted_target_data) |
| 224 | is_allowed = authority.allowed(rule, role) |
Felipe Monteiro | 78fc489 | 2017-04-12 21:33:39 +0100 | [diff] [blame] | 225 | |
Felipe Monteiro | 4ef7e53 | 2018-03-11 07:17:11 -0400 | [diff] [blame] | 226 | if is_allowed: |
| 227 | LOG.debug("[Action]: %s, [Role]: %s is allowed!", rule, |
| 228 | role) |
| 229 | else: |
| 230 | LOG.debug("[Action]: %s, [Role]: %s is NOT allowed!", |
| 231 | rule, role) |
| 232 | |
| 233 | return is_allowed |
Felipe Monteiro | 78fc489 | 2017-04-12 21:33:39 +0100 | [diff] [blame] | 234 | |
| 235 | |
Felipe Monteiro | 973a1bc | 2017-06-14 21:23:54 +0100 | [diff] [blame] | 236 | def _get_exception_type(expected_error_code=403): |
| 237 | """Dynamically calculate the expected exception to be caught. |
| 238 | |
| 239 | Dynamically calculate the expected exception to be caught by the test case. |
Felipe Monteiro | f2b58d7 | 2017-08-31 22:40:36 +0100 | [diff] [blame] | 240 | Only ``Forbidden`` and ``NotFound`` exceptions are permitted. ``NotFound`` |
| 241 | is supported because Neutron, for security reasons, masks ``Forbidden`` |
| 242 | exceptions as ``NotFound`` exceptions. |
Felipe Monteiro | 973a1bc | 2017-06-14 21:23:54 +0100 | [diff] [blame] | 243 | |
| 244 | :param expected_error_code: the integer representation of the expected |
Felipe Monteiro | f2b58d7 | 2017-08-31 22:40:36 +0100 | [diff] [blame] | 245 | exception to be caught. Must be contained in |
| 246 | ``_SUPPORTED_ERROR_CODES``. |
Felipe Monteiro | 973a1bc | 2017-06-14 21:23:54 +0100 | [diff] [blame] | 247 | :returns: tuple of the exception type corresponding to |
Felipe Monteiro | f2b58d7 | 2017-08-31 22:40:36 +0100 | [diff] [blame] | 248 | ``expected_error_code`` and a message explaining that a non-Forbidden |
Felipe Monteiro | 973a1bc | 2017-06-14 21:23:54 +0100 | [diff] [blame] | 249 | exception was expected, if applicable. |
| 250 | """ |
Rick Bartra | 1299894 | 2017-03-17 17:35:45 -0400 | [diff] [blame] | 251 | expected_exception = None |
| 252 | irregular_msg = None |
Felipe Monteiro | 973a1bc | 2017-06-14 21:23:54 +0100 | [diff] [blame] | 253 | |
| 254 | if not isinstance(expected_error_code, six.integer_types) \ |
Sean Pryor | 7f8993f | 2017-08-14 12:53:17 -0400 | [diff] [blame] | 255 | or expected_error_code not in _SUPPORTED_ERROR_CODES: |
Felipe Monteiro | 973a1bc | 2017-06-14 21:23:54 +0100 | [diff] [blame] | 256 | msg = ("Please pass an expected error code. Currently " |
| 257 | "supported codes: {0}".format(_SUPPORTED_ERROR_CODES)) |
| 258 | LOG.error(msg) |
| 259 | raise rbac_exceptions.RbacInvalidErrorCode(msg) |
Felipe Monteiro | 78fc489 | 2017-04-12 21:33:39 +0100 | [diff] [blame] | 260 | |
Rick Bartra | 1299894 | 2017-03-17 17:35:45 -0400 | [diff] [blame] | 261 | if expected_error_code == 403: |
| 262 | expected_exception = exceptions.Forbidden |
| 263 | elif expected_error_code == 404: |
| 264 | expected_exception = exceptions.NotFound |
| 265 | irregular_msg = ("NotFound exception was caught for policy action " |
| 266 | "{0}. The service {1} throws a 404 instead of a 403, " |
| 267 | "which is irregular.") |
Rick Bartra | 1299894 | 2017-03-17 17:35:45 -0400 | [diff] [blame] | 268 | |
| 269 | return expected_exception, irregular_msg |
Felipe Monteiro | fd1db98 | 2017-04-13 21:19:41 +0100 | [diff] [blame] | 270 | |
| 271 | |
| 272 | def _format_extra_target_data(test_obj, extra_target_data): |
| 273 | """Formats the "extra_target_data" dictionary with correct test data. |
| 274 | |
| 275 | Before being formatted, "extra_target_data" is a dictionary that maps a |
Felipe Monteiro | 01d633b | 2017-08-16 20:17:26 +0100 | [diff] [blame] | 276 | policy string like "trust.trustor_user_id" to a nested list of |
Felipe Monteiro | f2b58d7 | 2017-08-31 22:40:36 +0100 | [diff] [blame] | 277 | ``tempest.test.BaseTestCase`` attributes. For example, the attribute list |
Masayuki Igawa | 80b9aab | 2018-01-09 17:00:45 +0900 | [diff] [blame] | 278 | in:: |
Felipe Monteiro | fd1db98 | 2017-04-13 21:19:41 +0100 | [diff] [blame] | 279 | |
Masayuki Igawa | 80b9aab | 2018-01-09 17:00:45 +0900 | [diff] [blame] | 280 | "trust.trustor_user_id": "os.auth_provider.credentials.user_id" |
Felipe Monteiro | fd1db98 | 2017-04-13 21:19:41 +0100 | [diff] [blame] | 281 | |
Felipe Monteiro | 01d633b | 2017-08-16 20:17:26 +0100 | [diff] [blame] | 282 | is parsed by iteratively calling ``getattr`` until the value of "user_id" |
Masayuki Igawa | 80b9aab | 2018-01-09 17:00:45 +0900 | [diff] [blame] | 283 | is resolved. The resulting dictionary returns:: |
Felipe Monteiro | fd1db98 | 2017-04-13 21:19:41 +0100 | [diff] [blame] | 284 | |
Masayuki Igawa | 80b9aab | 2018-01-09 17:00:45 +0900 | [diff] [blame] | 285 | "trust.trustor_user_id": "the user_id of the `os_primary` credential" |
Felipe Monteiro | fd1db98 | 2017-04-13 21:19:41 +0100 | [diff] [blame] | 286 | |
Felipe Monteiro | f2b58d7 | 2017-08-31 22:40:36 +0100 | [diff] [blame] | 287 | :param test_obj: An instance or subclass of ``tempest.test.BaseTestCase``. |
| 288 | :param extra_target_data: Dictionary, keyed with ``oslo.policy`` generic |
Felipe Monteiro | 01d633b | 2017-08-16 20:17:26 +0100 | [diff] [blame] | 289 | check names, whose values are string literals that reference nested |
Felipe Monteiro | f2b58d7 | 2017-08-31 22:40:36 +0100 | [diff] [blame] | 290 | ``tempest.test.BaseTestCase`` attributes. Used by ``oslo.policy`` for |
Felipe Monteiro | 01d633b | 2017-08-16 20:17:26 +0100 | [diff] [blame] | 291 | performing matching against attributes that are sent along with the API |
| 292 | calls. |
| 293 | :returns: Dictionary containing additional object data needed by |
Felipe Monteiro | f2b58d7 | 2017-08-31 22:40:36 +0100 | [diff] [blame] | 294 | ``oslo.policy`` to validate generic checks. |
Felipe Monteiro | fd1db98 | 2017-04-13 21:19:41 +0100 | [diff] [blame] | 295 | """ |
| 296 | attr_value = test_obj |
| 297 | formatted_target_data = {} |
| 298 | |
| 299 | for user_attribute, attr_string in extra_target_data.items(): |
| 300 | attrs = attr_string.split('.') |
| 301 | for attr in attrs: |
| 302 | attr_value = getattr(attr_value, attr) |
| 303 | formatted_target_data[user_attribute] = attr_value |
| 304 | |
| 305 | return formatted_target_data |