2021-05-02 22:49:16 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2023-08-26 17:40:48 +00:00
|
|
|
_ "embed"
|
2022-12-09 13:34:20 +00:00
|
|
|
"errors"
|
2022-07-05 20:26:33 +00:00
|
|
|
"fmt"
|
2022-07-26 09:33:35 +00:00
|
|
|
"net/url"
|
|
|
|
"os"
|
2023-04-28 22:39:34 +00:00
|
|
|
"path/filepath"
|
2022-07-26 09:33:35 +00:00
|
|
|
"reflect"
|
2021-12-20 20:23:19 +00:00
|
|
|
"strings"
|
2021-05-04 12:07:12 +00:00
|
|
|
|
2021-07-29 09:30:30 +00:00
|
|
|
env "github.com/Netflix/go-env"
|
2021-05-02 22:49:16 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2023-08-26 17:40:48 +00:00
|
|
|
"goauthentik.io/authentik/lib"
|
2021-05-04 12:07:12 +00:00
|
|
|
"gopkg.in/yaml.v2"
|
2021-05-02 22:49:16 +00:00
|
|
|
)
|
|
|
|
|
2022-07-26 09:33:35 +00:00
|
|
|
var cfg *Config
|
2021-05-02 22:49:16 +00:00
|
|
|
|
2023-08-26 17:40:48 +00:00
|
|
|
const defaultConfigPath = "./authentik/lib/default.yml"
|
|
|
|
|
2023-04-28 22:39:34 +00:00
|
|
|
func getConfigPaths() []string {
|
2023-08-26 17:40:48 +00:00
|
|
|
configPaths := []string{defaultConfigPath, "/etc/authentik/config.yml", ""}
|
2023-04-28 22:39:34 +00:00
|
|
|
globConfigPaths, _ := filepath.Glob("/etc/authentik/config.d/*.yml")
|
|
|
|
configPaths = append(configPaths, globConfigPaths...)
|
|
|
|
|
|
|
|
environment := "local"
|
|
|
|
if v, ok := os.LookupEnv("AUTHENTIK_ENV"); ok {
|
|
|
|
environment = v
|
|
|
|
}
|
|
|
|
|
|
|
|
computedConfigPaths := []string{}
|
|
|
|
|
|
|
|
for _, path := range configPaths {
|
|
|
|
path, err := filepath.Abs(path)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if stat, err := os.Stat(path); err == nil {
|
|
|
|
if !stat.IsDir() {
|
|
|
|
computedConfigPaths = append(computedConfigPaths, path)
|
|
|
|
} else {
|
|
|
|
envPaths := []string{
|
|
|
|
filepath.Join(path, environment+".yml"),
|
|
|
|
filepath.Join(path, environment+".env.yml"),
|
|
|
|
}
|
|
|
|
for _, envPath := range envPaths {
|
|
|
|
if stat, err = os.Stat(envPath); err == nil && !stat.IsDir() {
|
|
|
|
computedConfigPaths = append(computedConfigPaths, envPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return computedConfigPaths
|
|
|
|
}
|
|
|
|
|
2022-07-26 09:33:35 +00:00
|
|
|
func Get() *Config {
|
|
|
|
if cfg == nil {
|
2023-04-28 22:58:44 +00:00
|
|
|
c := &Config{}
|
2023-04-28 22:39:34 +00:00
|
|
|
c.Setup(getConfigPaths()...)
|
2022-08-05 22:24:49 +00:00
|
|
|
cfg = c
|
2022-07-26 09:33:35 +00:00
|
|
|
}
|
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) Setup(paths ...string) {
|
2023-08-26 17:40:48 +00:00
|
|
|
// initially try to load the default config which is compiled in
|
|
|
|
err := c.LoadConfig(lib.DefaultConfig())
|
|
|
|
// this should never fail
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("failed to load inbuilt config: %v", err))
|
|
|
|
}
|
|
|
|
log.WithField("path", "inbuilt-default").Debug("Loaded config")
|
2022-07-26 09:33:35 +00:00
|
|
|
for _, path := range paths {
|
2023-08-26 17:40:48 +00:00
|
|
|
err := c.LoadConfigFromFile(path)
|
2022-07-26 09:33:35 +00:00
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Info("failed to load config, skipping")
|
|
|
|
}
|
|
|
|
}
|
2023-08-26 17:40:48 +00:00
|
|
|
err = c.fromEnv()
|
2022-07-26 09:48:57 +00:00
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Info("failed to load env vars")
|
|
|
|
}
|
2022-07-26 09:33:35 +00:00
|
|
|
c.configureLogger()
|
|
|
|
}
|
|
|
|
|
2023-08-26 17:40:48 +00:00
|
|
|
func (c *Config) LoadConfig(raw []byte) error {
|
|
|
|
err := yaml.Unmarshal(raw, c)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse YAML: %w", err)
|
|
|
|
}
|
|
|
|
c.walkScheme(c)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) LoadConfigFromFile(path string) error {
|
2022-08-08 19:00:45 +00:00
|
|
|
raw, err := os.ReadFile(path)
|
2021-05-04 12:07:12 +00:00
|
|
|
if err != nil {
|
2022-12-09 13:34:20 +00:00
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
|
|
return nil
|
|
|
|
}
|
2022-11-15 15:05:29 +00:00
|
|
|
return fmt.Errorf("failed to load config file: %w", err)
|
2021-05-02 22:49:16 +00:00
|
|
|
}
|
2023-08-26 17:40:48 +00:00
|
|
|
err = c.LoadConfig(raw)
|
2021-05-04 12:07:12 +00:00
|
|
|
if err != nil {
|
2023-08-26 17:40:48 +00:00
|
|
|
return err
|
2021-05-04 12:07:12 +00:00
|
|
|
}
|
|
|
|
log.WithField("path", path).Debug("Loaded config")
|
|
|
|
return nil
|
2021-05-02 22:49:16 +00:00
|
|
|
}
|
|
|
|
|
2022-07-26 09:33:35 +00:00
|
|
|
func (c *Config) fromEnv() error {
|
2022-08-05 22:24:49 +00:00
|
|
|
_, err := env.UnmarshalFromEnviron(c)
|
2021-07-29 09:30:30 +00:00
|
|
|
if err != nil {
|
2022-07-05 20:26:33 +00:00
|
|
|
return fmt.Errorf("failed to load environment variables: %w", err)
|
2021-07-29 09:30:30 +00:00
|
|
|
}
|
2022-07-26 09:33:35 +00:00
|
|
|
c.walkScheme(c)
|
2021-07-29 09:30:30 +00:00
|
|
|
log.Debug("Loaded config from environment")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-26 09:33:35 +00:00
|
|
|
func (c *Config) walkScheme(v interface{}) {
|
|
|
|
rv := reflect.ValueOf(v)
|
|
|
|
if rv.Kind() != reflect.Ptr || rv.IsNil() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = rv.Elem()
|
|
|
|
if rv.Kind() != reflect.Struct {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t := rv.Type()
|
|
|
|
for i := 0; i < rv.NumField(); i++ {
|
|
|
|
valueField := rv.Field(i)
|
|
|
|
switch valueField.Kind() {
|
|
|
|
case reflect.Struct:
|
|
|
|
if !valueField.Addr().CanInterface() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
iface := valueField.Addr().Interface()
|
|
|
|
c.walkScheme(iface)
|
|
|
|
}
|
|
|
|
|
|
|
|
typeField := t.Field(i)
|
|
|
|
if typeField.Type.Kind() != reflect.String {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
valueField.SetString(c.parseScheme(valueField.String()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) parseScheme(rawVal string) string {
|
|
|
|
u, err := url.Parse(rawVal)
|
|
|
|
if err != nil {
|
|
|
|
return rawVal
|
|
|
|
}
|
|
|
|
if u.Scheme == "env" {
|
|
|
|
e, ok := os.LookupEnv(u.Host)
|
|
|
|
if ok {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
return u.RawQuery
|
|
|
|
} else if u.Scheme == "file" {
|
2022-08-08 19:00:45 +00:00
|
|
|
d, err := os.ReadFile(u.Path)
|
2022-07-26 09:33:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return u.RawQuery
|
|
|
|
}
|
2023-01-09 14:29:22 +00:00
|
|
|
return strings.TrimSpace(string(d))
|
2022-07-26 09:33:35 +00:00
|
|
|
}
|
|
|
|
return rawVal
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) configureLogger() {
|
|
|
|
switch strings.ToLower(c.LogLevel) {
|
2021-05-02 22:49:16 +00:00
|
|
|
case "trace":
|
|
|
|
log.SetLevel(log.TraceLevel)
|
|
|
|
case "debug":
|
|
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
case "info":
|
|
|
|
log.SetLevel(log.InfoLevel)
|
|
|
|
case "warning":
|
|
|
|
log.SetLevel(log.WarnLevel)
|
|
|
|
case "error":
|
|
|
|
log.SetLevel(log.ErrorLevel)
|
|
|
|
default:
|
|
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
}
|
|
|
|
|
2021-12-20 20:23:19 +00:00
|
|
|
fm := log.FieldMap{
|
|
|
|
log.FieldKeyMsg: "event",
|
|
|
|
log.FieldKeyTime: "timestamp",
|
|
|
|
}
|
|
|
|
|
2022-07-26 09:33:35 +00:00
|
|
|
if c.Debug {
|
2021-12-20 20:23:19 +00:00
|
|
|
log.SetFormatter(&log.TextFormatter{FieldMap: fm})
|
2021-05-04 12:07:12 +00:00
|
|
|
} else {
|
2022-04-04 20:57:55 +00:00
|
|
|
log.SetFormatter(&log.JSONFormatter{FieldMap: fm, DisableHTMLEscape: true})
|
2021-05-02 22:49:16 +00:00
|
|
|
}
|
|
|
|
}
|