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