From 46fd81dbf1be2eddd54ee2ad5bc3f50f72bc8448 Mon Sep 17 00:00:00 2001 From: Colin Dean Date: Tue, 13 Dec 2022 15:26:13 -0500 Subject: [PATCH 1/2] Emit an error when the Auth method is specified but no creds were passed This avoids a frustrating timeout error when the secrets weren't passed correctly. --- 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..3513118 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.Username) > 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 { From eba1b652014e1a68611826f530a89972c3cdbef8 Mon Sep 17 00:00:00 2001 From: Colin Dean Date: Sat, 7 Jan 2023 21:35:27 -0500 Subject: [PATCH 2/2] Fix copypasta comparison error And a godot error --- cmd/vela-email/plugin.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/vela-email/plugin.go b/cmd/vela-email/plugin.go index 3513118..ca5b3db 100644 --- a/cmd/vela-email/plugin.go +++ b/cmd/vela-email/plugin.go @@ -32,7 +32,7 @@ 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 is returned when the plugin is missing credentials when auth type was specified. ErrorAuthSpecifiedButCredentialsMissing = errors.New("missing credentials when auth type was specified") ) @@ -144,7 +144,7 @@ func (p *Plugin) Validate() error { return ErrorMissingSMTPParam } - if len(p.Auth) > 0 && !(len(p.SMTPHost.Username) > 0 && len(p.SMTPHost.Username) > 0) { + if len(p.Auth) > 0 && !(len(p.SMTPHost.Username) > 0 && len(p.SMTPHost.Password) > 0) { return ErrorAuthSpecifiedButCredentialsMissing }