2019-11-16 10:38:21 +00:00
|
|
|
package validator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2021-12-02 05:36:03 +00:00
|
|
|
"fmt"
|
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:
|
2022-10-22 08:27:59 +00:00
|
|
|
validateMySQLConfiguration(config.MySQL, validator)
|
2022-02-28 03:15:01 +00:00
|
|
|
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) {
|
2023-05-07 06:39:17 +00:00
|
|
|
if config.Address == nil {
|
|
|
|
if config.Host == "" { //nolint:staticcheck
|
|
|
|
validator.Push(fmt.Errorf(errFmtStorageOptionMustBeProvided, provider, "address"))
|
|
|
|
} else {
|
|
|
|
host := config.Host //nolint:staticcheck
|
|
|
|
port := config.Port //nolint:staticcheck
|
|
|
|
|
|
|
|
if address, err := schema.NewAddressFromNetworkValuesDefault(host, port, schema.AddressSchemeTCP, schema.AddressSchemeUnix); err != nil {
|
|
|
|
validator.Push(fmt.Errorf(errFmtStorageFailedToConvertHostPortToAddress, provider, err))
|
|
|
|
} else {
|
|
|
|
config.Address = &schema.AddressTCP{Address: *address}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if config.Host != "" || config.Port != 0 { //nolint:staticcheck
|
|
|
|
validator.Push(fmt.Errorf(errFmtStorageOptionAddressConflictWithHostPort, provider))
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2021-08-06 05:35:14 +00:00
|
|
|
|
2023-05-07 06:39:17 +00:00
|
|
|
if err = config.Address.ValidateSQL(); err != nil {
|
|
|
|
validator.Push(fmt.Errorf(errFmtServerAddress, config.Address.String(), err))
|
|
|
|
}
|
2021-12-02 05:36:03 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2023-05-07 06:39:17 +00:00
|
|
|
|
|
|
|
if config.Timeout == 0 {
|
|
|
|
config.Timeout = schema.DefaultSQLStorageConfiguration.Timeout
|
|
|
|
}
|
2019-11-16 10:38:21 +00:00
|
|
|
}
|
|
|
|
|
2022-10-22 08:27:59 +00:00
|
|
|
func validateMySQLConfiguration(config *schema.MySQLStorageConfiguration, validator *schema.StructValidator) {
|
|
|
|
validateSQLConfiguration(&config.SQLStorageConfiguration, validator, "mysql")
|
|
|
|
|
|
|
|
if config.TLS != nil {
|
|
|
|
configDefaultTLS := &schema.TLSConfig{
|
|
|
|
MinimumVersion: schema.DefaultMySQLStorageConfiguration.TLS.MinimumVersion,
|
|
|
|
MaximumVersion: schema.DefaultMySQLStorageConfiguration.TLS.MaximumVersion,
|
|
|
|
}
|
|
|
|
|
2023-05-07 06:39:17 +00:00
|
|
|
if config.Address != nil {
|
|
|
|
configDefaultTLS.ServerName = config.Address.Hostname()
|
|
|
|
}
|
|
|
|
|
2022-10-22 08:27:59 +00:00
|
|
|
if err := ValidateTLSConfig(config.TLS, configDefaultTLS); err != nil {
|
|
|
|
validator.Push(fmt.Errorf(errFmtStorageTLSConfigInvalid, "mysql", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-10-22 08:27:59 +00:00
|
|
|
switch {
|
|
|
|
case config.TLS != nil && config.SSL != nil:
|
|
|
|
validator.Push(fmt.Errorf(errFmtStoragePostgreSQLInvalidSSLAndTLSConfig))
|
|
|
|
case config.TLS != nil:
|
|
|
|
configDefaultTLS := &schema.TLSConfig{
|
2023-05-07 06:39:17 +00:00
|
|
|
ServerName: config.Address.Hostname(),
|
2022-10-22 08:27:59 +00:00
|
|
|
MinimumVersion: schema.DefaultPostgreSQLStorageConfiguration.TLS.MinimumVersion,
|
|
|
|
MaximumVersion: schema.DefaultPostgreSQLStorageConfiguration.TLS.MaximumVersion,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ValidateTLSConfig(config.TLS, configDefaultTLS); err != nil {
|
|
|
|
validator.Push(fmt.Errorf(errFmtStorageTLSConfigInvalid, "postgres", err))
|
|
|
|
}
|
|
|
|
case config.SSL != nil:
|
|
|
|
validator.PushWarning(fmt.Errorf(warnFmtStoragePostgreSQLInvalidSSLDeprecated))
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case config.SSL.Mode == "":
|
|
|
|
config.SSL.Mode = schema.DefaultPostgreSQLStorageConfiguration.SSL.Mode
|
|
|
|
case !utils.IsStringInSlice(config.SSL.Mode, validStoragePostgreSQLSSLModes):
|
2023-04-13 10:58:18 +00:00
|
|
|
validator.Push(fmt.Errorf(errFmtStoragePostgreSQLInvalidSSLMode, strJoinOr(validStoragePostgreSQLSSLModes), config.SSL.Mode))
|
2022-10-22 08:27:59 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|