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.
|
2020-02-06 09:53:02 +07:00
|
|
|
func ValidateStorage(configuration schema.StorageConfiguration, validator *schema.StructValidator) {
|
2019-11-17 02:50:58 +07:00
|
|
|
if configuration.Local == nil && configuration.MySQL == nil && configuration.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 {
|
|
|
|
case configuration.MySQL != nil:
|
2021-12-02 12:36:03 +07:00
|
|
|
validateSQLConfiguration(&configuration.MySQL.SQLStorageConfiguration, validator, "mysql")
|
2020-05-06 07:52:06 +07:00
|
|
|
case configuration.PostgreSQL != nil:
|
2019-11-17 02:50:58 +07:00
|
|
|
validatePostgreSQLConfiguration(configuration.PostgreSQL, validator)
|
2020-05-06 07:52:06 +07:00
|
|
|
case configuration.Local != nil:
|
2019-11-16 17:38:21 +07:00
|
|
|
validateLocalStorageConfiguration(configuration.Local, validator)
|
|
|
|
}
|
2021-11-25 08:56:58 +07:00
|
|
|
|
|
|
|
if configuration.EncryptionKey == "" {
|
2021-12-02 12:36:03 +07:00
|
|
|
validator.Push(errors.New(errStrStorageEncryptionKeyMustBeProvided))
|
2021-11-25 08:56:58 +07:00
|
|
|
} else if len(configuration.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
|
|
|
}
|
|
|
|
|
2021-12-02 12:36:03 +07:00
|
|
|
func validateSQLConfiguration(configuration *schema.SQLStorageConfiguration, validator *schema.StructValidator, provider string) {
|
2021-08-06 12:35:14 +07:00
|
|
|
if configuration.Timeout == 0 {
|
2021-12-02 12:36:03 +07:00
|
|
|
configuration.Timeout = schema.DefaultSQLStorageConfiguration.Timeout
|
2021-08-06 12:35:14 +07:00
|
|
|
}
|
|
|
|
|
2021-12-02 12:36:03 +07: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 17:38:21 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
if configuration.Database == "" {
|
2021-12-02 12:36:03 +07:00
|
|
|
validator.Push(fmt.Errorf(errFmtStorageOptionMustBeProvided, provider, "database"))
|
2019-11-16 17:38:21 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-17 02:50:58 +07:00
|
|
|
func validatePostgreSQLConfiguration(configuration *schema.PostgreSQLStorageConfiguration, validator *schema.StructValidator) {
|
2021-12-02 12:36:03 +07:00
|
|
|
validateSQLConfiguration(&configuration.SQLStorageConfiguration, validator, "postgres")
|
2019-11-17 02:50:58 +07:00
|
|
|
|
2021-12-03 13:29:55 +07:00
|
|
|
if configuration.Schema == "" {
|
|
|
|
configuration.Schema = schema.DefaultPostgreSQLStorageConfiguration.Schema
|
|
|
|
}
|
|
|
|
|
2021-12-02 12:36:03 +07:00
|
|
|
// Deprecated. TODO: Remove in v4.36.0.
|
|
|
|
if configuration.SSLMode != "" && configuration.SSL.Mode == "" {
|
|
|
|
configuration.SSL.Mode = configuration.SSLMode
|
2019-11-17 02:50:58 +07:00
|
|
|
}
|
|
|
|
|
2021-12-02 12:36:03 +07:00
|
|
|
if configuration.SSL.Mode == "" {
|
2021-12-03 13:29:55 +07:00
|
|
|
configuration.SSL.Mode = schema.DefaultPostgreSQLStorageConfiguration.SSL.Mode
|
2021-12-02 12:36:03 +07:00
|
|
|
} else if !utils.IsStringInSlice(configuration.SSL.Mode, storagePostgreSQLValidSSLModes) {
|
|
|
|
validator.Push(fmt.Errorf(errFmtStoragePostgreSQLInvalidSSLMode, configuration.SSL.Mode, strings.Join(storagePostgreSQLValidSSLModes, "', '")))
|
2019-11-17 02:50:58 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-16 17:38:21 +07:00
|
|
|
func validateLocalStorageConfiguration(configuration *schema.LocalStorageConfiguration, validator *schema.StructValidator) {
|
|
|
|
if configuration.Path == "" {
|
2021-12-02 12:36:03 +07:00
|
|
|
validator.Push(fmt.Errorf(errFmtStorageOptionMustBeProvided, "local", "path"))
|
2019-11-16 17:38:21 +07:00
|
|
|
}
|
|
|
|
}
|