2019-04-24 21:52:08 +00:00
|
|
|
package configuration
|
|
|
|
|
|
|
|
import (
|
2020-01-21 20:56:44 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2020-04-05 12:37:21 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
2019-12-24 02:14:52 +00:00
|
|
|
"github.com/authelia/authelia/internal/configuration/schema"
|
|
|
|
"github.com/authelia/authelia/internal/configuration/validator"
|
2019-04-24 21:52:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func check(e error) {
|
|
|
|
if e != nil {
|
|
|
|
panic(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read a YAML configuration and create a Configuration object out of it.
|
|
|
|
func Read(configPath string) (*schema.Configuration, []error) {
|
2020-01-21 20:56:44 +00:00
|
|
|
viper.SetEnvPrefix("AUTHELIA")
|
|
|
|
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
2020-01-21 22:02:03 +00:00
|
|
|
|
|
|
|
// we need to bind all env variables as long as https://github.com/spf13/viper/issues/761
|
|
|
|
// is not resolved.
|
|
|
|
viper.BindEnv("jwt_secret")
|
|
|
|
viper.BindEnv("duo_api.secret_key")
|
|
|
|
viper.BindEnv("session.secret")
|
|
|
|
viper.BindEnv("authentication_backend.ldap.password")
|
|
|
|
viper.BindEnv("notifier.smtp.password")
|
|
|
|
viper.BindEnv("session.redis.password")
|
|
|
|
viper.BindEnv("storage.mysql.password")
|
|
|
|
viper.BindEnv("storage.postgres.password")
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2020-01-21 20:56:44 +00:00
|
|
|
viper.SetConfigFile(configPath)
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2020-01-21 20:56:44 +00: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-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
2020-01-21 20:56:44 +00:00
|
|
|
var configuration schema.Configuration
|
|
|
|
viper.Unmarshal(&configuration)
|
|
|
|
|
2019-04-24 21:52:08 +00:00
|
|
|
val := schema.NewStructValidator()
|
2020-01-21 20:56:44 +00:00
|
|
|
validator.Validate(&configuration, val)
|
2019-04-24 21:52:08 +00:00
|
|
|
|
|
|
|
if val.HasErrors() {
|
|
|
|
return nil, val.Errors()
|
|
|
|
}
|
|
|
|
|
2020-01-21 20:56:44 +00:00
|
|
|
return &configuration, nil
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|