From 108388a72f8867f790dfa147b9d53ee1cde1d24b Mon Sep 17 00:00:00 2001 From: Guillaume Le Floch Date: Fri, 9 Nov 2018 13:45:07 +0100 Subject: [PATCH] notify/email: Throw error if no auth mechanism matches (#1608) Signed-off-by: glefloch --- notify/impl.go | 10 ++++++++-- notify/impl_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/notify/impl.go b/notify/impl.go index d797ebb5..dd6cda05 100644 --- a/notify/impl.go +++ b/notify/impl.go @@ -220,12 +220,13 @@ func NewEmail(c *config.EmailConfig, t *template.Template, l log.Logger) *Email // auth resolves a string of authentication mechanisms. func (n *Email) auth(mechs string) (smtp.Auth, error) { username := n.conf.AuthUsername - + err := &types.MultiError{} for _, mech := range strings.Split(mechs, " ") { switch mech { case "CRAM-MD5": secret := string(n.conf.AuthSecret) if secret == "" { + err.Add(errors.New("missing secret for CRAM-MD5 auth mechanism")) continue } return smtp.CRAMMD5Auth(username, secret), nil @@ -233,6 +234,7 @@ func (n *Email) auth(mechs string) (smtp.Auth, error) { case "PLAIN": password := string(n.conf.AuthPassword) if password == "" { + err.Add(errors.New("missing password for PLAIN auth mechanism")) continue } identity := n.conf.AuthIdentity @@ -246,12 +248,16 @@ func (n *Email) auth(mechs string) (smtp.Auth, error) { case "LOGIN": password := string(n.conf.AuthPassword) if password == "" { + err.Add(errors.New("missing password for LOGIN auth mechanism")) continue } return LoginAuth(username, password), nil } } - return nil, nil + if err.Len() == 0 { + err.Add(errors.New("unknown auth mechanism: " + mechs)) + } + return nil, err } // Notify implements the Notifier interface. diff --git a/notify/impl_test.go b/notify/impl_test.go index deba46dd..3ecd6a66 100644 --- a/notify/impl_test.go +++ b/notify/impl_test.go @@ -290,3 +290,35 @@ func TestOpsGenie(t *testing.T) { require.Error(t, err) require.Equal(t, err.Error(), "templating error: template: :1: function \"kaput\" not defined") } + +func TestEmailConfigNoAuthMechs(t *testing.T) { + + email := &Email{ + conf: &config.EmailConfig{}, tmpl: &template.Template{}, logger: log.NewNopLogger(), + } + _, err := email.auth("") + require.Error(t, err) + require.Equal(t, err.Error(), "unknown auth mechanism: ") +} + +func TestEmailConfigMissingAuthParam(t *testing.T) { + + email := &Email{ + conf: &config.EmailConfig{}, tmpl: &template.Template{}, logger: log.NewNopLogger(), + } + _, err := email.auth("CRAM-MD5") + require.Error(t, err) + require.Equal(t, err.Error(), "missing secret for CRAM-MD5 auth mechanism") + + _, err = email.auth("PLAIN") + require.Error(t, err) + require.Equal(t, err.Error(), "missing password for PLAIN auth mechanism") + + _, err = email.auth("LOGIN") + require.Error(t, err) + require.Equal(t, err.Error(), "missing password for LOGIN auth mechanism") + + _, err = email.auth("PLAIN LOGIN") + require.Error(t, err) + require.Equal(t, err.Error(), "missing password for PLAIN auth mechanism; missing password for LOGIN auth mechanism") +}