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:
|
2019-11-16 19:50:58 +00:00
|
|
|
validateSQLConfiguration(&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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateSQLConfiguration(configuration *schema.SQLStorageConfiguration, validator *schema.StructValidator) {
|
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) {
|
|
|
|
validateSQLConfiguration(&configuration.SQLStorageConfiguration, validator)
|
|
|
|
|
|
|
|
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'"))
|
|
|
|
}
|
|
|
|
}
|