2020-01-22 03:15:40 +07:00
package validator
2020-04-05 19:37:21 +07:00
import (
2021-01-04 17:28:55 +07:00
"errors"
2020-04-05 19:37:21 +07:00
"fmt"
2020-01-22 03:15:40 +07:00
2020-04-05 19:37:21 +07:00
"github.com/authelia/authelia/internal/configuration/schema"
)
2020-01-22 03:15:40 +07:00
2020-04-21 04:03:38 +07:00
// ValidateNotifier validates and update notifier configuration.
2021-01-04 17:28:55 +07:00
//nolint:gocyclo // TODO: Remove in 4.28. Should be able to remove this during the removal of deprecated config.
2020-01-22 03:15:40 +07:00
func ValidateNotifier ( configuration * schema . NotifierConfiguration , validator * schema . StructValidator ) {
if configuration . SMTP == nil && configuration . FileSystem == nil {
validator . Push ( fmt . Errorf ( "Notifier should be either `smtp` or `filesystem`" ) )
return
}
if configuration . SMTP != nil && configuration . FileSystem != nil {
validator . Push ( fmt . Errorf ( "Notifier should be either `smtp` or `filesystem`" ) )
return
}
if configuration . FileSystem != nil {
if configuration . FileSystem . Filename == "" {
validator . Push ( fmt . Errorf ( "Filename of filesystem notifier must not be empty" ) )
}
2020-05-06 02:35:32 +07:00
2020-01-22 03:15:40 +07:00
return
}
if configuration . SMTP != nil {
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 11:59:38 +07:00
if configuration . SMTP . StartupCheckAddress == "" {
configuration . SMTP . StartupCheckAddress = "test@authelia.com"
}
2020-05-06 02:35:32 +07:00
2020-01-22 03:15:40 +07:00
if configuration . SMTP . Host == "" {
validator . Push ( fmt . Errorf ( "Host of SMTP notifier must be provided" ) )
}
if configuration . SMTP . Port == 0 {
validator . Push ( fmt . Errorf ( "Port of SMTP notifier must be provided" ) )
}
if configuration . SMTP . Sender == "" {
validator . Push ( fmt . Errorf ( "Sender of SMTP notifier must be provided" ) )
}
2020-04-09 07:21:28 +07:00
if configuration . SMTP . Subject == "" {
configuration . SMTP . Subject = schema . DefaultSMTPNotifierConfiguration . Subject
}
2020-05-06 02:35:32 +07:00
2020-11-05 06:22:10 +07:00
if configuration . SMTP . Identifier == "" {
configuration . SMTP . Identifier = schema . DefaultSMTPNotifierConfiguration . Identifier
}
2021-01-04 17:28:55 +07:00
if configuration . SMTP . TLS == nil {
configuration . SMTP . TLS = schema . DefaultSMTPNotifierConfiguration . TLS
// Deprecated. Maps deprecated values to the new ones. TODO: Remove in 4.28.
if configuration . SMTP . DisableVerifyCert != nil {
validator . PushWarning ( errors . New ( "DEPRECATED: SMTP Notifier `disable_verify_cert` option has been replaced by `notifier.smtp.tls.skip_verify` (will be removed in 4.28.0)" ) )
configuration . SMTP . TLS . SkipVerify = * configuration . SMTP . DisableVerifyCert
}
}
// Deprecated. Maps deprecated values to the new ones. TODO: Remove in 4.28.
if configuration . SMTP . TrustedCert != "" {
validator . PushWarning ( errors . New ( "DEPRECATED: SMTP Notifier `trusted_cert` option has been replaced by the global option `certificates_directory` (will be removed in 4.28.0)" ) )
}
if configuration . SMTP . TLS . ServerName == "" {
configuration . SMTP . TLS . ServerName = configuration . SMTP . Host
}
2020-01-22 03:15:40 +07:00
return
}
}