notify/email: Throw error if no auth mechanism matches (#1608)

Signed-off-by: glefloch <glfloch@gmail.com>
This commit is contained in:
Guillaume Le Floch 2018-11-09 13:45:07 +01:00 committed by Max Inden
parent 3a577968a0
commit 108388a72f
2 changed files with 40 additions and 2 deletions

View File

@ -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.

View File

@ -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")
}