2019-11-16 10:38:21 +00:00
|
|
|
package validator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2021-12-02 05:36:03 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2019-11-16 10:38:21 +00:00
|
|
|
|
2021-08-11 01:04:35 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
2021-12-02 05:36:03 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/utils"
|
2019-11-16 10:38:21 +00:00
|
|
|
)
|
|
|
|
|
2020-04-20 21:03:38 +00:00
|
|
|
// ValidateStorage validates storage configuration.
|
2020-02-06 02:53:02 +00:00
|
|
|
func ValidateStorage(configuration schema.StorageConfiguration, validator *schema.StructValidator) {
|
2019-11-16 19:50:58 +00:00
|
|
|
if configuration.Local == nil && configuration.MySQL == nil && configuration.PostgreSQL == nil {
|
2021-12-02 05:36:03 +00:00
|
|
|
validator.Push(errors.New(errStrStorage))
|
2019-11-16 10:38:21 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 00:52:06 +00:00
|
|
|
switch {
|
|
|
|
case configuration.MySQL != nil:
|
2021-12-02 05:36:03 +00:00
|
|
|
validateSQLConfiguration(&configuration.MySQL.SQLStorageConfiguration, validator, "mysql")
|
2020-05-06 00:52:06 +00:00
|
|
|
case configuration.PostgreSQL != nil:
|
2019-11-16 19:50:58 +00:00
|
|
|
validatePostgreSQLConfiguration(configuration.PostgreSQL, validator)
|
2020-05-06 00:52:06 +00:00
|
|
|
case configuration.Local != nil:
|
2019-11-16 10:38:21 +00:00
|
|
|
validateLocalStorageConfiguration(configuration.Local, validator)
|
|
|
|
}
|
2021-11-25 01:56:58 +00:00
|
|
|
|
|
|
|
if configuration.EncryptionKey == "" {
|
2021-12-02 05:36:03 +00:00
|
|
|
validator.Push(errors.New(errStrStorageEncryptionKeyMustBeProvided))
|
2021-11-25 01:56:58 +00:00
|
|
|
} else if len(configuration.EncryptionKey) < 20 {
|
2021-12-02 05:36:03 +00:00
|
|
|
validator.Push(errors.New(errStrStorageEncryptionKeyTooShort))
|
2021-11-25 01:56:58 +00:00
|
|
|
}
|
2019-11-16 10:38:21 +00:00
|
|
|
}
|
|
|
|
|
2021-12-02 05:36:03 +00:00
|
|
|
func validateSQLConfiguration(configuration *schema.SQLStorageConfiguration, validator *schema.StructValidator, provider string) {
|
2021-08-06 05:35:14 +00:00
|
|
|
if configuration.Timeout == 0 {
|
2021-12-02 05:36:03 +00:00
|
|
|
configuration.Timeout = schema.DefaultSQLStorageConfiguration.Timeout
|
2021-08-06 05:35:14 +00:00
|
|
|
}
|
|
|
|
|
2021-12-02 05:36:03 +00:00
|
|
|
if configuration.Host == "" {
|
|
|
|
validator.Push(fmt.Errorf(errFmtStorageOptionMustBeProvided, provider, "host"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if configuration.Username == "" || configuration.Password == "" {
|
|
|
|
validator.Push(fmt.Errorf(errFmtStorageUserPassMustBeProvided, provider))
|
2019-11-16 10:38:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if configuration.Database == "" {
|
2021-12-02 05:36:03 +00:00
|
|
|
validator.Push(fmt.Errorf(errFmtStorageOptionMustBeProvided, provider, "database"))
|
2019-11-16 10:38:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-16 19:50:58 +00:00
|
|
|
func validatePostgreSQLConfiguration(configuration *schema.PostgreSQLStorageConfiguration, validator *schema.StructValidator) {
|
2021-12-02 05:36:03 +00:00
|
|
|
validateSQLConfiguration(&configuration.SQLStorageConfiguration, validator, "postgres")
|
2019-11-16 19:50:58 +00:00
|
|
|
|
2021-12-02 05:36:03 +00:00
|
|
|
// Deprecated. TODO: Remove in v4.36.0.
|
|
|
|
if configuration.SSLMode != "" && configuration.SSL.Mode == "" {
|
|
|
|
configuration.SSL.Mode = configuration.SSLMode
|
2019-11-16 19:50:58 +00:00
|
|
|
}
|
|
|
|
|
2021-12-02 05:36:03 +00:00
|
|
|
if configuration.SSL.Mode == "" {
|
|
|
|
configuration.SSL.Mode = testModeDisabled
|
|
|
|
} else if !utils.IsStringInSlice(configuration.SSL.Mode, storagePostgreSQLValidSSLModes) {
|
|
|
|
validator.Push(fmt.Errorf(errFmtStoragePostgreSQLInvalidSSLMode, configuration.SSL.Mode, strings.Join(storagePostgreSQLValidSSLModes, "', '")))
|
2019-11-16 19:50:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-16 10:38:21 +00:00
|
|
|
func validateLocalStorageConfiguration(configuration *schema.LocalStorageConfiguration, validator *schema.StructValidator) {
|
|
|
|
if configuration.Path == "" {
|
2021-12-02 05:36:03 +00:00
|
|
|
validator.Push(fmt.Errorf(errFmtStorageOptionMustBeProvided, "local", "path"))
|
2019-11-16 10:38:21 +00:00
|
|
|
}
|
|
|
|
}
|