-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsns.go
76 lines (69 loc) · 2.65 KB
/
sns.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"github.com/pkg/errors"
"time"
)
var (
ErrInvalidRecords = errors.New("invalid records")
ErrInvalidMessage = errors.New("ivnalid records")
ErrInvalidSubject = errors.New("invalid subject")
)
var sampleMessage = `
{
"Records": [
{
"EventSource": "aws:sns",
"EventVersion": "1.0",
"EventSubscriptionArn": "arn:aws:sns:us-east-1...ElasticBeanstalkNotifications-Environment-foo-app...",
"Sns": {
"Type": "Notification",
"MessageId": "111",
"TopicArn": "arn:aws:sns:us-east-1...ElasticBeanstalkNotifications-Environment-foo-app",
"Subject": "AWS Elastic Beanstalk Notification - New application version was deployed to running EC2 instances",
"Message": "Timestamp: Thu May 07 23:38:22 UTC 2015\nMessage: Lambda Function Test: New application version was deployed to running EC2 instances.\n\nEnvironment: foo-app\nApplication: FooApp\n\nEnvironment URL: http://foo.com\nRequestId: 222\nNotificationProcessId: 333",
"Timestamp": "2015-05-07T23:39:18.628Z",
"SignatureVersion": "1",
"Signature": "hello-sig",
"SigningCertUrl": "https://sns.us-east-1.amazonaws.com/...",
"UnsubscribeUrl": "https://sns.us-east-1.amazonaws.com/...",
"MessageAttributes": {}
}
}
]
}`
// SNSMessage for our case is containing just a single record
type SNSMessage struct {
Records []Record `json:"Records"`
}
type Record struct {
EventSource string `json:"EventSource"`
EventVersion string `json:"EventVersion"`
EventSubscriptionArn string `json:"EventSubscriptionArn"`
SNS SNS `json:"Sns"`
}
type SNS struct {
Type string `json:"Type"`
MessageID string `json:"MessageId"`
TopicARN string `json:"TopicArn"`
Subject string `json:"Subject"`
Message string `json:"Message"`
Timestamp time.Time `json:"Timestamp"`
SignatureVersion string `json:"SignatureVersion"`
Signature string `json:"Signature"`
SigningCertURL string `json:"SigningCertUrl"`
UnsubscribeURL string `json:"UnsubscribeUrl"`
MessageAttributes map[string]interface{} `json:"MessageAttributes"`
}
// Validate returns error if something is wrong with SNSMessage struct
func (m SNSMessage) Validate() error {
switch {
case len(m.Records) != 1:
return ErrInvalidRecords
case m.Records[0].SNS.Message == "":
return ErrInvalidMessage
case m.Records[0].SNS.Subject == "":
return ErrInvalidSubject
default:
return nil
}
}