From 8642c0b46e0f58a09ee2b14b398f91f0a059a934 Mon Sep 17 00:00:00 2001 From: Jo Walsh <369631+metazool@users.noreply.github.com> Date: Fri, 1 Mar 2019 14:53:18 +0000 Subject: [PATCH] Allow sending of unauthenticated SMTP requests when smtp_auth_username is not supplied (#1739) * try a more complicated but clearer approach explicitly returning a no-auth stmp.Auth when no username is supplied in config Signed-off-by: Jo Walsh * fix test to expect no error from auth if username is not supplied Signed-off-by: Jo Walsh * clean up some formatting errors in surplus comments Signed-off-by: Jo Walsh * keep noAuth / loginAuth functions all together Signed-off-by: Jo Walsh * Address latest comments Co-Authored-By: Jo Walsh Signed-off-by: Simon Pasquier --- notify/impl.go | 7 +++++++ notify/impl_test.go | 14 ++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/notify/impl.go b/notify/impl.go index e7739866..09c9a5b3 100644 --- a/notify/impl.go +++ b/notify/impl.go @@ -221,6 +221,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 + + // If no username is set, keep going without authentication. + if n.conf.AuthUsername == "" { + level.Debug(n.logger).Log("msg", "smtp_auth_username is not configured. Attempting to send email without authenticating") + return nil, nil + } + err := &types.MultiError{} for _, mech := range strings.Split(mechs, " ") { switch mech { diff --git a/notify/impl_test.go b/notify/impl_test.go index db46f143..bac38d81 100644 --- a/notify/impl_test.go +++ b/notify/impl_test.go @@ -295,7 +295,7 @@ func TestOpsGenie(t *testing.T) { func TestEmailConfigNoAuthMechs(t *testing.T) { email := &Email{ - conf: &config.EmailConfig{}, tmpl: &template.Template{}, logger: log.NewNopLogger(), + conf: &config.EmailConfig{AuthUsername: "test"}, tmpl: &template.Template{}, logger: log.NewNopLogger(), } _, err := email.auth("") require.Error(t, err) @@ -304,8 +304,9 @@ func TestEmailConfigNoAuthMechs(t *testing.T) { func TestEmailConfigMissingAuthParam(t *testing.T) { + conf := &config.EmailConfig{AuthUsername: "test"} email := &Email{ - conf: &config.EmailConfig{}, tmpl: &template.Template{}, logger: log.NewNopLogger(), + conf: conf, tmpl: &template.Template{}, logger: log.NewNopLogger(), } _, err := email.auth("CRAM-MD5") require.Error(t, err) @@ -324,6 +325,15 @@ func TestEmailConfigMissingAuthParam(t *testing.T) { require.Equal(t, err.Error(), "missing password for PLAIN auth mechanism; missing password for LOGIN auth mechanism") } +func TestEmailNoUsernameStillOk(t *testing.T) { + email := &Email{ + conf: &config.EmailConfig{}, tmpl: &template.Template{}, logger: log.NewNopLogger(), + } + a, err := email.auth("CRAM-MD5") + require.NoError(t, err) + require.Nil(t, a) +} + func TestVictorOpsCustomFields(t *testing.T) { logger := log.NewNopLogger() tmpl := createTmpl(t)