Add SMTP_TSL_INSECURE and SMTP_TSL_SERVER_NAME for better SMTP TSL control

This commit is contained in:
Denis Arh
2020-09-02 13:09:41 +02:00
parent e1ded02615
commit 3f1b094cc9
3 changed files with 41 additions and 4 deletions
+23 -1
View File
@@ -2,7 +2,9 @@ package corteza
import (
"context"
"crypto/tls"
"github.com/cortezaproject/corteza-server/pkg/healthcheck"
gomail "gopkg.in/mail.v2"
"time"
"github.com/pkg/errors"
@@ -51,7 +53,27 @@ func (app *App) Setup(log *zap.Logger, opts *app.Options) (err error) {
defer sentry.Recover()
auth.SetupDefault(opts.Auth.Secret, int(opts.Auth.Expiry/time.Minute))
mail.SetupDialer(opts.SMTP.Host, opts.SMTP.Port, opts.SMTP.User, opts.SMTP.Pass, opts.SMTP.From)
mail.SetupDialer(
opts.SMTP.Host,
opts.SMTP.Port,
opts.SMTP.User,
opts.SMTP.Pass,
opts.SMTP.From,
// Apply TLS configuration
func(d *gomail.Dialer) {
if d.TLSConfig == nil {
d.TLSConfig = &tls.Config{ServerName: d.Host}
}
if opts.SMTP.TlsInsecure {
d.TLSConfig.InsecureSkipVerify = true
}
if opts.SMTP.TlsServerName != "" {
d.TLSConfig.ServerName = opts.SMTP.TlsServerName
}
})
http.SetupDefaults(
opts.HTTPClient.HttpClientTimeout,
+7 -1
View File
@@ -7,16 +7,22 @@ type (
User string `env:"SMTP_USER"`
Pass string `env:"SMTP_PASS"`
From string `env:"SMTP_FROM"`
TlsInsecure bool `env:"SMTP_TSL_INSECURE"`
TlsServerName string `env:"SMTP_TSL_SERVER_NAME"`
}
)
func SMTP(pfix string) (o *SMTPOpt) {
o = &SMTPOpt{
Host: "localhost:25",
Host: "localhost",
Port: 25,
User: "",
Pass: "",
From: "",
TlsInsecure: false,
TlsServerName: "",
}
fill(o, pfix)
+11 -2
View File
@@ -13,6 +13,8 @@ type (
Dialer interface {
DialAndSend(...*gomail.Message) error
}
applyCfg func(*gomail.Dialer)
)
const (
@@ -33,7 +35,7 @@ func init() {
// SetupDialer setups SMTP dialer
//
// Host variable can contain "<host>:<port>" that will override port value
func SetupDialer(host string, port int, user, pass, from string) {
func SetupDialer(host string, port int, user, pass, from string, ff ...applyCfg) {
if host == "" {
defaultDialerError = errors.New("No hostname provided for SMTP")
return
@@ -59,12 +61,19 @@ func SetupDialer(host string, port int, user, pass, from string) {
}
defaultFrom = from
defaultDialer = gomail.NewDialer(
dialer := gomail.NewDialer(
host,
port,
user,
pass,
)
dialer.SSL = true
for _, fn := range ff {
fn(dialer)
}
defaultDialer = dialer
}
func New() *gomail.Message {