2020-01-21 20:15:40 +00:00
|
|
|
package validator
|
|
|
|
|
2020-04-05 12:37:21 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
2022-04-03 12:24:51 +00:00
|
|
|
"os"
|
2020-01-21 20:15:40 +00:00
|
|
|
|
2021-08-11 01:04:35 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
2020-04-05 12:37:21 +00:00
|
|
|
)
|
2020-01-21 20:15:40 +00:00
|
|
|
|
2020-04-20 21:03:38 +00:00
|
|
|
// ValidateNotifier validates and update notifier configuration.
|
2022-02-28 03:15:01 +00:00
|
|
|
func ValidateNotifier(config *schema.NotifierConfiguration, validator *schema.StructValidator) {
|
2022-04-15 23:34:26 +00:00
|
|
|
if config.SMTP == nil && config.FileSystem == nil {
|
2021-08-07 03:58:08 +00:00
|
|
|
validator.Push(fmt.Errorf(errFmtNotifierNotConfigured))
|
|
|
|
|
|
|
|
return
|
2022-02-28 03:15:01 +00:00
|
|
|
} else if config.SMTP != nil && config.FileSystem != nil {
|
2021-08-07 03:58:08 +00:00
|
|
|
validator.Push(fmt.Errorf(errFmtNotifierMultipleConfigured))
|
2020-01-21 20:15:40 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-28 03:15:01 +00:00
|
|
|
if config.FileSystem != nil {
|
|
|
|
if config.FileSystem.Filename == "" {
|
2021-08-07 03:58:08 +00:00
|
|
|
validator.Push(fmt.Errorf(errFmtNotifierFileSystemFileNameNotConfigured))
|
2020-01-21 20:15:40 +00:00
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2020-01-21 20:15:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-28 03:15:01 +00:00
|
|
|
validateSMTPNotifier(config.SMTP, validator)
|
2022-04-03 12:24:51 +00:00
|
|
|
|
|
|
|
validateNotifierTemplates(config, validator)
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateNotifierTemplates(config *schema.NotifierConfiguration, validator *schema.StructValidator) {
|
|
|
|
if config.TemplatePath == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-19 04:27:10 +00:00
|
|
|
switch _, err := os.Stat(config.TemplatePath); {
|
2022-04-03 12:24:51 +00:00
|
|
|
case os.IsNotExist(err):
|
|
|
|
validator.Push(fmt.Errorf(errFmtNotifierTemplatePathNotExist, config.TemplatePath))
|
|
|
|
return
|
|
|
|
case err != nil:
|
|
|
|
validator.Push(fmt.Errorf(errFmtNotifierTemplatePathUnknownError, config.TemplatePath, err))
|
|
|
|
return
|
|
|
|
}
|
2021-04-16 01:44:37 +00:00
|
|
|
}
|
2020-04-09 00:21:28 +00:00
|
|
|
|
2022-02-28 03:15:01 +00:00
|
|
|
func validateSMTPNotifier(config *schema.SMTPNotifierConfiguration, validator *schema.StructValidator) {
|
2023-05-07 06:39:17 +00:00
|
|
|
validateSMTPNotifierAddress(config, validator)
|
|
|
|
|
2022-07-18 00:56:09 +00:00
|
|
|
if config.StartupCheckAddress.Address == "" {
|
2022-02-28 03:15:01 +00:00
|
|
|
config.StartupCheckAddress = schema.DefaultSMTPNotifierConfiguration.StartupCheckAddress
|
2021-04-16 01:44:37 +00:00
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2022-02-28 03:15:01 +00:00
|
|
|
if config.Timeout == 0 {
|
|
|
|
config.Timeout = schema.DefaultSMTPNotifierConfiguration.Timeout
|
2021-08-10 00:52:41 +00:00
|
|
|
}
|
|
|
|
|
2022-02-28 03:15:01 +00:00
|
|
|
if config.Sender.Address == "" {
|
2021-08-07 03:58:08 +00:00
|
|
|
validator.Push(fmt.Errorf(errFmtNotifierSMTPNotConfigured, "sender"))
|
2021-04-16 01:44:37 +00:00
|
|
|
}
|
2021-01-04 10:28:55 +00:00
|
|
|
|
2022-02-28 03:15:01 +00:00
|
|
|
if config.Subject == "" {
|
|
|
|
config.Subject = schema.DefaultSMTPNotifierConfiguration.Subject
|
2021-04-16 01:44:37 +00:00
|
|
|
}
|
2021-01-04 10:28:55 +00:00
|
|
|
|
2022-02-28 03:15:01 +00:00
|
|
|
if config.Identifier == "" {
|
|
|
|
config.Identifier = schema.DefaultSMTPNotifierConfiguration.Identifier
|
2021-04-16 01:44:37 +00:00
|
|
|
}
|
2021-01-04 10:28:55 +00:00
|
|
|
|
2022-02-28 03:15:01 +00:00
|
|
|
if config.TLS == nil {
|
2022-10-21 08:41:33 +00:00
|
|
|
config.TLS = &schema.TLSConfig{}
|
2021-04-16 01:44:37 +00:00
|
|
|
}
|
2021-01-04 10:28:55 +00:00
|
|
|
|
2022-10-21 08:41:33 +00:00
|
|
|
configDefaultTLS := &schema.TLSConfig{
|
|
|
|
MinimumVersion: schema.DefaultSMTPNotifierConfiguration.TLS.MinimumVersion,
|
|
|
|
MaximumVersion: schema.DefaultSMTPNotifierConfiguration.TLS.MaximumVersion,
|
|
|
|
}
|
|
|
|
|
2023-05-07 06:39:17 +00:00
|
|
|
if config.Address != nil {
|
|
|
|
configDefaultTLS.ServerName = config.Address.Hostname()
|
|
|
|
}
|
|
|
|
|
2022-10-21 08:41:33 +00:00
|
|
|
if err := ValidateTLSConfig(config.TLS, configDefaultTLS); err != nil {
|
|
|
|
validator.Push(fmt.Errorf(errFmtNotifierSMTPTLSConfigInvalid, err))
|
2020-01-21 20:15:40 +00:00
|
|
|
}
|
2022-10-02 02:51:19 +00:00
|
|
|
|
|
|
|
if config.DisableStartTLS {
|
|
|
|
validator.PushWarning(fmt.Errorf(errFmtNotifierStartTlsDisabled))
|
|
|
|
}
|
2020-01-21 20:15:40 +00:00
|
|
|
}
|
2023-05-07 06:39:17 +00:00
|
|
|
|
|
|
|
func validateSMTPNotifierAddress(config *schema.SMTPNotifierConfiguration, validator *schema.StructValidator) {
|
|
|
|
if config.Address == nil {
|
|
|
|
if config.Host == "" && config.Port == 0 { //nolint:staticcheck
|
|
|
|
validator.Push(fmt.Errorf(errFmtNotifierSMTPNotConfigured, "address"))
|
|
|
|
} else {
|
|
|
|
host := config.Host //nolint:staticcheck
|
|
|
|
port := config.Port //nolint:staticcheck
|
|
|
|
|
|
|
|
config.Address = schema.NewSMTPAddress("", host, port)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if config.Host != "" || config.Port != 0 { //nolint:staticcheck
|
|
|
|
validator.Push(fmt.Errorf(errFmtNotifierSMTPAddressLegacyAndModern))
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if err = config.Address.ValidateSMTP(); err != nil {
|
|
|
|
validator.Push(fmt.Errorf(errFmtNotifierSMTPAddress, config.Address.String(), err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|