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 <jowalsh@bgs.ac.uk>

* fix test to expect no error from auth if username is not supplied
Signed-off-by: Jo Walsh <jowalsh@bgs.ac.uk>

* clean up some formatting errors in surplus comments

Signed-off-by: Jo Walsh <jowalsh@bgs.ac.uk>

* keep noAuth / loginAuth functions all together

Signed-off-by: Jo Walsh <jowalsh@bgs.ac.uk>

* Address latest comments

Co-Authored-By: Jo Walsh <jowalsh@bgs.ac.uk>
Signed-off-by: Simon Pasquier <spasquie@redhat.com>
This commit is contained in:
Jo Walsh 2019-03-01 14:53:18 +00:00 committed by Simon Pasquier
parent a732f6dbe4
commit 8642c0b46e
2 changed files with 19 additions and 2 deletions

View File

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

View File

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