Skip to content

Commit a10d6fb

Browse files
authored
Merge pull request #10 from alaniyonu/master
Support the option to add tags to newly created Detective graphs
2 parents d9f7f58 + a684f22 commit a10d6fb

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

enableDetective.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,14 @@ def _master_account_type(val: str, pattern: str = r'[0-9]{12}'):
4242
raise argparse.ArgumentTypeError
4343
return val
4444

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
4653
parser = argparse.ArgumentParser(description=('Link AWS Accounts to central '
4754
'Detective Account.'))
4855
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}'):
6168
help=('Don\'t send emails to the member accounts. Member '
6269
'accounts must still accept the invitation before '
6370
'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. \'=\')')
6476
return parser.parse_args(args)
6577

6678

@@ -291,15 +303,15 @@ def accept_invitations(role: str, accounts: typing.Set[str], graph: str, region:
291303
except Exception as e:
292304
logging.exception(f'error accepting invitation {e.args}')
293305

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):
295307
graphs = get_graphs(d_client)
296308

297309
if not graphs:
298310
confirm = input('Should Amazon Detective be enabled in {}? Enter [Y/N]: '.format(region))
299311

300312
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']]
303315
else:
304316
logging.info(f'Skipping {region}')
305317
return None
@@ -331,7 +343,7 @@ def enable_detective(d_client: botocore.client.BaseClient, region: str):
331343
for region in detective_regions:
332344
try:
333345
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)
335347

336348
if graphs is None:
337349
continue

tests/test_scripts.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ def test_setup_command_line_enableDetective():
2424

2525
args = enableDetective.setup_command_line(['--master_account', '000000000001', '--assume_role', 'detectiveAdmin', '--enabled_regions', 'us-east-1,us-east-2,us-west-2,ap-northeast-1,eu-west-1', '--input_file', 'accounts.csv'])
2626
assert args.master_account == '000000000001'
27+
assert args.tags == None
28+
29+
args = enableDetective.setup_command_line("--master_account 123456789012 --assume_role detectiveAdmin --input_file accounts.csv --tags TagKey1=TagValue1,TagKey2=TagValue2,TagKey3=TagValue3,TagKey4=,TagKey5=TagValue5,TagKey6".split(" "))
30+
assert args.tags == {
31+
"TagKey1": "TagValue1",
32+
"TagKey2": "TagValue2",
33+
"TagKey3": "TagValue3",
34+
"TagKey4": "",
35+
"TagKey5": "TagValue5",
36+
"TagKey6": "",
37+
}
2738

2839
args = enableDetective.setup_command_line(['--disable_email', '--master_account', '000000000001', '--assume_role', 'detectiveAdmin', '--input_file', 'accounts.csv'])
2940
assert args.disable_email == True

0 commit comments

Comments
 (0)