@@ -220,13 +220,19 @@ def create_users(config, verify):
220220 # user count
221221 user_count = 0
222222
223+ # updated bindings count
224+ update_binding_count = 0
225+
226+ # updated bindings names
227+ update_bindings_names = []
228+
223229 # form the goc db url
224230 goc_db_url = goc_db_url_arch .replace ("{{service-type}}" , srv_type )
225231 LOGGER .info ("\n Accessing url: " + goc_db_url )
226232 LOGGER .info ("\n Started the process for service-type: " + srv_type )
227233
228234 # grab the xml data from goc db
229- goc_request = requests .get (goc_db_url , verify = False )
235+ goc_request = requests .get (url = goc_db_url , cert = cert_creds , verify = False )
230236 LOGGER .info (goc_request .text )
231237
232238 # users from goc db that don't have a dn registered
@@ -260,7 +266,7 @@ def create_users(config, verify):
260266 site_url = goc_db_site_url .replace ("{{sitename}}" , site_name )
261267 goc_site_request = requests .get (site_url , cert = cert_creds , verify = False )
262268 site_xml_obj = ET .fromstring (goc_site_request .text )
263-
269+
264270 # check if the site is in production
265271 in_prod = site_xml_obj .find ("SITE" ).find ("PRODUCTION_INFRASTRUCTURE" )
266272 if in_prod .text != 'Production' :
@@ -269,7 +275,7 @@ def create_users(config, verify):
269275 # check for certified or uncertified
270276 cert_uncert = site_xml_obj .find ("SITE" ).find ("CERTIFICATION_STATUS" )
271277 if cert_uncert .text != "Certified" and cert_uncert .text != "Uncertified" :
272- raise Exception ("Neither certified not uncertified" )
278+ raise Exception ("Neither certified nor uncertified" )
273279
274280 contact_email = site_xml_obj .find ("SITE" ).find ("CONTACT_EMAIL" ).text
275281 site_contact_emails [site_name ] = contact_email
@@ -295,10 +301,8 @@ def create_users(config, verify):
295301 usr_create = {'projects' : [project ], 'email' : contact_email }
296302
297303 # create the user
298- ams_usr_crt_req = requests .post (
299- "https://" + ams_host + "/v1/users/" + user_binding_name +
300- "?key=" + ams_token ,
301- data = json .dumps (usr_create ), verify = verify )
304+ api_url = 'https://{0}/v1/projects/{1}/members/{2}?key={3}' .format (ams_host , ams_project , user_binding_name , ams_token )
305+ ams_usr_crt_req = requests .post (url = api_url , data = json .dumps (usr_create ), verify = verify )
302306 LOGGER .info (ams_usr_crt_req .text )
303307
304308 ams_user_uuid = ""
@@ -320,10 +324,8 @@ def create_users(config, verify):
320324
321325 # If the user already exists, Get user by username
322326 if ams_usr_crt_req .status_code == 409 :
323-
324- ams_usr_get_req = requests .get (
325- "https://" + ams_host + "/v1/users/" +
326- user_binding_name + "?key=" + ams_token , verify = verify )
327+ proj_member_list_url = "https://{0}/v1/projects/{1}/members/{2}?key={3}" .format (ams_host , ams_project , user_binding_name , ams_token )
328+ ams_usr_get_req = requests .get (url = proj_member_list_url , verify = verify )
327329
328330 # if the user retrieval was ok
329331 if ams_usr_get_req .status_code == 200 :
@@ -337,17 +339,16 @@ def create_users(config, verify):
337339
338340 # Create the respective AUTH binding
339341 bd_data = {
340- 'name' : user_binding_name ,
341342 'service_uuid' : authn_service_uuid ,
342343 'host' : authn_service_host ,
343344 'auth_identifier' : service_dn ,
344345 'unique_key' : ams_user_uuid ,
345346 "auth_type" : "x509"
346347 }
347-
348- authn_binding_crt_req = requests . post (
349- "https://" + authn_host + "/v1/bindings?key=" + authn_token ,
350- data = json .dumps (bd_data ), verify = verify )
348+
349+ create_binding_url = "https://{0}/v1/bindings/{1}?key={2}" . format ( authn_host , user_binding_name , authn_token )
350+
351+ authn_binding_crt_req = requests . post ( url = create_binding_url , data = json .dumps (bd_data ), verify = verify )
351352 LOGGER .info (authn_binding_crt_req .text )
352353
353354 # if the response is neither a 201(Created) nor a 409(already exists)
@@ -358,6 +359,32 @@ def create_users(config, verify):
358359 authn_binding_crt_req .text )
359360 continue
360361
362+ # if the binding already exists, check for an updated DN from gocdb
363+ if authn_binding_crt_req .status_code == 409 :
364+ retrieve_binding_url = "https://{0}/v1/bindings/{1}?key={2}" .format (authn_host , user_binding_name , authn_token )
365+ authn_ret_bind_req = requests .get (url = retrieve_binding_url , verify = verify )
366+ # if the binding retrieval was ok
367+ if authn_ret_bind_req .status_code == 200 :
368+ LOGGER .info ("\n Successfully retrieved binding {} from authn. Checking for DN update." .format (user_binding_name ))
369+ binding = authn_ret_bind_req .json ()
370+ # check if the dn has changed
371+ if binding ["auth_identifier" ] != service_dn :
372+ # update the respective binding with the new dn
373+ bind_upd_req_url = "https://{0}/v1/bindings/{1}?key={2}" .format (authn_host , user_binding_name , authn_token )
374+ upd_bd_data = {
375+ "auth_identifier" : service_dn
376+ }
377+ authn_bind_upd_req = requests .put (url = bind_upd_req_url , data = json .dumps (upd_bd_data ), verify = verify )
378+ LOGGER .info (authn_bind_upd_req .text )
379+ if authn_bind_upd_req .status_code == 200 :
380+ update_binding_count += 1
381+ update_bindings_names .append (user_binding_name )
382+ else :
383+ LOGGER .critical (
384+ "\n Could not retrieve binding {} from authn."
385+ "\n Response {}" .format (user_binding_name , authn_ret_bind_req .text ))
386+ continue
387+
361388 # since both the ams user was created or already existed AND the authn binding was created or already existed
362389 # move to topic and subscription creation
363390
@@ -442,6 +469,10 @@ def create_users(config, verify):
442469 LOGGER .critical ("Service Type: " + srv_type )
443470 LOGGER .critical ("Missing DNS: " + str (missing_dns ))
444471 LOGGER .critical ("Total Users Created: " + str (user_count ))
472+ LOGGER .critical ("Total Bindings Updated: " + str (update_binding_count ))
473+ LOGGER .critical ("Updated bingings: " + str (update_bindings_names ))
474+
475+
445476 LOGGER .critical ("-----------------------------------------" )
446477
447478
@@ -490,4 +521,4 @@ def main(args=None):
490521 "-verify" , "--Verify" , help = "SSL verification for requests" ,
491522 action = "store_true" )
492523
493- sys .exit (main (parser .parse_args ()))
524+ sys .exit (main (parser .parse_args ()))
0 commit comments