@@ -520,15 +520,52 @@ def _no_data():
520520
521521 return all_tables
522522
523- def update_glue_catalog_resource_policy (self , region : str , producer_account_id : str , consumer_account_id : str ,
524- database_name : str , tables : list ):
523+ def write_glue_catalog_resource_policy (self , policy : dict , current_hash : str = None ):
524+ '''
525+ Write a new glue catalog policy document. This is a low level interface that just performs the mechanic of
526+ correctly writing the supplied policy, including where a hash must be supplied.
527+ :param policy:
528+ :param current_hash:
529+ :return:
530+ '''
525531 glue_client = self ._get_client ('glue' )
526- new_resource_policy = None
527- current_resource_policy = None
532+
528533 try :
529534 current_resource_policy = glue_client .get_resource_policy ()
535+
536+ # if no external hash has been provided, then just use the current hash from the doc.
537+ if current_hash is None :
538+ current_hash = current_resource_policy .get ('PolicyHash' )
539+
540+ glue_client .put_resource_policy (
541+ PolicyInJson = json .dumps (policy ),
542+ PolicyHashCondition = current_hash ,
543+ PolicyExistsCondition = 'MUST_EXIST' ,
544+ EnableHybrid = 'TRUE'
545+ )
530546 except glue_client .exceptions .EntityNotFoundException :
531- pass
547+ # write the resource policy as new
548+ glue_client .put_resource_policy (
549+ PolicyInJson = json .dumps (policy ),
550+ PolicyExistsCondition = 'NOT_EXIST' ,
551+ EnableHybrid = 'TRUE'
552+ )
553+
554+ def get_current_glue_policy (self ):
555+ glue_client = self ._get_client ('glue' )
556+
557+ try :
558+ current_resource_policy = glue_client .get_resource_policy ()
559+ glue_policy = json .loads (current_resource_policy .get ('PolicyInJson' ))
560+ current_hash = current_resource_policy .get ('PolicyHash' )
561+
562+ return glue_policy , current_hash
563+ except glue_client .exceptions .EntityNotFoundException :
564+ return None , None
565+
566+ def add_tbac_glue_catalog_resource_policy (self , region : str , producer_account_id : str , consumer_account_id : str ,
567+ database_name : str , tables : list ):
568+ current_resource_policy , current_hash = self .get_current_glue_policy ()
532569
533570 cf = {
534571 'region' : region ,
@@ -542,44 +579,33 @@ def update_glue_catalog_resource_policy(self, region: str, producer_account_id:
542579 cf ['table_list' ] = tables
543580 policy = json .loads (utils .generate_policy ('lf_cross_account_tbac.pystache' , config = cf ))
544581
545- policy_condition = None
546582 if current_resource_policy is None :
547583 new_resource_policy = {
548584 "Version" : "2012-10-17" ,
549585 "Statement" : policy
550586 }
551- glue_client .put_resource_policy (
552- PolicyInJson = json .dumps (new_resource_policy ),
553- PolicyExistsCondition = 'NOT_EXIST' ,
554- EnableHybrid = 'TRUE'
587+ self .write_glue_catalog_resource_policy (
588+ policy = new_resource_policy
555589 )
556590 self ._logger .info (
557591 f"Created new Catalog Resource Policy on { producer_account_id } allowing Tag Based Access by { consumer_account_id } " )
558592 else :
559- new_resource_policy = json .loads (current_resource_policy .get ('PolicyInJson' ))
560- current_hash = current_resource_policy .get ('PolicyHash' )
561-
562593 update_statement , policy_index , did_modification = self ._get_glue_resource_policy_statement_to_modify (
563594 region = region ,
564- policy = new_resource_policy , producer_account_id = producer_account_id ,
595+ policy = current_resource_policy , producer_account_id = producer_account_id ,
565596 consumer_account_id = consumer_account_id ,
566597 database_name = database_name , tables = tables
567598 )
568599
569600 # add the new statement
570601 if update_statement is None :
571- new_resource_policy ['Statement' ].append (policy )
602+ current_resource_policy ['Statement' ].append (policy )
572603 did_modification = True
573604 elif update_statement is not None :
574- new_resource_policy ['Statement' ][policy_index ] = update_statement
605+ current_resource_policy ['Statement' ][policy_index ] = update_statement
575606
576607 if did_modification is True :
577- glue_client .put_resource_policy (
578- PolicyInJson = json .dumps (new_resource_policy ),
579- PolicyHashCondition = current_hash ,
580- PolicyExistsCondition = 'MUST_EXIST' ,
581- EnableHybrid = 'TRUE'
582- )
608+ self .write_glue_catalog_resource_policy (current_hash = current_hash , policy = current_resource_policy )
583609 self ._logger .info (
584610 f"Updated Catalog Resource Policy on { producer_account_id } allowing Tag Based Access by { consumer_account_id } " )
585611
0 commit comments