Files
notifications-api/app/notifications/sns_handlers.py

83 lines
2.7 KiB
Python
Raw Normal View History

2022-10-03 09:05:34 -07:00
import enum
from json import decoder
import requests
from flask import current_app, json
from app.errors import InvalidRequest
2022-10-03 17:16:59 -07:00
from app.notifications.sns_cert_validator import validate_sns_cert
2022-10-03 09:05:34 -07:00
class SNSMessageType(enum.Enum):
2023-08-29 14:54:30 -07:00
SubscriptionConfirmation = "SubscriptionConfirmation"
Notification = "Notification"
UnsubscribeConfirmation = "UnsubscribeConfirmation"
2022-10-03 09:05:34 -07:00
class InvalidMessageTypeException(Exception):
pass
def verify_message_type(message_type: str):
try:
SNSMessageType(message_type)
except ValueError:
raise InvalidRequest("SES-SNS callback failed: invalid message type", 400)
def sns_notification_handler(data, headers):
2023-08-29 14:54:30 -07:00
message_type = headers.get("x-amz-sns-message-type")
2022-10-03 09:05:34 -07:00
try:
verify_message_type(message_type)
except InvalidMessageTypeException:
2023-08-29 14:54:30 -07:00
current_app.logger.exception(
f"Response headers: {headers}\nResponse data: {data}"
)
2022-10-03 09:05:34 -07:00
raise InvalidRequest("SES-SNS callback failed: invalid message type", 400)
try:
2023-08-29 14:54:30 -07:00
message = json.loads(data.decode("utf-8"))
2022-10-03 09:05:34 -07:00
except decoder.JSONDecodeError:
2023-08-29 14:54:30 -07:00
current_app.logger.exception(
f"Response headers: {headers}\nResponse data: {data}"
)
2022-10-03 09:05:34 -07:00
raise InvalidRequest("SES-SNS callback failed: invalid JSON given", 400)
try:
validate_sns_cert(message)
2024-08-15 10:40:26 -07:00
except Exception:
2022-10-14 14:45:27 +00:00
current_app.logger.error(
"SES-SNS callback failed: validation failed with error: Signature validation failed"
2022-10-14 14:45:27 +00:00
)
2022-10-03 09:05:34 -07:00
raise InvalidRequest("SES-SNS callback failed: validation failed", 400)
2023-08-29 14:54:30 -07:00
if message.get("Type") == "SubscriptionConfirmation":
2022-10-03 17:16:59 -07:00
# NOTE once a request is sent to SubscribeURL, AWS considers Notify a confirmed subscriber to this topic
2023-08-29 14:54:30 -07:00
url = (
message.get("SubscribeUrl")
if "SubscribeUrl" in message
else message.get("SubscribeURL")
)
2025-04-01 08:53:33 -07:00
response = requests.get(url, timeout=30)
2022-10-03 09:05:34 -07:00
try:
response.raise_for_status()
except Exception as e:
2022-10-14 14:45:27 +00:00
current_app.logger.warning(
2022-10-21 13:29:52 +00:00
f"Attempt to raise_for_status()SubscriptionConfirmation Type "
f"message files for response: {response.text} with error {e}"
2022-10-14 14:45:27 +00:00
)
raise InvalidRequest(
2022-10-21 13:29:52 +00:00
"SES-SNS callback failed: attempt to raise_for_status()SubscriptionConfirmation "
2023-08-29 14:54:30 -07:00
"Type message failed",
400,
2022-10-14 14:45:27 +00:00
)
2022-10-03 09:05:34 -07:00
current_app.logger.info("SES-SNS auto-confirm subscription callback succeeded")
return message
# TODO remove after smoke testing on prod is implemented
2023-08-29 14:54:30 -07:00
current_app.logger.info(
f"SNS message: {message} is a valid message. Attempting to process it now."
)
2022-10-14 14:45:27 +00:00
2022-10-03 09:05:34 -07:00
return message