@@ -286,6 +286,22 @@ def accept_invitations(role: str, accounts: typing.Set[str], graph: str, region:
286
286
except Exception as e :
287
287
logging .exception (f'error accepting invitation { e .args } ' )
288
288
289
+ def enable_detective (d_client : botocore .client .BaseClient , region : str ):
290
+ graphs = get_graphs (d_client )
291
+
292
+ if not graphs :
293
+ confirm = input ('Should Amazon Detective be enabled in {}? Enter [Y/N]: ' .format (region ))
294
+
295
+ if confirm == 'Y' or confirm == 'y' :
296
+ logging .info (f'Enabling Amazon Detective in { region } ' )
297
+ graphs = [d_client .create_graph ()['GraphArn' ]]
298
+ else :
299
+ logging .info (f'Skipping { region } ' )
300
+ return None
301
+ logging .info (f'Amazon Detective is enabled in region { region } ' )
302
+
303
+ return graphs
304
+
289
305
if __name__ == '__main__' :
290
306
args = setup_command_line ()
291
307
aws_account_dict = read_accounts_csv (args .input_file )
@@ -310,19 +326,18 @@ def accept_invitations(role: str, accounts: typing.Set[str], graph: str, region:
310
326
for region in detective_regions :
311
327
try :
312
328
d_client = master_session .client ('detective' , region_name = region )
313
- graphs = get_graphs (d_client )
314
- if not graphs :
315
- logging . info ( f'AWS Detective is NOT enabled in { region } ' )
329
+ graphs = enable_detective (d_client , region )
330
+
331
+ if graphs is None :
316
332
continue
317
- logging .info (f'AWS Detective is enabled in region { region } ' )
318
333
319
334
try :
320
335
all_members , pending = get_members (d_client , graphs )
321
336
322
337
for graph , members in all_members .items ():
323
338
new_accounts = create_members (
324
339
d_client , graph , members , aws_account_dict )
325
- print ("Sleeping for 5s to allow new members' invitations to propagate through DDB ." )
340
+ print ("Sleeping for 5s to allow new members' invitations to propagate." )
326
341
time .sleep (5 )
327
342
accept_invitations (args .assume_role , itertools .chain (
328
343
new_accounts , pending [graph ]), graph , region )
0 commit comments