@@ -29,6 +29,7 @@ import (
29
29
"github.com/aws/aws-sdk-go/service/sns"
30
30
"github.com/go-kit/log"
31
31
"github.com/go-kit/log/level"
32
+ "github.com/pkg/errors"
32
33
commoncfg "github.com/prometheus/common/config"
33
34
34
35
"github.com/prometheus/alertmanager/config"
@@ -63,12 +64,12 @@ func New(c *config.SNSConfig, t *template.Template, l log.Logger, httpOpts ...co
63
64
64
65
func (n * Notifier ) Notify (ctx context.Context , alert ... * types.Alert ) (bool , error ) {
65
66
var (
66
- err error
67
- data = notify .GetTemplateData (ctx , n .tmpl , alert , n .logger )
68
- tmpl = notify .TmplText (n .tmpl , data , & err )
67
+ tmplErr error
68
+ data = notify .GetTemplateData (ctx , n .tmpl , alert , n .logger )
69
+ tmpl = notify .TmplText (n .tmpl , data , & tmplErr )
69
70
)
70
71
71
- client , err := n .createSNSClient (tmpl )
72
+ client , err := n .createSNSClient (tmpl , & tmplErr )
72
73
if err != nil {
73
74
var e awserr.RequestFailure
74
75
if errors .As (err , & e ) {
@@ -77,7 +78,7 @@ func (n *Notifier) Notify(ctx context.Context, alert ...*types.Alert) (bool, err
77
78
return true , err
78
79
}
79
80
80
- publishInput , err := n .createPublishInput (ctx , tmpl )
81
+ publishInput , err := n .createPublishInput (ctx , tmpl , & tmplErr )
81
82
if err != nil {
82
83
return true , err
83
84
}
@@ -99,7 +100,7 @@ func (n *Notifier) Notify(ctx context.Context, alert ...*types.Alert) (bool, err
99
100
return false , nil
100
101
}
101
102
102
- func (n * Notifier ) createSNSClient (tmpl func (string ) string ) (* sns.SNS , error ) {
103
+ func (n * Notifier ) createSNSClient (tmpl func (string ) string , tmplErr * error ) (* sns.SNS , error ) {
103
104
var creds * credentials.Credentials
104
105
// If there are provided sigV4 credentials we want to use those to create a session.
105
106
if n .conf .Sigv4 .AccessKey != "" && n .conf .Sigv4 .SecretKey != "" {
@@ -115,6 +116,9 @@ func (n *Notifier) createSNSClient(tmpl func(string) string) (*sns.SNS, error) {
115
116
if err != nil {
116
117
return nil , err
117
118
}
119
+ if * tmplErr != nil {
120
+ return nil , notify .NewErrorWithReason (notify .ClientErrorReason , errors .Wrap (* tmplErr , "execute 'api_url' template" ))
121
+ }
118
122
119
123
if n .conf .Sigv4 .RoleARN != "" {
120
124
var stsSess * session.Session
@@ -144,13 +148,19 @@ func (n *Notifier) createSNSClient(tmpl func(string) string) (*sns.SNS, error) {
144
148
return client , nil
145
149
}
146
150
147
- func (n * Notifier ) createPublishInput (ctx context.Context , tmpl func (string ) string ) (* sns.PublishInput , error ) {
151
+ func (n * Notifier ) createPublishInput (ctx context.Context , tmpl func (string ) string , tmplErr * error ) (* sns.PublishInput , error ) {
148
152
publishInput := & sns.PublishInput {}
149
153
messageAttributes := n .createMessageAttributes (tmpl )
154
+ if * tmplErr != nil {
155
+ return nil , notify .NewErrorWithReason (notify .ClientErrorReason , errors .Wrap (* tmplErr , "execute 'attributes' template" ))
156
+ }
150
157
// Max message size for a message in a SNS publish request is 256KB, except for SMS messages where the limit is 1600 characters/runes.
151
158
messageSizeLimit := 256 * 1024
152
159
if n .conf .TopicARN != "" {
153
160
topicARN := tmpl (n .conf .TopicARN )
161
+ if * tmplErr != nil {
162
+ return nil , notify .NewErrorWithReason (notify .ClientErrorReason , errors .Wrap (* tmplErr , "execute 'topic_arn' template" ))
163
+ }
154
164
publishInput .SetTopicArn (topicARN )
155
165
// If we are using a topic ARN, it could be a FIFO topic specified by the topic's suffix ".fifo".
156
166
if strings .HasSuffix (topicARN , ".fifo" ) {
@@ -165,14 +175,24 @@ func (n *Notifier) createPublishInput(ctx context.Context, tmpl func(string) str
165
175
}
166
176
if n .conf .PhoneNumber != "" {
167
177
publishInput .SetPhoneNumber (tmpl (n .conf .PhoneNumber ))
178
+ if * tmplErr != nil {
179
+ return nil , notify .NewErrorWithReason (notify .ClientErrorReason , errors .Wrap (* tmplErr , "execute 'phone_number' template" ))
180
+ }
168
181
// If we have an SMS message, we need to truncate to 1600 characters/runes.
169
182
messageSizeLimit = 1600
170
183
}
171
184
if n .conf .TargetARN != "" {
172
185
publishInput .SetTargetArn (tmpl (n .conf .TargetARN ))
186
+ if * tmplErr != nil {
187
+ return nil , notify .NewErrorWithReason (notify .ClientErrorReason , errors .Wrap (* tmplErr , "execute 'target_arn' template" ))
188
+ }
173
189
}
174
190
175
- messageToSend , isTrunc , err := validateAndTruncateMessage (tmpl (n .conf .Message ), messageSizeLimit )
191
+ tmplMessage := tmpl (n .conf .Message )
192
+ if * tmplErr != nil {
193
+ return nil , notify .NewErrorWithReason (notify .ClientErrorReason , errors .Wrap (* tmplErr , "execute 'message' template" ))
194
+ }
195
+ messageToSend , isTrunc , err := validateAndTruncateMessage (tmplMessage , messageSizeLimit )
176
196
if err != nil {
177
197
return nil , err
178
198
}
@@ -186,6 +206,9 @@ func (n *Notifier) createPublishInput(ctx context.Context, tmpl func(string) str
186
206
187
207
if n .conf .Subject != "" {
188
208
publishInput .SetSubject (tmpl (n .conf .Subject ))
209
+ if * tmplErr != nil {
210
+ return nil , notify .NewErrorWithReason (notify .ClientErrorReason , errors .Wrap (* tmplErr , "execute 'subject' template" ))
211
+ }
189
212
}
190
213
191
214
return publishInput , nil
0 commit comments