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.
|
2022-02-28 03:15:01 +00:00
|
|
|
func ValidateStorage(config schema.StorageConfiguration, validator *schema.StructValidator) {
|
|
|
|
if config.Local == nil && config.MySQL == nil && config.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 {
|
2022-02-28 03:15:01 +00: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 10:38:21 +00:00
|
|
|
}
|
2021-11-25 01:56:58 +00:00
|
|
|
|
2022-02-28 03:15:01 +00:00
|
|
|
if config.EncryptionKey == "" {
|
2021-12-02 05:36:03 +00:00
|
|
|
validator.Push(errors.New(errStrStorageEncryptionKeyMustBeProvided))
|
2022-02-28 03:15:01 +00:00
|
|
|
} else if len(config.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
|
|
|
}
|
|
|
|
|
2022-02-28 03:15:01 +00:00
|
|
|
func validateSQLConfiguration(config *schema.SQLStorageConfiguration, validator *schema.StructValidator, provider string) {
|
|
|
|
if config.Timeout == 0 {
|
|
|
|
config.Timeout = schema.DefaultSQLStorageConfiguration.Timeout
|
2021-08-06 05:35:14 +00:00
|
|
|
}
|
|
|
|
|
2022-02-28 03:15:01 +00:00
|
|
|
if config.Host == "" {
|
2021-12-02 05:36:03 +00:00
|
|
|
validator.Push(fmt.Errorf(errFmtStorageOptionMustBeProvided, provider, "host"))
|
|
|
|
}
|
|
|
|
|
2022-02-28 03:15:01 +00:00
|
|
|
if config.Username == "" || config.Password == "" {
|
2021-12-02 05:36:03 +00:00
|
|
|
validator.Push(fmt.Errorf(errFmtStorageUserPassMustBeProvided, provider))
|
2019-11-16 10:38:21 +00:00
|
|
|
}
|
|
|
|
|
2022-02-28 03:15:01 +00:00
|
|
|
if config.Database == "" {
|
2021-12-02 05:36:03 +00:00
|
|
|
validator.Push(fmt.Errorf(errFmtStorageOptionMustBeProvided, provider, "database"))
|
2019-11-16 10:38:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-28 03:15:01 +00:00
|
|
|
func validatePostgreSQLConfiguration(config *schema.PostgreSQLStorageConfiguration, validator *schema.StructValidator) {
|
|
|
|
validateSQLConfiguration(&config.SQLStorageConfiguration, validator, "postgres")
|
2019-11-16 19:50:58 +00:00
|
|
|
|
2022-02-28 03:15:01 +00:00
|
|
|
if config.Schema == "" {
|
|
|
|
config.Schema = schema.DefaultPostgreSQLStorageConfiguration.Schema
|
2021-12-03 06:29:55 +00:00
|
|
|
}
|
|
|
|
|
2021-12-02 05:36:03 +00:00
|
|
|
// Deprecated. TODO: Remove in v4.36.0.
|
2022-02-28 03:15:01 +00:00
|
|
|
if config.SSLMode != "" && config.SSL.Mode == "" {
|
|
|
|
config.SSL.Mode = config.SSLMode
|
2019-11-16 19:50:58 +00:00
|
|
|
}
|
|
|
|
|
2022-02-28 03:15:01 +00: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-16 19:50:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-28 03:15:01 +00:00
|
|
|
func validateLocalStorageConfiguration(config *schema.LocalStorageConfiguration, validator *schema.StructValidator) {
|
|
|
|
if config.Path == "" {
|
2021-12-02 05:36:03 +00:00
|
|
|
validator.Push(fmt.Errorf(errFmtStorageOptionMustBeProvided, "local", "path"))
|
2019-11-16 10:38:21 +00:00
|
|
|
}
|
|
|
|
}
|