1+ import io
2+ import json
3+ import logging
4+ import oci
5+ from fdk import response
6+
7+ # Get Resource Principal Credentials
8+ signer = oci .auth .signers .get_resource_principals_signer ()
9+
10+ identity_client = oci .identity .IdentityClient (config = {}, signer = signer )
11+
12+ def process_users (user ,identity_domains_client ,tag_namespace ,manage_capability ,execution_mode ):
13+ change_in_capability = {}
14+ tag_capability = {}
15+ user_ocid = user .ocid
16+ tags = {}
17+ if hasattr (user ,"urn_ietf_params_scim_schemas_oracle_idcs_extension_oci_tags" ):
18+ if hasattr (user .urn_ietf_params_scim_schemas_oracle_idcs_extension_oci_tags ,"defined_tags" ):
19+ tags = user .urn_ietf_params_scim_schemas_oracle_idcs_extension_oci_tags .defined_tags
20+ for tag in tags :
21+ if tag .namespace == tag_namespace :
22+ tag_capability .update ({tag .key : tag .value })
23+
24+ capabilities = user .urn_ietf_params_scim_schemas_oracle_idcs_extension_capabilities_user
25+ attribute_dict = capabilities .attribute_map
26+
27+ # Loop through input configuration
28+ for tag_key in manage_capability :
29+ key = "can_use_" + tag_key
30+ if ("disable" in execution_mode .lower ()) and ((getattr (capabilities , key )) and (not tag_key in tag_capability .keys ())):
31+ # print("changing value " + tag_key)
32+ change_in_capability .update ({attribute_dict [key ]: False })
33+ # Uncomment below line to enable capability through this script
34+ elif ("enable" in execution_mode .lower ()) and ((not getattr (capabilities ,key )) and (tag_key in tag_capability .keys ())):
35+ change_in_capability .update ({attribute_dict [key ] : True })
36+
37+ if change_in_capability :
38+ logging .getLogger ().info (f'Change in capability for user { user .user_name } ' )
39+ patch_ops = oci .identity_domains .models .PatchOp ()
40+ patch_ops .schemas = ["urn:ietf:params:scim:api:messages:2.0:PatchOp" ]
41+
42+ patch_ops_operations = []
43+ for k , v in change_in_capability .items ():
44+ patch_ops_operations .append (oci .identity_domains .models .Operations (
45+ op = "REPLACE" ,
46+ path = "urn:ietf:params:scim:schemas:oracle:idcs:extension:capabilities:User:" + k ,
47+ value = v
48+ )
49+ )
50+ patch_ops .operations = patch_ops_operations
51+ identity_domains_client .patch_user (user_id = user_ocid , patch_op = patch_ops )
52+
53+
54+ def handler (ctx , data : io .BytesIO = None ):
55+ try :
56+ # Extracting values from triggered OCI event
57+ domain_endpoints = []
58+ payload = False
59+ cfg = ctx .Config ()
60+ manage_capability = cfg ["manage_capability" ].split ("," )
61+ execution_mode = cfg ["execution_mode" ].strip ()
62+ tag_namespace = cfg ["tag_namespace" ].strip ()
63+
64+ try :
65+ body = json .loads (data .getvalue ())
66+ user_ocid = str (body ["data" ]["resourceId" ]).lstrip ()
67+ details = body ["data" ]["additionalDetails" ]
68+ domain_ocid = str (details ["domainId" ]).lstrip ()
69+ domain_ocids = [domain_ocid ]
70+ payload = True
71+ logging .getLogger ().info (f'Fixing capabilities for new user { user_ocid } ' )
72+ except Exception as ex :
73+ logging .getLogger ().info (ex )
74+ domain_ocids = cfg ["domain_ocids" ].split ("," )
75+
76+ for ocid in domain_ocids :
77+ logging .getLogger ().info (f'Processing domain ocid { str (ocid )} ' )
78+ domain_data = identity_client .get_domain (domain_id = ocid ).data
79+ url = domain_data .url
80+ domain_endpoint = (url .split (":443" ))[0 ]
81+ domain_endpoints .append (domain_endpoint )
82+
83+ for domain_endpoint in domain_endpoints :
84+
85+ identity_domains_client = oci .identity_domains .IdentityDomainsClient (config = {}, signer = signer ,
86+ service_endpoint = domain_endpoint
87+ )
88+
89+ if payload :
90+ users = [identity_domains_client .get_user (user_ocid ).data ]
91+ else :
92+ list_users_response = identity_domains_client .list_users ()
93+ users = list_users_response .data .resources
94+ while list_users_response .has_next_page :
95+ list_users_response = identity_domains_client .list_users (page = list_users_response .next_page )
96+ users .extend (list_users_response .data .resources )
97+ count = 0
98+
99+ for user in users :
100+ process_users (user , identity_domains_client ,tag_namespace ,manage_capability ,execution_mode )
101+ count += 1
102+
103+ logging .getLogger ().info (f'Processed { str (count )} users....' )
104+
105+
106+ except (Exception , ValueError ) as ex :
107+ logging .getLogger ().info ('error parsing json payload: ' + str (ex ))
108+
109+ return response .Response (ctx , response_data = json .dumps ({"message" : "success" }),headers = {"Content-Type" : "application/json" })
0 commit comments