Debian Codegraph @

neutron_lib/policy/_engine.py

raw
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 import sys
14
15 from oslo_config import cfg
16 from oslo_policy import opts
17 from oslo_policy import policy
18
19
20 _ROLE_ENFORCER = None
21 _ADMIN_CTX_POLICY = 'context_is_admin'
22 _GLOBAL_CTX_POLICY = 'context_with_global_access'
23 _ADVSVC_CTX_POLICY = 'context_is_advsvc'
24 _SERVICE_ROLE = 'service_api'
25 _CAN_SET_PROJECT_ID_CTX_POLICY = 'context_can_set_project_id'
26
27
28 opts.set_defaults(cfg.CONF)
29
30
31 _BASE_RULES = [
32 policy.RuleDefault(
33 _ADMIN_CTX_POLICY,
34 'role:admin',
35 description='Rule for cloud admin access'),
36 policy.RuleDefault(
37 # By default, no one has global access to the resources.
38 # That is special meaning of the "!" in rule, see
39 # https://docs.openstack.org/oslo.policy/latest/admin/policy-yaml-file.html#examples.
40 # This policy rule should be overridden by the cloud administrator if
41 # there is need to have any custom role with global access to the
42 # resources from all projects.
43 _GLOBAL_CTX_POLICY,
44 '!',
45 description='Rule for context with global access to the resources'),
46 policy.RuleDefault(
47 # By default, no one except admin can send project id in the request
48 # body.
49 # That is special meaning of the "!" in rule, see
50 # https://docs.openstack.org/oslo.policy/latest/admin/policy-yaml-file.html#examples.
51 # This policy rule should be overridden by the cloud administrator if
52 # there is need to have any custom role with privileges to send
53 # project_id in the request body of the POST requests to create
54 # resources for other projects.
55 _CAN_SET_PROJECT_ID_CTX_POLICY,
56 '!',
57 description='Rule for context with privileges to send project_id in '
58 'the request body of the requests to create resources '
59 'for other projects'),
60 policy.RuleDefault(
61 _ADVSVC_CTX_POLICY,
62 'role:advsvc',
63 description='Rule for advanced service role access'),
64 policy.RuleDefault(
65 _SERVICE_ROLE,
66 'role:service',
67 description='Default rule for the service-to-service APIs.'),
68 ]
69
70
71 def init(conf=cfg.CONF, policy_file=None):
72 """Initialize the global enforcer if not already initialized.
73
74 Initialize the global enforcer (and load its rules) if not already
75 initialized; otherwise this is a no-op.
76
77 :param conf: The configuration to initialize the global enforcer with.
78 Defaults to oslo_config.cfg.CONF.
79 :param policy_file: The policy file to initialize the global enforcer
80 with.
81 :returns: None.
82 """
83
84 global _ROLE_ENFORCER
85 if not _ROLE_ENFORCER:
86 _ROLE_ENFORCER = policy.Enforcer(conf, policy_file=policy_file)
87 # Skip the undefined rule check to avoid unnecessary warning messages.
88 _ROLE_ENFORCER.skip_undefined_check = True
89 _ROLE_ENFORCER.register_defaults(_BASE_RULES)
90 _ROLE_ENFORCER.load_rules(True)
91
92
93 def _check_rule(context, rule):
94 init()
95 # the target is user-self
96 credentials = context.to_policy_values()
97 try:
98 return _ROLE_ENFORCER.authorize(rule, credentials, credentials)
99 except policy.PolicyNotRegistered:
100 return False
101
102
103 def check_is_admin(context):
104 """Verify context has admin rights according to the global policy settings.
105
106 :param context: The context object.
107 :returns: True if the context has admin rights (as per the global
108 enforcer) and False otherwise.
109 """
110 return _check_rule(context, _ADMIN_CTX_POLICY)
111
112
113 def check_has_global_access(context):
114 """Verify context has rights to fetch resources no matter of the owner
115
116 :param context: The context object.
117 :returns: True if the context has global rights (as per the global
118 enforcer) and False otherwise.
119 """
120 return _check_rule(context, _GLOBAL_CTX_POLICY)
121
122
123 def check_can_set_project_id(context):
124 """Verify context has rights to send project_id in the request body
125
126 :param context: The context object.
127 :returns: True if the context has rights to send project_id in
128 the request body (as per the global enforcer) and False otherwise.
129 """
130 return _check_rule(context, _CAN_SET_PROJECT_ID_CTX_POLICY)
131
132
133 def check_is_advsvc(context):
134 """Verify context has advsvc rights according to global policy settings.
135
136 :param context: The context object.
137 :returns: True if the context has advsvc rights (as per the global
138 enforcer) and False otherwise.
139 """
140 return _check_rule(context, _ADVSVC_CTX_POLICY)
141
142
143 def check_is_service_role(context):
144 """Verify context is service role according to global policy settings.
145
146 :param context: The context object.
147 :returns: True if the context is service role (as per the global
148 enforcer) and False otherwise.
149 """
150 return _check_rule(context, _SERVICE_ROLE)
151
152
153 def list_rules():
154 return _BASE_RULES
155
156
157 def get_enforcer():
158 # NOTE(amotoki): This was borrowed from nova/policy.py.
159 # This method is for use by oslo.policy CLI scripts. Those scripts need the
160 # 'output-file' and 'namespace' options, but having those in sys.argv means
161 # loading the neutron config options will fail as those are not expected to
162 # be present. So we pass in an arg list with those stripped out.
163 conf_args = []
164 # Start at 1 because cfg.CONF expects the equivalent of sys.argv[1:]
165 i = 1
166 while i < len(sys.argv):
167 if sys.argv[i].strip('-') in ['namespace', 'output-file']:
168 i += 2
169 continue
170 conf_args.append(sys.argv[i])
171 i += 1
172
173 # 'project' must be 'neutron' so that get_enforcer looks at
174 # /etc/neutron/policy.yaml by default.
175 cfg.CONF(conf_args, project='neutron')
176 init()
177 return _ROLE_ENFORCER