9
9
import ssl
10
10
import six
11
11
import urllib3
12
+ from copy import copy
12
13
import nipyapi
13
14
14
15
15
16
log = logging .getLogger (__name__ )
16
17
17
18
18
- __all__ = ['create_service_user' , 'create_service_user_group' , 'service_login' ,
19
+ __all__ = ['create_service_user' , 'create_service_user_group' ,
19
20
'set_service_auth_token' , 'service_logout' ,
20
21
'get_service_access_status' , 'add_user_to_access_policy' ,
21
22
'update_access_policy' , 'get_access_policy_for_resource' ,
22
23
'create_access_policy' , 'list_service_users' , 'get_service_user' ,
23
- 'set_service_ssl_context' , 'add_user_group_to_access_policy' ]
24
+ 'set_service_ssl_context' , 'add_user_group_to_access_policy' ,
25
+ 'bootstrap_security_policies' , 'service_login'
26
+ ]
24
27
25
28
# These are the known-valid policy actions
26
29
_valid_actions = ['read' , 'write' , 'delete' ]
27
30
# These are the services that these functions know how to configure
28
31
_valid_services = ['nifi' , 'registry' ]
29
32
30
33
31
- def create_service_user (identity , service = 'nifi' ):
34
+ def create_service_user (identity , service = 'nifi' , strict = True ):
32
35
"""
33
36
Attempts to create a user with the provided identity in the given service
34
37
35
38
Args:
36
39
identity (str): Identity string for the user
37
40
service (str): 'nifi' or 'registry'
41
+ strict (bool): If Strict, will error if user already exists
38
42
39
43
Returns:
40
44
The new (User) or (UserEntity) object
@@ -61,6 +65,8 @@ def create_service_user(identity, service='nifi'):
61
65
except (
62
66
nipyapi .nifi .rest .ApiException ,
63
67
nipyapi .registry .rest .ApiException ) as e :
68
+ if 'already exists' in e .body and not strict :
69
+ return get_service_user (identity , service = service )
64
70
raise ValueError (e .body )
65
71
66
72
@@ -274,15 +280,18 @@ def get_service_access_status(service='nifi', bool_response=False):
274
280
raise e
275
281
276
282
277
- def add_user_to_access_policy (user , policy , service = 'nifi' , refresh = True ):
283
+ def add_user_to_access_policy (user , policy , service = 'nifi' , refresh = True ,
284
+ strict = True ):
278
285
"""
279
286
Attempts to add the given user object to the given access policy
280
287
281
288
Args:
282
289
user (User) or (UserEntity): User object to add
283
290
policy (AccessPolicyEntity) or (AccessPolicy): Access Policy object
284
291
service (str): 'nifi' or 'registry' to identify the target service
285
- refresh (bool): Whether to refresh the policy object before submission
292
+ refresh (bool): Whether to refresh the policy object before submit
293
+ strict (bool): If True, will return error if user already present,
294
+ if False will ignore the already exists
286
295
287
296
Returns:
288
297
Updated Policy object
@@ -322,15 +331,17 @@ def add_user_to_access_policy(user, policy, service='nifi', refresh=True):
322
331
policy_user_ids = [
323
332
i .identifier if service == 'registry' else i .id for i in policy_users
324
333
]
334
+ if user_id not in policy_user_ids :
335
+ if service == 'registry' :
336
+ policy_tgt .users .append (user )
337
+ elif service == 'nifi' :
338
+ policy_tgt .component .users .append ({'id' : user_id })
325
339
326
- assert user_id not in policy_user_ids
327
-
328
- if service == 'registry' :
329
- policy_tgt .users .append (user )
330
- elif service == 'nifi' :
331
- policy_tgt .component .users .append ({'id' : user_id })
332
-
333
- return nipyapi .security .update_access_policy (policy_tgt , service )
340
+ return nipyapi .security .update_access_policy (policy_tgt , service )
341
+ else :
342
+ if strict :
343
+ assert user_id not in policy_user_ids , "Strict is True and user already " \
344
+ "in Policy"
334
345
335
346
336
347
def add_user_group_to_access_policy (user_group , policy , service = 'nifi' ,
@@ -450,18 +461,25 @@ def get_access_policy_for_resource(resource,
450
461
log .info ("Called get_access_policy_for_resource with Args %s" , locals ())
451
462
452
463
# Strip leading '/' from resource as lookup endpoint prepends a '/'
453
- stripped_resource = resource [1 :] if resource .startswith (
454
- '/' ) else resource
455
- log . info ( "Getting %s Policy for %s:%s:%s" , service , action , resource , str (r_id ))
464
+ resource = resource [1 :] if resource .startswith ('/' ) else resource
465
+ log . info ( "Getting %s Policy for %s:%s:%s" , service , action ,
466
+ resource , str (r_id ))
456
467
if service == 'nifi' :
457
468
pol_api = nipyapi .nifi .PoliciesApi ()
469
+ config = nipyapi .config .nifi_config
458
470
else :
459
471
pol_api = nipyapi .registry .PoliciesApi ()
472
+ config = nipyapi .config .registry_config
473
+ default_safe_chars = copy (config .safe_chars_for_path_param )
460
474
try :
461
- return pol_api .get_access_policy_for_resource (
475
+ if '/' not in config .safe_chars_for_path_param :
476
+ config .safe_chars_for_path_param += '/'
477
+ response = pol_api .get_access_policy_for_resource (
462
478
action = action ,
463
- resource = stripped_resource
479
+ resource = '/' . join ([ resource , r_id ]) if r_id else resource
464
480
)
481
+ config .safe_chars_for_path_param = copy (default_safe_chars )
482
+ return response
465
483
except nipyapi .nifi .rest .ApiException as e :
466
484
if 'Unable to find access policy' in e .body :
467
485
log .info ("Access policy not found" )
@@ -472,6 +490,8 @@ def get_access_policy_for_resource(resource,
472
490
)
473
491
log .info ("Unexpected Error, raising..." )
474
492
raise ValueError (e .body )
493
+ finally :
494
+ config .safe_chars_for_path_param = copy (default_safe_chars )
475
495
476
496
477
497
def create_access_policy (resource , action , r_id = None , service = 'nifi' ):
@@ -610,3 +630,81 @@ def set_service_ssl_context(
610
630
if service == 'registry' :
611
631
nipyapi .config .registry_config .ssl_context = ssl_context
612
632
nipyapi .config .nifi_config .ssl_context = ssl_context
633
+
634
+
635
+ def bootstrap_security_policies (service , admin = 'CN=user1, OU=nifi' ,
636
+ proxy = 'CN=localhost, OU=nifi' ):
637
+ assert service in _valid_services
638
+ if 'nifi' in service :
639
+ rpg_id = nipyapi .canvas .get_root_pg_id ()
640
+ nifi_user_identity = nipyapi .security .get_service_user (
641
+ admin ,
642
+ service = 'nifi'
643
+ )
644
+ access_policies = [
645
+ ('write' , 'process-groups' , rpg_id ),
646
+ ('read' , 'process-groups' , rpg_id ),
647
+ ('write' , 'data/process-groups' , rpg_id ),
648
+ ('read' , 'data/process-groups' , rpg_id ),
649
+ ('read' , 'system' , None ),
650
+ ]
651
+ for pol in access_policies :
652
+ ap = nipyapi .security .get_access_policy_for_resource (
653
+ action = pol [0 ],
654
+ resource = pol [1 ],
655
+ r_id = pol [2 ],
656
+ service = 'nifi' ,
657
+ auto_create = True
658
+ )
659
+ nipyapi .security .add_user_to_access_policy (
660
+ user = nifi_user_identity ,
661
+ policy = ap ,
662
+ service = 'nifi' ,
663
+ strict = False
664
+ )
665
+ else :
666
+ reg_user_identity = nipyapi .security .get_service_user (
667
+ admin ,
668
+ service = 'registry'
669
+ )
670
+ all_buckets_access_policies = [
671
+ ("read" , "/buckets" ),
672
+ ("write" , "/buckets" ),
673
+ ("delete" , "/buckets" )
674
+ ]
675
+ for action , resource in all_buckets_access_policies :
676
+ pol = nipyapi .security .get_access_policy_for_resource (
677
+ resource = resource ,
678
+ action = action ,
679
+ service = 'registry' ,
680
+ auto_create = True
681
+ )
682
+ nipyapi .security .add_user_to_access_policy (
683
+ user = reg_user_identity ,
684
+ policy = pol ,
685
+ service = 'registry' ,
686
+ strict = False
687
+ )
688
+ # Setup Proxy Access
689
+ nifi_proxy = nipyapi .security .create_service_user (
690
+ identity = proxy ,
691
+ service = 'registry' ,
692
+ strict = False
693
+ )
694
+ proxy_access_policies = [
695
+ ("write" , "/proxy" ),
696
+ ("read" , "/buckets" )
697
+ ]
698
+ for action , resource in proxy_access_policies :
699
+ pol = nipyapi .security .get_access_policy_for_resource (
700
+ resource = resource ,
701
+ action = action ,
702
+ service = 'registry' ,
703
+ auto_create = True
704
+ )
705
+ nipyapi .security .add_user_to_access_policy (
706
+ user = nifi_proxy ,
707
+ policy = pol ,
708
+ service = 'registry' ,
709
+ strict = False
710
+ )
0 commit comments