2019-04-24 21:52:08 +00:00
|
|
|
package configuration
|
|
|
|
|
|
|
|
import (
|
2021-02-21 23:07:06 +00:00
|
|
|
_ "embed" // Embed config.template.yml.
|
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"
|
2021-01-04 10:28:55 +00:00
|
|
|
"github.com/authelia/authelia/internal/logging"
|
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) {
|
2021-01-16 23:23:35 +00:00
|
|
|
logger := logging.Logger()
|
|
|
|
|
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 {
|
2020-06-17 06:25:35 +00:00
|
|
|
errs := []error{
|
|
|
|
fmt.Errorf("Unable to find config file: %v", configPath),
|
|
|
|
fmt.Errorf("Generating config file: %v", configPath),
|
|
|
|
}
|
|
|
|
|
|
|
|
err = generateConfigFromTemplate(configPath)
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
} else {
|
|
|
|
errs = append(errs, fmt.Errorf("Generated configuration at: %v", configPath))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errs
|
2020-06-08 22:22:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2021-01-04 10:28:55 +00:00
|
|
|
if val.HasWarnings() {
|
|
|
|
for _, warn := range val.Warnings() {
|
2021-01-16 23:23:35 +00:00
|
|
|
logger.Warnf(warn.Error())
|
2021-01-04 10:28:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-21 20:56:44 +00:00
|
|
|
return &configuration, nil
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
2020-06-17 06:25:35 +00:00
|
|
|
|
2021-02-21 23:07:06 +00:00
|
|
|
//go:embed config.template.yml
|
|
|
|
var cfg []byte
|
2020-06-17 06:25:35 +00:00
|
|
|
|
2021-02-21 23:07:06 +00:00
|
|
|
func generateConfigFromTemplate(configPath string) error {
|
|
|
|
err := ioutil.WriteFile(configPath, cfg, 0600)
|
2020-06-17 06:25:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Unable to generate %v: %v", configPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|