Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AWS SES notifications (and process_recipient fix for previous PR) #810

Merged
merged 17 commits into from
Aug 27, 2020
Merged

Conversation

anandvenkatanarayanan
Copy link
Contributor

@anandvenkatanarayanan anandvenkatanarayanan commented Aug 6, 2020

This Pull request adds support for processing AWS SES notifications. To enable processing, the following steps are required:

  1. Create a hook API for parsing notifications (See example below)
  2. Add the hook API as a SNS end point (This has to be done in the AWS production terraform code once we update the hook location)
import json

from flask import Flask, request, Response

from aws_ses import Validator, ValidatorException, Type, SesEvent


class ValidatorApp(Flask):

    def __init__(self, import_name):
        super().__init__(import_name)
        topic_arn = TOPIC_ARN # Must be read from a CONFIG VARIABLE
        self.ses_validator = Validator(topics=[topic_arn])


app = ValidatorApp(__name__)


@app.route('/ses-event-processor', methods=["POST"])
def main():
    # Try to decode the JSON
    try:
        message = json.loads(request.data)
    except json.decoder.JSONDecodeError:
        error_msg = 'Request body is not in json format.'
        return Response(error_msg, status=400)

    # Validate message
    try:
        app.ses_validator.check(message)
    except ValidatorException as ex:
        return Response(ex.args, status=400)

    # Message Type
    m_type = message.get('Type')

    # Subscription confirmation
    if m_type == Type.SubscriptionConfirmation.value:
        resp = requests.get(message.get('SubscribeURL'))
        if resp.status_code != 200:
            return Response('Request to SubscribeURL failed. Unable to confirm subscription.', status=500)
        return Response('Subscription is successfully confirmed.', status=200)

    # unsubscribe confirmation.
    if m_type == Type.UnsubscribeConfirmation.value:
        resp = requests.get(message.get('UnsubscribeURL'))
        if resp.status_code != 200:
            return Response('Request to UnsubscribeURL failed. Unable to unsubscribe.', status=500)
        return Response('Successfully unsubscribed.', status=200)

    # This is a Notification and we need to process it.
    if m_type == Type.Notification.value:
        ses_event: SesEvent = SesEvent.from_json(message.get('Message'))
        # Add Processing code for ses_event
        return Response(f'Notification Processed for event {ses_event.eventType}', status=200)


if __name__ == '__main__':
    app.run()

anandv added 2 commits August 6, 2020 10:28
2. Also contains Unit Tests for Data classes and Notifications handler.

3. Example usage is available in the Pull request.
@anandvenkatanarayanan anandvenkatanarayanan marked this pull request as ready for review August 6, 2020 08:26
Copy link
Member

@jace jace left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cleaned this up and the tests are passing, but the Send/Delivery event looks suspicious. Is that how it's supposed to work?

funnel/transports/email/aws_ses/messages.py Outdated Show resolved Hide resolved
funnel/views/email_events.py Outdated Show resolved Hide resolved
@jace jace changed the title Aws SES Notifications AWS SES notifications (and process_recipient fix for previous PR) Aug 27, 2020
@jace jace merged commit e8137af into master Aug 27, 2020
@jace jace deleted the aws-ses branch August 27, 2020 21:59
vidya-ram pushed a commit that referenced this pull request Nov 23, 2021
Co-authored-by: anandv <anand@hasgeek.com>
Co-authored-by: Kiran Jonnalagadda <kiran@hasgeek.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants