2019-04-25 04:52:08 +07:00
|
|
|
package configuration
|
|
|
|
|
|
|
|
import (
|
2020-01-22 03:56:44 +07:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2020-04-05 19:37:21 +07:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
2019-12-24 09:14:52 +07:00
|
|
|
"github.com/authelia/authelia/internal/configuration/schema"
|
|
|
|
"github.com/authelia/authelia/internal/configuration/validator"
|
2019-04-25 04:52:08 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
// Read a YAML configuration and create a Configuration object out of it.
|
|
|
|
func Read(configPath string) (*schema.Configuration, []error) {
|
2020-01-22 03:56:44 +07:00
|
|
|
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
2020-01-22 05:02:03 +07:00
|
|
|
|
2020-04-23 08:11:32 +07:00
|
|
|
viper.BindEnv("authelia.jwt_secret.file") //nolint:errcheck // TODO: Legacy code, consider refactoring time permitting.
|
|
|
|
viper.BindEnv("authelia.duo_api.secret_key.file") //nolint:errcheck // TODO: Legacy code, consider refactoring time permitting.
|
|
|
|
viper.BindEnv("authelia.session.secret.file") //nolint:errcheck // TODO: Legacy code, consider refactoring time permitting.
|
|
|
|
viper.BindEnv("authelia.authentication_backend.ldap.password.file") //nolint:errcheck // TODO: Legacy code, consider refactoring time permitting.
|
|
|
|
viper.BindEnv("authelia.notifier.smtp.password.file") //nolint:errcheck // TODO: Legacy code, consider refactoring time permitting.
|
|
|
|
viper.BindEnv("authelia.session.redis.password.file") //nolint:errcheck // TODO: Legacy code, consider refactoring time permitting.
|
|
|
|
viper.BindEnv("authelia.storage.mysql.password.file") //nolint:errcheck // TODO: Legacy code, consider refactoring time permitting.
|
|
|
|
viper.BindEnv("authelia.storage.postgres.password.file") //nolint:errcheck // TODO: Legacy code, consider refactoring time permitting.
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2020-01-22 03:56:44 +07:00
|
|
|
viper.SetConfigFile(configPath)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2020-01-22 03:56:44 +07:00
|
|
|
if err := viper.ReadInConfig(); err != nil {
|
|
|
|
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
|
|
|
return nil, []error{fmt.Errorf("unable to find config file %s", configPath)}
|
|
|
|
}
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
2020-01-22 03:56:44 +07:00
|
|
|
var configuration schema.Configuration
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2020-04-22 10:33:14 +07:00
|
|
|
viper.Unmarshal(&configuration) //nolint:errcheck // TODO: Legacy code, consider refactoring time permitting.
|
2020-01-22 03:56:44 +07:00
|
|
|
|
2019-04-25 04:52:08 +07:00
|
|
|
val := schema.NewStructValidator()
|
2020-04-23 08:11:32 +07:00
|
|
|
validator.ValidateSecrets(&configuration, val, viper.GetViper())
|
|
|
|
validator.ValidateConfiguration(&configuration, val)
|
2020-04-23 08:47:27 +07:00
|
|
|
validator.ValidateKeys(val, viper.AllKeys())
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
if val.HasErrors() {
|
|
|
|
return nil, val.Errors()
|
|
|
|
}
|
|
|
|
|
2020-01-22 03:56:44 +07:00
|
|
|
return &configuration, nil
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|