Skip to content

Instantly share code, notes, and snippets.

@flibustenet
Last active June 13, 2018 21:00
Show Gist options
  • Save flibustenet/7be52636979ed4e365162e06664eeef7 to your computer and use it in GitHub Desktop.
Save flibustenet/7be52636979ed4e365162e06664eeef7 to your computer and use it in GitHub Desktop.
sns aws
package sns
import (
"io/ioutil"
"strings"
"testing"
)
var sns_delivery = `
{
"Type" : "Notification",
"MessageId" : "39ab7c5d-4586-5174-81f0-2f329ff09218",
"TopicArn" : "arn:aws:sns:eu-west-1:542719421142:medibounce",
"Message" : "{\"notificationType\":\"Delivery\",\"mail\":{\"timestamp\":\"2018-05-11T17:51:50.105Z\",\"source\":\"[email protected]\",\"sourceArn\":\"arn:aws:ses:eu-west-1:xyz:identity/mydomain.com\",\"sourceIp\":\"12.23.45.65\",\"sendingAccountId\":\"542719421142\",\"messageId\":\"0102016350549359-877314b4-8484-46be-a92c-42057ffe7c6f-000000\",\"destination\":[\"[email protected]\"],\"headersTruncated\":false,\"headers\":[{\"name\":\"Received\",\"value\":\"from localhost (vro12-1-12.23.34.56.fbx.proxad.net [12.23.34.56]) by email-smtp.amazonaws.com with SMTP (SimpleEmailService-2762311919) id ZhtyeF1Yu5lS8YpYy402 for [email protected]; Fri, 11 May 2018 17:51:50 +0000 (UTC)\"},{\"name\":\"Mime-Version\",\"value\":\"1.0\"},{\"name\":\"Date\",\"value\":\"Fri, 11 May 2018 19:51:50 +0200\"},{\"name\":\"From\",\"value\":\"[email protected]\"},{\"name\":\"To\",\"value\":\"[email protected]\"},{\"name\":\"Subject\",\"value\":\"mimi\"},{\"name\":\"X-Agence\",\"value\":\"35\"},{\"name\":\"Content-Type\",\"value\":\"text/plain; charset=UTF-8\"},{\"name\":\"Content-Transfer-Encoding\",\"value\":\"quoted-printable\"}],\"commonHeaders\":{\"from\":[\"[email protected]\"],\"date\":\"Fri, 11 May 2018 19:51:50 +0200\",\"to\":[\"[email protected]\"],\"subject\":\"mimi\"}},\"delivery\":{\"timestamp\":\"2018-05-11T17:51:50.872Z\",\"processingTimeMillis\":767,\"recipients\":[\"[email protected]\"],\"smtpResponse\":\"250 2.6.0 Message received\",\"remoteMtaIp\":\"72.21.215.231\",\"reportingMTA\":\"a7-18.smtp-out.eu-west-1.amazonses.com\"}}",
"Timestamp" : "2018-05-11T17:51:50.927Z",
"SignatureVersion" : "1",
"Signature" : "uJ7k40HHxxJ0oqztvsd93jhMIFgowGhsLRnaWTbyC1u8fDc1ZMT5Jvfgz3BM5LiwmBlbq4ISXTonHknjO/A+z0kBfQBWIfm4oMPWXYmKL5/pFwArCfIZD7eVus9iCtgt0Vn+VgRX0+oMiKfKTRSJwdTO99dJnlL9fqO3pQnCNp3PHhjfALlVwtsnZKRx88Qbv42G+x1rb6r48HfaT9pxFqrlkG+aGyK9G8pOnA40ePkY0fiY9FzinqDg6ajwQdBak6UtLErNKiPae2RjTD+q5fWwmcYiUxeiIdDVBMdAP77u9SjjowzPjRcAt4myeLxxZ6D+nRkmSZom+UVS4jVpbg==",
"SigningCertURL" : "https://sns.eu-west-1.amazonaws.com/SimpleNotificationService-eaea6120e66ea12e88dcd8bcbddca752.pem",
"UnsubscribeURL" : "https://sns.eu-west-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:eu-west-1:542719421142:medibounce:1814bbfd-58a9-4651-bac9-6d87e3a99804"
}`
var sns_complaint = `
{
"Type" : "Notification",
"MessageId" : "828b098d-1d8f-5f3e-9a4c-5ae2c738c83c",
"TopicArn" : "arn:aws:sns:eu-west-1:542719421142:medibounce",
"Message" : "{\"notificationType\":\"Complaint\",\"complaint\":{\"complainedRecipients\":[{\"emailAddress\":\"[email protected]\"}],\"timestamp\":\"2018-05-11T17:52:54.000Z\",\"feedbackId\":\"0102016350559118-caf8c3be-c523-4019-852a-a217515038de-000000\",\"userAgent\":\"Amazon SES Mailbox Simulator\",\"complaintFeedbackType\":\"abuse\"},\"mail\":{\"timestamp\":\"2018-05-11T17:52:53.000Z\",\"source\":\"[email protected]\",\"sourceArn\":\"arn:aws:ses:eu-west-1:542719421142:identity/mydomain.com\",\"sourceIp\":\"12.23.34.56\",\"sendingAccountId\":\"542719421142\",\"messageId\":\"0102016350558b21-639ac2d5-0925-4f23-9e45-07f5d9a4f8ad-000000\",\"destination\":[\"[email protected]\"],\"headersTruncated\":false,\"headers\":[{\"name\":\"Received\",\"value\":\"from localhost (vro12-.fbx.proxad.net [12.23.45.43]) by email-smtp.amazonaws.com with SMTP (SimpleEmailService-2762311919) id GtbnIKzi4cdzNrdrOq59 for [email protected]; Fri, 11 May 2018 17:52:53 +0000 (UTC)\"},{\"name\":\"Mime-Version\",\"value\":\"1.0\"},{\"name\":\"Date\",\"value\":\"Fri, 11 May 2018 19:52:53 +0200\"},{\"name\":\"From\",\"value\":\"[email protected]\"},{\"name\":\"To\",\"value\":\"[email protected]\"},{\"name\":\"Subject\",\"value\":\"mimi\"},{\"name\":\"X-Agence\",\"value\":\"35\"},{\"name\":\"Content-Type\",\"value\":\"text/plain; charset=UTF-8\"},{\"name\":\"Content-Transfer-Encoding\",\"value\":\"quoted-printable\"}],\"commonHeaders\":{\"from\":[\"[email protected]\"],\"date\":\"Fri, 11 May 2018 19:52:53 +0200\",\"to\":[\"[email protected]\"],\"subject\":\"mimi\"}}}",
"Timestamp" : "2018-05-11T17:52:55.189Z",
"SignatureVersion" : "1",
"Signature" : "fCW9M2N2IPVq7JN8QxW9wVJwJdTgXFeoG/PXP3eczwxrE/W6BDOM/wvtPnXXvIk4iRPzWVRRcqyHNgwao9pv409qjxEUPzDfW40dxZmF/quTHj49s3h8UTXFQ1MFiDrI+EbMZdnXn7RdFpCIorwySc3zZevdEdTKHCXTp6wdjj/mGLjf8dIeRHlDfJinZtu7m4T+FQUHmmxZCL/7TAIxR6ZkxOTMvXtOrFyMRRObsHBXhpbydXShe9EqzL0Ronmfir8IkuVhFv3JvYbDTi98dLvz44baKPWUqICWeX44DU3pUoNKLqvpyKp58JYCAVi4AMI/SpjWZygc13/Vyl6KEw==",
"SigningCertURL" : "https://sns.eu-west-1.amazonaws.com/SimpleNotificationService-eaea6120e66ea12e88dcd8bcbddca752.pem",
"UnsubscribeURL" : "https://sns.eu-west-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:eu-west-1:542719421142:medibounce:1814bbfd-58a9-4651-bac9-6d87e3a99804"
}`
var sns_transient = `
{
"Type" : "Notification",
"MessageId" : "f4613286-6191-5586-8992-61e9bb560afb",
"TopicArn" : "arn:aws:sns:eu-west-1:542719421142:medibounce",
"Message" : "{\"notificationType\":\"Bounce\",\"bounce\":{\"bounceType\":\"Transient\",\"bounceSubType\":\"General\",\"bouncedRecipients\":[{\"emailAddress\":\"[email protected]\"}],\"timestamp\":\"2018-05-11T17:53:10.000Z\",\"feedbackId\":\"010201635055d03d-aff7692e-6fa8-44d9-a2e1-b73dc4b862dc-000000\"},\"mail\":{\"timestamp\":\"2018-05-11T17:53:10.000Z\",\"source\":\"[email protected]\",\"sourceArn\":\"arn:aws:ses:eu-west-1:542719421142:identity/mydomain.com\",\"sourceIp\":\"12.23.34.56\",\"sendingAccountId\":\"542719421142\",\"messageId\":\"010201635055cbcc-b847c887-b663-479d-a57f-eaf6e3ac670d-000000\",\"destination\":[\"[email protected]\"],\"headersTruncated\":false,\"headers\":[{\"name\":\"Received\",\"value\":\"from localhost (vro12-1-12.23.34.56.fbx.proxad.net [12.23.34.56]) by email-smtp.amazonaws.com with SMTP (SimpleEmailService-3043523693) id yu5OZeYqxhvM275YJA0H for [email protected]; Fri, 11 May 2018 17:53:10 +0000 (UTC)\"},{\"name\":\"Mime-Version\",\"value\":\"1.0\"},{\"name\":\"Date\",\"value\":\"Fri, 11 May 2018 19:53:10 +0200\"},{\"name\":\"X-Agence\",\"value\":\"35\"},{\"name\":\"From\",\"value\":\"[email protected]\"},{\"name\":\"To\",\"value\":\"[email protected]\"},{\"name\":\"Subject\",\"value\":\"mimi\"},{\"name\":\"Content-Type\",\"value\":\"text/plain; charset=UTF-8\"},{\"name\":\"Content-Transfer-Encoding\",\"value\":\"quoted-printable\"}],\"commonHeaders\":{\"from\":[\"[email protected]\"],\"date\":\"Fri, 11 May 2018 19:53:10 +0200\",\"to\":[\"[email protected]\"],\"subject\":\"mimi\"}}}",
"Timestamp" : "2018-05-11T17:53:11.305Z",
"SignatureVersion" : "1",
"Signature" : "zs5SVwS10KiOMpErkUDrNn6Z01CoZjRAqEnBeyLlzlZquX33WDk9dS7IftNzrVs9Y3oJHz2PARLGEN+9EIxKWYGkMn/poVi6m3m6gwu2xeX+gDm356jUOMk8HSxwAyVFvDNx6c/hLDN6Hb3e9JDJx1HEx6WoeoQdW3zcLuPh5na1OEqQac+2ZdQr/0FwDXx91z+ugnrJKNgmg3JKEuFFClW3XUqHfflBVF2ikNohXQI5+KDf8vMR8PdupQBz6Li5Qf0Wth5LsbucqQpr379irioiTZm/yCjx1JoWPO9u9FkLomJQ0KFAl7JF3XkTgRn90lcLDy40+I0OyjNtYcUNKg==",
"SigningCertURL" : "https://sns.eu-west-1.amazonaws.com/SimpleNotificationService-eaea6120e66ea12e88dcd8bcbddca752.pem",
"UnsubscribeURL" : "https://sns.eu-west-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:eu-west-1:542719421142:medibounce:1814bbfd-58a9-4651-bac9-6d87e3a99804"
}
`
var sns_permanent = `
{
"Type" : "Notification",
"MessageId" : "d97b96db-bd1b-5ef0-99a6-c5b45cf7c83a",
"TopicArn" : "arn:aws:sns:eu-west-1:542719421142:medibounce",
"Message" : "{\"notificationType\":\"Bounce\",\"bounce\":{\"bounceType\":\"Permanent\",\"bounceSubType\":\"General\",\"bouncedRecipients\":[{\"emailAddress\":\"[email protected]\",\"action\":\"failed\",\"status\":\"5.1.1\",\"diagnosticCode\":\"smtp; 550 5.1.1 user unknown\"}],\"timestamp\":\"2018-05-11T18:23:40.216Z\",\"feedbackId\":\"010201635071b89e-828467cf-dd98-4319-8cf3-6a28dea8294c-000000\",\"remoteMtaIp\":\"205.251.242.49\",\"reportingMTA\":\"dsn; a7-12.smtp-out.eu-west-1.amazonses.com\"},\"mail\":{\"timestamp\":\"2018-05-11T18:23:39.000Z\",\"source\":\"[email protected]\",\"sourceArn\":\"arn:aws:ses:eu-west-1:542719421142:identity/mydomain.com\",\"sourceIp\":\"12.23.34.56\",\"sendingAccountId\":\"542719421142\",\"messageId\":\"010201635071b5cf-52f5447e-9fe3-4654-8ef4-7b40019f11c8-000000\",\"destination\":[\"[email protected]\"],\"headersTruncated\":false,\"headers\":[{\"name\":\"Received\",\"value\":\"from localhost (vro12-1-12.23.34.56.fbx.proxad.net [12.23.34.56]) by email-smtp.amazonaws.com with SMTP (SimpleEmailService-2762311919) id MO3mLCJHaZ0pZ9hNw89X for [email protected]; Fri, 11 May 2018 18:23:39 +0000 (UTC)\"},{\"name\":\"Mime-Version\",\"value\":\"1.0\"},{\"name\":\"Date\",\"value\":\"Fri, 11 May 2018 20:23:39 +0200\"},{\"name\":\"From\",\"value\":\"[email protected]\"},{\"name\":\"To\",\"value\":\"[email protected]\"},{\"name\":\"Subject\",\"value\":\"mimi\"},{\"name\":\"X-Agence\",\"value\":\"35\"},{\"name\":\"Content-Type\",\"value\":\"text/plain; charset=UTF-8\"},{\"name\":\"Content-Transfer-Encoding\",\"value\":\"quoted-printable\"}],\"commonHeaders\":{\"from\":[\"[email protected]\"],\"date\":\"Fri, 11 May 2018 20:23:39 +0200\",\"to\":[\"[email protected]\"],\"subject\":\"mimi\"}}}",
"Timestamp" : "2018-05-11T18:23:40.251Z",
"SignatureVersion" : "1",
"Signature" : "sOTiCWemov15A7bHP+mvaV3+hv8vDpugL6q4LQhuFp7AVaIPnmipZoQv7nxl2gEmH8oW/dxXvg45vaGlIs8T0PeeoUn4Fc4Qn88NjvhVgdPIjNgbpe2KSI59I1H1FKfJVGndarQQfCO34rxr4CfF6mvmYhBZuBYlO29ft2ntanqV1wkuygax/2QrVYHLF9Z4h4SmeJo/78cg8ZPRb1dBsaryjAayVoO17jS5MqGtg1AU0YkiyGOqs0noCydUt2fsqKzRWp1JvT5HO9d0mXh4S5dH++kqjhVU7SAlHmbba5PVthfOA/elIqDzJTi7p5Q+5TxraoIQC5ohMQdHYWFivA==",
"SigningCertURL" : "https://sns.eu-west-1.amazonaws.com/SimpleNotificationService-eaea6120e66ea12e88dcd8bcbddca752.pem",
"UnsubscribeURL" : "https://sns.eu-west-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:eu-west-1:542719421142:medibounce:1814bbfd-58a9-4651-bac9-6d87e3a99804"
}
`
func TestAwsSnsBouncePermanent(t *testing.T) {
s, err := ParseSNS(ioutil.NopCloser(strings.NewReader(sns_permanent)))
if err != nil {
t.Fatal(err)
}
if s.NotificationType != "Bounce" {
t.Fatalf("Type %s", s.NotificationType)
}
if s.Bounce.BounceType != "Permanent" {
t.Fatalf("bounce Type :%s", s.Bounce.BounceType)
}
if s.Mail.Source != "[email protected]" {
t.Fatalf("source :%s", s.Mail.Source)
}
if len(s.Bounce.BouncedRecipients) != 1 {
t.Fatalf("len recipients :%s", s.Bounce.BouncedRecipients)
}
if s.Bounce.BouncedRecipients[0].EmailAdresse != "[email protected]" {
t.Fatalf("recipients %d:%s", len(s.Bounce.BouncedRecipients), s.Bounce.BouncedRecipients)
}
}
func TestAwsSnsBounceTransient(t *testing.T) {
s, err := ParseSNS(ioutil.NopCloser(strings.NewReader(sns_transient)))
if err != nil {
t.Fatal(err)
}
if s.NotificationType != "Bounce" {
t.Fatalf("Type %s", s.NotificationType)
}
if s.Bounce.BounceType != "Transient" {
t.Fatalf("bounce Type : %s", s.Bounce.BounceType)
}
if s.Mail.Source != "[email protected]" {
t.Fatalf("source :%s", s.Mail.Source)
}
if len(s.Bounce.BouncedRecipients) != 1 {
t.Fatalf("len recipients :%s", s.Bounce.BouncedRecipients)
}
if s.Bounce.BouncedRecipients[0].EmailAdresse != "[email protected]" {
t.Fatalf("recipients %d:%s", len(s.Bounce.BouncedRecipients), s.Bounce.BouncedRecipients)
}
}
func TestAwsSnsBounceComplaint(t *testing.T) {
s, err := ParseSNS(ioutil.NopCloser(strings.NewReader(sns_complaint)))
if err != nil {
t.Fatal(err)
}
if s.NotificationType != "Complaint" {
t.Fatalf("Type %s", s.NotificationType)
}
if s.Mail.Source != "[email protected]" {
t.Fatalf("source :%s", s.Mail.Source)
}
if len(s.Complaint.ComplainedRecipients) != 1 {
t.Fatalf("len recipients :%s", s.Complaint.ComplainedRecipients)
}
if s.Complaint.ComplainedRecipients[0].EmailAdresse != "[email protected]" {
t.Fatalf("recipients %d:%s", len(s.Complaint.ComplainedRecipients), s.Complaint.ComplainedRecipients)
}
}
package sns
import (
"encoding/json"
"fmt"
"io"
"github.com/pkg/errors"
)
type Data struct {
Subject string `json:"Subject"`
Message string `json:"Message"`
MessageID string `json:"MessageId"`
Signature string `json:"Signature"`
SignatureVersion string `json:"SignatureVersion"`
SigningCertURL string `json:"SigningCertURL"`
SubscribeURL string `json:"SubscribeURL"`
Timestamp string `json:"Timestamp"`
Token string `json:"Token"`
TopicArn string `json:"TopicArn"`
Type string `json:"Type"`
}
type Message struct {
NotificationType string `json:"notificationType"`
Mail Mail `json:"mail"`
Bounce Bounce `json:"bounce"`
Complaint Complaint `json:"complaint"`
Delivery Delivery `json:"delivery"`
}
func (m *Message) String() string {
return fmt.Sprintf("Bounce type:%s Recipients:%s", m.NotificationType, m.Recipients())
}
func (m *Message) Recipients() []Recipient {
switch m.NotificationType {
case "Bounce":
return m.Bounce.BouncedRecipients
case "Complaint":
return m.Complaint.ComplainedRecipients
}
return []Recipient{}
}
type Bounce struct {
BounceType string `json:"bounceType"`
BouncedRecipients []Recipient `json:"bouncedRecipients"`
}
type Complaint struct {
ComplainedRecipients []Recipient `json:"complainedRecipients"`
}
type Mail struct {
Source string `json:"source"`
Destination []string `json:"destination"`
}
type Delivery struct {
}
type Recipient struct {
EmailAdresse string `json:"emailAddress"`
}
func ParseSNS(body io.ReadCloser) (*Message, error) {
defer body.Close()
sns := &Data{}
decoder := json.NewDecoder(body)
err := decoder.Decode(&sns)
if err != nil {
return nil, errors.Wrapf(err, "Décodage sns")
}
msg := &Message{}
err = json.Unmarshal([]byte(sns.Message), msg)
if err != nil {
return nil, errors.Wrapf(err, "Décodage message")
}
return msg, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment