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 | |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 20 | from oslo_log import log as logging |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 21 | from oslo_policy import policy |
Felipe Monteiro | ae2ebab | 2017-03-23 22:49:06 +0000 | [diff] [blame] | 22 | import stevedore |
ghanshyam | 0df097d | 2017-08-08 09:28:17 +0300 | [diff] [blame] | 23 | from tempest import clients |
Rick Bartra | 503c557 | 2017-03-09 13:49:58 -0500 | [diff] [blame] | 24 | from tempest.common import credentials_factory as credentials |
Felipe Monteiro | 7be94e8 | 2017-07-26 02:17:08 +0100 | [diff] [blame] | 25 | from tempest import config |
Rick Bartra | 503c557 | 2017-03-09 13:49:58 -0500 | [diff] [blame] | 26 | |
Felipe Monteiro | 31e308e | 2018-05-22 12:05:10 -0700 | [diff] [blame] | 27 | from patrole_tempest_plugin.rbac_authority import RbacAuthority |
Felipe Monteiro | b059565 | 2017-01-23 16:51:58 -0500 | [diff] [blame] | 28 | from patrole_tempest_plugin import rbac_exceptions |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 29 | |
Felipe Monteiro | 7be94e8 | 2017-07-26 02:17:08 +0100 | [diff] [blame] | 30 | CONF = config.CONF |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 31 | LOG = logging.getLogger(__name__) |
| 32 | |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 33 | |
Felipe Monteiro | 88a5bab | 2017-08-31 04:00:32 +0100 | [diff] [blame] | 34 | class PolicyAuthority(RbacAuthority): |
Felipe Monteiro | f2b58d7 | 2017-08-31 22:40:36 +0100 | [diff] [blame] | 35 | """A class that uses ``oslo.policy`` for validating RBAC.""" |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 36 | |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 37 | def __init__(self, project_id, user_id, service, extra_target_data=None): |
Felipe Monteiro | f2b58d7 | 2017-08-31 22:40:36 +0100 | [diff] [blame] | 38 | """Initialization of Policy Authority class. |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 39 | |
Felipe Monteiro | f2b58d7 | 2017-08-31 22:40:36 +0100 | [diff] [blame] | 40 | Validates whether a test role can perform a policy action by querying |
| 41 | ``oslo.policy`` with necessary test data. |
Felipe Monteiro | 9c97850 | 2017-01-27 17:07:54 -0500 | [diff] [blame] | 42 | |
Felipe Monteiro | f2b58d7 | 2017-08-31 22:40:36 +0100 | [diff] [blame] | 43 | If a policy file does not exist, checks whether the policy file is |
| 44 | registered as a namespace under "oslo.policy.policies". Nova, for |
| 45 | example, doesn't use a policy file by default; its policies are |
| 46 | implemented in code and registered as "nova" under |
| 47 | "oslo.policy.policies". |
Felipe Monteiro | 9c97850 | 2017-01-27 17:07:54 -0500 | [diff] [blame] | 48 | |
Felipe Monteiro | f2b58d7 | 2017-08-31 22:40:36 +0100 | [diff] [blame] | 49 | If the policy file is not found in either code or in a policy file, |
| 50 | then an exception is raised. |
| 51 | |
| 52 | Additionally, if a custom policy file exists along with the default |
| 53 | policy in code implementation, the custom policy is prioritized. |
Felipe Monteiro | b059565 | 2017-01-23 16:51:58 -0500 | [diff] [blame] | 54 | |
Felipe Monteiro | 3ab2c35 | 2017-07-05 22:25:34 +0100 | [diff] [blame] | 55 | :param uuid project_id: project_id of object performing API call |
| 56 | :param uuid user_id: user_id of object performing API call |
| 57 | :param string service: service of the policy file |
| 58 | :param dict extra_target_data: dictionary containing additional object |
| 59 | data needed by oslo.policy to validate generic checks |
Felipe Monteiro | f2b58d7 | 2017-08-31 22:40:36 +0100 | [diff] [blame] | 60 | |
| 61 | Example: |
| 62 | |
| 63 | .. code-block:: python |
| 64 | |
| 65 | # Below is the default policy implementation in code, defined in |
| 66 | # a service like Nova. |
| 67 | test_policies = [ |
| 68 | policy.DocumentedRuleDefault( |
| 69 | 'service:test_rule', |
| 70 | base.RULE_ADMIN_OR_OWNER, |
| 71 | "This is a description for a test policy", |
| 72 | [ |
| 73 | { |
| 74 | 'method': 'POST', |
| 75 | 'path': '/path/to/test/resource' |
| 76 | } |
| 77 | ]), |
| 78 | 'service:another_test_rule', |
| 79 | base.RULE_ADMIN_OR_OWNER, |
| 80 | "This is a description for another test policy", |
| 81 | [ |
| 82 | { |
| 83 | 'method': 'GET', |
| 84 | 'path': '/path/to/test/resource' |
| 85 | } |
| 86 | ]), |
| 87 | ] |
| 88 | |
| 89 | .. code-block:: yaml |
| 90 | |
| 91 | # Below is the custom override of the default policy in a YAML |
| 92 | # policy file. Note that the default rule is "rule:admin_or_owner" |
| 93 | # and the custom rule is "rule:admin_api". The `PolicyAuthority` |
| 94 | # class will use the "rule:admin_api" definition for this policy |
| 95 | # action. |
| 96 | "service:test_rule" : "rule:admin_api" |
| 97 | |
| 98 | # Note below that no override is provided for |
| 99 | # "service:another_test_rule", which means that the default policy |
| 100 | # rule is used: "rule:admin_or_owner". |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 101 | """ |
Felipe Monteiro | ae2ebab | 2017-03-23 22:49:06 +0000 | [diff] [blame] | 102 | |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 103 | if extra_target_data is None: |
| 104 | extra_target_data = {} |
| 105 | |
Felipe Monteiro | d9607c4 | 2017-06-12 19:28:45 +0100 | [diff] [blame] | 106 | self.validate_service(service) |
Rick Bartra | 503c557 | 2017-03-09 13:49:58 -0500 | [diff] [blame] | 107 | |
Felipe Monteiro | 3ab2c35 | 2017-07-05 22:25:34 +0100 | [diff] [blame] | 108 | # Prioritize dynamically searching for policy files over relying on |
| 109 | # deprecated service-specific policy file locations. |
Felipe Monteiro | bbd6a3c | 2017-11-01 01:57:49 +0000 | [diff] [blame] | 110 | self.path = None |
Felipe Monteiro | f6eb862 | 2017-08-06 06:08:02 +0100 | [diff] [blame] | 111 | if CONF.patrole.custom_policy_files: |
Felipe Monteiro | 3ab2c35 | 2017-07-05 22:25:34 +0100 | [diff] [blame] | 112 | self.discover_policy_files() |
| 113 | self.path = self.policy_files.get(service) |
Felipe Monteiro | 3ab2c35 | 2017-07-05 22:25:34 +0100 | [diff] [blame] | 114 | |
Felipe Monteiro | ae2ebab | 2017-03-23 22:49:06 +0000 | [diff] [blame] | 115 | self.rules = policy.Rules.load(self._get_policy_data(service), |
| 116 | 'default') |
Felipe Monteiro | fd1db98 | 2017-04-13 21:19:41 +0100 | [diff] [blame] | 117 | self.project_id = project_id |
Felipe Monteiro | 889264e | 2017-03-01 17:19:35 -0500 | [diff] [blame] | 118 | self.user_id = user_id |
Felipe Monteiro | fd1db98 | 2017-04-13 21:19:41 +0100 | [diff] [blame] | 119 | self.extra_target_data = extra_target_data |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 120 | |
Felipe Monteiro | d9607c4 | 2017-06-12 19:28:45 +0100 | [diff] [blame] | 121 | @classmethod |
| 122 | def validate_service(cls, service): |
Felipe Monteiro | 3ab2c35 | 2017-07-05 22:25:34 +0100 | [diff] [blame] | 123 | """Validate whether the service passed to ``__init__`` exists.""" |
Felipe Monteiro | d9607c4 | 2017-06-12 19:28:45 +0100 | [diff] [blame] | 124 | service = service.lower().strip() if service else None |
| 125 | |
| 126 | # Cache the list of available services in memory to avoid needlessly |
| 127 | # doing an API call every time. |
| 128 | if not hasattr(cls, 'available_services'): |
ghanshyam | 0df097d | 2017-08-08 09:28:17 +0300 | [diff] [blame] | 129 | admin_mgr = clients.Manager( |
| 130 | credentials.get_configured_admin_credentials()) |
Felipe Monteiro | 7be94e8 | 2017-07-26 02:17:08 +0100 | [diff] [blame] | 131 | services_client = (admin_mgr.identity_services_v3_client |
| 132 | if CONF.identity_feature_enabled.api_v3 |
| 133 | else admin_mgr.identity_services_client) |
| 134 | services = services_client.list_services()['services'] |
Felipe Monteiro | d9607c4 | 2017-06-12 19:28:45 +0100 | [diff] [blame] | 135 | cls.available_services = [s['name'] for s in services] |
| 136 | |
| 137 | if not service or service not in cls.available_services: |
| 138 | LOG.debug("%s is NOT a valid service.", service) |
Felipe Monteiro | 51299a1 | 2018-06-28 20:03:27 -0400 | [diff] [blame] | 139 | raise rbac_exceptions.RbacInvalidServiceException( |
Felipe Monteiro | d9607c4 | 2017-06-12 19:28:45 +0100 | [diff] [blame] | 140 | "%s is NOT a valid service." % service) |
| 141 | |
Felipe Monteiro | 3ab2c35 | 2017-07-05 22:25:34 +0100 | [diff] [blame] | 142 | @classmethod |
| 143 | def discover_policy_files(cls): |
Felipe Monteiro | f2b58d7 | 2017-08-31 22:40:36 +0100 | [diff] [blame] | 144 | """Dynamically discover the policy file for each service in |
| 145 | ``cls.available_services``. Pick the first candidate path found |
| 146 | out of the potential paths in ``[patrole] custom_policy_files``. |
| 147 | """ |
Felipe Monteiro | 3ab2c35 | 2017-07-05 22:25:34 +0100 | [diff] [blame] | 148 | if not hasattr(cls, 'policy_files'): |
| 149 | cls.policy_files = {} |
| 150 | for service in cls.available_services: |
Felipe Monteiro | f6eb862 | 2017-08-06 06:08:02 +0100 | [diff] [blame] | 151 | for candidate_path in CONF.patrole.custom_policy_files: |
Felipe Monteiro | 3ab2c35 | 2017-07-05 22:25:34 +0100 | [diff] [blame] | 152 | if os.path.isfile(candidate_path % service): |
| 153 | cls.policy_files.setdefault(service, |
| 154 | candidate_path % service) |
| 155 | |
Felipe Monteiro | b059565 | 2017-01-23 16:51:58 -0500 | [diff] [blame] | 156 | def allowed(self, rule_name, role): |
Felipe Monteiro | f2b58d7 | 2017-08-31 22:40:36 +0100 | [diff] [blame] | 157 | """Checks if a given rule in a policy is allowed with given role. |
| 158 | |
Felipe Monteiro | 778b780 | 2018-05-31 19:52:58 -0400 | [diff] [blame] | 159 | :param string rule_name: Policy name to pass to``oslo.policy``. |
| 160 | :param string role: Role to validate for authorization. |
| 161 | :raises RbacParsingException: If ``rule_name`` does not exist in the |
Felipe Monteiro | 4ef7e53 | 2018-03-11 07:17:11 -0400 | [diff] [blame] | 162 | cloud (in policy file or among registered in-code policy defaults). |
Felipe Monteiro | f2b58d7 | 2017-08-31 22:40:36 +0100 | [diff] [blame] | 163 | """ |
Felipe Monteiro | 9c97850 | 2017-01-27 17:07:54 -0500 | [diff] [blame] | 164 | is_admin_context = self._is_admin_context(role) |
Felipe Monteiro | b059565 | 2017-01-23 16:51:58 -0500 | [diff] [blame] | 165 | is_allowed = self._allowed( |
Felipe Monteiro | 9c97850 | 2017-01-27 17:07:54 -0500 | [diff] [blame] | 166 | access=self._get_access_token(role), |
Felipe Monteiro | b059565 | 2017-01-23 16:51:58 -0500 | [diff] [blame] | 167 | apply_rule=rule_name, |
Felipe Monteiro | 9c97850 | 2017-01-27 17:07:54 -0500 | [diff] [blame] | 168 | is_admin=is_admin_context) |
Felipe Monteiro | 9c97850 | 2017-01-27 17:07:54 -0500 | [diff] [blame] | 169 | return is_allowed |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 170 | |
Felipe Monteiro | ae2ebab | 2017-03-23 22:49:06 +0000 | [diff] [blame] | 171 | def _get_policy_data(self, service): |
| 172 | file_policy_data = {} |
| 173 | mgr_policy_data = {} |
| 174 | policy_data = {} |
| 175 | |
Felipe Monteiro | 3ab2c35 | 2017-07-05 22:25:34 +0100 | [diff] [blame] | 176 | # Check whether policy file exists and attempt to read it. |
| 177 | if self.path and os.path.isfile(self.path): |
Felipe Monteiro | ae2ebab | 2017-03-23 22:49:06 +0000 | [diff] [blame] | 178 | try: |
Samantha Blanco | 85f79d7 | 2017-04-21 11:09:14 -0400 | [diff] [blame] | 179 | with open(self.path, 'r') as policy_file: |
| 180 | file_policy_data = policy_file.read() |
Felipe Monteiro | ae2ebab | 2017-03-23 22:49:06 +0000 | [diff] [blame] | 181 | file_policy_data = json.loads(file_policy_data) |
Samantha Blanco | 85f79d7 | 2017-04-21 11:09:14 -0400 | [diff] [blame] | 182 | except (IOError, ValueError) as e: |
| 183 | msg = "Failed to read policy file for service. " |
| 184 | if isinstance(e, IOError): |
| 185 | msg += "Please check that policy path exists." |
| 186 | else: |
| 187 | msg += "JSON may be improperly formatted." |
| 188 | LOG.debug(msg) |
| 189 | file_policy_data = {} |
Felipe Monteiro | ae2ebab | 2017-03-23 22:49:06 +0000 | [diff] [blame] | 190 | |
| 191 | # Check whether policy actions are defined in code. Nova and Keystone, |
| 192 | # for example, define their default policy actions in code. |
| 193 | mgr = stevedore.named.NamedExtensionManager( |
| 194 | 'oslo.policy.policies', |
| 195 | names=[service], |
| 196 | on_load_failure_callback=None, |
| 197 | invoke_on_load=True, |
| 198 | warn_on_missing_entrypoint=False) |
| 199 | |
| 200 | if mgr: |
| 201 | policy_generator = {policy.name: policy.obj for policy in mgr} |
| 202 | if policy_generator and service in policy_generator: |
| 203 | for rule in policy_generator[service]: |
| 204 | mgr_policy_data[rule.name] = str(rule.check) |
| 205 | |
| 206 | # If data from both file and code exist, combine both together. |
| 207 | if file_policy_data and mgr_policy_data: |
| 208 | # Add the policy actions from code first. |
| 209 | for action, rule in mgr_policy_data.items(): |
| 210 | policy_data[action] = rule |
| 211 | # Overwrite with any custom policy actions defined in policy.json. |
| 212 | for action, rule in file_policy_data.items(): |
| 213 | policy_data[action] = rule |
| 214 | elif file_policy_data: |
| 215 | policy_data = file_policy_data |
| 216 | elif mgr_policy_data: |
| 217 | policy_data = mgr_policy_data |
| 218 | else: |
Felipe Monteiro | 3ab2c35 | 2017-07-05 22:25:34 +0100 | [diff] [blame] | 219 | error_message = ( |
Felipe Monteiro | 4ef7e53 | 2018-03-11 07:17:11 -0400 | [diff] [blame] | 220 | 'Policy file for {0} service was not found among the ' |
| 221 | 'registered in-code policies or in any of the possible policy ' |
| 222 | 'files: {1}.'.format(service, |
| 223 | [loc % service for loc in |
| 224 | CONF.patrole.custom_policy_files]) |
Felipe Monteiro | 3ab2c35 | 2017-07-05 22:25:34 +0100 | [diff] [blame] | 225 | ) |
Felipe Monteiro | ae2ebab | 2017-03-23 22:49:06 +0000 | [diff] [blame] | 226 | raise rbac_exceptions.RbacParsingException(error_message) |
| 227 | |
| 228 | try: |
| 229 | policy_data = json.dumps(policy_data) |
Felipe Monteiro | b580963 | 2017-10-26 04:12:12 +0100 | [diff] [blame] | 230 | except (TypeError, ValueError): |
Felipe Monteiro | ae2ebab | 2017-03-23 22:49:06 +0000 | [diff] [blame] | 231 | error_message = 'Policy file for {0} service is invalid.'.format( |
| 232 | service) |
| 233 | raise rbac_exceptions.RbacParsingException(error_message) |
| 234 | |
| 235 | return policy_data |
| 236 | |
Felipe Monteiro | 9c97850 | 2017-01-27 17:07:54 -0500 | [diff] [blame] | 237 | def _is_admin_context(self, role): |
| 238 | """Checks whether a role has admin context. |
| 239 | |
| 240 | If context_is_admin is contained in the policy file, then checks |
| 241 | whether the given role is contained in context_is_admin. If it is not |
| 242 | in the policy file, then default to context_is_admin: admin. |
| 243 | """ |
| 244 | if 'context_is_admin' in self.rules.keys(): |
| 245 | return self._allowed( |
| 246 | access=self._get_access_token(role), |
| 247 | apply_rule='context_is_admin') |
Felipe Monteiro | f6b69e2 | 2017-05-04 21:55:04 +0100 | [diff] [blame] | 248 | return role == CONF.identity.admin_role |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 249 | |
Felipe Monteiro | b059565 | 2017-01-23 16:51:58 -0500 | [diff] [blame] | 250 | def _get_access_token(self, role): |
| 251 | access_token = { |
| 252 | "token": { |
| 253 | "roles": [ |
| 254 | { |
| 255 | "name": role |
| 256 | } |
| 257 | ], |
Felipe Monteiro | fd1db98 | 2017-04-13 21:19:41 +0100 | [diff] [blame] | 258 | "project_id": self.project_id, |
| 259 | "tenant_id": self.project_id, |
Felipe Monteiro | 889264e | 2017-03-01 17:19:35 -0500 | [diff] [blame] | 260 | "user_id": self.user_id |
Felipe Monteiro | b059565 | 2017-01-23 16:51:58 -0500 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | return access_token |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 264 | |
Felipe Monteiro | 9c97850 | 2017-01-27 17:07:54 -0500 | [diff] [blame] | 265 | def _allowed(self, access, apply_rule, is_admin=False): |
Felipe Monteiro | f2b58d7 | 2017-08-31 22:40:36 +0100 | [diff] [blame] | 266 | """Checks if a given rule in a policy is allowed with given ``access``. |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 267 | |
Felipe Monteiro | f2b58d7 | 2017-08-31 22:40:36 +0100 | [diff] [blame] | 268 | :param dict access: Dictionary from ``_get_access_token``. |
| 269 | :param string apply_rule: Rule to be checked using ``oslo.policy``. |
| 270 | :param bool is_admin: Whether admin context is used. |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 271 | """ |
Felipe Monteiro | b059565 | 2017-01-23 16:51:58 -0500 | [diff] [blame] | 272 | access_data = copy.copy(access['token']) |
| 273 | access_data['roles'] = [role['name'] for role in access_data['roles']] |
Felipe Monteiro | b059565 | 2017-01-23 16:51:58 -0500 | [diff] [blame] | 274 | access_data['is_admin'] = is_admin |
Felipe Monteiro | 9c97850 | 2017-01-27 17:07:54 -0500 | [diff] [blame] | 275 | # TODO(felipemonteiro): Dynamically calculate is_admin_project rather |
| 276 | # than hard-coding it to True. is_admin_project cannot be determined |
Felipe Monteiro | 94fc2ca | 2018-05-22 12:08:43 -0700 | [diff] [blame] | 277 | # from the role, but rather from project and domain names. For more |
| 278 | # information, see: |
| 279 | # https://github.com/openstack/keystone/blob/37ce5417418f8acbd27f3dacb70c605b0fe48301/keystone/token/providers/common.py#L150 |
Felipe Monteiro | 9c97850 | 2017-01-27 17:07:54 -0500 | [diff] [blame] | 280 | access_data['is_admin_project'] = True |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 281 | |
Felipe Monteiro | b059565 | 2017-01-23 16:51:58 -0500 | [diff] [blame] | 282 | class Object(object): |
| 283 | pass |
| 284 | o = Object() |
Felipe Monteiro | 9c97850 | 2017-01-27 17:07:54 -0500 | [diff] [blame] | 285 | o.rules = self.rules |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 286 | |
Felipe Monteiro | 9fc782e | 2017-02-01 15:38:46 -0500 | [diff] [blame] | 287 | target = {"project_id": access_data['project_id'], |
| 288 | "tenant_id": access_data['project_id'], |
Felipe Monteiro | 889264e | 2017-03-01 17:19:35 -0500 | [diff] [blame] | 289 | "network:tenant_id": access_data['project_id'], |
| 290 | "user_id": access_data['user_id']} |
Felipe Monteiro | fd1db98 | 2017-04-13 21:19:41 +0100 | [diff] [blame] | 291 | if self.extra_target_data: |
| 292 | target.update(self.extra_target_data) |
Felipe Monteiro | b059565 | 2017-01-23 16:51:58 -0500 | [diff] [blame] | 293 | |
Felipe Monteiro | 9c97850 | 2017-01-27 17:07:54 -0500 | [diff] [blame] | 294 | result = self._try_rule(apply_rule, target, access_data, o) |
Felipe Monteiro | b059565 | 2017-01-23 16:51:58 -0500 | [diff] [blame] | 295 | return result |
| 296 | |
Felipe Monteiro | 9c97850 | 2017-01-27 17:07:54 -0500 | [diff] [blame] | 297 | def _try_rule(self, apply_rule, target, access_data, o): |
Samantha Blanco | 0d88008 | 2017-03-23 18:14:37 -0400 | [diff] [blame] | 298 | if apply_rule not in self.rules: |
Felipe Monteiro | 398a09f | 2018-01-23 01:39:25 +0000 | [diff] [blame] | 299 | message = ("Policy action \"{0}\" not found in policy file: {1} or" |
| 300 | " among registered policy in code defaults for service." |
| 301 | ).format(apply_rule, self.path) |
Felipe Monteiro | 48c913d | 2017-03-15 12:07:48 -0400 | [diff] [blame] | 302 | LOG.debug(message) |
| 303 | raise rbac_exceptions.RbacParsingException(message) |
Samantha Blanco | 0d88008 | 2017-03-23 18:14:37 -0400 | [diff] [blame] | 304 | else: |
| 305 | rule = self.rules[apply_rule] |
| 306 | return rule(target, access_data, o) |