summaryrefslogtreecommitdiff
path: root/keystone/common/policies/grant.py
blob: 0e1b92876efdcc515852a910e426d903ed799df0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from oslo_log import versionutils
from oslo_policy import policy

from keystone.common.policies import base

# Two of the three portions of this check string are specific to domain
# readers. The first catches domain readers who are checking or listing grants
# for users. The second does the same for groups. We have to overload the check
# string to handle both cases because `identity:check_grant` is used to protect
# both user and group grant APIs. If the `identity:check_grant` policy is every
# broken apart, we can write specific check strings that are tailored to either
# users or groups (e.g., `identity:check_group_grant` or
# `identity:check_user_grant`) and prevent overloading like this.
DOMAIN_MATCHES_USER_DOMAIN = 'domain_id:%(target.user.domain_id)s'
DOMAIN_MATCHES_GROUP_DOMAIN = 'domain_id:%(target.group.domain_id)s'
DOMAIN_MATCHES_PROJECT_DOMAIN = 'domain_id:%(target.project.domain_id)s'
DOMAIN_MATCHES_TARGET_DOMAIN = 'domain_id:%(target.domain.id)s'
DOMAIN_MATCHES_ROLE = (
    'domain_id:%(target.role.domain_id)s or None:%(target.role.domain_id)s'
)
GRANTS_DOMAIN_READER = (
    '(role:reader and ' + DOMAIN_MATCHES_USER_DOMAIN + ' and'
    ' ' + DOMAIN_MATCHES_PROJECT_DOMAIN + ') or '
    '(role:reader and ' + DOMAIN_MATCHES_USER_DOMAIN + ' and'
    ' ' + DOMAIN_MATCHES_TARGET_DOMAIN + ') or '
    '(role:reader and ' + DOMAIN_MATCHES_GROUP_DOMAIN + ' and'
    ' ' + DOMAIN_MATCHES_PROJECT_DOMAIN + ') or '
    '(role:reader and ' + DOMAIN_MATCHES_GROUP_DOMAIN + ' and'
    ' ' + DOMAIN_MATCHES_TARGET_DOMAIN + ')'
)
SYSTEM_READER_OR_DOMAIN_READER = (
    '(' + base.SYSTEM_READER + ') or '
    '(' + GRANTS_DOMAIN_READER + ') and '
    '(' + DOMAIN_MATCHES_ROLE + ')'
)

SYSTEM_READER_OR_DOMAIN_READER_LIST = (
    '(' + base.SYSTEM_READER + ') or ' + GRANTS_DOMAIN_READER
)

GRANTS_DOMAIN_ADMIN = (
    '(role:admin and ' + DOMAIN_MATCHES_USER_DOMAIN + ' and'
    ' ' + DOMAIN_MATCHES_PROJECT_DOMAIN + ') or '
    '(role:admin and ' + DOMAIN_MATCHES_USER_DOMAIN + ' and'
    ' ' + DOMAIN_MATCHES_TARGET_DOMAIN + ') or '
    '(role:admin and ' + DOMAIN_MATCHES_GROUP_DOMAIN + ' and'
    ' ' + DOMAIN_MATCHES_PROJECT_DOMAIN + ') or '
    '(role:admin and ' + DOMAIN_MATCHES_GROUP_DOMAIN + ' and'
    ' ' + DOMAIN_MATCHES_TARGET_DOMAIN + ')'
)
SYSTEM_ADMIN_OR_DOMAIN_ADMIN = (
    '(' + base.SYSTEM_ADMIN + ') or '
    '(' + GRANTS_DOMAIN_ADMIN + ') and '
    '(' + DOMAIN_MATCHES_ROLE + ')'
)

DEPRECATED_REASON = (
    "The assignment API is now aware of system scope and default roles."
)

deprecated_check_system_grant_for_user = policy.DeprecatedRule(
    name=base.IDENTITY % 'check_system_grant_for_user',
    check_str=base.RULE_ADMIN_REQUIRED,
    deprecated_reason=DEPRECATED_REASON,
    deprecated_since=versionutils.deprecated.STEIN
)
deprecated_list_system_grants_for_user = policy.DeprecatedRule(
    name=base.IDENTITY % 'list_system_grants_for_user',
    check_str=base.RULE_ADMIN_REQUIRED,
    deprecated_reason=DEPRECATED_REASON,
    deprecated_since=versionutils.deprecated.STEIN
)
deprecated_create_system_grant_for_user = policy.DeprecatedRule(
    name=base.IDENTITY % 'create_system_grant_for_user',
    check_str=base.RULE_ADMIN_REQUIRED,
    deprecated_reason=DEPRECATED_REASON,
    deprecated_since=versionutils.deprecated.STEIN
)
deprecated_revoke_system_grant_for_user = policy.DeprecatedRule(
    name=base.IDENTITY % 'revoke_system_grant_for_user',
    check_str=base.RULE_ADMIN_REQUIRED,
    deprecated_reason=DEPRECATED_REASON,
    deprecated_since=versionutils.deprecated.STEIN
)
deprecated_check_system_grant_for_group = policy.DeprecatedRule(
    name=base.IDENTITY % 'check_system_grant_for_group',
    check_str=base.RULE_ADMIN_REQUIRED,
    deprecated_reason=DEPRECATED_REASON,
    deprecated_since=versionutils.deprecated.STEIN
)
deprecated_list_system_grants_for_group = policy.DeprecatedRule(
    name=base.IDENTITY % 'list_system_grants_for_group',
    check_str=base.RULE_ADMIN_REQUIRED,
    deprecated_reason=DEPRECATED_REASON,
    deprecated_since=versionutils.deprecated.STEIN
)
deprecated_create_system_grant_for_group = policy.DeprecatedRule(
    name=base.IDENTITY % 'create_system_grant_for_group',
    check_str=base.RULE_ADMIN_REQUIRED,
    deprecated_reason=DEPRECATED_REASON,
    deprecated_since=versionutils.deprecated.STEIN
)
deprecated_revoke_system_grant_for_group = policy.DeprecatedRule(
    name=base.IDENTITY % 'revoke_system_grant_for_group',
    check_str=base.RULE_ADMIN_REQUIRED,
    deprecated_reason=DEPRECATED_REASON,
    deprecated_since=versionutils.deprecated.STEIN
)
deprecated_list_grants = policy.DeprecatedRule(
    name=base.IDENTITY % 'list_grants', check_str=base.RULE_ADMIN_REQUIRED,
    deprecated_reason=DEPRECATED_REASON,
    deprecated_since=versionutils.deprecated.STEIN
)
deprecated_check_grant = policy.DeprecatedRule(
    name=base.IDENTITY % 'check_grant', check_str=base.RULE_ADMIN_REQUIRED,
    deprecated_reason=DEPRECATED_REASON,
    deprecated_since=versionutils.deprecated.STEIN
)
deprecated_create_grant = policy.DeprecatedRule(
    name=base.IDENTITY % 'create_grant', check_str=base.RULE_ADMIN_REQUIRED,
    deprecated_reason=DEPRECATED_REASON,
    deprecated_since=versionutils.deprecated.STEIN
)
deprecated_revoke_grant = policy.DeprecatedRule(
    name=base.IDENTITY % 'revoke_grant', check_str=base.RULE_ADMIN_REQUIRED,
    deprecated_reason=DEPRECATED_REASON,
    deprecated_since=versionutils.deprecated.STEIN
)


resource_paths = [
    '/projects/{project_id}/users/{user_id}/roles/{role_id}',
    '/projects/{project_id}/groups/{group_id}/roles/{role_id}',
    '/domains/{domain_id}/users/{user_id}/roles/{role_id}',
    '/domains/{domain_id}/groups/{group_id}/roles/{role_id}',
]


resource_paths += ['/OS-INHERIT' + path + '/inherited_to_projects'
                   for path in resource_paths]


collection_paths = [
    '/projects/{project_id}/users/{user_id}/roles',
    '/projects/{project_id}/groups/{group_id}/roles',
    '/domains/{domain_id}/users/{user_id}/roles',
    '/domains/{domain_id}/groups/{group_id}/roles'
]


inherited_collection_paths = [
    ('/OS-INHERIT/domains/{domain_id}/groups/{group_id}/roles/'
     'inherited_to_projects'),
    ('/OS-INHERIT/domains/{domain_id}/users/{user_id}/roles/'
     'inherited_to_projects')
]


def list_operations(paths, methods):
    return [{'path': '/v3' + path, 'method': method}
            for path in paths for method in methods]


# NOTE(samueldmq): Unlike individual resource paths, collection
# paths for the inherited grants do not contain a HEAD API
list_grants_operations = (
    list_operations(collection_paths, ['GET', 'HEAD']) +
    list_operations(inherited_collection_paths, ['GET']))


grant_policies = [
    policy.DocumentedRuleDefault(
        name=base.IDENTITY % 'check_grant',
        check_str=SYSTEM_READER_OR_DOMAIN_READER,
        scope_types=['system', 'domain'],
        description=('Check a role grant between a target and an actor. A '
                     'target can be either a domain or a project. An actor '
                     'can be either a user or a group. These terms also apply '
                     'to the OS-INHERIT APIs, where grants on the target '
                     'are inherited to all projects in the subtree, if '
                     'applicable.'),
        operations=list_operations(resource_paths, ['HEAD', 'GET']),
        deprecated_rule=deprecated_check_grant),
    policy.DocumentedRuleDefault(
        name=base.IDENTITY % 'list_grants',
        check_str=SYSTEM_READER_OR_DOMAIN_READER_LIST,
        scope_types=['system', 'domain'],
        description=('List roles granted to an actor on a target. A target '
                     'can be either a domain or a project. An actor can be '
                     'either a user or a group. For the OS-INHERIT APIs, it '
                     'is possible to list inherited role grants for actors on '
                     'domains, where grants are inherited to all projects '
                     'in the specified domain.'),
        operations=list_grants_operations,
        deprecated_rule=deprecated_list_grants),
    policy.DocumentedRuleDefault(
        name=base.IDENTITY % 'create_grant',
        check_str=SYSTEM_ADMIN_OR_DOMAIN_ADMIN,
        scope_types=['system', 'domain'],
        description=('Create a role grant between a target and an actor. A '
                     'target can be either a domain or a project. An actor '
                     'can be either a user or a group. These terms also apply '
                     'to the OS-INHERIT APIs, where grants on the target '
                     'are inherited to all projects in the subtree, if '
                     'applicable.'),
        operations=list_operations(resource_paths, ['PUT']),
        deprecated_rule=deprecated_create_grant),
    policy.DocumentedRuleDefault(
        name=base.IDENTITY % 'revoke_grant',
        check_str=SYSTEM_ADMIN_OR_DOMAIN_ADMIN,
        scope_types=['system', 'domain'],
        description=('Revoke a role grant between a target and an actor. A '
                     'target can be either a domain or a project. An actor '
                     'can be either a user or a group. These terms also apply '
                     'to the OS-INHERIT APIs, where grants on the target '
                     'are inherited to all projects in the subtree, if '
                     'applicable. In that case, revoking the role grant in '
                     'the target would remove the logical effect of '
                     'inheriting it to the target\'s projects subtree.'),
        operations=list_operations(resource_paths, ['DELETE']),
        deprecated_rule=deprecated_revoke_grant),
    policy.DocumentedRuleDefault(
        name=base.IDENTITY % 'list_system_grants_for_user',
        check_str=base.SYSTEM_READER,
        scope_types=['system'],
        description='List all grants a specific user has on the system.',
        operations=[
            {
                'path': '/v3/system/users/{user_id}/roles',
                'method': ['HEAD', 'GET']
            }
        ],
        deprecated_rule=deprecated_list_system_grants_for_user,
    ),
    policy.DocumentedRuleDefault(
        name=base.IDENTITY % 'check_system_grant_for_user',
        check_str=base.SYSTEM_READER,
        scope_types=['system'],
        description='Check if a user has a role on the system.',
        operations=[
            {
                'path': '/v3/system/users/{user_id}/roles/{role_id}',
                'method': ['HEAD', 'GET']
            }
        ],
        deprecated_rule=deprecated_check_system_grant_for_user,
    ),
    policy.DocumentedRuleDefault(
        name=base.IDENTITY % 'create_system_grant_for_user',
        check_str=base.SYSTEM_ADMIN,
        scope_types=['system'],
        description='Grant a user a role on the system.',
        operations=[
            {
                'path': '/v3/system/users/{user_id}/roles/{role_id}',
                'method': ['PUT']
            }
        ],
        deprecated_rule=deprecated_create_system_grant_for_user,
    ),
    policy.DocumentedRuleDefault(
        name=base.IDENTITY % 'revoke_system_grant_for_user',
        check_str=base.SYSTEM_ADMIN,
        scope_types=['system'],
        description='Remove a role from a user on the system.',
        operations=[
            {
                'path': '/v3/system/users/{user_id}/roles/{role_id}',
                'method': ['DELETE']
            }
        ],
        deprecated_rule=deprecated_revoke_system_grant_for_user,
    ),
    policy.DocumentedRuleDefault(
        name=base.IDENTITY % 'list_system_grants_for_group',
        check_str=base.SYSTEM_READER,
        scope_types=['system'],
        description='List all grants a specific group has on the system.',
        operations=[
            {
                'path': '/v3/system/groups/{group_id}/roles',
                'method': ['HEAD', 'GET']
            }
        ],
        deprecated_rule=deprecated_list_system_grants_for_group,
    ),
    policy.DocumentedRuleDefault(
        name=base.IDENTITY % 'check_system_grant_for_group',
        check_str=base.SYSTEM_READER,
        scope_types=['system'],
        description='Check if a group has a role on the system.',
        operations=[
            {
                'path': '/v3/system/groups/{group_id}/roles/{role_id}',
                'method': ['HEAD', 'GET']
            }
        ],
        deprecated_rule=deprecated_check_system_grant_for_group,
    ),
    policy.DocumentedRuleDefault(
        name=base.IDENTITY % 'create_system_grant_for_group',
        check_str=base.SYSTEM_ADMIN,
        scope_types=['system'],
        description='Grant a group a role on the system.',
        operations=[
            {
                'path': '/v3/system/groups/{group_id}/roles/{role_id}',
                'method': ['PUT']
            }
        ],
        deprecated_rule=deprecated_create_system_grant_for_group,
    ),
    policy.DocumentedRuleDefault(
        name=base.IDENTITY % 'revoke_system_grant_for_group',
        check_str=base.SYSTEM_ADMIN,
        scope_types=['system'],
        description='Remove a role from a group on the system.',
        operations=[
            {
                'path': '/v3/system/groups/{group_id}/roles/{role_id}',
                'method': ['DELETE']
            }
        ],
        deprecated_rule=deprecated_revoke_system_grant_for_group,
    )
]


def list_rules():
    return grant_policies