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 copy |
Felipe Monteiro | ae2ebab | 2017-03-23 22:49:06 +0000 | [diff] [blame] | 17 | import json |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 18 | import os |
| 19 | |
Felipe Monteiro | ae2ebab | 2017-03-23 22:49:06 +0000 | [diff] [blame] | 20 | from oslo_config import cfg |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 21 | from oslo_log import log as logging |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 22 | from oslo_policy import policy |
Felipe Monteiro | ae2ebab | 2017-03-23 22:49:06 +0000 | [diff] [blame] | 23 | import stevedore |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 24 | |
Rick Bartra | 503c557 | 2017-03-09 13:49:58 -0500 | [diff] [blame] | 25 | from tempest.common import credentials_factory as credentials |
| 26 | |
Felipe Monteiro | b059565 | 2017-01-23 16:51:58 -0500 | [diff] [blame] | 27 | from patrole_tempest_plugin import rbac_exceptions |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 28 | |
Felipe Monteiro | ae2ebab | 2017-03-23 22:49:06 +0000 | [diff] [blame] | 29 | CONF = cfg.CONF |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 30 | LOG = logging.getLogger(__name__) |
| 31 | |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 32 | |
Felipe Monteiro | 322c5b6 | 2017-02-26 02:44:21 +0000 | [diff] [blame] | 33 | class RbacPolicyParser(object): |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 34 | """A class for parsing policy rules into lists of allowed roles. |
| 35 | |
| 36 | RBAC testing requires that each rule in a policy file be broken up into |
| 37 | the roles that constitute it. This class automates that process. |
Felipe Monteiro | b059565 | 2017-01-23 16:51:58 -0500 | [diff] [blame] | 38 | |
| 39 | The list of roles per rule can be reverse-engineered by checking, for |
| 40 | each role, whether a given rule is allowed using oslo policy. |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 41 | """ |
| 42 | |
Samantha Blanco | 85f79d7 | 2017-04-21 11:09:14 -0400 | [diff] [blame] | 43 | def __init__(self, project_id, user_id, service=None, |
Felipe Monteiro | fd1db98 | 2017-04-13 21:19:41 +0100 | [diff] [blame] | 44 | extra_target_data={}): |
Felipe Monteiro | 322c5b6 | 2017-02-26 02:44:21 +0000 | [diff] [blame] | 45 | """Initialization of Rbac Policy Parser. |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 46 | |
Felipe Monteiro | 9c97850 | 2017-01-27 17:07:54 -0500 | [diff] [blame] | 47 | Parses a policy file to create a dictionary, mapping policy actions to |
| 48 | roles. If a policy file does not exist, checks whether the policy file |
| 49 | is registered as a namespace under oslo.policy.policies. Nova, for |
| 50 | example, doesn't use a policy.json file by default; its policy is |
| 51 | implemented in code and registered as 'nova' under |
| 52 | oslo.policy.policies. |
| 53 | |
| 54 | If the policy file is not found in either place, raises an exception. |
| 55 | |
| 56 | Additionally, if the policy file exists in both code and as a |
| 57 | policy.json (for example, by creating a custom nova policy.json file), |
| 58 | the custom policy file over the default policy implementation is |
| 59 | prioritized. |
Felipe Monteiro | b059565 | 2017-01-23 16:51:58 -0500 | [diff] [blame] | 60 | |
Felipe Monteiro | fd1db98 | 2017-04-13 21:19:41 +0100 | [diff] [blame] | 61 | :param project_id: type uuid |
Felipe Monteiro | 889264e | 2017-03-01 17:19:35 -0500 | [diff] [blame] | 62 | :param user_id: type uuid |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 63 | :param service: type string |
| 64 | :param path: type string |
| 65 | """ |
Felipe Monteiro | ae2ebab | 2017-03-23 22:49:06 +0000 | [diff] [blame] | 66 | |
Rick Bartra | 503c557 | 2017-03-09 13:49:58 -0500 | [diff] [blame] | 67 | # First check if the service is valid |
| 68 | service = service.lower().strip() if service else None |
| 69 | self.admin_mgr = credentials.AdminManager() |
| 70 | services = self.admin_mgr.identity_services_v3_client.\ |
| 71 | list_services()['services'] |
| 72 | service_names = [s['name'] for s in services] |
| 73 | if not service or not any(service in name for name in service_names): |
| 74 | LOG.debug(str(service) + " is NOT a valid service.") |
| 75 | raise rbac_exceptions.RbacInvalidService |
| 76 | |
Felipe Monteiro | ae2ebab | 2017-03-23 22:49:06 +0000 | [diff] [blame] | 77 | # Use default path in /etc/<service_name/policy.json if no path |
| 78 | # is provided. |
Samantha Blanco | 85f79d7 | 2017-04-21 11:09:14 -0400 | [diff] [blame] | 79 | path = getattr(CONF.rbac, '%s_policy_file' % str(service), None) |
| 80 | if not path: |
| 81 | LOG.info("No config option found for %s," |
| 82 | " using default path" % str(service)) |
| 83 | path = os.path.join('/etc', service, 'policy.json') |
| 84 | self.path = path |
Felipe Monteiro | ae2ebab | 2017-03-23 22:49:06 +0000 | [diff] [blame] | 85 | self.rules = policy.Rules.load(self._get_policy_data(service), |
| 86 | 'default') |
Felipe Monteiro | fd1db98 | 2017-04-13 21:19:41 +0100 | [diff] [blame] | 87 | self.project_id = project_id |
Felipe Monteiro | 889264e | 2017-03-01 17:19:35 -0500 | [diff] [blame] | 88 | self.user_id = user_id |
Felipe Monteiro | fd1db98 | 2017-04-13 21:19:41 +0100 | [diff] [blame] | 89 | self.extra_target_data = extra_target_data |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 90 | |
Felipe Monteiro | b059565 | 2017-01-23 16:51:58 -0500 | [diff] [blame] | 91 | def allowed(self, rule_name, role): |
Felipe Monteiro | 9c97850 | 2017-01-27 17:07:54 -0500 | [diff] [blame] | 92 | is_admin_context = self._is_admin_context(role) |
Felipe Monteiro | b059565 | 2017-01-23 16:51:58 -0500 | [diff] [blame] | 93 | is_allowed = self._allowed( |
Felipe Monteiro | 9c97850 | 2017-01-27 17:07:54 -0500 | [diff] [blame] | 94 | access=self._get_access_token(role), |
Felipe Monteiro | b059565 | 2017-01-23 16:51:58 -0500 | [diff] [blame] | 95 | apply_rule=rule_name, |
Felipe Monteiro | 9c97850 | 2017-01-27 17:07:54 -0500 | [diff] [blame] | 96 | is_admin=is_admin_context) |
Felipe Monteiro | 9c97850 | 2017-01-27 17:07:54 -0500 | [diff] [blame] | 97 | return is_allowed |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 98 | |
Felipe Monteiro | ae2ebab | 2017-03-23 22:49:06 +0000 | [diff] [blame] | 99 | def _get_policy_data(self, service): |
| 100 | file_policy_data = {} |
| 101 | mgr_policy_data = {} |
| 102 | policy_data = {} |
| 103 | |
| 104 | # Check whether policy file exists. |
| 105 | if os.path.isfile(self.path): |
Felipe Monteiro | ae2ebab | 2017-03-23 22:49:06 +0000 | [diff] [blame] | 106 | try: |
Samantha Blanco | 85f79d7 | 2017-04-21 11:09:14 -0400 | [diff] [blame] | 107 | with open(self.path, 'r') as policy_file: |
| 108 | file_policy_data = policy_file.read() |
Felipe Monteiro | ae2ebab | 2017-03-23 22:49:06 +0000 | [diff] [blame] | 109 | file_policy_data = json.loads(file_policy_data) |
Samantha Blanco | 85f79d7 | 2017-04-21 11:09:14 -0400 | [diff] [blame] | 110 | except (IOError, ValueError) as e: |
| 111 | msg = "Failed to read policy file for service. " |
| 112 | if isinstance(e, IOError): |
| 113 | msg += "Please check that policy path exists." |
| 114 | else: |
| 115 | msg += "JSON may be improperly formatted." |
| 116 | LOG.debug(msg) |
| 117 | file_policy_data = {} |
Felipe Monteiro | ae2ebab | 2017-03-23 22:49:06 +0000 | [diff] [blame] | 118 | |
| 119 | # Check whether policy actions are defined in code. Nova and Keystone, |
| 120 | # for example, define their default policy actions in code. |
| 121 | mgr = stevedore.named.NamedExtensionManager( |
| 122 | 'oslo.policy.policies', |
| 123 | names=[service], |
| 124 | on_load_failure_callback=None, |
| 125 | invoke_on_load=True, |
| 126 | warn_on_missing_entrypoint=False) |
| 127 | |
| 128 | if mgr: |
| 129 | policy_generator = {policy.name: policy.obj for policy in mgr} |
| 130 | if policy_generator and service in policy_generator: |
| 131 | for rule in policy_generator[service]: |
| 132 | mgr_policy_data[rule.name] = str(rule.check) |
| 133 | |
| 134 | # If data from both file and code exist, combine both together. |
| 135 | if file_policy_data and mgr_policy_data: |
| 136 | # Add the policy actions from code first. |
| 137 | for action, rule in mgr_policy_data.items(): |
| 138 | policy_data[action] = rule |
| 139 | # Overwrite with any custom policy actions defined in policy.json. |
| 140 | for action, rule in file_policy_data.items(): |
| 141 | policy_data[action] = rule |
| 142 | elif file_policy_data: |
| 143 | policy_data = file_policy_data |
| 144 | elif mgr_policy_data: |
| 145 | policy_data = mgr_policy_data |
| 146 | else: |
| 147 | error_message = 'Policy file for {0} service neither found in '\ |
| 148 | 'code nor at {1}.'.format(service, self.path) |
| 149 | raise rbac_exceptions.RbacParsingException(error_message) |
| 150 | |
| 151 | try: |
| 152 | policy_data = json.dumps(policy_data) |
| 153 | except ValueError: |
| 154 | error_message = 'Policy file for {0} service is invalid.'.format( |
| 155 | service) |
| 156 | raise rbac_exceptions.RbacParsingException(error_message) |
| 157 | |
| 158 | return policy_data |
| 159 | |
Felipe Monteiro | 9c97850 | 2017-01-27 17:07:54 -0500 | [diff] [blame] | 160 | def _is_admin_context(self, role): |
| 161 | """Checks whether a role has admin context. |
| 162 | |
| 163 | If context_is_admin is contained in the policy file, then checks |
| 164 | whether the given role is contained in context_is_admin. If it is not |
| 165 | in the policy file, then default to context_is_admin: admin. |
| 166 | """ |
| 167 | if 'context_is_admin' in self.rules.keys(): |
| 168 | return self._allowed( |
| 169 | access=self._get_access_token(role), |
| 170 | apply_rule='context_is_admin') |
Felipe Monteiro | f6b69e2 | 2017-05-04 21:55:04 +0100 | [diff] [blame] | 171 | return role == CONF.identity.admin_role |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 172 | |
Felipe Monteiro | b059565 | 2017-01-23 16:51:58 -0500 | [diff] [blame] | 173 | def _get_access_token(self, role): |
| 174 | access_token = { |
| 175 | "token": { |
| 176 | "roles": [ |
| 177 | { |
| 178 | "name": role |
| 179 | } |
| 180 | ], |
Felipe Monteiro | fd1db98 | 2017-04-13 21:19:41 +0100 | [diff] [blame] | 181 | "project_id": self.project_id, |
| 182 | "tenant_id": self.project_id, |
Felipe Monteiro | 889264e | 2017-03-01 17:19:35 -0500 | [diff] [blame] | 183 | "user_id": self.user_id |
Felipe Monteiro | b059565 | 2017-01-23 16:51:58 -0500 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | return access_token |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 187 | |
Felipe Monteiro | 9c97850 | 2017-01-27 17:07:54 -0500 | [diff] [blame] | 188 | def _allowed(self, access, apply_rule, is_admin=False): |
Felipe Monteiro | b059565 | 2017-01-23 16:51:58 -0500 | [diff] [blame] | 189 | """Checks if a given rule in a policy is allowed with given access. |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 190 | |
Felipe Monteiro | b059565 | 2017-01-23 16:51:58 -0500 | [diff] [blame] | 191 | Adapted from oslo_policy.shell. |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 192 | |
Felipe Monteiro | b059565 | 2017-01-23 16:51:58 -0500 | [diff] [blame] | 193 | :param access: type dict: dictionary from ``_get_access_token`` |
| 194 | :param apply_rule: type string: rule to be checked |
| 195 | :param is_admin: type bool: whether admin context is used |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 196 | """ |
Felipe Monteiro | b059565 | 2017-01-23 16:51:58 -0500 | [diff] [blame] | 197 | access_data = copy.copy(access['token']) |
| 198 | access_data['roles'] = [role['name'] for role in access_data['roles']] |
Felipe Monteiro | b059565 | 2017-01-23 16:51:58 -0500 | [diff] [blame] | 199 | access_data['is_admin'] = is_admin |
Felipe Monteiro | 9c97850 | 2017-01-27 17:07:54 -0500 | [diff] [blame] | 200 | # TODO(felipemonteiro): Dynamically calculate is_admin_project rather |
| 201 | # than hard-coding it to True. is_admin_project cannot be determined |
| 202 | # from the role, but rather from project and domain names. See |
| 203 | # _populate_is_admin_project in keystone.token.providers.common |
| 204 | # for more information. |
| 205 | access_data['is_admin_project'] = True |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 206 | |
Felipe Monteiro | b059565 | 2017-01-23 16:51:58 -0500 | [diff] [blame] | 207 | class Object(object): |
| 208 | pass |
| 209 | o = Object() |
Felipe Monteiro | 9c97850 | 2017-01-27 17:07:54 -0500 | [diff] [blame] | 210 | o.rules = self.rules |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 211 | |
Felipe Monteiro | 9fc782e | 2017-02-01 15:38:46 -0500 | [diff] [blame] | 212 | target = {"project_id": access_data['project_id'], |
| 213 | "tenant_id": access_data['project_id'], |
Felipe Monteiro | 889264e | 2017-03-01 17:19:35 -0500 | [diff] [blame] | 214 | "network:tenant_id": access_data['project_id'], |
| 215 | "user_id": access_data['user_id']} |
Felipe Monteiro | fd1db98 | 2017-04-13 21:19:41 +0100 | [diff] [blame] | 216 | if self.extra_target_data: |
| 217 | target.update(self.extra_target_data) |
Felipe Monteiro | b059565 | 2017-01-23 16:51:58 -0500 | [diff] [blame] | 218 | |
Felipe Monteiro | 9c97850 | 2017-01-27 17:07:54 -0500 | [diff] [blame] | 219 | result = self._try_rule(apply_rule, target, access_data, o) |
Felipe Monteiro | b059565 | 2017-01-23 16:51:58 -0500 | [diff] [blame] | 220 | return result |
| 221 | |
Felipe Monteiro | 9c97850 | 2017-01-27 17:07:54 -0500 | [diff] [blame] | 222 | def _try_rule(self, apply_rule, target, access_data, o): |
Samantha Blanco | 0d88008 | 2017-03-23 18:14:37 -0400 | [diff] [blame] | 223 | if apply_rule not in self.rules: |
Felipe Monteiro | 48c913d | 2017-03-15 12:07:48 -0400 | [diff] [blame] | 224 | message = "Policy action: {0} not found in policy file: {1}."\ |
| 225 | .format(apply_rule, self.path) |
| 226 | LOG.debug(message) |
| 227 | raise rbac_exceptions.RbacParsingException(message) |
Samantha Blanco | 0d88008 | 2017-03-23 18:14:37 -0400 | [diff] [blame] | 228 | else: |
| 229 | rule = self.rules[apply_rule] |
| 230 | return rule(target, access_data, o) |