blob: 0a1aa13349b96cdc7da0877497b0f8752e41cb8f [file] [log] [blame]
DavidPurcellb25f93d2017-01-27 12:46:27 -05001# Copyright 2017 AT&T Corporation.
DavidPurcell029d8c32017-01-06 15:27:41 -05002# 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 Monteirob0595652017-01-23 16:51:58 -050016import copy
Felipe Monteiroae2ebab2017-03-23 22:49:06 +000017import json
DavidPurcell029d8c32017-01-06 15:27:41 -050018import os
19
DavidPurcell029d8c32017-01-06 15:27:41 -050020from oslo_log import log as logging
DavidPurcell029d8c32017-01-06 15:27:41 -050021from oslo_policy import policy
Felipe Monteiroae2ebab2017-03-23 22:49:06 +000022import stevedore
ghanshyam0df097d2017-08-08 09:28:17 +030023from tempest import clients
Rick Bartra503c5572017-03-09 13:49:58 -050024from tempest.common import credentials_factory as credentials
Felipe Monteiro7be94e82017-07-26 02:17:08 +010025from tempest import config
Rick Bartra503c5572017-03-09 13:49:58 -050026
Felipe Monteirob0595652017-01-23 16:51:58 -050027from patrole_tempest_plugin import rbac_exceptions
Rick Bartraed950052017-06-29 17:20:33 -040028from patrole_tempest_plugin.rbac_utils import RbacAuthority
DavidPurcell029d8c32017-01-06 15:27:41 -050029
Felipe Monteiro7be94e82017-07-26 02:17:08 +010030CONF = config.CONF
DavidPurcell029d8c32017-01-06 15:27:41 -050031LOG = logging.getLogger(__name__)
32
DavidPurcell029d8c32017-01-06 15:27:41 -050033
Felipe Monteiro88a5bab2017-08-31 04:00:32 +010034class PolicyAuthority(RbacAuthority):
Felipe Monteirof2b58d72017-08-31 22:40:36 +010035 """A class that uses ``oslo.policy`` for validating RBAC."""
DavidPurcell029d8c32017-01-06 15:27:41 -050036
Felipe Monteiro0854ded2017-05-05 16:30:55 +010037 def __init__(self, project_id, user_id, service, extra_target_data=None):
Felipe Monteirof2b58d72017-08-31 22:40:36 +010038 """Initialization of Policy Authority class.
DavidPurcell029d8c32017-01-06 15:27:41 -050039
Felipe Monteirof2b58d72017-08-31 22:40:36 +010040 Validates whether a test role can perform a policy action by querying
41 ``oslo.policy`` with necessary test data.
Felipe Monteiro9c978502017-01-27 17:07:54 -050042
Felipe Monteirof2b58d72017-08-31 22:40:36 +010043 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 Monteiro9c978502017-01-27 17:07:54 -050048
Felipe Monteirof2b58d72017-08-31 22:40:36 +010049 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 Monteirob0595652017-01-23 16:51:58 -050054
Felipe Monteiro3ab2c352017-07-05 22:25:34 +010055 :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 Monteirof2b58d72017-08-31 22:40:36 +010060
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".
DavidPurcell029d8c32017-01-06 15:27:41 -0500101 """
Felipe Monteiroae2ebab2017-03-23 22:49:06 +0000102
Felipe Monteiro0854ded2017-05-05 16:30:55 +0100103 if extra_target_data is None:
104 extra_target_data = {}
105
Felipe Monteirod9607c42017-06-12 19:28:45 +0100106 self.validate_service(service)
Rick Bartra503c5572017-03-09 13:49:58 -0500107
Felipe Monteiro3ab2c352017-07-05 22:25:34 +0100108 # Prioritize dynamically searching for policy files over relying on
109 # deprecated service-specific policy file locations.
Felipe Monteirof6eb8622017-08-06 06:08:02 +0100110 if CONF.patrole.custom_policy_files:
Felipe Monteiro3ab2c352017-07-05 22:25:34 +0100111 self.discover_policy_files()
112 self.path = self.policy_files.get(service)
113 else:
Felipe Monteirof6eb8622017-08-06 06:08:02 +0100114 self.path = getattr(CONF.patrole, '%s_policy_file' % str(service),
Felipe Monteiro3ab2c352017-07-05 22:25:34 +0100115 None)
116
Felipe Monteiroae2ebab2017-03-23 22:49:06 +0000117 self.rules = policy.Rules.load(self._get_policy_data(service),
118 'default')
Felipe Monteirofd1db982017-04-13 21:19:41 +0100119 self.project_id = project_id
Felipe Monteiro889264e2017-03-01 17:19:35 -0500120 self.user_id = user_id
Felipe Monteirofd1db982017-04-13 21:19:41 +0100121 self.extra_target_data = extra_target_data
DavidPurcell029d8c32017-01-06 15:27:41 -0500122
Felipe Monteirod9607c42017-06-12 19:28:45 +0100123 @classmethod
124 def validate_service(cls, service):
Felipe Monteiro3ab2c352017-07-05 22:25:34 +0100125 """Validate whether the service passed to ``__init__`` exists."""
Felipe Monteirod9607c42017-06-12 19:28:45 +0100126 service = service.lower().strip() if service else None
127
128 # Cache the list of available services in memory to avoid needlessly
129 # doing an API call every time.
130 if not hasattr(cls, 'available_services'):
ghanshyam0df097d2017-08-08 09:28:17 +0300131 admin_mgr = clients.Manager(
132 credentials.get_configured_admin_credentials())
Felipe Monteiro7be94e82017-07-26 02:17:08 +0100133 services_client = (admin_mgr.identity_services_v3_client
134 if CONF.identity_feature_enabled.api_v3
135 else admin_mgr.identity_services_client)
136 services = services_client.list_services()['services']
Felipe Monteirod9607c42017-06-12 19:28:45 +0100137 cls.available_services = [s['name'] for s in services]
138
139 if not service or service not in cls.available_services:
140 LOG.debug("%s is NOT a valid service.", service)
141 raise rbac_exceptions.RbacInvalidService(
142 "%s is NOT a valid service." % service)
143
Felipe Monteiro3ab2c352017-07-05 22:25:34 +0100144 @classmethod
145 def discover_policy_files(cls):
Felipe Monteirof2b58d72017-08-31 22:40:36 +0100146 """Dynamically discover the policy file for each service in
147 ``cls.available_services``. Pick the first candidate path found
148 out of the potential paths in ``[patrole] custom_policy_files``.
149 """
Felipe Monteiro3ab2c352017-07-05 22:25:34 +0100150 if not hasattr(cls, 'policy_files'):
151 cls.policy_files = {}
152 for service in cls.available_services:
Felipe Monteirof6eb8622017-08-06 06:08:02 +0100153 for candidate_path in CONF.patrole.custom_policy_files:
Felipe Monteiro3ab2c352017-07-05 22:25:34 +0100154 if os.path.isfile(candidate_path % service):
155 cls.policy_files.setdefault(service,
156 candidate_path % service)
157
Felipe Monteirob0595652017-01-23 16:51:58 -0500158 def allowed(self, rule_name, role):
Felipe Monteirof2b58d72017-08-31 22:40:36 +0100159 """Checks if a given rule in a policy is allowed with given role.
160
161 :param string rule_name: Rule to be checked using ``oslo.policy``.
162 :param bool is_admin: Whether admin context is used.
163 """
Felipe Monteiro9c978502017-01-27 17:07:54 -0500164 is_admin_context = self._is_admin_context(role)
Felipe Monteirob0595652017-01-23 16:51:58 -0500165 is_allowed = self._allowed(
Felipe Monteiro9c978502017-01-27 17:07:54 -0500166 access=self._get_access_token(role),
Felipe Monteirob0595652017-01-23 16:51:58 -0500167 apply_rule=rule_name,
Felipe Monteiro9c978502017-01-27 17:07:54 -0500168 is_admin=is_admin_context)
Felipe Monteiro9c978502017-01-27 17:07:54 -0500169 return is_allowed
DavidPurcell029d8c32017-01-06 15:27:41 -0500170
Felipe Monteiroae2ebab2017-03-23 22:49:06 +0000171 def _get_policy_data(self, service):
172 file_policy_data = {}
173 mgr_policy_data = {}
174 policy_data = {}
175
Felipe Monteiro3ab2c352017-07-05 22:25:34 +0100176 # Check whether policy file exists and attempt to read it.
177 if self.path and os.path.isfile(self.path):
Felipe Monteiroae2ebab2017-03-23 22:49:06 +0000178 try:
Samantha Blanco85f79d72017-04-21 11:09:14 -0400179 with open(self.path, 'r') as policy_file:
180 file_policy_data = policy_file.read()
Felipe Monteiroae2ebab2017-03-23 22:49:06 +0000181 file_policy_data = json.loads(file_policy_data)
Samantha Blanco85f79d72017-04-21 11:09:14 -0400182 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 Monteiroae2ebab2017-03-23 22:49:06 +0000190
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 Monteiro3ab2c352017-07-05 22:25:34 +0100219 error_message = (
220 'Policy file for {0} service neither found in code nor at {1}.'
221 .format(service, [loc % service for loc in
Felipe Monteirof6eb8622017-08-06 06:08:02 +0100222 CONF.patrole.custom_policy_files])
Felipe Monteiro3ab2c352017-07-05 22:25:34 +0100223 )
Felipe Monteiroae2ebab2017-03-23 22:49:06 +0000224 raise rbac_exceptions.RbacParsingException(error_message)
225
226 try:
227 policy_data = json.dumps(policy_data)
Felipe Monteirob5809632017-10-26 04:12:12 +0100228 except (TypeError, ValueError):
Felipe Monteiroae2ebab2017-03-23 22:49:06 +0000229 error_message = 'Policy file for {0} service is invalid.'.format(
230 service)
231 raise rbac_exceptions.RbacParsingException(error_message)
232
233 return policy_data
234
Felipe Monteiro9c978502017-01-27 17:07:54 -0500235 def _is_admin_context(self, role):
236 """Checks whether a role has admin context.
237
238 If context_is_admin is contained in the policy file, then checks
239 whether the given role is contained in context_is_admin. If it is not
240 in the policy file, then default to context_is_admin: admin.
241 """
242 if 'context_is_admin' in self.rules.keys():
243 return self._allowed(
244 access=self._get_access_token(role),
245 apply_rule='context_is_admin')
Felipe Monteirof6b69e22017-05-04 21:55:04 +0100246 return role == CONF.identity.admin_role
DavidPurcell029d8c32017-01-06 15:27:41 -0500247
Felipe Monteirob0595652017-01-23 16:51:58 -0500248 def _get_access_token(self, role):
249 access_token = {
250 "token": {
251 "roles": [
252 {
253 "name": role
254 }
255 ],
Felipe Monteirofd1db982017-04-13 21:19:41 +0100256 "project_id": self.project_id,
257 "tenant_id": self.project_id,
Felipe Monteiro889264e2017-03-01 17:19:35 -0500258 "user_id": self.user_id
Felipe Monteirob0595652017-01-23 16:51:58 -0500259 }
260 }
261 return access_token
DavidPurcell029d8c32017-01-06 15:27:41 -0500262
Felipe Monteiro9c978502017-01-27 17:07:54 -0500263 def _allowed(self, access, apply_rule, is_admin=False):
Felipe Monteirof2b58d72017-08-31 22:40:36 +0100264 """Checks if a given rule in a policy is allowed with given ``access``.
DavidPurcell029d8c32017-01-06 15:27:41 -0500265
Felipe Monteirof2b58d72017-08-31 22:40:36 +0100266 :param dict access: Dictionary from ``_get_access_token``.
267 :param string apply_rule: Rule to be checked using ``oslo.policy``.
268 :param bool is_admin: Whether admin context is used.
DavidPurcell029d8c32017-01-06 15:27:41 -0500269 """
Felipe Monteirob0595652017-01-23 16:51:58 -0500270 access_data = copy.copy(access['token'])
271 access_data['roles'] = [role['name'] for role in access_data['roles']]
Felipe Monteirob0595652017-01-23 16:51:58 -0500272 access_data['is_admin'] = is_admin
Felipe Monteiro9c978502017-01-27 17:07:54 -0500273 # TODO(felipemonteiro): Dynamically calculate is_admin_project rather
274 # than hard-coding it to True. is_admin_project cannot be determined
275 # from the role, but rather from project and domain names. See
276 # _populate_is_admin_project in keystone.token.providers.common
277 # for more information.
278 access_data['is_admin_project'] = True
DavidPurcell029d8c32017-01-06 15:27:41 -0500279
Felipe Monteirob0595652017-01-23 16:51:58 -0500280 class Object(object):
281 pass
282 o = Object()
Felipe Monteiro9c978502017-01-27 17:07:54 -0500283 o.rules = self.rules
DavidPurcell029d8c32017-01-06 15:27:41 -0500284
Felipe Monteiro9fc782e2017-02-01 15:38:46 -0500285 target = {"project_id": access_data['project_id'],
286 "tenant_id": access_data['project_id'],
Felipe Monteiro889264e2017-03-01 17:19:35 -0500287 "network:tenant_id": access_data['project_id'],
288 "user_id": access_data['user_id']}
Felipe Monteirofd1db982017-04-13 21:19:41 +0100289 if self.extra_target_data:
290 target.update(self.extra_target_data)
Felipe Monteirob0595652017-01-23 16:51:58 -0500291
Felipe Monteiro9c978502017-01-27 17:07:54 -0500292 result = self._try_rule(apply_rule, target, access_data, o)
Felipe Monteirob0595652017-01-23 16:51:58 -0500293 return result
294
Felipe Monteiro9c978502017-01-27 17:07:54 -0500295 def _try_rule(self, apply_rule, target, access_data, o):
Samantha Blanco0d880082017-03-23 18:14:37 -0400296 if apply_rule not in self.rules:
Felipe Monteiro48c913d2017-03-15 12:07:48 -0400297 message = "Policy action: {0} not found in policy file: {1}."\
298 .format(apply_rule, self.path)
299 LOG.debug(message)
300 raise rbac_exceptions.RbacParsingException(message)
Samantha Blanco0d880082017-03-23 18:14:37 -0400301 else:
302 rule = self.rules[apply_rule]
303 return rule(target, access_data, o)