From 04524c2b37e19d2d566e9536f7061dd5c2212832 Mon Sep 17 00:00:00 2001 From: tomasmota Date: Sat, 10 Feb 2024 13:05:22 +0100 Subject: [PATCH] [confmap] log a warning when using $VAR in config --- confmap/converter/expandconverter/expand.go | 38 ++++++++++++++++----- confmap/go.mod | 2 +- confmap/go.sum | 4 +-- 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/confmap/converter/expandconverter/expand.go b/confmap/converter/expandconverter/expand.go index 0296b4b21ed..252a82ac60d 100644 --- a/confmap/converter/expandconverter/expand.go +++ b/confmap/converter/expandconverter/expand.go @@ -5,42 +5,53 @@ package expandconverter // import "go.opentelemetry.io/collector/confmap/convert import ( "context" + "fmt" "os" + "regexp" "go.opentelemetry.io/collector/confmap" + "go.uber.org/zap" ) -type converter struct{} +type converter struct { + logger *zap.Logger + + // Record of which env vars we have logged a warning for + loggedDeprecations map[string]struct{} +} // New returns a confmap.Converter, that expands all environment variables for a given confmap.Conf. // // Notice: This API is experimental. func New(_ confmap.ConverterSettings) confmap.Converter { - return converter{} + return converter{ + loggedDeprecations: make(map[string]struct{}), + logger: zap.NewNop(), // TODO: pass logger in ConverterSettings + } } -func (converter) Convert(_ context.Context, conf *confmap.Conf) error { +func (c converter) Convert(_ context.Context, conf *confmap.Conf) error { out := make(map[string]any) for _, k := range conf.AllKeys() { - out[k] = expandStringValues(conf.Get(k)) + out[k] = c.expandStringValues(conf.Get(k)) } return conf.Merge(confmap.NewFromStringMap(out)) } -func expandStringValues(value any) any { +func (c converter) expandStringValues(value any) any { switch v := value.(type) { case string: - return expandEnv(v) + return c.expandEnv(v) case []any: nslice := make([]any, 0, len(v)) for _, vint := range v { - nslice = append(nslice, expandStringValues(vint)) + nslice = append(nslice, c.expandStringValues(vint)) } return nslice case map[string]any: nmap := map[string]any{} for mk, mv := range v { - nmap[mk] = expandStringValues(mv) + nmap[mk] = c.expandStringValues(mv) } return nmap default: @@ -48,8 +59,17 @@ func expandStringValues(value any) any { } } -func expandEnv(s string) string { +func (c converter) expandEnv(s string) string { return os.Expand(s, func(str string) string { + // Matches on $VAR style environment variables + // in order to make sure we don't log a warning for ${VAR} + var regex = regexp.MustCompile(fmt.Sprintf(`\$%s`, str)) + if _, exists := c.loggedDeprecations[str]; !exists && regex.MatchString(s) { + msg := fmt.Sprintf("Variable substitution using $VAR will be deprecated in favor of ${VAR} and ${env:VAR}, please update $%s", str) + c.logger.Warn(msg, zap.String("variable", str)) + c.loggedDeprecations[str] = struct{}{} + } + // This allows escaping environment variable substitution via $$, e.g. // - $FOO will be substituted with env var FOO // - $$FOO will be replaced with $FOO diff --git a/confmap/go.mod b/confmap/go.mod index aff5f830305..95f73a28d3a 100644 --- a/confmap/go.mod +++ b/confmap/go.mod @@ -10,13 +10,13 @@ require ( github.com/stretchr/testify v1.8.4 go.uber.org/goleak v1.3.0 go.uber.org/multierr v1.11.0 + go.uber.org/zap v1.26.0 gopkg.in/yaml.v3 v3.0.1 ) require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 // indirect - github.com/kr/text v0.2.0 // indirect github.com/mitchellh/copystructure v1.2.0 // indirect github.com/mitchellh/reflectwalk v1.0.2 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect diff --git a/confmap/go.sum b/confmap/go.sum index 657413b139a..88e3cbd411a 100644 --- a/confmap/go.sum +++ b/confmap/go.sum @@ -1,4 +1,3 @@ -github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 h1:TQcrn6Wq+sKGkpyPvppOz99zsMBaUOKXq6HSv655U1c= @@ -11,7 +10,6 @@ github.com/knadh/koanf/v2 v2.1.0 h1:eh4QmHHBuU8BybfIJ8mB8K8gsGCD/AUQTdwGq/GzId8= github.com/knadh/koanf/v2 v2.1.0/go.mod h1:4mnTRbZCK+ALuBXHZMjDfG9y714L7TykVnZkXbMU3Es= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= -github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw= github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s= github.com/mitchellh/mapstructure v1.5.1-0.20231216201459-8508981c8b6c h1:cqn374mizHuIWj+OSJCajGr/phAmuMug9qIX3l9CflE= @@ -26,6 +24,8 @@ go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= +go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo= +go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=