From a99f61f04dcad319cfd68af8329fbfa79613076c Mon Sep 17 00:00:00 2001 From: Karim Radhouani Date: Mon, 4 Nov 2024 23:14:08 -0800 Subject: [PATCH] properly expand env vars in config items defined as lists --- pkg/config/actions.go | 9 +++---- pkg/config/environment.go | 50 ++++++++++++++++++++++++++++++++------- pkg/config/inputs.go | 2 +- pkg/config/loader.go | 14 +++++++++-- pkg/config/locker.go | 2 +- pkg/config/outputs.go | 2 +- pkg/config/processors.go | 2 +- 7 files changed, 63 insertions(+), 18 deletions(-) diff --git a/pkg/config/actions.go b/pkg/config/actions.go index 6110eb23..42a20cce 100644 --- a/pkg/config/actions.go +++ b/pkg/config/actions.go @@ -40,10 +40,11 @@ func (c *Config) GetActions() (map[string]map[string]interface{}, error) { } for n := range c.Actions { expandMapEnv(c.Actions[n], - "target", "paths", "values", // gnmi action templates - "url", "body", // http action templates - "template", // template action templates - ) + expandExcept( + "target", "paths", "values", // gnmi action templates + "url", "body", // http action templates + "template", // template action templates + )) } if c.Debug { c.logger.Printf("actions: %+v", c.Actions) diff --git a/pkg/config/environment.go b/pkg/config/environment.go index 4b95e40c..351fb54e 100644 --- a/pkg/config/environment.go +++ b/pkg/config/environment.go @@ -78,20 +78,54 @@ func (c *Config) SetGlobalsFromEnv(cmd *cobra.Command) { }) } -func expandMapEnv(m map[string]interface{}, except ...string) { -OUTER: +func expandMapEnv(m map[string]interface{}, fn func(string, string) string) { for f := range m { switch v := m[f].(type) { case string: - for _, e := range except { - if f == e { - continue OUTER + m[f] = fn(f, v) + case map[string]interface{}: + expandMapEnv(v, fn) + m[f] = v + case []any: + for i, item := range v { + switch item := item.(type) { + case string: + v[i] = os.ExpandEnv(item) + case map[string]interface{}: + expandMapEnv(item, fn) + case []any: + expandSliceEnv(item, fn) } } - m[f] = os.ExpandEnv(v) - case map[string]interface{}: - expandMapEnv(v, except...) m[f] = v } } } + +func expandSliceEnv(s []any, fn func(string, string) string) { + for i, item := range s { + switch item := item.(type) { + case string: + s[i] = os.ExpandEnv(item) + case map[string]interface{}: + expandMapEnv(item, fn) + case []any: + expandSliceEnv(item, fn) + } + } +} + +func expandExcept(except ...string) func(string, string) string { + return func(k, v string) string { + for _, e := range except { + if k == e { + return v + } + } + return os.ExpandEnv(v) + } +} + +func expandAll() func(string, string) string { + return expandExcept() +} diff --git a/pkg/config/inputs.go b/pkg/config/inputs.go index 16580e7b..64ca389f 100644 --- a/pkg/config/inputs.go +++ b/pkg/config/inputs.go @@ -51,7 +51,7 @@ func (c *Config) GetInputs() (map[string]map[string]interface{}, error) { return nil, fmt.Errorf("there was %d error(s) when getting inputs configuration", len(errs)) } for n := range c.Inputs { - expandMapEnv(c.Inputs[n]) + expandMapEnv(c.Inputs[n], expandAll()) } if c.Debug { c.logger.Printf("inputs: %+v", c.Inputs) diff --git a/pkg/config/loader.go b/pkg/config/loader.go index 9b936bef..ccb038c0 100644 --- a/pkg/config/loader.go +++ b/pkg/config/loader.go @@ -11,6 +11,8 @@ package config import ( "errors" "fmt" + "os" + "strings" "github.com/openconfig/gnmic/pkg/loaders" _ "github.com/openconfig/gnmic/pkg/loaders/all" @@ -39,12 +41,20 @@ func (c *Config) GetLoader() error { if lds, ok := c.Loader["type"].(string); ok { for _, lt := range loaders.LoadersTypes { if lt == lds { - expandMapEnv(c.Loader) + expandMapEnv(c.Loader, func(k, v string) string { + if k == "password" { + if strings.HasPrefix(v, "${") && strings.HasSuffix(v, "}") { + return os.ExpandEnv(v) + } + return v + } + return os.ExpandEnv(v) + }) + fmt.Printf("LOADER: %+v\n", c.Loader) return nil } } return fmt.Errorf("unknown loader type %q", lds) } return fmt.Errorf("field 'type' not a string, found a %T", c.Loader["type"]) - } diff --git a/pkg/config/locker.go b/pkg/config/locker.go index eba5ec80..a0dfc639 100644 --- a/pkg/config/locker.go +++ b/pkg/config/locker.go @@ -32,7 +32,7 @@ func (c *Config) getLocker() error { default: return errors.New("wrong locker type format") } - expandMapEnv(c.Clustering.Locker) + expandMapEnv(c.Clustering.Locker, expandAll()) return nil } return errors.New("missing locker type") diff --git a/pkg/config/outputs.go b/pkg/config/outputs.go index e7cc947e..ec95743d 100644 --- a/pkg/config/outputs.go +++ b/pkg/config/outputs.go @@ -53,7 +53,7 @@ func (c *Config) GetOutputs() (map[string]map[string]interface{}, error) { } } for n := range c.Outputs { - expandMapEnv(c.Outputs[n], "msg-template", "target-template") + expandMapEnv(c.Outputs[n], expandExcept("msg-template", "target-template")) } namedOutputs := c.FileConfig.GetStringSlice("subscribe-output") if len(namedOutputs) == 0 { diff --git a/pkg/config/processors.go b/pkg/config/processors.go index 0c5e6e14..085ff6ec 100644 --- a/pkg/config/processors.go +++ b/pkg/config/processors.go @@ -39,7 +39,7 @@ func (c *Config) GetEventProcessors() (map[string]map[string]interface{}, error) c.Processors[n] = es } for n := range c.Processors { - expandMapEnv(c.Processors[n], "expression", "condition") + expandMapEnv(c.Processors[n], expandExcept("expression", "condition")) } if c.Debug { c.logger.Printf("processors: %+v", c.Processors)