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
|
|
|
)
|
|
|
|
|
|
|
|
// 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.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.
|
2020-04-23 01:11:32 +00:00
|
|
|
viper.BindEnv("authelia.jwt_secret") //nolint:errcheck // TODO: Legacy code, consider refactoring time permitting.
|
|
|
|
viper.BindEnv("authelia.duo_api.secret_key") //nolint:errcheck // TODO: Legacy code, consider refactoring time permitting.
|
|
|
|
viper.BindEnv("authelia.session.secret") //nolint:errcheck // TODO: Legacy code, consider refactoring time permitting.
|
|
|
|
viper.BindEnv("authelia.authentication_backend.ldap.password") //nolint:errcheck // TODO: Legacy code, consider refactoring time permitting.
|
|
|
|
viper.BindEnv("authelia.notifier.smtp.password") //nolint:errcheck // TODO: Legacy code, consider refactoring time permitting.
|
|
|
|
viper.BindEnv("authelia.session.redis.password") //nolint:errcheck // TODO: Legacy code, consider refactoring time permitting.
|
|
|
|
viper.BindEnv("authelia.storage.mysql.password") //nolint:errcheck // TODO: Legacy code, consider refactoring time permitting.
|
|
|
|
viper.BindEnv("authelia.storage.postgres.password") //nolint:errcheck // TODO: Legacy code, consider refactoring time permitting.
|
|
|
|
|
|
|
|
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-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
|
2020-04-22 03:33:14 +00:00
|
|
|
viper.Unmarshal(&configuration) //nolint:errcheck // TODO: Legacy code, consider refactoring time permitting.
|
2020-01-21 20:56:44 +00:00
|
|
|
|
2019-04-24 21:52:08 +00:00
|
|
|
val := schema.NewStructValidator()
|
2020-04-23 01:11:32 +00:00
|
|
|
validator.ValidateSecrets(&configuration, val, viper.GetViper())
|
|
|
|
validator.ValidateConfiguration(&configuration, val)
|
2020-04-23 01:47:27 +00:00
|
|
|
validator.ValidateKeys(val, viper.AllKeys())
|
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
|
|
|
}
|