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