satellite: do not validate smtp from / host when emails are disabled

Today we got the following error address, even if: `mail.auth-type: nomail`

```
Error: SMTP server address '' couldn't be parsed: missing port in address
```

It's make more sense to print the error message only if the SMTP address (or from address) is expected.

Change-Id: Ie07620099b6aac27630fcfd1cda9921ef4b6060c
This commit is contained in:
Márton Elek 2023-10-16 09:51:08 +02:00 committed by Storj Robot
parent 1891f6501d
commit d64e2167ce

View File

@ -228,20 +228,23 @@ type Config struct {
} }
func setupMailService(log *zap.Logger, config Config) (*mailservice.Service, error) { func setupMailService(log *zap.Logger, config Config) (*mailservice.Service, error) {
// TODO(yar): test multiple satellites using same OAUTH credentials fromAndHost := func(cfg mailservice.Config) (*mail.Address, string, error) {
mailConfig := config.Mail
// validate from mail address // validate from mail address
from, err := mail.ParseAddress(mailConfig.From) from, err := mail.ParseAddress(cfg.From)
if err != nil { if err != nil {
return nil, errs.New("SMTP from address '%s' couldn't be parsed: %v", mailConfig.From, err) return nil, "", errs.New("SMTP from address '%s' couldn't be parsed: %v", cfg.From, err)
} }
// validate smtp server address // validate smtp server address
host, _, err := net.SplitHostPort(mailConfig.SMTPServerAddress) host, _, err := net.SplitHostPort(cfg.SMTPServerAddress)
if err != nil && mailConfig.AuthType != "simulate" && mailConfig.AuthType != "nologin" { if err != nil && cfg.AuthType != "simulate" && cfg.AuthType != "nologin" {
return nil, errs.New("SMTP server address '%s' couldn't be parsed: %v", mailConfig.SMTPServerAddress, err) return nil, "", errs.New("SMTP server address '%s' couldn't be parsed: %v", cfg.SMTPServerAddress, err)
} }
return from, host, err
}
// TODO(yar): test multiple satellites using same OAUTH credentials
mailConfig := config.Mail
var sender mailservice.Sender var sender mailservice.Sender
switch mailConfig.AuthType { switch mailConfig.AuthType {
@ -256,6 +259,11 @@ func setupMailService(log *zap.Logger, config Config) (*mailservice.Service, err
return nil, err return nil, err
} }
from, _, err := fromAndHost(mailConfig)
if err != nil {
return nil, err
}
sender = &post.SMTPSender{ sender = &post.SMTPSender{
From: *from, From: *from,
Auth: &oauth2.Auth{ Auth: &oauth2.Auth{
@ -265,12 +273,22 @@ func setupMailService(log *zap.Logger, config Config) (*mailservice.Service, err
ServerAddress: mailConfig.SMTPServerAddress, ServerAddress: mailConfig.SMTPServerAddress,
} }
case "plain": case "plain":
from, host, err := fromAndHost(mailConfig)
if err != nil {
return nil, err
}
sender = &post.SMTPSender{ sender = &post.SMTPSender{
From: *from, From: *from,
Auth: smtp.PlainAuth("", mailConfig.Login, mailConfig.Password, host), Auth: smtp.PlainAuth("", mailConfig.Login, mailConfig.Password, host),
ServerAddress: mailConfig.SMTPServerAddress, ServerAddress: mailConfig.SMTPServerAddress,
} }
case "login": case "login":
from, _, err := fromAndHost(mailConfig)
if err != nil {
return nil, err
}
sender = &post.SMTPSender{ sender = &post.SMTPSender{
From: *from, From: *from,
Auth: post.LoginAuth{ Auth: post.LoginAuth{
@ -280,6 +298,10 @@ func setupMailService(log *zap.Logger, config Config) (*mailservice.Service, err
ServerAddress: mailConfig.SMTPServerAddress, ServerAddress: mailConfig.SMTPServerAddress,
} }
case "insecure": case "insecure":
from, _, err := fromAndHost(mailConfig)
if err != nil {
return nil, err
}
sender = &post.SMTPSender{ sender = &post.SMTPSender{
From: *from, From: *from,
ServerAddress: mailConfig.SMTPServerAddress, ServerAddress: mailConfig.SMTPServerAddress,