-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
150 lines (132 loc) · 3.8 KB
/
main.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sns"
"github.com/sensu-community/sensu-plugin-sdk/sensu"
"github.com/sensu-community/sensu-plugin-sdk/templates"
corev2 "github.com/sensu/sensu-go/api/core/v2"
)
// Config represents the handler plugin config.
type Config struct {
sensu.PluginConfig
TopicARN string
Message string
Subject string
AssumeRoleARN string
UseEC2Region bool
}
var (
plugin = Config{
PluginConfig: sensu.PluginConfig{
Name: "sensu-aws-sns-handler",
Short: "AWS Simple Notification Service Handler",
Keyspace: "sensu.io/plugins/sensu-aws-sns-handler/config",
},
}
options = []*sensu.PluginConfigOption{
{
Path: "topic-arn",
Env: "SNS_TOPIC_ARN",
Argument: "topic-arn",
Shorthand: "t",
Default: "",
Usage: "The SNS Topic ARN",
Value: &plugin.TopicARN,
},
{
Path: "message-template",
Env: "SNS_MESSAGE_TEMPLATE",
Argument: "message-template",
Shorthand: "m",
Default: "{{.Entity.Name}}/{{.Check.Name}}: {{.Check.State}}",
Usage: "The template for the message sent via SNS",
Value: &plugin.Message,
},
{
Path: "subject-template",
Env: "SNS_SUBJECT_TEMPLATE",
Argument: "subject-template",
Shorthand: "s",
Default: "{{.Check.State}} - {{.Entity.Name}}/{{.Check.Name}}",
Usage: "The template for the subject sent via SNS",
Value: &plugin.Subject,
},
{
Path: "assume-role-arn",
Env: "SNS_ASSUME_ROLE_ARN",
Argument: "assume-role-arn",
Shorthand: "a",
Default: "",
Usage: "The IAM role to assume upon succssful authentication",
Value: &plugin.AssumeRoleARN,
},
{
Path: "use-ec2-region",
Env: "",
Argument: "use-ec2-region",
Shorthand: "u",
Default: false,
Usage: "Query the EC2 metadata for the region to use for SNS",
Value: &plugin.UseEC2Region,
},
}
)
func main() {
handler := sensu.NewGoHandler(&plugin.PluginConfig, options, checkArgs, executeHandler)
handler.Execute()
}
func checkArgs(_ *corev2.Event) error {
if len(plugin.TopicARN) == 0 {
return fmt.Errorf("--topic-arn or SNS_TOPIC_ARN environment variable is required")
}
if len(plugin.AssumeRoleARN) > 0 {
if !arn.IsARN(plugin.AssumeRoleARN) {
return fmt.Errorf("aws-assume-role-arn %s is not a valid ARN", plugin.AssumeRoleARN)
}
}
return nil
}
func executeHandler(event *corev2.Event) error {
var svc *sns.SNS
message, err := templates.EvalTemplate("SNSMessage", plugin.Message, event)
if err != nil {
return err
}
subject, err := templates.EvalTemplate("SNSSubject", plugin.Subject, event)
if err != nil {
return err
}
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
awsConfig := &aws.Config{}
if plugin.UseEC2Region {
ec2md := ec2metadata.New(sess)
region, err := ec2md.Region()
if err != nil {
return fmt.Errorf("Cannot determine region from EC2 metadata: %v", err)
}
awsConfig.Region = aws.String(region)
}
if arn.IsARN(plugin.AssumeRoleARN) {
awsConfig.Credentials = stscreds.NewCredentials(sess, plugin.AssumeRoleARN)
}
svc = sns.New(sess, awsConfig)
// message should be a template with a specific default
publishOut, err := svc.Publish(&sns.PublishInput{
Subject: &subject,
Message: &message,
TopicArn: &plugin.TopicARN,
})
if err != nil {
return fmt.Errorf("Failed to publish message to SNS: %v", err)
}
// FUTURE: send to activities hub
fmt.Printf("SNS message published, message id %s\n", *publishOut.MessageId)
return nil
}