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 | |
| 20 | import six |
Felipe Monteiro | b059565 | 2017-01-23 16:51:58 -0500 | [diff] [blame] | 21 | |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 22 | from tempest import config |
| 23 | from tempest.lib import exceptions |
raiesmh08 | 8590c0c | 2017-03-14 18:06:52 +0530 | [diff] [blame] | 24 | from tempest import test |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 25 | |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 26 | from patrole_tempest_plugin import rbac_exceptions |
Felipe Monteiro | 78fc489 | 2017-04-12 21:33:39 +0100 | [diff] [blame] | 27 | from patrole_tempest_plugin import rbac_policy_parser |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 28 | |
| 29 | CONF = config.CONF |
| 30 | LOG = logging.getLogger(__name__) |
| 31 | |
| 32 | |
Felipe Monteiro | e7e552e | 2017-05-02 17:04:12 +0100 | [diff] [blame] | 33 | def action(service, rule='', admin_only=False, expected_error_code=403, |
Felipe Monteiro | fd1db98 | 2017-04-13 21:19:41 +0100 | [diff] [blame] | 34 | extra_target_data={}): |
Felipe Monteiro | d5d76b8 | 2017-03-20 23:18:50 +0000 | [diff] [blame] | 35 | """A decorator which does a policy check and matches it against test run. |
| 36 | |
| 37 | A decorator which allows for positive and negative RBAC testing. Given |
| 38 | an OpenStack service and a policy action enforced by that service, an |
| 39 | oslo.policy lookup is performed by calling `authority.get_permission`. |
| 40 | The following cases are possible: |
| 41 | |
| 42 | * If `allowed` is True and the test passes, this is a success. |
| 43 | * If `allowed` is True and the test fails, this is a failure. |
| 44 | * If `allowed` is False and the test passes, this is a failure. |
| 45 | * If `allowed` is False and the test fails, this is a success. |
| 46 | |
| 47 | :param service: A OpenStack service: for example, "nova" or "neutron". |
| 48 | :param rule: A policy action defined in a policy.json file (or in code). |
| 49 | :param admin_only: Skips over oslo.policy check because the policy action |
| 50 | defined by `rule` is not enforced by the service's |
| 51 | policy enforcement logic. For example, Keystone v2 |
| 52 | performs an admin check for most of its endpoints. If |
| 53 | True, `rule` is effectively ignored. |
| 54 | :param expected_error_code: Overrides default value of 403 (Forbidden) |
| 55 | with endpoint-specific error code. Currently |
| 56 | only supports 403 and 404. Support for 404 |
| 57 | is needed because some services, like Neutron, |
| 58 | intentionally throw a 404 for security reasons. |
| 59 | |
| 60 | :raises NotFound: if `service` is invalid or |
| 61 | if Tempest credentials cannot be found. |
| 62 | :raises Forbidden: for bullet (2) above. |
| 63 | :raises RbacOverPermission: for bullet (3) above. |
| 64 | """ |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 65 | def decorator(func): |
Felipe Monteiro | 78fc489 | 2017-04-12 21:33:39 +0100 | [diff] [blame] | 66 | role = CONF.rbac.rbac_test_role |
| 67 | |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 68 | def wrapper(*args, **kwargs): |
Felipe Monteiro | 78fc489 | 2017-04-12 21:33:39 +0100 | [diff] [blame] | 69 | if args and isinstance(args[0], test.BaseTestCase): |
| 70 | test_obj = args[0] |
| 71 | else: |
| 72 | raise rbac_exceptions.RbacResourceSetupFailed( |
| 73 | '`rbac_rule_validation` decorator can only be applied to ' |
| 74 | 'an instance of `tempest.test.BaseTestCase`.') |
raiesmh08 | 8590c0c | 2017-03-14 18:06:52 +0530 | [diff] [blame] | 75 | |
Felipe Monteiro | d5d76b8 | 2017-03-20 23:18:50 +0000 | [diff] [blame] | 76 | if admin_only: |
| 77 | LOG.info("As admin_only is True, only admin role should be " |
| 78 | "allowed to perform the API. Skipping oslo.policy " |
| 79 | "check for policy action {0}.".format(rule)) |
Felipe Monteiro | f6b69e2 | 2017-05-04 21:55:04 +0100 | [diff] [blame] | 80 | allowed = CONF.rbac.rbac_test_role == CONF.identity.admin_role |
Felipe Monteiro | d5d76b8 | 2017-03-20 23:18:50 +0000 | [diff] [blame] | 81 | else: |
Felipe Monteiro | 78fc489 | 2017-04-12 21:33:39 +0100 | [diff] [blame] | 82 | allowed = _is_authorized(test_obj, service, rule, |
| 83 | extra_target_data) |
Felipe Monteiro | d5d76b8 | 2017-03-20 23:18:50 +0000 | [diff] [blame] | 84 | |
Rick Bartra | 1299894 | 2017-03-17 17:35:45 -0400 | [diff] [blame] | 85 | expected_exception, irregular_msg = _get_exception_type( |
| 86 | expected_error_code) |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 87 | |
| 88 | try: |
Felipe Monteiro | 78fc489 | 2017-04-12 21:33:39 +0100 | [diff] [blame] | 89 | func(*args, **kwargs) |
Rick Bartra | 503c557 | 2017-03-09 13:49:58 -0500 | [diff] [blame] | 90 | except rbac_exceptions.RbacInvalidService as e: |
Felipe Monteiro | 48c913d | 2017-03-15 12:07:48 -0400 | [diff] [blame] | 91 | msg = ("%s is not a valid service." % service) |
| 92 | LOG.error(msg) |
| 93 | raise exceptions.NotFound( |
Felipe Monteiro | 78fc489 | 2017-04-12 21:33:39 +0100 | [diff] [blame] | 94 | "%s RbacInvalidService was: %s" % (msg, e)) |
Felipe Monteiro | 8eda8cc | 2017-03-22 14:15:14 +0000 | [diff] [blame] | 95 | except (expected_exception, rbac_exceptions.RbacActionFailed) as e: |
| 96 | if irregular_msg: |
| 97 | LOG.warning(irregular_msg.format(rule, service)) |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 98 | if allowed: |
| 99 | msg = ("Role %s was not allowed to perform %s." % |
Felipe Monteiro | 78fc489 | 2017-04-12 21:33:39 +0100 | [diff] [blame] | 100 | (role, rule)) |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 101 | LOG.error(msg) |
| 102 | raise exceptions.Forbidden( |
Felipe Monteiro | 78fc489 | 2017-04-12 21:33:39 +0100 | [diff] [blame] | 103 | "%s exception was: %s" % (msg, e)) |
Felipe Monteiro | 8eda8cc | 2017-03-22 14:15:14 +0000 | [diff] [blame] | 104 | except Exception as e: |
| 105 | exc_info = sys.exc_info() |
| 106 | error_details = exc_info[1].__str__() |
| 107 | msg = ("%s An unexpected exception has occurred: Expected " |
| 108 | "exception was %s, which was not thrown." |
| 109 | % (error_details, expected_exception.__name__)) |
| 110 | LOG.error(msg) |
| 111 | six.reraise(exc_info[0], exc_info[0](msg), exc_info[2]) |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 112 | else: |
| 113 | if not allowed: |
| 114 | LOG.error("Role %s was allowed to perform %s" % |
Felipe Monteiro | 78fc489 | 2017-04-12 21:33:39 +0100 | [diff] [blame] | 115 | (role, rule)) |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 116 | raise rbac_exceptions.RbacOverPermission( |
| 117 | "OverPermission: Role %s was allowed to perform %s" % |
Felipe Monteiro | 78fc489 | 2017-04-12 21:33:39 +0100 | [diff] [blame] | 118 | (role, rule)) |
raiesmh08 | 8590c0c | 2017-03-14 18:06:52 +0530 | [diff] [blame] | 119 | finally: |
Felipe Monteiro | 78fc489 | 2017-04-12 21:33:39 +0100 | [diff] [blame] | 120 | test_obj.rbac_utils.switch_role(test_obj, |
| 121 | toggle_rbac_role=False) |
| 122 | |
| 123 | _wrapper = testtools.testcase.attr(role)(wrapper) |
| 124 | return _wrapper |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 125 | return decorator |
Rick Bartra | 1299894 | 2017-03-17 17:35:45 -0400 | [diff] [blame] | 126 | |
| 127 | |
Felipe Monteiro | 78fc489 | 2017-04-12 21:33:39 +0100 | [diff] [blame] | 128 | def _is_authorized(test_obj, service, rule_name, extra_target_data): |
| 129 | try: |
| 130 | project_id = test_obj.auth_provider.credentials.project_id |
| 131 | user_id = test_obj.auth_provider.credentials.user_id |
| 132 | except AttributeError as e: |
| 133 | msg = ("{0}: project_id/user_id not found in " |
| 134 | "cls.auth_provider.credentials".format(e)) |
| 135 | LOG.error(msg) |
| 136 | raise rbac_exceptions.RbacResourceSetupFailed(msg) |
| 137 | |
| 138 | try: |
| 139 | role = CONF.rbac.rbac_test_role |
| 140 | formatted_target_data = _format_extra_target_data( |
| 141 | test_obj, extra_target_data) |
| 142 | policy_parser = rbac_policy_parser.RbacPolicyParser( |
| 143 | project_id, user_id, service, |
| 144 | extra_target_data=formatted_target_data) |
| 145 | is_allowed = policy_parser.allowed(rule_name, role) |
| 146 | |
| 147 | if is_allowed: |
| 148 | LOG.debug("[Action]: %s, [Role]: %s is allowed!", rule_name, |
| 149 | role) |
| 150 | else: |
| 151 | LOG.debug("[Action]: %s, [Role]: %s is NOT allowed!", |
| 152 | rule_name, role) |
| 153 | return is_allowed |
| 154 | except rbac_exceptions.RbacParsingException as e: |
| 155 | if CONF.rbac.strict_policy_check: |
| 156 | raise e |
| 157 | else: |
| 158 | raise testtools.TestCase.skipException(str(e)) |
| 159 | return False |
| 160 | |
| 161 | |
Rick Bartra | 1299894 | 2017-03-17 17:35:45 -0400 | [diff] [blame] | 162 | def _get_exception_type(expected_error_code): |
| 163 | expected_exception = None |
| 164 | irregular_msg = None |
| 165 | supported_error_codes = [403, 404] |
Felipe Monteiro | 78fc489 | 2017-04-12 21:33:39 +0100 | [diff] [blame] | 166 | |
Rick Bartra | 1299894 | 2017-03-17 17:35:45 -0400 | [diff] [blame] | 167 | if expected_error_code == 403: |
| 168 | expected_exception = exceptions.Forbidden |
| 169 | elif expected_error_code == 404: |
| 170 | expected_exception = exceptions.NotFound |
| 171 | irregular_msg = ("NotFound exception was caught for policy action " |
| 172 | "{0}. The service {1} throws a 404 instead of a 403, " |
| 173 | "which is irregular.") |
| 174 | else: |
| 175 | msg = ("Please pass an expected error code. Currently " |
| 176 | "supported codes: {0}".format(str(supported_error_codes))) |
| 177 | LOG.error(msg) |
| 178 | raise rbac_exceptions.RbacInvalidErrorCode() |
| 179 | |
| 180 | return expected_exception, irregular_msg |
Felipe Monteiro | fd1db98 | 2017-04-13 21:19:41 +0100 | [diff] [blame] | 181 | |
| 182 | |
| 183 | def _format_extra_target_data(test_obj, extra_target_data): |
| 184 | """Formats the "extra_target_data" dictionary with correct test data. |
| 185 | |
| 186 | Before being formatted, "extra_target_data" is a dictionary that maps a |
| 187 | policy string like "trust.trustor_user_id" to a nested list of BaseTestCase |
| 188 | attributes. For example, the attribute list in: |
| 189 | |
| 190 | "trust.trustor_user_id": "os.auth_provider.credentials.user_id" |
| 191 | |
| 192 | is parsed by iteratively calling `getattr` until the value of "user_id" |
| 193 | is resolved. The resulting dictionary returns: |
| 194 | |
| 195 | "trust.trustor_user_id": "the user_id of the `primary` credential" |
| 196 | |
| 197 | :param test_obj: type BaseTestCase (tempest base test class) |
| 198 | :param extra_target_data: dictionary with unresolved string literals that |
| 199 | reference nested BaseTestCase attributes |
| 200 | :returns: dictionary with resolved BaseTestCase attributes |
| 201 | """ |
| 202 | attr_value = test_obj |
| 203 | formatted_target_data = {} |
| 204 | |
| 205 | for user_attribute, attr_string in extra_target_data.items(): |
| 206 | attrs = attr_string.split('.') |
| 207 | for attr in attrs: |
| 208 | attr_value = getattr(attr_value, attr) |
| 209 | formatted_target_data[user_attribute] = attr_value |
| 210 | |
| 211 | return formatted_target_data |