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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
|
# 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.
# This file handles all flask-restful resources for /v3/domains
import flask
import flask_restful
import functools
import http.client
from keystone.common import json_home
from keystone.common import provider_api
from keystone.common import rbac_enforcer
from keystone.common import validation
import keystone.conf
from keystone import exception
from keystone.resource import schema
from keystone.server import flask as ks_flask
CONF = keystone.conf.CONF
ENFORCER = rbac_enforcer.RBACEnforcer
PROVIDERS = provider_api.ProviderAPIs
def _build_domain_enforcement_target():
target = {}
try:
target['domain'] = PROVIDERS.resource_api.get_domain(
flask.request.view_args.get('domain_id')
)
except exception.NotFound: # nosec
# Defer existence in the event the domain doesn't exist, we'll
# check this later anyway.
pass
return target
def _build_enforcement_target(allow_non_existing=False):
target = {}
if flask.request.view_args:
domain_id = flask.request.view_args.get('domain_id', None)
if domain_id:
target['domain'] = PROVIDERS.resource_api.get_domain(domain_id)
role_id = flask.request.view_args.get('role_id', None)
if role_id:
target['role'] = PROVIDERS.role_api.get_role(role_id)
if flask.request.view_args.get('user_id'):
try:
target['user'] = PROVIDERS.identity_api.get_user(
flask.request.view_args['user_id'])
except exception.UserNotFound:
if not allow_non_existing:
raise
else:
try:
target['group'] = PROVIDERS.identity_api.get_group(
flask.request.view_args.get('group_id'))
except exception.GroupNotFound:
if not allow_non_existing:
raise
return target
class DomainResource(ks_flask.ResourceBase):
collection_key = 'domains'
member_key = 'domain'
get_member_from_driver = PROVIDERS.deferred_provider_lookup(
api='resource_api', method='get_domain')
def get(self, domain_id=None):
"""Get domain or list domains.
GET/HEAD /v3/domains
GET/HEAD /v3/domains/{domain_id}
"""
if domain_id is not None:
return self._get_domain(domain_id)
return self._list_domains()
def _get_domain(self, domain_id):
ENFORCER.enforce_call(
action='identity:get_domain',
build_target=_build_domain_enforcement_target
)
domain = PROVIDERS.resource_api.get_domain(domain_id)
return self.wrap_member(domain)
def _list_domains(self):
filters = ['name', 'enabled']
ENFORCER.enforce_call(action='identity:list_domains',
filters=filters)
hints = self.build_driver_hints(filters)
refs = PROVIDERS.resource_api.list_domains(hints=hints)
return self.wrap_collection(refs, hints=hints)
def post(self):
"""Create domain.
POST /v3/domains
"""
ENFORCER.enforce_call(action='identity:create_domain')
domain = self.request_body_json.get('domain', {})
validation.lazy_validate(schema.domain_create, domain)
domain_id = domain.get('explicit_domain_id')
if domain_id is None:
domain = self._assign_unique_id(domain)
else:
# Domain ID validation provided by PyCADF
try:
self._validate_id_format(domain_id)
except ValueError:
raise exception.DomainIdInvalid
domain['id'] = domain_id
domain = self._normalize_dict(domain)
ref = PROVIDERS.resource_api.create_domain(
domain['id'], domain, initiator=self.audit_initiator)
return self.wrap_member(ref), http.client.CREATED
def patch(self, domain_id):
"""Update domain.
PATCH /v3/domains/{domain_id}
"""
ENFORCER.enforce_call(action='identity:update_domain')
domain = self.request_body_json.get('domain', {})
validation.lazy_validate(schema.domain_update, domain)
PROVIDERS.resource_api.get_domain(domain_id)
ref = PROVIDERS.resource_api.update_domain(
domain_id, domain, initiator=self.audit_initiator)
return self.wrap_member(ref)
def delete(self, domain_id):
"""Delete domain.
DELETE /v3/domains/{domain_id}
"""
ENFORCER.enforce_call(action='identity:delete_domain')
PROVIDERS.resource_api.delete_domain(
domain_id, initiator=self.audit_initiator)
return None, http.client.NO_CONTENT
class DomainConfigBase(ks_flask.ResourceBase):
member_key = 'config'
def get(self, domain_id=None, group=None, option=None):
"""Check if config option exists.
GET/HEAD /v3/domains/{domain_id}/config
GET/HEAD /v3/domains/{domain_id}/config/{group}
GET/HEAD /v3/domains/{domain_id}/config/{group}/{option}
"""
err = None
config = {}
try:
PROVIDERS.resource_api.get_domain(domain_id)
except Exception as e: # nosec
# We don't raise out here, we raise out after enforcement, this
# ensures we do not leak domain existance.
err = e
finally:
if group and group == 'security_compliance':
config = self._get_security_compliance_config(
domain_id, group, option)
else:
config = self._get_config(domain_id, group, option)
if err is not None:
raise err
return {self.member_key: config}
def _get_config(self, domain_id, group, option):
ENFORCER.enforce_call(action='identity:get_domain_config')
return PROVIDERS.domain_config_api.get_config(
domain_id, group=group, option=option)
def _get_security_compliance_config(self, domain_id, group, option):
ENFORCER.enforce_call(
action='identity:get_security_compliance_domain_config')
return PROVIDERS.domain_config_api.get_security_compliance_config(
domain_id, group, option=option)
def patch(self, domain_id=None, group=None, option=None):
"""Update domain config option.
PATCH /v3/domains/{domain_id}/config
PATCH /v3/domains/{domain_id}/config/{group}
PATCH /v3/domains/{domain_id}/config/{group}/{option}
"""
ENFORCER.enforce_call(action='identity:update_domain_config')
PROVIDERS.resource_api.get_domain(domain_id)
config = self.request_body_json.get('config', {})
ref = PROVIDERS.domain_config_api.update_config(
domain_id, config, group, option=option)
return {self.member_key: ref}
def delete(self, domain_id=None, group=None, option=None):
"""Delete domain config.
DELETE /v3/domains/{domain_id}/config
DELETE /v3/domains/{domain_id}/config/{group}
DELETE /v3/domains/{domain_id}/config/{group}/{option}
"""
ENFORCER.enforce_call(action='identity:delete_domain_config')
PROVIDERS.resource_api.get_domain(domain_id)
PROVIDERS.domain_config_api.delete_config(
domain_id, group, option=option)
return None, http.client.NO_CONTENT
class DomainConfigResource(DomainConfigBase):
"""Provides config routing functionality.
This class leans on DomainConfigBase to provide the following APIs:
GET/HEAD /v3/domains/{domain_id}/config
PATCH /v3/domains/{domain_id}/config
DELETE /v3/domains/{domain_id}/config
"""
def put(self, domain_id):
"""Create domain config.
PUT /v3/domains/{domain_id}/config
"""
ENFORCER.enforce_call(action='identity:create_domain_config')
PROVIDERS.resource_api.get_domain(domain_id)
config = self.request_body_json.get('config', {})
original_config = (
PROVIDERS.domain_config_api.get_config_with_sensitive_info(
domain_id
)
)
ref = PROVIDERS.domain_config_api.create_config(domain_id, config)
if original_config:
return {self.member_key: ref}
else:
return {self.member_key: ref}, http.client.CREATED
class DomainConfigGroupResource(DomainConfigBase):
"""Provides config group routing functionality.
This class leans on DomainConfigBase to provide the following APIs:
GET/HEAD /v3/domains/{domain_id}/config/{group}
PATCH /v3/domains/{domain_id}/config/{group}
DELETE /v3/domains/{domain_id}/config/{group}
"""
class DomainConfigOptionResource(DomainConfigBase):
"""Provides config option routing functionality.
This class leans on DomainConfigBase to provide the following APIs:
GET/HEAD /v3/domains/{domain_id}/config/{group}/{option}
PATCH /v3/domains/{domain_id}/config/{group}/{option}
DELETE /v3/domains/{domain_id}/config/{group}/{option}
"""
class DefaultConfigResource(flask_restful.Resource):
def get(self):
"""Get default domain config.
GET/HEAD /v3/domains/config/default
"""
ENFORCER.enforce_call(action='identity:get_domain_config_default')
ref = PROVIDERS.domain_config_api.get_config_default()
return {'config': ref}
class DefaultConfigGroupResource(flask_restful.Resource):
def get(self, group=None):
"""Get default domain group config.
GET/HEAD /v3/domains/config/{group}/default
"""
ENFORCER.enforce_call(action='identity:get_domain_config_default')
ref = PROVIDERS.domain_config_api.get_config_default(group=group)
return {'config': ref}
class DefaultConfigOptionResource(flask_restful.Resource):
def get(self, group=None, option=None):
"""Get default domain group option config.
GET/HEAD /v3/domains/config/{group}/{option}/default
"""
ENFORCER.enforce_call(action='identity:get_domain_config_default')
ref = PROVIDERS.domain_config_api.get_config_default(
group=group, option=option)
return {'config': ref}
class DomainUserListResource(flask_restful.Resource):
def get(self, domain_id=None, user_id=None):
"""Get user grant.
GET/HEAD /v3/domains/{domain_id}/users/{user_id}/roles
"""
ENFORCER.enforce_call(
action='identity:list_grants',
build_target=_build_enforcement_target)
refs = PROVIDERS.assignment_api.list_grants(
domain_id=domain_id, user_id=user_id,
inherited_to_projects=False)
return ks_flask.ResourceBase.wrap_collection(
refs, collection_name='roles')
class DomainUserResource(ks_flask.ResourceBase):
member_key = 'grant'
collection_key = 'grants'
def get(self, domain_id=None, user_id=None, role_id=None):
"""Check if a user has a specific role on the domain.
GET/HEAD /v3/domains/{domain_id}/users/{user_id}/roles/{role_id}
"""
ENFORCER.enforce_call(
action='identity:check_grant',
build_target=_build_enforcement_target)
PROVIDERS.assignment_api.get_grant(
role_id, domain_id=domain_id, user_id=user_id,
inherited_to_projects=False)
return None, http.client.NO_CONTENT
def put(self, domain_id=None, user_id=None, role_id=None):
"""Create a role to a user on a domain.
PUT /v3/domains/{domain_id}/users/{user_id}/roles/{role_id}
"""
ENFORCER.enforce_call(
action='identity:create_grant',
build_target=_build_enforcement_target)
PROVIDERS.assignment_api.create_grant(
role_id, domain_id=domain_id, user_id=user_id,
inherited_to_projects=False, initiator=self.audit_initiator)
return None, http.client.NO_CONTENT
def delete(self, domain_id=None, user_id=None, role_id=None):
"""Revoke a role from user on a domain.
DELETE /v3/domains/{domain_id}/users/{user_id}/roles/{role_id}
"""
ENFORCER.enforce_call(
action='identity:revoke_grant',
build_target=functools.partial(_build_enforcement_target,
allow_non_existing=True))
PROVIDERS.assignment_api.delete_grant(
role_id, domain_id=domain_id, user_id=user_id,
inherited_to_projects=False, initiator=self.audit_initiator)
return None, http.client.NO_CONTENT
class DomainGroupListResource(flask_restful.Resource):
def get(self, domain_id=None, group_id=None):
"""List all domain grats for a specific group.
GET/HEAD /v3/domains/{domain_id}/groups/{group_id}/roles
"""
ENFORCER.enforce_call(
action='identity:list_grants',
build_target=_build_enforcement_target)
refs = PROVIDERS.assignment_api.list_grants(
domain_id=domain_id, group_id=group_id,
inherited_to_projects=False)
return ks_flask.ResourceBase.wrap_collection(
refs, collection_name='roles')
class DomainGroupResource(ks_flask.ResourceBase):
member_key = 'grant'
collection_key = 'grants'
def get(self, domain_id=None, group_id=None, role_id=None):
"""Check if a group has a specific role on a domain.
GET/HEAD /v3/domains/{domain_id}/groups/{group_id}/roles/{role_id}
"""
ENFORCER.enforce_call(
action='identity:check_grant',
build_target=_build_enforcement_target)
PROVIDERS.assignment_api.get_grant(
role_id, domain_id=domain_id, group_id=group_id,
inherited_to_projects=False)
return None, http.client.NO_CONTENT
def put(self, domain_id=None, group_id=None, role_id=None):
"""Grant a role to a group on a domain.
PUT /v3/domains/{domain_id}/groups/{group_id}/roles/{role_id}
"""
ENFORCER.enforce_call(
action='identity:create_grant',
build_target=_build_enforcement_target)
PROVIDERS.assignment_api.create_grant(
role_id, domain_id=domain_id, group_id=group_id,
inherited_to_projects=False, initiator=self.audit_initiator)
return None, http.client.NO_CONTENT
def delete(self, domain_id=None, group_id=None, role_id=None):
"""Revoke a role from a group on a domain.
DELETE /v3/domains/{domain_id}/groups/{group_id}/roles/{role_id}
"""
ENFORCER.enforce_call(
action='identity:revoke_grant',
build_target=functools.partial(_build_enforcement_target,
allow_non_existing=True))
PROVIDERS.assignment_api.delete_grant(
role_id, domain_id=domain_id, group_id=group_id,
inherited_to_projects=False, initiator=self.audit_initiator)
return None, http.client.NO_CONTENT
class DomainAPI(ks_flask.APIBase):
CONFIG_GROUP = json_home.build_v3_parameter_relation('config_group')
CONFIG_OPTION = json_home.build_v3_parameter_relation('config_option')
_name = 'domains'
_import_name = __name__
resources = [DomainResource]
resource_mapping = [
ks_flask.construct_resource_map(
resource=DomainConfigResource,
url=('/domains/<string:domain_id>/config'),
resource_kwargs={},
rel='domain_config',
path_vars={
'domain_id': json_home.Parameters.DOMAIN_ID}),
ks_flask.construct_resource_map(
resource=DomainConfigGroupResource,
url='/domains/<string:domain_id>/config/<string:group>',
resource_kwargs={},
rel='domain_config_group',
path_vars={
'domain_id': json_home.Parameters.DOMAIN_ID,
'group': CONFIG_GROUP}),
ks_flask.construct_resource_map(
resource=DomainConfigOptionResource,
url=('/domains/<string:domain_id>/config/<string:group>'
'/<string:option>'),
resource_kwargs={},
rel='domain_config_option',
path_vars={
'domain_id': json_home.Parameters.DOMAIN_ID,
'group': CONFIG_GROUP,
'option': CONFIG_OPTION}),
ks_flask.construct_resource_map(
resource=DefaultConfigResource,
url=('/domains/config/default'),
resource_kwargs={},
rel='domain_config_default',
path_vars={}),
ks_flask.construct_resource_map(
resource=DefaultConfigGroupResource,
url='/domains/config/<string:group>/default',
resource_kwargs={},
rel='domain_config_default_group',
path_vars={
'group': CONFIG_GROUP}),
ks_flask.construct_resource_map(
resource=DefaultConfigOptionResource,
url=('/domains/config/<string:group>'
'/<string:option>/default'),
resource_kwargs={},
rel='domain_config_default_option',
path_vars={
'group': CONFIG_GROUP,
'option': CONFIG_OPTION}),
ks_flask.construct_resource_map(
resource=DomainUserListResource,
url=('/domains/<string:domain_id>/users'
'/<string:user_id>/roles'),
resource_kwargs={},
rel='domain_user_roles',
path_vars={
'domain_id': json_home.Parameters.DOMAIN_ID,
'user_id': json_home.Parameters.USER_ID,
}),
ks_flask.construct_resource_map(
resource=DomainUserResource,
url=('/domains/<string:domain_id>/users'
'/<string:user_id>/roles/<string:role_id>'),
resource_kwargs={},
rel='domain_user_role',
path_vars={
'domain_id': json_home.Parameters.DOMAIN_ID,
'user_id': json_home.Parameters.USER_ID,
'role_id': json_home.Parameters.ROLE_ID
}),
ks_flask.construct_resource_map(
resource=DomainGroupListResource,
url=('/domains/<string:domain_id>/groups'
'/<string:group_id>/roles'),
resource_kwargs={},
rel='domain_group_roles',
path_vars={
'domain_id': json_home.Parameters.DOMAIN_ID,
'group_id': json_home.Parameters.GROUP_ID,
}),
ks_flask.construct_resource_map(
resource=DomainGroupResource,
url=('/domains/<string:domain_id>/groups'
'/<string:group_id>/roles/<string:role_id>'),
resource_kwargs={},
rel='domain_group_role',
path_vars={
'domain_id': json_home.Parameters.DOMAIN_ID,
'group_id': json_home.Parameters.GROUP_ID,
'role_id': json_home.Parameters.ROLE_ID
})
]
APIs = (DomainAPI,)
|