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