2021-08-03 09:55:21 +00:00
|
|
|
package configuration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-12-22 06:34:20 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2021-08-03 09:55:21 +00:00
|
|
|
|
|
|
|
"github.com/knadh/koanf/parsers/yaml"
|
2022-06-01 23:18:45 +00:00
|
|
|
"github.com/knadh/koanf/providers/confmap"
|
2021-08-03 09:55:21 +00:00
|
|
|
"github.com/knadh/koanf/providers/env"
|
2021-11-23 09:45:38 +00:00
|
|
|
"github.com/knadh/koanf/providers/posflag"
|
2023-02-19 00:49:08 +00:00
|
|
|
"github.com/knadh/koanf/v2"
|
2021-11-23 09:45:38 +00:00
|
|
|
"github.com/spf13/pflag"
|
2021-08-03 09:55:21 +00:00
|
|
|
|
2021-08-11 01:04:35 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
2021-08-03 09:55:21 +00:00
|
|
|
)
|
|
|
|
|
2022-12-22 06:34:20 +00:00
|
|
|
// NewFileSource returns a configuration.Source configured to load from a specified path. If there is an issue
|
2022-12-21 09:48:14 +00:00
|
|
|
// accessing this path it also returns an error.
|
2022-12-22 06:34:20 +00:00
|
|
|
func NewFileSource(path string) (source *FileSource) {
|
|
|
|
return &FileSource{
|
2021-08-03 09:55:21 +00:00
|
|
|
koanf: koanf.New(constDelimiter),
|
|
|
|
path: path,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 06:34:20 +00:00
|
|
|
// NewFilteredFileSource returns a configuration.Source configured to load from a specified path. If there is
|
2022-12-21 09:48:14 +00:00
|
|
|
// an issue accessing this path it also returns an error.
|
2022-12-22 06:34:20 +00:00
|
|
|
func NewFilteredFileSource(path string, filters ...FileFilter) (source *FileSource) {
|
|
|
|
return &FileSource{
|
2022-12-21 09:48:14 +00:00
|
|
|
koanf: koanf.New(constDelimiter),
|
|
|
|
path: path,
|
|
|
|
filters: filters,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 06:34:20 +00:00
|
|
|
// NewFileSources returns a slice of configuration.Source configured to load from specified files.
|
|
|
|
func NewFileSources(paths []string) (sources []*FileSource) {
|
2021-08-03 09:55:21 +00:00
|
|
|
for _, path := range paths {
|
2022-12-22 06:34:20 +00:00
|
|
|
source := NewFileSource(path)
|
2021-08-03 09:55:21 +00:00
|
|
|
|
|
|
|
sources = append(sources, source)
|
|
|
|
}
|
|
|
|
|
|
|
|
return sources
|
|
|
|
}
|
|
|
|
|
2022-12-22 06:34:20 +00:00
|
|
|
// NewFilteredFileSources returns a slice of configuration.Source configured to load from specified files.
|
|
|
|
func NewFilteredFileSources(paths []string, filters []FileFilter) (sources []*FileSource) {
|
2022-12-21 09:48:14 +00:00
|
|
|
for _, path := range paths {
|
2022-12-22 06:34:20 +00:00
|
|
|
source := NewFilteredFileSource(path, filters...)
|
2022-12-21 09:48:14 +00:00
|
|
|
|
|
|
|
sources = append(sources, source)
|
|
|
|
}
|
|
|
|
|
|
|
|
return sources
|
|
|
|
}
|
|
|
|
|
2021-08-03 09:55:21 +00:00
|
|
|
// Name of the Source.
|
2022-12-22 06:34:20 +00:00
|
|
|
func (s *FileSource) Name() (name string) {
|
|
|
|
return fmt.Sprintf("file path(%s)", s.path)
|
2021-08-03 09:55:21 +00:00
|
|
|
}
|
|
|
|
|
2022-12-22 06:34:20 +00:00
|
|
|
// Merge the FileSource koanf.Koanf into the provided one.
|
|
|
|
func (s *FileSource) Merge(ko *koanf.Koanf, _ *schema.StructValidator) (err error) {
|
2021-08-03 09:55:21 +00:00
|
|
|
return ko.Merge(s.koanf)
|
|
|
|
}
|
|
|
|
|
2022-12-22 06:34:20 +00:00
|
|
|
// Load the Source into the FileSource koanf.Koanf.
|
|
|
|
func (s *FileSource) Load(val *schema.StructValidator) (err error) {
|
2021-08-03 09:55:21 +00:00
|
|
|
if s.path == "" {
|
2022-12-22 06:34:20 +00:00
|
|
|
return errors.New("invalid file path source configuration")
|
|
|
|
}
|
|
|
|
|
|
|
|
var info os.FileInfo
|
|
|
|
|
|
|
|
if info, err = os.Stat(s.path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.IsDir() {
|
|
|
|
return s.loadDir(val)
|
2021-08-03 09:55:21 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 09:48:14 +00:00
|
|
|
return s.koanf.Load(FilteredFileProvider(s.path, s.filters...), yaml.Parser())
|
2021-08-03 09:55:21 +00:00
|
|
|
}
|
|
|
|
|
2022-12-22 06:34:20 +00:00
|
|
|
func (s *FileSource) loadDir(_ *schema.StructValidator) (err error) {
|
|
|
|
var entries []os.DirEntry
|
|
|
|
|
|
|
|
if entries, err = os.ReadDir(s.path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, entry := range entries {
|
|
|
|
if entry.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
name := entry.Name()
|
|
|
|
|
|
|
|
switch ext := filepath.Ext(name); ext {
|
|
|
|
case ".yml", ".yaml":
|
|
|
|
if err = s.koanf.Load(FilteredFileProvider(filepath.Join(s.path, name), s.filters...), yaml.Parser()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-03 09:55:21 +00:00
|
|
|
// NewEnvironmentSource returns a Source configured to load from environment variables.
|
|
|
|
func NewEnvironmentSource(prefix, delimiter string) (source *EnvironmentSource) {
|
|
|
|
return &EnvironmentSource{
|
|
|
|
koanf: koanf.New(constDelimiter),
|
|
|
|
prefix: prefix,
|
|
|
|
delimiter: delimiter,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name of the Source.
|
2022-09-16 04:21:05 +00:00
|
|
|
func (s *EnvironmentSource) Name() (name string) {
|
2021-08-03 09:55:21 +00:00
|
|
|
return "environment"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge the EnvironmentSource koanf.Koanf into the provided one.
|
|
|
|
func (s *EnvironmentSource) Merge(ko *koanf.Koanf, _ *schema.StructValidator) (err error) {
|
|
|
|
return ko.Merge(s.koanf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the Source into the EnvironmentSource koanf.Koanf.
|
|
|
|
func (s *EnvironmentSource) Load(_ *schema.StructValidator) (err error) {
|
2022-04-16 09:00:39 +00:00
|
|
|
keyMap, ignoredKeys := getEnvConfigMap(schema.Keys, s.prefix, s.delimiter)
|
2021-08-03 09:55:21 +00:00
|
|
|
|
|
|
|
return s.koanf.Load(env.ProviderWithValue(s.prefix, constDelimiter, koanfEnvironmentCallback(keyMap, ignoredKeys, s.prefix, s.delimiter)), nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSecretsSource returns a Source configured to load from secrets.
|
|
|
|
func NewSecretsSource(prefix, delimiter string) (source *SecretsSource) {
|
|
|
|
return &SecretsSource{
|
|
|
|
koanf: koanf.New(constDelimiter),
|
|
|
|
prefix: prefix,
|
|
|
|
delimiter: delimiter,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name of the Source.
|
2022-09-16 04:21:05 +00:00
|
|
|
func (s *SecretsSource) Name() (name string) {
|
2021-08-03 09:55:21 +00:00
|
|
|
return "secrets"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge the SecretsSource koanf.Koanf into the provided one.
|
|
|
|
func (s *SecretsSource) Merge(ko *koanf.Koanf, val *schema.StructValidator) (err error) {
|
|
|
|
for _, key := range s.koanf.Keys() {
|
|
|
|
value, ok := ko.Get(key).(string)
|
|
|
|
|
|
|
|
if ok && value != "" {
|
|
|
|
val.Push(fmt.Errorf(errFmtSecretAlreadyDefined, key))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ko.Merge(s.koanf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the Source into the SecretsSource koanf.Koanf.
|
|
|
|
func (s *SecretsSource) Load(val *schema.StructValidator) (err error) {
|
2022-04-16 09:00:39 +00:00
|
|
|
keyMap := getSecretConfigMap(schema.Keys, s.prefix, s.delimiter)
|
2021-08-03 09:55:21 +00:00
|
|
|
|
|
|
|
return s.koanf.Load(env.ProviderWithValue(s.prefix, constDelimiter, koanfEnvironmentSecretsCallback(keyMap, val)), nil)
|
|
|
|
}
|
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
// NewCommandLineSourceWithMapping creates a new command line configuration source with a map[string]string which converts
|
|
|
|
// flag names into other config key names. If includeValidKeys is true we also allow any flag with a name which matches
|
|
|
|
// the list of valid keys into the koanf.Koanf, otherwise everything not in the map is skipped. Unchanged flags are also
|
|
|
|
// skipped unless includeUnchangedKeys is set to true.
|
|
|
|
func NewCommandLineSourceWithMapping(flags *pflag.FlagSet, mapping map[string]string, includeValidKeys, includeUnchangedKeys bool) (source *CommandLineSource) {
|
|
|
|
return &CommandLineSource{
|
|
|
|
koanf: koanf.New(constDelimiter),
|
|
|
|
flags: flags,
|
|
|
|
callback: koanfCommandLineWithMappingCallback(mapping, includeValidKeys, includeUnchangedKeys),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name of the Source.
|
2022-09-16 04:21:05 +00:00
|
|
|
func (s *CommandLineSource) Name() (name string) {
|
2021-11-23 09:45:38 +00:00
|
|
|
return "command-line"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge the CommandLineSource koanf.Koanf into the provided one.
|
|
|
|
func (s *CommandLineSource) Merge(ko *koanf.Koanf, val *schema.StructValidator) (err error) {
|
|
|
|
return ko.Merge(s.koanf)
|
|
|
|
}
|
|
|
|
|
2022-12-22 06:34:20 +00:00
|
|
|
// Load the Source into the FileSource koanf.Koanf.
|
2021-11-23 09:45:38 +00:00
|
|
|
func (s *CommandLineSource) Load(_ *schema.StructValidator) (err error) {
|
|
|
|
if s.callback != nil {
|
|
|
|
return s.koanf.Load(posflag.ProviderWithFlag(s.flags, ".", s.koanf, s.callback), nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.koanf.Load(posflag.Provider(s.flags, ".", s.koanf), nil)
|
|
|
|
}
|
|
|
|
|
2022-10-05 05:05:23 +00:00
|
|
|
// NewMapSource returns a new map[string]any source.
|
|
|
|
func NewMapSource(m map[string]any) (source *MapSource) {
|
2022-06-01 23:18:45 +00:00
|
|
|
return &MapSource{
|
|
|
|
m: m,
|
|
|
|
koanf: koanf.New(constDelimiter),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name of the Source.
|
2022-09-16 04:21:05 +00:00
|
|
|
func (s *MapSource) Name() (name string) {
|
2022-06-01 23:18:45 +00:00
|
|
|
return "map"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge the CommandLineSource koanf.Koanf into the provided one.
|
|
|
|
func (s *MapSource) Merge(ko *koanf.Koanf, val *schema.StructValidator) (err error) {
|
|
|
|
return ko.Merge(s.koanf)
|
|
|
|
}
|
|
|
|
|
2022-12-22 06:34:20 +00:00
|
|
|
// Load the Source into the FileSource koanf.Koanf.
|
2022-06-01 23:18:45 +00:00
|
|
|
func (s *MapSource) Load(_ *schema.StructValidator) (err error) {
|
|
|
|
return s.koanf.Load(confmap.Provider(s.m, constDelimiter), nil)
|
|
|
|
}
|
|
|
|
|
2021-08-03 09:55:21 +00:00
|
|
|
// NewDefaultSources returns a slice of Source configured to load from specified YAML files.
|
2022-12-22 06:34:20 +00:00
|
|
|
func NewDefaultSources(paths []string, prefix, delimiter string, additionalSources ...Source) (sources []Source) {
|
|
|
|
fileSources := NewFileSources(paths)
|
2021-08-03 09:55:21 +00:00
|
|
|
for _, source := range fileSources {
|
|
|
|
sources = append(sources, source)
|
|
|
|
}
|
|
|
|
|
|
|
|
sources = append(sources, NewEnvironmentSource(prefix, delimiter))
|
|
|
|
sources = append(sources, NewSecretsSource(prefix, delimiter))
|
|
|
|
|
2022-06-01 23:18:45 +00:00
|
|
|
if len(additionalSources) != 0 {
|
|
|
|
sources = append(sources, additionalSources...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return sources
|
|
|
|
}
|
|
|
|
|
2022-12-21 09:48:14 +00:00
|
|
|
// NewDefaultSourcesFiltered returns a slice of Source configured to load from specified YAML files.
|
2022-12-22 06:34:20 +00:00
|
|
|
func NewDefaultSourcesFiltered(paths []string, filters []FileFilter, prefix, delimiter string, additionalSources ...Source) (sources []Source) {
|
|
|
|
fileSources := NewFilteredFileSources(paths, filters)
|
2022-12-21 09:48:14 +00:00
|
|
|
for _, source := range fileSources {
|
|
|
|
sources = append(sources, source)
|
|
|
|
}
|
|
|
|
|
|
|
|
sources = append(sources, NewEnvironmentSource(prefix, delimiter))
|
|
|
|
sources = append(sources, NewSecretsSource(prefix, delimiter))
|
|
|
|
|
|
|
|
if len(additionalSources) != 0 {
|
|
|
|
sources = append(sources, additionalSources...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return sources
|
|
|
|
}
|
|
|
|
|
2022-06-01 23:18:45 +00:00
|
|
|
// NewDefaultSourcesWithDefaults returns a slice of Source configured to load from specified YAML files with additional sources.
|
2022-12-22 06:34:20 +00:00
|
|
|
func NewDefaultSourcesWithDefaults(paths []string, filters []FileFilter, prefix, delimiter string, defaults Source, additionalSources ...Source) (sources []Source) {
|
2022-12-22 00:21:29 +00:00
|
|
|
if defaults != nil {
|
|
|
|
sources = []Source{defaults}
|
|
|
|
}
|
2022-06-01 23:18:45 +00:00
|
|
|
|
2022-12-21 09:48:14 +00:00
|
|
|
if len(filters) == 0 {
|
2022-12-22 06:34:20 +00:00
|
|
|
sources = append(sources, NewDefaultSources(paths, prefix, delimiter, additionalSources...)...)
|
2022-12-21 09:48:14 +00:00
|
|
|
} else {
|
2022-12-22 06:34:20 +00:00
|
|
|
sources = append(sources, NewDefaultSourcesFiltered(paths, filters, prefix, delimiter, additionalSources...)...)
|
2022-12-21 09:48:14 +00:00
|
|
|
}
|
2022-06-01 23:18:45 +00:00
|
|
|
|
2021-08-03 09:55:21 +00:00
|
|
|
return sources
|
|
|
|
}
|