Forked from hide32767/confirm_sns_subs_with_disabled_unsubs_link.py
Last active
October 4, 2022 12:46
-
-
Save mrtaxi/22bbd33cb2add68d283aac1cc771ca5c to your computer and use it in GitHub Desktop.
Confirmation of Amazon SNS Subscription with disabled `Unsubscribe` Link
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# | |
# Confirmation of Amazon SNS Subscription with disabled `Unsubscribe` Link. | |
# cf. https://aws.amazon.com/premiumsupport/knowledge-center/prevent-unsubscribe-all-sns-topic/ | |
# | |
# Usage: ./confirm_sns_subs_with_disabled_unsubs_link.py "URL_to_confirm_SNS_Subscrption from e-mail" | |
# | |
#import pdb | |
#pdb.set_trace() | |
import sys | |
import boto3 | |
from urllib.parse import urlparse, parse_qs | |
url_to_confirm_sns_subs = sys.argv[1] | |
session = boto3.Session(profile_name='danads') #change profile_name for YOUR AWS profile!!! | |
# To parse the URL and query. | |
# Notice: items parsed query are 'list' type. | |
# | |
# >>> parsed_query | |
# {'TopicArn': ['arn:aws:sns:region-name:012345678901:your-topic-name'], | |
# 'Token': ['<[0-9a-f]+>'], | |
# 'Endpoint': ['<e.g. mailaddr>']} | |
parsed_query = parse_qs(urlparse(url_to_confirm_sns_subs).query) | |
# To get region-name from ARN. | |
# cf. https://docs.aws.amazon.com/IAM/latest/UserGuide/list_amazonsns.html#amazonsns-resources-for-iam-policies | |
region = parsed_query['TopicArn'][0].split(':')[3] | |
# To initialize SNS Client with region-name | |
sns = boto3.client('sns', region_name=region) | |
# Confirmation | |
response = sns.confirm_subscription( | |
TopicArn=parsed_query['TopicArn'][0], | |
Token=parsed_query['Token'][0], | |
AuthenticateOnUnsubscribe='true' | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment