Skip to content

Commit 3eac0d4

Browse files
authored
Merge pull request #547 from nokia/fix545
properly expand env vars in config items defined as lists
2 parents fdfa12a + a99f61f commit 3eac0d4

File tree

7 files changed

+63
-18
lines changed

7 files changed

+63
-18
lines changed

pkg/config/actions.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@ func (c *Config) GetActions() (map[string]map[string]interface{}, error) {
4040
}
4141
for n := range c.Actions {
4242
expandMapEnv(c.Actions[n],
43-
"target", "paths", "values", // gnmi action templates
44-
"url", "body", // http action templates
45-
"template", // template action templates
46-
)
43+
expandExcept(
44+
"target", "paths", "values", // gnmi action templates
45+
"url", "body", // http action templates
46+
"template", // template action templates
47+
))
4748
}
4849
if c.Debug {
4950
c.logger.Printf("actions: %+v", c.Actions)

pkg/config/environment.go

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,20 +78,54 @@ func (c *Config) SetGlobalsFromEnv(cmd *cobra.Command) {
7878
})
7979
}
8080

81-
func expandMapEnv(m map[string]interface{}, except ...string) {
82-
OUTER:
81+
func expandMapEnv(m map[string]interface{}, fn func(string, string) string) {
8382
for f := range m {
8483
switch v := m[f].(type) {
8584
case string:
86-
for _, e := range except {
87-
if f == e {
88-
continue OUTER
85+
m[f] = fn(f, v)
86+
case map[string]interface{}:
87+
expandMapEnv(v, fn)
88+
m[f] = v
89+
case []any:
90+
for i, item := range v {
91+
switch item := item.(type) {
92+
case string:
93+
v[i] = os.ExpandEnv(item)
94+
case map[string]interface{}:
95+
expandMapEnv(item, fn)
96+
case []any:
97+
expandSliceEnv(item, fn)
8998
}
9099
}
91-
m[f] = os.ExpandEnv(v)
92-
case map[string]interface{}:
93-
expandMapEnv(v, except...)
94100
m[f] = v
95101
}
96102
}
97103
}
104+
105+
func expandSliceEnv(s []any, fn func(string, string) string) {
106+
for i, item := range s {
107+
switch item := item.(type) {
108+
case string:
109+
s[i] = os.ExpandEnv(item)
110+
case map[string]interface{}:
111+
expandMapEnv(item, fn)
112+
case []any:
113+
expandSliceEnv(item, fn)
114+
}
115+
}
116+
}
117+
118+
func expandExcept(except ...string) func(string, string) string {
119+
return func(k, v string) string {
120+
for _, e := range except {
121+
if k == e {
122+
return v
123+
}
124+
}
125+
return os.ExpandEnv(v)
126+
}
127+
}
128+
129+
func expandAll() func(string, string) string {
130+
return expandExcept()
131+
}

pkg/config/inputs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (c *Config) GetInputs() (map[string]map[string]interface{}, error) {
5151
return nil, fmt.Errorf("there was %d error(s) when getting inputs configuration", len(errs))
5252
}
5353
for n := range c.Inputs {
54-
expandMapEnv(c.Inputs[n])
54+
expandMapEnv(c.Inputs[n], expandAll())
5555
}
5656
if c.Debug {
5757
c.logger.Printf("inputs: %+v", c.Inputs)

pkg/config/loader.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ package config
1111
import (
1212
"errors"
1313
"fmt"
14+
"os"
15+
"strings"
1416

1517
"github.com/openconfig/gnmic/pkg/loaders"
1618
_ "github.com/openconfig/gnmic/pkg/loaders/all"
@@ -39,12 +41,20 @@ func (c *Config) GetLoader() error {
3941
if lds, ok := c.Loader["type"].(string); ok {
4042
for _, lt := range loaders.LoadersTypes {
4143
if lt == lds {
42-
expandMapEnv(c.Loader)
44+
expandMapEnv(c.Loader, func(k, v string) string {
45+
if k == "password" {
46+
if strings.HasPrefix(v, "${") && strings.HasSuffix(v, "}") {
47+
return os.ExpandEnv(v)
48+
}
49+
return v
50+
}
51+
return os.ExpandEnv(v)
52+
})
53+
fmt.Printf("LOADER: %+v\n", c.Loader)
4354
return nil
4455
}
4556
}
4657
return fmt.Errorf("unknown loader type %q", lds)
4758
}
4859
return fmt.Errorf("field 'type' not a string, found a %T", c.Loader["type"])
49-
5060
}

pkg/config/locker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (c *Config) getLocker() error {
3232
default:
3333
return errors.New("wrong locker type format")
3434
}
35-
expandMapEnv(c.Clustering.Locker)
35+
expandMapEnv(c.Clustering.Locker, expandAll())
3636
return nil
3737
}
3838
return errors.New("missing locker type")

pkg/config/outputs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (c *Config) GetOutputs() (map[string]map[string]interface{}, error) {
5353
}
5454
}
5555
for n := range c.Outputs {
56-
expandMapEnv(c.Outputs[n], "msg-template", "target-template")
56+
expandMapEnv(c.Outputs[n], expandExcept("msg-template", "target-template"))
5757
}
5858
namedOutputs := c.FileConfig.GetStringSlice("subscribe-output")
5959
if len(namedOutputs) == 0 {

pkg/config/processors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (c *Config) GetEventProcessors() (map[string]map[string]interface{}, error)
3939
c.Processors[n] = es
4040
}
4141
for n := range c.Processors {
42-
expandMapEnv(c.Processors[n], "expression", "condition")
42+
expandMapEnv(c.Processors[n], expandExcept("expression", "condition"))
4343
}
4444
if c.Debug {
4545
c.logger.Printf("processors: %+v", c.Processors)

0 commit comments

Comments
 (0)