2019-11-16 10:38:21 +00:00
|
|
|
package validator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
2019-12-24 02:14:52 +00:00
|
|
|
"github.com/authelia/authelia/internal/configuration/schema"
|
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 {
|
|
|
|
validator.Push(errors.New("A storage configuration must be provided. It could be 'local', 'mysql' or 'postgres'"))
|
2019-11-16 10:38:21 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 00:52:06 +00:00
|
|
|
switch {
|
|
|
|
case configuration.MySQL != nil:
|
2021-08-06 05:35:14 +00:00
|
|
|
validateMySQLConfiguration(&configuration.MySQL.SQLStorageConfiguration, validator)
|
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-08-06 05:35:14 +00:00
|
|
|
func validateMySQLConfiguration(configuration *schema.SQLStorageConfiguration, validator *schema.StructValidator) {
|
|
|
|
if configuration.Timeout == 0 {
|
|
|
|
configuration.Timeout = schema.DefaultMySQLStorageConfiguration.Timeout
|
|
|
|
}
|
|
|
|
|
2020-02-06 02:53:02 +00:00
|
|
|
if configuration.Password == "" || configuration.Username == "" {
|
2021-03-22 09:04:09 +00:00
|
|
|
validator.Push(errors.New("the SQL username and password must be provided"))
|
2019-11-16 10:38:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if configuration.Database == "" {
|
2021-03-22 09:04:09 +00:00
|
|
|
validator.Push(errors.New("the SQL database must be provided"))
|
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-08-06 05:35:14 +00:00
|
|
|
validateMySQLConfiguration(&configuration.SQLStorageConfiguration, validator)
|
|
|
|
|
|
|
|
if configuration.Timeout == 0 {
|
|
|
|
configuration.Timeout = schema.DefaultPostgreSQLStorageConfiguration.Timeout
|
|
|
|
}
|
2019-11-16 19:50:58 +00:00
|
|
|
|
|
|
|
if configuration.SSLMode == "" {
|
2020-05-02 16:20:40 +00:00
|
|
|
configuration.SSLMode = testModeDisabled
|
2019-11-16 19:50:58 +00:00
|
|
|
}
|
|
|
|
|
2020-05-02 16:20:40 +00:00
|
|
|
if !(configuration.SSLMode == testModeDisabled || configuration.SSLMode == "require" ||
|
2019-11-16 19:50:58 +00:00
|
|
|
configuration.SSLMode == "verify-ca" || configuration.SSLMode == "verify-full") {
|
2020-04-23 01:11:32 +00:00
|
|
|
validator.Push(errors.New("SSL mode must be 'disable', 'require', 'verify-ca', or 'verify-full'"))
|
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 == "" {
|
|
|
|
validator.Push(errors.New("A file path must be provided with key 'path'"))
|
|
|
|
}
|
|
|
|
}
|