Skip to content

Commit a52e239

Browse files
authored
Merge pull request #4 from smsilb/master
Enable Amazon Detective in regions where no graph exists
2 parents d0034f6 + ff8c855 commit a52e239

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

disableDetective.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,9 @@ def delete_members(d_client: botocore.client.BaseClient, graph_arn: str,
277277
d_client = master_session.client('detective', region_name=region)
278278
graphs = get_graphs(d_client)
279279
if not graphs:
280-
logging.info(f'AWS Detective is NOT disabled in {region}')
280+
logging.info(f'Amazon Detective is NOT disabled in {region}')
281281
continue
282-
logging.info(f'AWS Detective is disabled in region {region}')
282+
logging.info(f'Amazon Detective is disabled in region {region}')
283283

284284
try:
285285

enableDetective.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,22 @@ def accept_invitations(role: str, accounts: typing.Set[str], graph: str, region:
286286
except Exception as e:
287287
logging.exception(f'error accepting invitation {e.args}')
288288

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+
289305
if __name__ == '__main__':
290306
args = setup_command_line()
291307
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:
310326
for region in detective_regions:
311327
try:
312328
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:
316332
continue
317-
logging.info(f'AWS Detective is enabled in region {region}')
318333

319334
try:
320335
all_members, pending = get_members(d_client, graphs)
321336

322337
for graph, members in all_members.items():
323338
new_accounts = create_members(
324339
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.")
326341
time.sleep(5)
327342
accept_invitations(args.assume_role, itertools.chain(
328343
new_accounts, pending[graph]), graph, region)

tests/test_scripts.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,17 @@ def test_create_members():
180180
created_members = enableDetective.create_members(d_client, "graph1", {"333333333333"}, {"111111111111": "1@gmail.com", "222222222222": "2@gmail.com"})
181181
assert created_members == {"222222222222"}
182182

183+
def test_enable_detective():
184+
d_client = Mock()
185+
186+
d_client.list_graphs.return_value = {'GraphList': []}
187+
d_client.create_graph.return_value = {'GraphArn': 'fooGraph123'}
188+
189+
graphs = enableDetective.enable_detective(d_client, "us-east-2")
183190

191+
assert graphs == ['fooGraph123']
192+
193+
184194

185195

186196

0 commit comments

Comments
 (0)