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
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
_, err = os.Stat(config.TemplatePath)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
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) {
|
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.Host == "" {
|
2021-08-07 03:58:08 +00:00
|
|
|
validator.Push(fmt.Errorf(errFmtNotifierSMTPNotConfigured, "host"))
|
2021-04-16 01:44:37 +00:00
|
|
|
}
|
2020-11-04 23:22:10 +00:00
|
|
|
|
2022-02-28 03:15:01 +00:00
|
|
|
if config.Port == 0 {
|
2021-08-07 03:58:08 +00:00
|
|
|
validator.Push(fmt.Errorf(errFmtNotifierSMTPNotConfigured, "port"))
|
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.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 {
|
|
|
|
config.TLS = schema.DefaultSMTPNotifierConfiguration.TLS
|
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.ServerName == "" {
|
|
|
|
config.TLS.ServerName = config.Host
|
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
|
|
|
}
|