validate sns message body
Signed-off-by: Yijie Qin <qinyijie@amazon.com>
This commit is contained in:
parent
4030e3670b
commit
cda63f9f08
|
@ -36,6 +36,12 @@ import (
|
||||||
"github.com/prometheus/alertmanager/types"
|
"github.com/prometheus/alertmanager/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// The errors
|
||||||
|
MessageNotValidUtf8 = "Error - not a valid UTF-8 encoded string"
|
||||||
|
MessageIsEmpty = "Error - the message should not be empty"
|
||||||
|
)
|
||||||
|
|
||||||
// Notifier implements a Notifier for SNS notifications.
|
// Notifier implements a Notifier for SNS notifications.
|
||||||
type Notifier struct {
|
type Notifier struct {
|
||||||
conf *config.SNSConfig
|
conf *config.SNSConfig
|
||||||
|
@ -189,7 +195,10 @@ func (n *Notifier) createPublishInput(ctx context.Context, tmpl func(string) str
|
||||||
|
|
||||||
func validateAndTruncateMessage(message string, maxMessageSizeInBytes int) (string, bool, error) {
|
func validateAndTruncateMessage(message string, maxMessageSizeInBytes int) (string, bool, error) {
|
||||||
if !utf8.ValidString(message) {
|
if !utf8.ValidString(message) {
|
||||||
return "", false, fmt.Errorf("non utf8 encoded message string")
|
return MessageNotValidUtf8, false, fmt.Errorf("non utf8 encoded message string")
|
||||||
|
}
|
||||||
|
if len(message) == 0 {
|
||||||
|
return MessageIsEmpty, false, fmt.Errorf("message is empty")
|
||||||
}
|
}
|
||||||
if len(message) <= maxMessageSizeInBytes {
|
if len(message) <= maxMessageSizeInBytes {
|
||||||
return message, false, nil
|
return message, false, nil
|
||||||
|
|
|
@ -42,4 +42,8 @@ func TestValidateAndTruncateMessage(t *testing.T) {
|
||||||
invalidUtf8String := "\xc3\x28"
|
invalidUtf8String := "\xc3\x28"
|
||||||
_, _, err = validateAndTruncateMessage(invalidUtf8String, 100)
|
_, _, err = validateAndTruncateMessage(invalidUtf8String, 100)
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
|
|
||||||
|
emptyString := ""
|
||||||
|
_, _, err = validateAndTruncateMessage(emptyString, 100)
|
||||||
|
require.Error(t, err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue