feat(alerting): make authentication optional for email provider (#608)
* feat(alerting): Made authentication optional for the email alert provider * docs: Added parameter to email alert provider docs * feat(alerting): Updated email alert to set the LocalName attribute based on the From key * Updated email provider to disable authentication when no credentials are provided * Removed `disable-authentication` flag from email provider documentation * Apply suggestions from code review --------- Co-authored-by: TwiN <twin@linux.com>
This commit is contained in:
@ -49,7 +49,7 @@ func (provider *AlertProvider) IsValid() bool {
|
||||
}
|
||||
}
|
||||
|
||||
return len(provider.From) > 0 && len(provider.Password) > 0 && len(provider.Host) > 0 && len(provider.To) > 0 && provider.Port > 0 && provider.Port < math.MaxUint16
|
||||
return len(provider.From) > 0 && len(provider.Host) > 0 && len(provider.To) > 0 && provider.Port > 0 && provider.Port < math.MaxUint16
|
||||
}
|
||||
|
||||
// Send an alert using the provider
|
||||
@ -66,7 +66,20 @@ func (provider *AlertProvider) Send(endpoint *core.Endpoint, alert *alert.Alert,
|
||||
m.SetHeader("To", strings.Split(provider.getToForGroup(endpoint.Group), ",")...)
|
||||
m.SetHeader("Subject", subject)
|
||||
m.SetBody("text/plain", body)
|
||||
d := gomail.NewDialer(provider.Host, provider.Port, username, provider.Password)
|
||||
var d *gomail.Dialer
|
||||
if len(provider.Password) == 0 {
|
||||
// Get the domain in the From address
|
||||
localName := "localhost"
|
||||
fromParts := strings.Split(provider.From, `@`)
|
||||
if len(fromParts) == 2 {
|
||||
localName = fromParts[1]
|
||||
}
|
||||
// Create a dialer with no authentication
|
||||
d = &gomail.Dialer{Host: provider.Host, Port: provider.Port, LocalName: localName}
|
||||
} else {
|
||||
// Create an authenticated dialer
|
||||
d = gomail.NewDialer(provider.Host, provider.Port, username, provider.Password)
|
||||
}
|
||||
if provider.ClientConfig != nil && provider.ClientConfig.Insecure {
|
||||
d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
|
||||
}
|
||||
|
@ -18,6 +18,13 @@ func TestAlertDefaultProvider_IsValid(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAlertProvider_IsValidWithNoCredentials(t *testing.T) {
|
||||
validProvider := AlertProvider{From: "from@example.com", Host: "smtp-relay.gmail.com", Port: 587, To: "to@example.com"}
|
||||
if !validProvider.IsValid() {
|
||||
t.Error("provider should've been valid")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAlertProvider_IsValidWithOverride(t *testing.T) {
|
||||
providerWithInvalidOverrideGroup := AlertProvider{
|
||||
Overrides: []Override{
|
||||
|
Reference in New Issue
Block a user