11import copy
22import time
33import logging
4- from typing import List , Tuple
4+ from typing import List
55from datetime import datetime , timezone
66
77from spaceone .inventory .connector .aws_iam_connector .schema .data import (
3333from spaceone .inventory .libs .connector import SchematicAWSConnector
3434from spaceone .inventory .libs .schema .resource import (
3535 ReferenceModel ,
36- ErrorResourceResponse ,
3736 CloudTrailModel ,
3837)
3938
@@ -249,10 +248,12 @@ def request_group_data(self, users, policies) -> List[Group]:
249248 matched_users = self ._get_matched_users_with_attached_user_info (
250249 users , group_user_info
251250 )
252- policy_infos = self .list_policy_with_group_name (group_name )
251+ attached_managed_policies = self .list_attached_managed_policy_to_group (group_name )
252+ attached_inline_policies = self .list_attached_inline_policy_to_group (group_name )
253+
253254 matched_policies = (
254255 self .get_matched_policies_with_attached_policy_info (
255- policies , policy_infos
256+ policies , attached_managed_policies , attached_inline_policies
256257 )
257258 )
258259
@@ -313,10 +314,12 @@ def request_user_data(self, policies):
313314 )
314315 sign_in_link = self ._get_sign_in_link (user_info .get ("Arn" ))
315316
316- attached_policies = self .list_attached_policy_to_user (user_name )
317+ attached_managed_policies = self .list_attached_managed_policy_to_user (user_name )
318+ attached_inline_policies = self .list_attached_inline_policy_to_user (user_name )
319+
317320 matching_policies = (
318321 self .get_matched_policies_with_attached_policy_info (
319- policies , attached_policies
322+ policies , attached_managed_policies , attached_inline_policies
320323 )
321324 )
322325
@@ -386,10 +389,12 @@ def request_role_data(self, policies) -> List[Role]:
386389 last_activity ,
387390 ) = self ._get_role_last_used_and_activity (role_info )
388391
389- attached_policies = self .list_attached_policy_to_role (role_name )
392+ attached_managed_policies = self .list_attached_managed_policy_to_role (role_name )
393+ attached_inline_policies = self .list_attached_inline_policy_to_role (role_name )
394+
390395 matched_policies = (
391396 self .get_matched_policies_with_attached_policy_info (
392- policies , attached_policies
397+ policies , attached_managed_policies , attached_inline_policies
393398 )
394399 )
395400 (
@@ -535,9 +540,6 @@ def list_local_managed_policies(self, **query):
535540 try :
536541 policy_arn = policy .get ("Arn" )
537542 description = self .list_policy_description (policy_arn )
538- query = self ._generate_key_query (
539- "PolicyArn" , policy_arn , "Scope" , is_paginate = True , ** query
540- )
541543
542544 permission_summary = self .list_policy_summary (
543545 policy_arn , policy .get ("DefaultVersionId" )
@@ -553,7 +555,7 @@ def list_local_managed_policies(self, **query):
553555 "cloudtrail" : self .set_cloudtrail (
554556 "us-east-1" , cloudtrail_resource_type , policy ["Arn" ]
555557 ),
556- "policy_type" : "Custom Managed" ,
558+ "policy_type" : "Customer Managed" ,
557559 }
558560 )
559561
@@ -680,7 +682,7 @@ def get_groups_for_user(self, groups):
680682 groups_for_user = []
681683 for group in groups :
682684 group_name = group .get ("GroupName" )
683- policy_vos = self .list_policy_with_group_name (group_name )
685+ policy_vos = self .list_attached_managed_policy_to_group (group_name )
684686 groups_for_user .append (
685687 {
686688 "group_name" : group_name ,
@@ -693,31 +695,57 @@ def get_groups_for_user(self, groups):
693695
694696 return groups_for_user
695697
696- def list_policy_with_group_name (self , group_name , ** query ):
697- policies = []
698- query = self ._generate_key_query (
699- "GroupName" , group_name , "" , is_paginate = True , ** query
700- )
701- paginator = self .client .get_paginator ("list_attached_group_policies" )
702- response_iterator = paginator .paginate (** query )
703-
704- for data in response_iterator :
705- policies .extend (data .get ("AttachedPolicies" , []))
706-
707- return policies
708-
709698 def list_role_info_with_role_name (self , role_name ):
710699 response = self .client .get_role (RoleName = role_name )
711700 return response .get ("Role" , {})
712701
713- def list_attached_policy_to_user (self , user_name ):
702+ def list_attached_managed_policy_to_group (self , group_name , ** query ):
703+ response = self .client .list_attached_group_policies (GroupName = group_name )
704+ return response .get ("AttachedPolicies" , [])
705+
706+ def list_attached_managed_policy_to_user (self , user_name ):
714707 response = self .client .list_attached_user_policies (UserName = user_name )
715708 return response .get ("AttachedPolicies" , [])
716709
717- def list_attached_policy_to_role (self , role_name ):
710+ def list_attached_managed_policy_to_role (self , role_name ):
718711 response = self .client .list_attached_role_policies (RoleName = role_name )
719712 return response .get ("AttachedPolicies" , [])
720713
714+ def list_attached_inline_policy_to_group (self , group_name , ** query ):
715+ response = self .client .list_group_policies (GroupName = group_name )
716+ policy_names = response .get ("PolicyNames" , [])
717+
718+ return self ._generate_policy_data (policy_names )
719+
720+ def list_attached_inline_policy_to_user (self , user_name ):
721+ response = self .client .list_user_policies (UserName = user_name )
722+ policy_names = response .get ("PolicyNames" , [])
723+
724+ return self ._generate_policy_data (policy_names )
725+
726+ def list_attached_inline_policy_to_role (self , role_name ):
727+ response = self .client .list_role_policies (RoleName = role_name )
728+ policy_names = response .get ("PolicyNames" , [])
729+
730+ return self ._generate_policy_data (policy_names )
731+
732+ @staticmethod
733+ def _generate_policy_data (policy_names ):
734+ policies = []
735+
736+ if policy_names :
737+ for policy_name in policy_names :
738+ print (policy_name )
739+ policy = {
740+ "PolicyName" : policy_name ,
741+ "policy_type" : "Customer Inline"
742+ }
743+
744+ policies .append (Policy (policy , strict = False ))
745+
746+ return policies
747+
748+
721749 def get_open_id_connect_provider_info_with_arn (self , oidcp_arn ):
722750 response = self .client .get_open_id_connect_provider (
723751 OpenIDConnectProviderArn = oidcp_arn
@@ -728,7 +756,7 @@ def get_access_key_last_used(self, access_key_id):
728756 response = self .client .get_access_key_last_used (AccessKeyId = access_key_id )
729757 return response .get ("AccessKeyLastUsed" , {})
730758
731- def list_policy_info (self , policy_arn ):
759+ def get_policy_info (self , policy_arn ):
732760 return self .client .get_policy (PolicyArn = policy_arn ).get ("Policy" , {})
733761
734762 def list_policy_description (self , policy_arn ):
@@ -769,32 +797,38 @@ def list_policy_summary(self, policy_arn, version_id):
769797 return return_value
770798
771799 def get_matched_policies_with_attached_policy_info (
772- self , policies , attached_policies
800+ self , policies , attached_managed_policies , attached_inline_policies
773801 ):
774802 matched_policies = []
775- attached_policy_arn = [
776- policy .get ("PolicyArn" , "" ) for policy in attached_policies
803+
804+ # Managed Policy
805+ attached_managed_policy_arn = [
806+ policy .get ("PolicyArn" , "" ) for policy in attached_managed_policies
777807 ]
778- for policy_arn in attached_policy_arn :
808+
809+ for policy_arn in attached_managed_policy_arn :
779810 policy = [p for p in policies if p .get ("arn" , "" ) == policy_arn ]
780811 if not policy :
781- new_policy = self .list_policy_info (policy_arn )
812+ aws_managed_policy = self .get_policy_info (policy_arn )
782813 permission_summary = self .list_policy_summary (
783- policy_arn , new_policy .get ("DefaultVersionId" )
814+ policy_arn , aws_managed_policy .get ("DefaultVersionId" )
784815 )
785- new_policy .update (
816+ aws_managed_policy .update (
786817 {
787818 "policy_usage" : self .list_policy_usage (policy_arn ),
788819 "permission" : permission_summary ,
789820 "permission_versions" : self .list_policy_versions (policy_arn ),
790821 "policy_type" : "AWS Managed" ,
791822 }
792823 )
793- policies .append (Policy (new_policy , strict = False ))
794- matched_policies .append (Policy (new_policy , strict = False ))
824+ matched_policies .append (Policy (aws_managed_policy , strict = False ))
795825 else :
796826 matched_policies .extend (policy )
797827
828+ #Inline Policy
829+ if attached_inline_policies :
830+ matched_policies .extend (attached_inline_policies )
831+
798832 return matched_policies
799833
800834 def list_policy_usage (self , policy_arn , ** query ):
0 commit comments