2019-04-24 21:52:08 +00:00
|
|
|
package configuration
|
|
|
|
|
|
|
|
import (
|
2020-06-08 22:22:41 +00:00
|
|
|
"errors"
|
2020-01-21 20:56:44 +00:00
|
|
|
"fmt"
|
2020-06-08 22:22:41 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2020-01-21 20:56:44 +00:00
|
|
|
"strings"
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2020-04-05 12:37:21 +00:00
|
|
|
"github.com/spf13/viper"
|
2020-06-08 22:22:41 +00:00
|
|
|
"gopkg.in/yaml.v2"
|
2020-04-05 12:37:21 +00:00
|
|
|
|
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-06-08 22:22:41 +00:00
|
|
|
if configPath == "" {
|
|
|
|
return nil, []error{errors.New("No config file path provided")}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := os.Stat(configPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, []error{fmt.Errorf("Unable to find config file: %v", configPath)}
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := ioutil.ReadFile(configPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, []error{fmt.Errorf("Failed to %v", err)}
|
|
|
|
}
|
|
|
|
|
|
|
|
var data interface{}
|
|
|
|
|
|
|
|
err = yaml.Unmarshal(file, &data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, []error{fmt.Errorf("Error malformed %v", err)}
|
|
|
|
}
|
|
|
|
|
2020-01-21 20:56:44 +00:00
|
|
|
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
2020-01-21 22:02:03 +00:00
|
|
|
|
2020-04-23 01:11:32 +00: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-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-06-08 22:22:41 +00:00
|
|
|
_ = viper.ReadInConfig()
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2020-01-21 20:56:44 +00:00
|
|
|
var configuration schema.Configuration
|
2020-05-05 19:35:32 +00:00
|
|
|
|
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
|
|
|
}
|