54
53
global _POLICY_PATH
55
54
global _POLICY_CACHE
56
55
if not _POLICY_PATH:
57
_POLICY_PATH = FLAGS.policy_file
56
_POLICY_PATH = CONF.policy_file
58
57
if not os.path.exists(_POLICY_PATH):
59
_POLICY_PATH = FLAGS.find_file(_POLICY_PATH)
58
_POLICY_PATH = CONF.find_file(_POLICY_PATH)
60
59
if not _POLICY_PATH:
61
raise exception.ConfigNotFound(path=FLAGS.policy_file)
60
raise exception.ConfigNotFound(path=CONF.policy_file)
62
61
utils.read_cached_file(_POLICY_PATH, _POLICY_CACHE,
63
reload_func=_set_brain)
67
default_rule = FLAGS.policy_default_rule
68
policy.set_brain(policy.Brain.load_json(data, default_rule))
71
def enforce(context, action, target):
62
reload_func=_set_rules)
66
default_rule = CONF.policy_default_rule
67
policy.set_rules(policy.Rules.load_json(data, default_rule))
70
def enforce(context, action, target, do_raise=True):
72
71
"""Verifies that the action is valid on the target in this context.
74
73
:param context: nova context
77
76
i.e. ``compute:create_instance``,
78
77
``compute:attach_volume``,
79
78
``volume:attach_volume``
81
:param object: dictionary representing the object of the action
79
:param target: dictionary representing the object of the action
82
80
for object creation this should be a dictionary representing the
83
81
location of the object e.g. ``{'project_id': context.project_id}``
85
:raises nova.exception.PolicyNotAllowed: if verification fails.
82
:param do_raise: if True (the default), raises PolicyNotAuthorized;
83
if False, returns False
85
:raises nova.exception.PolicyNotAuthorized: if verification fails
88
:return: returns a non-False value (not necessarily "True") if
89
authorized, and the exact value False if not authorized and
90
match_list = ('rule:%s' % action,)
91
94
credentials = context.to_dict()
93
# NOTE(vish): This is to work around the following launchpad bug:
94
# https://bugs.launchpad.net/openstack-common/+bug/1039132
95
# It can be removed when that bug is fixed.
96
credentials['is_admin'] = unicode(credentials['is_admin'])
96
# Add the exception arguments if asked to do a raise
99
extra.update(exc=exception.PolicyNotAuthorized, action=action)
98
policy.enforce(match_list, target, credentials,
99
exception.PolicyNotAuthorized, action=action)
101
return policy.check(action, target, credentials, **extra)
102
104
def check_is_admin(roles):
108
action = 'context_is_admin'
109
match_list = ('rule:%s' % action,)
111
111
credentials = {'roles': roles}
114
policy.enforce(match_list, target, credentials,
115
exception.PolicyNotAuthorized, action=action)
116
except exception.PolicyNotAuthorized:
113
return policy.check('context_is_admin', target, credentials)
116
@policy.register('is_admin')
117
class IsAdminCheck(policy.Check):
118
"""An explicit check for is_admin."""
120
def __init__(self, kind, match):
121
"""Initialize the check."""
123
self.expected = (match.lower() == 'true')
125
super(IsAdminCheck, self).__init__(kind, str(self.expected))
127
def __call__(self, target, creds):
128
"""Determine whether is_admin matches the requested value."""
130
return creds['is_admin'] == self.expected