From 50bba83a22fa0ae8eb5f0758b9927e72707abf3f Mon Sep 17 00:00:00 2001 From: Colin Dean Date: Thu, 19 Jan 2023 14:37:58 -0500 Subject: [PATCH] enhance: emit an error when the Auth method is specified but no creds were passed (#35) --- cmd/vela-email/plugin.go | 7 +++++++ cmd/vela-email/plugin_test.go | 36 +++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/cmd/vela-email/plugin.go b/cmd/vela-email/plugin.go index c00f77c..ca5b3db 100644 --- a/cmd/vela-email/plugin.go +++ b/cmd/vela-email/plugin.go @@ -31,6 +31,9 @@ var ( // ErrorMissingSMTPParam is returned when the plugin is missing a smtp host or port parameter. ErrorMissingSMTPParam = errors.New("missing smtp parameter (host/port)") + + // ErrorAuthSpecifiedButCredentialsMissing is returned when the plugin is missing credentials when auth type was specified. + ErrorAuthSpecifiedButCredentialsMissing = errors.New("missing credentials when auth type was specified") ) // Plugin represents the configuration loaded for the plugin. @@ -141,6 +144,10 @@ func (p *Plugin) Validate() error { return ErrorMissingSMTPParam } + if len(p.Auth) > 0 && !(len(p.SMTPHost.Username) > 0 && len(p.SMTPHost.Password) > 0) { + return ErrorAuthSpecifiedButCredentialsMissing + } + // set defaults if len(p.Email.Subject) == 0 { p.Email.Subject = DefaultSubject diff --git a/cmd/vela-email/plugin_test.go b/cmd/vela-email/plugin_test.go index 9ca1254..62a3c21 100644 --- a/cmd/vela-email/plugin_test.go +++ b/cmd/vela-email/plugin_test.go @@ -76,6 +76,28 @@ func TestValidateSuccess(t *testing.T) { Attachment: noAttachment, }, }, + { + name: "return no errors: auth specified with credentials", + parameters: Plugin{ + Email: mockEmail, + EmailFilename: "", + SMTPHost: mockSMTPHost, + Attachment: noAttachment, + Auth: "LoginAuth", + }, + }, + { + name: "return no errors: auth not specified, credentials absent", + parameters: Plugin{ + Email: mockEmail, + EmailFilename: "", + SMTPHost: &SMTPHost{ + Host: "smtphost.com", + Port: "234234", + }, + Attachment: noAttachment, + }, + }, { name: "return no errors: multiple To emails", parameters: Plugin{ @@ -279,6 +301,20 @@ func TestValidateErrors(t *testing.T) { }, wantErr: ErrorMissingSMTPParam, }, + { + name: "SMTP auth supplied but credentials missing", + parameters: Plugin{ + Auth: "LoginAuth", + SMTPHost: &SMTPHost{ + Host: "smtphost.com", + Port: "234234", + }, + Email: mockEmail, + EmailFilename: "", + Attachment: noAttachment, + }, + wantErr: ErrorAuthSpecifiedButCredentialsMissing, + }, } for _, test := range tests {