From e0a7d0b36546fe41f71febeba2bd2e368303dad2 Mon Sep 17 00:00:00 2001 From: Marc 'risson' Schmitt Date: Sat, 29 Apr 2023 00:39:34 +0200 Subject: [PATCH] root: config: config discovery parity between go and python Signed-off-by: Marc 'risson' Schmitt --- internal/config/config.go | 40 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/internal/config/config.go b/internal/config/config.go index 0b50729f9..039904013 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -5,6 +5,7 @@ import ( "fmt" "net/url" "os" + "path/filepath" "reflect" "strings" @@ -15,10 +16,47 @@ import ( var cfg *Config +func getConfigPaths() []string { + configPaths := []string{"./authentik/lib/default.yml", "/etc/authentik/config.yml", ""} + 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 +} + func Get() *Config { if cfg == nil { c := defaultConfig() - c.Setup("./authentik/lib/default.yml", "/etc/authentik/config.yml", "./local.env.yml") + c.Setup(getConfigPaths()...) cfg = c } return cfg