Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

properly expand env vars in config items defined as lists #547

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions pkg/config/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
50 changes: 42 additions & 8 deletions pkg/config/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
2 changes: 1 addition & 1 deletion pkg/config/inputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 12 additions & 2 deletions pkg/config/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ package config
import (
"errors"
"fmt"
"os"
"strings"

"github.com/openconfig/gnmic/pkg/loaders"
_ "github.com/openconfig/gnmic/pkg/loaders/all"
Expand Down Expand Up @@ -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"])

}
2 changes: 1 addition & 1 deletion pkg/config/locker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/outputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/processors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down