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.SetEnvPrefix("AUTHELIA")
|
|
|
|
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
2020-01-22 05:02:03 +07: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-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
|
|
|
|
viper.Unmarshal(&configuration)
|
|
|
|
|
2019-04-25 04:52:08 +07:00
|
|
|
val := schema.NewStructValidator()
|
2020-01-22 03:56:44 +07:00
|
|
|
validator.Validate(&configuration, val)
|
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
|
|
|
}
|