33import concurrent .futures
44import json
55import datetime
6+ import logging
7+ from tqdm import tqdm
8+
9+ log = logging .getLogger ('aws2tf' )
610
711
812def build_lists ():
9- print ("Building core resource lists ..." )
13+ log . info ("Building core resource lists ..." )
1014 context .tracking_message = "Stage 2 of 10, Building core resource lists ..."
1115
1216
@@ -20,7 +24,7 @@ def fetch_lambda_data():
2024 response .extend (page ['Functions' ])
2125 return [('lambda' , j ['FunctionName' ]) for j in response ]
2226 except Exception as e :
23- print ("Error fetching Lambda data:" , e )
27+ log . error ("Error fetching Lambda data: %s %s" , e )
2428 return []
2529
2630
@@ -34,7 +38,7 @@ def fetch_vpc_data():
3438 context .vpcs = response
3539 return [('vpc' , j ['VpcId' ]) for j in response ]
3640 except Exception as e :
37- print ("Error fetching ec2 data:" , e )
41+ log . error ("Error fetching ec2 data: %s %s" , e )
3842 return []
3943
4044 def fetch_s3_data ():
@@ -46,7 +50,7 @@ def fetch_s3_data():
4650 response .extend (page ['Buckets' ])
4751 return [('s3' , j ['Name' ]) for j in response ]
4852 except Exception as e :
49- print ("Error fetching s3 data:" , e )
53+ log . error ("Error fetching s3 data: %s %s" , e )
5054 return []
5155
5256 def fetch_sg_data ():
@@ -58,7 +62,7 @@ def fetch_sg_data():
5862 response .extend (page ['SecurityGroups' ])
5963 return [('sg' , j ['GroupId' ]) for j in response ]
6064 except Exception as e :
61- print ("Error fetching SG data:" , e )
65+ log . error ("Error fetching SG data: %s %s" , e )
6266 return []
6367
6468
@@ -75,7 +79,7 @@ def fetch_subnet_data():
7579 json .dump (response , f , indent = 2 , default = str )
7680 return [('subnet' , j ['SubnetId' ]) for j in response ]
7781 except Exception as e :
78- print ("Error fetching vpc data:" , e )
82+ log . error ("Error fetching vpc data: %s %s" , e )
7983 return []
8084
8185 def fetch_tgw_data ():
@@ -87,7 +91,7 @@ def fetch_tgw_data():
8791 response .extend (page ['TransitGateways' ])
8892 return [('tgw' , j ['TransitGatewayId' ]) for j in response ]
8993 except Exception as e :
90- print ("Error fetching transit gateways:" , e )
94+ log . error ("Error fetching transit gateways: %s %s" , e )
9195 return []
9296
9397 def fetch_roles_data ():
@@ -102,7 +106,7 @@ def fetch_roles_data():
102106 json .dump (response , f , indent = 2 , default = str )
103107 return [('iam' , j ['RoleName' ]) for j in response ]
104108 except Exception as e :
105- print ("Error fetching vpc data:" , e )
109+ log . error ("Error fetching vpc data: %s %s" , e )
106110 return []
107111
108112 def fetch_policies_data ():
@@ -114,7 +118,19 @@ def fetch_policies_data():
114118 response .extend (page ['Policies' ])
115119 return [('pol' , j ['Arn' ]) for j in response ]
116120 except Exception as e :
117- print ("Error fetching vpc data:" , e )
121+ log .error ("Error fetching vpc data: %s %s" , e )
122+ return []
123+
124+ def fetch_instprof_data ():
125+ try :
126+ client = boto3 .client ('iam' ,region_name = 'us-east-1' )
127+ response = []
128+ paginator = client .get_paginator ('list_instance_profiles' )
129+ for page in paginator .paginate ():
130+ response .extend (page ['InstanceProfiles' ])
131+ return [('inp' , j ['InstanceProfileName' ]) for j in response ]
132+ except Exception as e :
133+ log .error ("Error fetching vpc data: %s %s" , e )
118134 return []
119135
120136
@@ -128,7 +144,8 @@ def fetch_policies_data():
128144 executor .submit (fetch_subnet_data ),
129145 executor .submit (fetch_tgw_data ),
130146 executor .submit (fetch_roles_data ),
131- executor .submit (fetch_policies_data )
147+ executor .submit (fetch_policies_data ),
148+ executor .submit (fetch_instprof_data )
132149 ]
133150
134151 # Process results as they complete
@@ -149,13 +166,11 @@ def fetch_policies_data():
149166 elif resource_type == 's3' :
150167 client = boto3 .client ('s3' )
151168 for _ , bucket in result :
152- #here ?
153- #print("Buck from result=",bucket)
154169 try :
155170 ####### problematic call
156171 objs = client .list_objects_v2 (Bucket = bucket ,MaxKeys = 1 )
157172 except Exception as e :
158- print (f"Error details: { e } " )
173+ log . error (f"Error details: { e } " )
159174 continue
160175
161176 context .s3list [bucket ] = True
@@ -174,6 +189,9 @@ def fetch_policies_data():
174189 elif resource_type == 'pol' :
175190 for _ , policy_arn in result :
176191 context .policylist [policy_arn ] = True
192+ elif resource_type == 'inp' :
193+ for _ , inst_prof in result :
194+ context .inplist [inst_prof ] = True
177195 else :
178196 # Handle roles data
179197 with open ('imported/roles.json' , 'w' ) as f :
@@ -195,7 +213,7 @@ def fetch_policies_data():
195213def build_secondary_lists (id = None ):
196214 if id is None :
197215 st1 = datetime .datetime .now ()
198- print ("Building secondary IAM resource lists ..." )
216+ log . info ("Building secondary IAM resource lists ..." )
199217 context .esttime = (len (context .rolelist ) * 3 ) / 4
200218 context .tracking_message = "Stage 2 of 10, Building secondary IAM resource lists ..."
201219
@@ -214,7 +232,7 @@ def fetch_role_policies(role_name):
214232 'inline_policies' : inline_policies ['PolicyNames' ] if inline_policies ['PolicyNames' ] else False
215233 }
216234 except Exception as e :
217- print (f"Error fetching policies for role { role_name } : { e } " )
235+ log . error (f"Error fetching policies for role { role_name } : { e } " )
218236 return {
219237 'role_name' : role_name ,
220238 'attached_policies' : False ,
@@ -223,28 +241,29 @@ def fetch_role_policies(role_name):
223241
224242 # Use ThreadPoolExecutor to parallelize API calls
225243 rcl = len (context .rolelist )
244+ log .info (f"Fetching policies for { rcl } IAM roles..." )
245+
226246 with concurrent .futures .ThreadPoolExecutor (max_workers = context .cores ) as executor :
227247 # Submit all role policy fetch tasks
228248 future_to_role = {
229249 executor .submit (fetch_role_policies , role_name ): role_name
230250 for role_name in context .rolelist .keys ()
231251 }
232252
233- # Process results as they complete
234- completed = 0
235- for future in concurrent .futures .as_completed (future_to_role ):
236- completed += 1
237- context .tracking_message = f"Stage 2 of 10, Building secondary IAM resource lists... { completed } of { rcl } "
238-
253+ # Process results with progress bar
254+ for future in tqdm (concurrent .futures .as_completed (future_to_role ),
255+ total = len (future_to_role ),
256+ desc = "Fetching IAM policies" ,
257+ unit = "role" ):
239258 try :
240259 result = future .result ()
241260 role_name = result ['role_name' ]
242261 context .attached_role_policies_list [role_name ] = result ['attached_policies' ]
243262 context .role_policies_list [role_name ] = result ['inline_policies' ]
244263 except Exception as e :
245- print (f"Error processing result: { e } " )
264+ log . error (f"Error processing result: { e } " )
246265
247266 st2 = datetime .datetime .now ()
248- print ("secondary lists built in " + str (st2 - st1 ))
267+ log . info ("secondary lists built in " + str (st2 - st1 ))
249268
250269 return
0 commit comments