add escalator frame
[escalator.git] / api / escalator / api / policy.py
1 # Copyright (c) 2011 OpenStack Foundation
2 # Copyright 2013 IBM Corp.
3 # All Rights Reserved.
4 #
5 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
6 #    not use this file except in compliance with the License. You may obtain
7 #    a copy of the License at
8 #
9 #         http://www.apache.org/licenses/LICENSE-2.0
10 #
11 #    Unless required by applicable law or agreed to in writing, software
12 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 #    License for the specific language governing permissions and limitations
15 #    under the License.
16
17 """Policy Engine For Escalator"""
18
19
20 from oslo_config import cfg
21 from oslo_log import log as logging
22 from oslo_policy import policy
23
24 from escalator.common import exception
25 from escalator import i18n
26
27
28 LOG = logging.getLogger(__name__)
29 CONF = cfg.CONF
30
31 DEFAULT_RULES = policy.Rules.from_dict({
32     'context_is_admin': 'role:admin',
33     'default': '@',
34     'manage_image_cache': 'role:admin',
35 })
36
37 _ = i18n._
38 _LI = i18n._LI
39 _LW = i18n._LW
40
41
42 class Enforcer(policy.Enforcer):
43     """Responsible for loading and enforcing rules"""
44
45     def __init__(self):
46         if CONF.find_file(CONF.oslo_policy.policy_file):
47             kwargs = dict(rules=None, use_conf=True)
48         else:
49             kwargs = dict(rules=DEFAULT_RULES, use_conf=False)
50         super(Enforcer, self).__init__(CONF, overwrite=False, **kwargs)
51
52     def add_rules(self, rules):
53         """Add new rules to the Rules object"""
54         self.set_rules(rules, overwrite=False, use_conf=self.use_conf)
55
56     def enforce(self, context, action, target):
57         """Verifies that the action is valid on the target in this context.
58
59            :param context: Escalator request context
60            :param action: String representing the action to be checked
61            :param target: Dictionary representing the object of the action.
62            :raises: `escalator.common.exception.Forbidden`
63            :returns: A non-False value if access is allowed.
64         """
65         credentials = {
66             'roles': context.roles,
67             'user': context.user,
68             'tenant': context.tenant,
69         }
70         return super(Enforcer, self).enforce(action, target, credentials,
71                                              do_raise=True,
72                                              exc=exception.Forbidden,
73                                              action=action)
74
75     def check(self, context, action, target):
76         """Verifies that the action is valid on the target in this context.
77
78            :param context: Escalator request context
79            :param action: String representing the action to be checked
80            :param target: Dictionary representing the object of the action.
81            :returns: A non-False value if access is allowed.
82         """
83         credentials = {
84             'roles': context.roles,
85             'user': context.user,
86             'tenant': context.tenant,
87         }
88         return super(Enforcer, self).enforce(action, target, credentials)
89
90     def check_is_admin(self, context):
91         """Check if the given context is associated with an admin role,
92            as defined via the 'context_is_admin' RBAC rule.
93
94            :param context: Escalator request context
95            :returns: A non-False value if context role is admin.
96         """
97         return self.check(context, 'context_is_admin', context.to_dict())