2019-11-16 17:38:21 +07:00
|
|
|
package validator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
2019-12-24 09:14:52 +07:00
|
|
|
"github.com/authelia/authelia/internal/configuration/schema"
|
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 {
|
|
|
|
validator.Push(errors.New("A storage configuration must be provided. It could be 'local', 'mysql' or 'postgres'"))
|
2019-11-16 17:38:21 +07:00
|
|
|
}
|
|
|
|
|
2019-11-17 02:50:58 +07:00
|
|
|
if configuration.MySQL != nil {
|
|
|
|
validateSQLConfiguration(&configuration.MySQL.SQLStorageConfiguration, validator)
|
|
|
|
} else if configuration.PostgreSQL != nil {
|
|
|
|
validatePostgreSQLConfiguration(configuration.PostgreSQL, validator)
|
2019-11-16 17:38:21 +07:00
|
|
|
} else if configuration.Local != nil {
|
|
|
|
validateLocalStorageConfiguration(configuration.Local, validator)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateSQLConfiguration(configuration *schema.SQLStorageConfiguration, validator *schema.StructValidator) {
|
2020-02-06 09:53:02 +07:00
|
|
|
if configuration.Password == "" || configuration.Username == "" {
|
2019-11-16 17:38:21 +07:00
|
|
|
validator.Push(errors.New("Username and password must be provided"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if configuration.Database == "" {
|
|
|
|
validator.Push(errors.New("A database must be provided"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-17 02:50:58 +07:00
|
|
|
func validatePostgreSQLConfiguration(configuration *schema.PostgreSQLStorageConfiguration, validator *schema.StructValidator) {
|
|
|
|
validateSQLConfiguration(&configuration.SQLStorageConfiguration, validator)
|
|
|
|
|
|
|
|
if configuration.SSLMode == "" {
|
2020-05-02 23:20:40 +07:00
|
|
|
configuration.SSLMode = testModeDisabled
|
2019-11-17 02:50:58 +07:00
|
|
|
}
|
|
|
|
|
2020-05-02 23:20:40 +07:00
|
|
|
if !(configuration.SSLMode == testModeDisabled || configuration.SSLMode == "require" ||
|
2019-11-17 02:50:58 +07:00
|
|
|
configuration.SSLMode == "verify-ca" || configuration.SSLMode == "verify-full") {
|
2020-04-23 08:11:32 +07:00
|
|
|
validator.Push(errors.New("SSL mode must be 'disable', 'require', 'verify-ca', or 'verify-full'"))
|
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 == "" {
|
|
|
|
validator.Push(errors.New("A file path must be provided with key 'path'"))
|
|
|
|
}
|
|
|
|
}
|