fix(alerting): Add support for client.insecure in email alerting provider (#583)

* feat: adding client.insecure flag to email configuration

* chore(review): applying suggested changes

---------

Co-authored-by: TwiN <twin@linux.com>
This commit is contained in:
Max Partenfelder 2023-10-03 02:41:12 +02:00 committed by GitHub
parent 0fa3c5d114
commit e88bfa8518
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 0 deletions

View File

@ -476,6 +476,7 @@ endpoints:
| `alerting.email.port` | Port the mail server is listening to (e.g. `587`) | Required `0` | | `alerting.email.port` | Port the mail server is listening to (e.g. `587`) | Required `0` |
| `alerting.email.to` | Email(s) to send the alerts to | Required `""` | | `alerting.email.to` | Email(s) to send the alerts to | Required `""` |
| `alerting.email.default-alert` | Default alert configuration. <br />See [Setting a default alert](#setting-a-default-alert) | N/A | | `alerting.email.default-alert` | Default alert configuration. <br />See [Setting a default alert](#setting-a-default-alert) | N/A |
| `alerting.email.client.insecure` | Whether to skip TLS verification | `false` |
| `alerting.email.overrides` | List of overrides that may be prioritized over the default configuration | `[]` | | `alerting.email.overrides` | List of overrides that may be prioritized over the default configuration | `[]` |
| `alerting.email.overrides[].group` | Endpoint group for which the configuration will be overridden by this configuration | `""` | | `alerting.email.overrides[].group` | Endpoint group for which the configuration will be overridden by this configuration | `""` |
| `alerting.email.overrides[].to` | Email(s) to send the alerts to | `""` | | `alerting.email.overrides[].to` | Email(s) to send the alerts to | `""` |
@ -489,6 +490,8 @@ alerting:
host: "mail.example.com" host: "mail.example.com"
port: 587 port: 587
to: "recipient1@example.com,recipient2@example.com" to: "recipient1@example.com,recipient2@example.com"
client:
insecure: false
# You can also add group-specific to keys, which will # You can also add group-specific to keys, which will
# override the to key above for the specified groups # override the to key above for the specified groups
overrides: overrides:

View File

@ -1,11 +1,13 @@
package email package email
import ( import (
"crypto/tls"
"fmt" "fmt"
"math" "math"
"strings" "strings"
"github.com/TwiN/gatus/v5/alerting/alert" "github.com/TwiN/gatus/v5/alerting/alert"
"github.com/TwiN/gatus/v5/client"
"github.com/TwiN/gatus/v5/core" "github.com/TwiN/gatus/v5/core"
gomail "gopkg.in/mail.v2" gomail "gopkg.in/mail.v2"
) )
@ -19,6 +21,9 @@ type AlertProvider struct {
Port int `yaml:"port"` Port int `yaml:"port"`
To string `yaml:"to"` To string `yaml:"to"`
// ClientConfig is the configuration of the client used to communicate with the provider's target
ClientConfig *client.Config `yaml:"client,omitempty"`
// DefaultAlert is the default alert configuration to use for endpoints with an alert of the appropriate type // DefaultAlert is the default alert configuration to use for endpoints with an alert of the appropriate type
DefaultAlert *alert.Alert `yaml:"default-alert,omitempty"` DefaultAlert *alert.Alert `yaml:"default-alert,omitempty"`
@ -62,6 +67,9 @@ func (provider *AlertProvider) Send(endpoint *core.Endpoint, alert *alert.Alert,
m.SetHeader("Subject", subject) m.SetHeader("Subject", subject)
m.SetBody("text/plain", body) m.SetBody("text/plain", body)
d := gomail.NewDialer(provider.Host, provider.Port, username, provider.Password) d := gomail.NewDialer(provider.Host, provider.Port, username, provider.Password)
if provider.ClientConfig != nil && provider.ClientConfig.Insecure {
d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
}
return d.DialAndSend(m) return d.DialAndSend(m)
} }