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