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