From b59f75abbf6ec529d1512aa1f4bbe01008a29502 Mon Sep 17 00:00:00 2001 From: cktricky Date: Sun, 3 Jun 2018 00:14:27 -0400 Subject: [PATCH] Fixes issue #50 --- libs/sns.py | 37 ++++++++++++++++++++++++++++--------- modules/sns.py | 7 +++++++ target.txt | 0 3 files changed, 35 insertions(+), 9 deletions(-) create mode 100644 target.txt diff --git a/libs/sns.py b/libs/sns.py index 140c94e..361f52e 100644 --- a/libs/sns.py +++ b/libs/sns.py @@ -11,20 +11,25 @@ regions = ['us-east-1', 'us-east-2', 'us-west-1', 'us-west-2', 'ca-central-1', ' session = boto3.Session() credentials = session.get_credentials() AWS_ACCESS_KEY_ID = credentials.access_key +topics_list = {} -def list_sns_topics(): +def list_sns_topics(should_i_print=True): title = "SNS Topics" - print(title) - print("-" * len(title)) + if should_i_print: + print(title) + print("-" * len(title)) try: for region in regions: client = boto3.client('sns', region_name=region) topics = client.list_topics() - print(region) - print("=" * len(region)) + if should_i_print: + print(region) + print("=" * len(region)) if topics['Topics']: - for topic in topics['Topics']: - print(topic) + topics_list[region] = topics['Topics'] + if should_i_print: + for topic in topics['Topics']: + print(topic) except botocore.exceptions.ClientError as e: if e.response['Error']['Code'] == 'InvalidClientTokenId': sys.exit("The AWS KEY IS INVALID. Exiting") @@ -79,6 +84,20 @@ def delete_sns_topic(topic, region): except KeyboardInterrupt: print("CTRL-C received, exiting...") +def list_all_sns_subscribers(): + print("Scanning regions....") + list_sns_topics(False) + for region,topics in topics_list.items(): + for topic in topics: + region_title = "Region: {}".format(region) + print(region_title) + print("=" * len(region_title)) + list_sns_subscribers(topic['TopicArn'],region) + + + + + def delete_sns_subscriber(endpoint, region): try: client = boto3.client('sns', region_name=region) @@ -90,8 +109,8 @@ def delete_sns_subscriber(endpoint, region): print('{} : Is NOT a root key' .format(AWS_ACCESS_KEY_ID)) elif e.response['Error']['Code'] == 'SubscriptionRequiredException': print('{} : Has permissions but isnt signed up for service - usually means you have a root account' .format(AWS_ACCESS_KEY_ID)) - #elif e.response['Error']['Code'] == 'InvalidParameter': - # print('The region you provided ({}) is invalid for the Subscriber endpoint. Are you sure this subscriber exists in this region?'.format(region)) + elif e.response['Error']['Code'] == 'InvalidParameter': + print('The region you provided ({}) is invalid for the Subscriber endpoint. Are you sure this subscriber exists in this region?'.format(region)) else: print("Unexpected error: {}" .format(e)) except KeyboardInterrupt: diff --git a/modules/sns.py b/modules/sns.py index cf9460d..b529bab 100644 --- a/modules/sns.py +++ b/modules/sns.py @@ -22,6 +22,13 @@ def module_sns_list_subscribers(*args): except IndexError: print("Please provide a topic arn *AND* region, ex: -a arn:aws:sns:us-east-1:123456789123:sometopic,us-east-1") +def module_sns_list_all_subscribers(): + ''' + Rather than listing a single topics subscribers, we'll list all topics and all subscribersself. + python3 weirdAAL.py -m sns_list_all_subscribers + ''' + list_all_sns_subscribers() + def module_sns_delete_topic(*args): ''' SNS delete a topic. Takes two arguments - the topic arn and the region. diff --git a/target.txt b/target.txt new file mode 100644 index 0000000..e69de29