blob: fb0d2feb5d56905055f7212c225bf1a1b0dbb56c [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
DavidPurcell029d8c32017-01-06 15:27:41 -050017import os
18
DavidPurcell029d8c32017-01-06 15:27:41 -050019from oslo_log import log as logging
Felipe Monteiro9c978502017-01-27 17:07:54 -050020from oslo_policy import generator
DavidPurcell029d8c32017-01-06 15:27:41 -050021from oslo_policy import policy
22from tempest import config
23
Felipe Monteirob0595652017-01-23 16:51:58 -050024from patrole_tempest_plugin import rbac_exceptions
DavidPurcell029d8c32017-01-06 15:27:41 -050025
26CONF = config.CONF
27LOG = logging.getLogger(__name__)
28
DavidPurcell029d8c32017-01-06 15:27:41 -050029
30class RbacPolicyConverter(object):
31 """A class for parsing policy rules into lists of allowed roles.
32
33 RBAC testing requires that each rule in a policy file be broken up into
34 the roles that constitute it. This class automates that process.
Felipe Monteirob0595652017-01-23 16:51:58 -050035
36 The list of roles per rule can be reverse-engineered by checking, for
37 each role, whether a given rule is allowed using oslo policy.
DavidPurcell029d8c32017-01-06 15:27:41 -050038 """
39
Felipe Monteirob0595652017-01-23 16:51:58 -050040 def __init__(self, tenant_id, service, path=None):
41 """Initialization of Policy Converter.
DavidPurcell029d8c32017-01-06 15:27:41 -050042
Felipe Monteiro9c978502017-01-27 17:07:54 -050043 Parses a policy file to create a dictionary, mapping policy actions to
44 roles. If a policy file does not exist, checks whether the policy file
45 is registered as a namespace under oslo.policy.policies. Nova, for
46 example, doesn't use a policy.json file by default; its policy is
47 implemented in code and registered as 'nova' under
48 oslo.policy.policies.
49
50 If the policy file is not found in either place, raises an exception.
51
52 Additionally, if the policy file exists in both code and as a
53 policy.json (for example, by creating a custom nova policy.json file),
54 the custom policy file over the default policy implementation is
55 prioritized.
Felipe Monteirob0595652017-01-23 16:51:58 -050056
57 :param tenant_id: type uuid
DavidPurcell029d8c32017-01-06 15:27:41 -050058 :param service: type string
59 :param path: type string
60 """
Felipe Monteiro9c978502017-01-27 17:07:54 -050061 service = service.lower().strip()
DavidPurcell029d8c32017-01-06 15:27:41 -050062 if path is None:
Felipe Monteirob0595652017-01-23 16:51:58 -050063 self.path = '/etc/{0}/policy.json'.format(service)
64 else:
65 self.path = path
DavidPurcell029d8c32017-01-06 15:27:41 -050066
Felipe Monteiro9c978502017-01-27 17:07:54 -050067 policy_data = "{}"
DavidPurcell029d8c32017-01-06 15:27:41 -050068
Felipe Monteiro9c978502017-01-27 17:07:54 -050069 # First check whether policy file exists.
70 if os.path.isfile(self.path):
71 policy_data = open(self.path, 'r').read()
72 # Otherwise use oslo_policy to fetch the rules for provided service.
73 else:
74 policy_generator = generator._get_policies_dict([service])
75 if policy_generator and service in policy_generator:
76 policy_data = "{\n"
77 for r in policy_generator[service]:
78 policy_data = policy_data + r.__str__() + ",\n"
79 policy_data = policy_data[:-2] + "\n}"
80 # Otherwise raise an exception.
81 else:
82 raise rbac_exceptions.RbacResourceSetupFailed(
83 'Policy file for service: {0}, {1} not found.'
84 .format(service, self.path))
85
86 self.rules = policy.Rules.load(policy_data, "default")
Felipe Monteirob0595652017-01-23 16:51:58 -050087 self.tenant_id = tenant_id
DavidPurcell029d8c32017-01-06 15:27:41 -050088
Felipe Monteirob0595652017-01-23 16:51:58 -050089 def allowed(self, rule_name, role):
Felipe Monteiro9c978502017-01-27 17:07:54 -050090 is_admin_context = self._is_admin_context(role)
Felipe Monteirob0595652017-01-23 16:51:58 -050091 is_allowed = self._allowed(
Felipe Monteiro9c978502017-01-27 17:07:54 -050092 access=self._get_access_token(role),
Felipe Monteirob0595652017-01-23 16:51:58 -050093 apply_rule=rule_name,
Felipe Monteiro9c978502017-01-27 17:07:54 -050094 is_admin=is_admin_context)
DavidPurcell029d8c32017-01-06 15:27:41 -050095
Felipe Monteiro9c978502017-01-27 17:07:54 -050096 return is_allowed
DavidPurcell029d8c32017-01-06 15:27:41 -050097
Felipe Monteiro9c978502017-01-27 17:07:54 -050098 def _is_admin_context(self, role):
99 """Checks whether a role has admin context.
100
101 If context_is_admin is contained in the policy file, then checks
102 whether the given role is contained in context_is_admin. If it is not
103 in the policy file, then default to context_is_admin: admin.
104 """
105 if 'context_is_admin' in self.rules.keys():
106 return self._allowed(
107 access=self._get_access_token(role),
108 apply_rule='context_is_admin')
109 return role == 'admin'
DavidPurcell029d8c32017-01-06 15:27:41 -0500110
Felipe Monteirob0595652017-01-23 16:51:58 -0500111 def _get_access_token(self, role):
112 access_token = {
113 "token": {
114 "roles": [
115 {
116 "name": role
117 }
118 ],
119 "project": {
120 "id": self.tenant_id
121 }
122 }
123 }
124 return access_token
DavidPurcell029d8c32017-01-06 15:27:41 -0500125
Felipe Monteiro9c978502017-01-27 17:07:54 -0500126 def _allowed(self, access, apply_rule, is_admin=False):
Felipe Monteirob0595652017-01-23 16:51:58 -0500127 """Checks if a given rule in a policy is allowed with given access.
DavidPurcell029d8c32017-01-06 15:27:41 -0500128
Felipe Monteirob0595652017-01-23 16:51:58 -0500129 Adapted from oslo_policy.shell.
DavidPurcell029d8c32017-01-06 15:27:41 -0500130
Felipe Monteirob0595652017-01-23 16:51:58 -0500131 :param access: type dict: dictionary from ``_get_access_token``
132 :param apply_rule: type string: rule to be checked
133 :param is_admin: type bool: whether admin context is used
DavidPurcell029d8c32017-01-06 15:27:41 -0500134 """
Felipe Monteirob0595652017-01-23 16:51:58 -0500135 access_data = copy.copy(access['token'])
136 access_data['roles'] = [role['name'] for role in access_data['roles']]
137 access_data['project_id'] = access_data['project']['id']
138 access_data['is_admin'] = is_admin
Felipe Monteiro9c978502017-01-27 17:07:54 -0500139 # TODO(felipemonteiro): Dynamically calculate is_admin_project rather
140 # than hard-coding it to True. is_admin_project cannot be determined
141 # from the role, but rather from project and domain names. See
142 # _populate_is_admin_project in keystone.token.providers.common
143 # for more information.
144 access_data['is_admin_project'] = True
DavidPurcell029d8c32017-01-06 15:27:41 -0500145
Felipe Monteirob0595652017-01-23 16:51:58 -0500146 class Object(object):
147 pass
148 o = Object()
Felipe Monteiro9c978502017-01-27 17:07:54 -0500149 o.rules = self.rules
DavidPurcell029d8c32017-01-06 15:27:41 -0500150
Felipe Monteirob0595652017-01-23 16:51:58 -0500151 target = {"project_id": access_data['project_id']}
152
Felipe Monteiro9c978502017-01-27 17:07:54 -0500153 result = self._try_rule(apply_rule, target, access_data, o)
Felipe Monteirob0595652017-01-23 16:51:58 -0500154 return result
155
Felipe Monteiro9c978502017-01-27 17:07:54 -0500156 def _try_rule(self, apply_rule, target, access_data, o):
Felipe Monteirob0595652017-01-23 16:51:58 -0500157 try:
Felipe Monteiro9c978502017-01-27 17:07:54 -0500158 rule = self.rules[apply_rule]
Felipe Monteirob0595652017-01-23 16:51:58 -0500159 return rule(target, access_data, o)
Felipe Monteiro9c978502017-01-27 17:07:54 -0500160 except KeyError as e:
161 LOG.debug("{0} not found in policy file.".format(apply_rule))
162 return False
Felipe Monteirob0595652017-01-23 16:51:58 -0500163 except Exception as e:
Felipe Monteiro9c978502017-01-27 17:07:54 -0500164 LOG.debug("Exception: {0} for rule: {1}.".format(e, rule))
Felipe Monteirob0595652017-01-23 16:51:58 -0500165 return False