@@ -42,7 +42,14 @@ def _master_account_type(val: str, pattern: str = r'[0-9]{12}'):
42
42
raise argparse .ArgumentTypeError
43
43
return val
44
44
45
- # Setup command line arguments
45
+ class ParseCommaSeparatedKeyValuePairsAction (argparse .Action ):
46
+ def __call__ (self , parser , namespace , values , option_string = None ):
47
+ setattr (namespace , self .dest , dict ())
48
+ for kv_pairs in values .split ("," ):
49
+ key , _ , value = kv_pairs .partition ('=' )
50
+ getattr (namespace , self .dest )[key ] = value
51
+
52
+ # Setup command line arguments
46
53
parser = argparse .ArgumentParser (description = ('Link AWS Accounts to central '
47
54
'Detective Account.' ))
48
55
parser .add_argument ('--master_account' , type = _master_account_type ,
@@ -61,6 +68,11 @@ def _master_account_type(val: str, pattern: str = r'[0-9]{12}'):
61
68
help = ('Don\' t send emails to the member accounts. Member '
62
69
'accounts must still accept the invitation before '
63
70
'they are added to the behavior graph.' ))
71
+ parser .add_argument ('--tags' ,
72
+ action = ParseCommaSeparatedKeyValuePairsAction ,
73
+ help = 'Comma-separated list of tag key-value pairs to be added '
74
+ 'to any newly enabled Detective graphs. Values are optional '
75
+ 'and are separated from keys by the equal sign (i.e. \' =\' )' )
64
76
return parser .parse_args (args )
65
77
66
78
@@ -291,15 +303,15 @@ def accept_invitations(role: str, accounts: typing.Set[str], graph: str, region:
291
303
except Exception as e :
292
304
logging .exception (f'error accepting invitation { e .args } ' )
293
305
294
- def enable_detective (d_client : botocore .client .BaseClient , region : str ):
306
+ def enable_detective (d_client : botocore .client .BaseClient , region : str , tags : dict = None ):
295
307
graphs = get_graphs (d_client )
296
308
297
309
if not graphs :
298
310
confirm = input ('Should Amazon Detective be enabled in {}? Enter [Y/N]: ' .format (region ))
299
311
300
312
if confirm == 'Y' or confirm == 'y' :
301
- logging .info (f'Enabling Amazon Detective in { region } ' )
302
- graphs = [d_client .create_graph ()['GraphArn' ]]
313
+ logging .info (f'Enabling Amazon Detective in { region } ' + ( f' with tags { tags } ' if tags else '' ) )
314
+ graphs = [d_client .create_graph (Tags = tags )['GraphArn' ]]
303
315
else :
304
316
logging .info (f'Skipping { region } ' )
305
317
return None
@@ -331,7 +343,7 @@ def enable_detective(d_client: botocore.client.BaseClient, region: str):
331
343
for region in detective_regions :
332
344
try :
333
345
d_client = master_session .client ('detective' , region_name = region )
334
- graphs = enable_detective (d_client , region )
346
+ graphs = enable_detective (d_client , region , args . tags )
335
347
336
348
if graphs is None :
337
349
continue
0 commit comments