diff --git a/.chloggen/deprecate-expandconverter-2.yaml b/.chloggen/deprecate-expandconverter-2.yaml new file mode 100644 index 00000000000..4375a388747 --- /dev/null +++ b/.chloggen/deprecate-expandconverter-2.yaml @@ -0,0 +1,25 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: breaking + +# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) +component: otelcoltest + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: The `otelcol.LoadConfig` method no longer sets the `expandconverter`. + +# One or more tracking issues or pull requests related to the change +issues: [10510] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [api] diff --git a/.chloggen/deprecate-expandconverter-3.yaml b/.chloggen/deprecate-expandconverter-3.yaml new file mode 100644 index 00000000000..ee8001aa4f3 --- /dev/null +++ b/.chloggen/deprecate-expandconverter-3.yaml @@ -0,0 +1,25 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: breaking + +# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) +component: ocb + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Collectors built with OCB will no longer include the `expandconverter` + +# One or more tracking issues or pull requests related to the change +issues: [10510] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [api] diff --git a/.chloggen/deprecate-expandconverter.yaml b/.chloggen/deprecate-expandconverter.yaml new file mode 100644 index 00000000000..770ce3e0164 --- /dev/null +++ b/.chloggen/deprecate-expandconverter.yaml @@ -0,0 +1,25 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: deprecation + +# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) +component: expandconverter + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Deprecate `expandconverter`. + +# One or more tracking issues or pull requests related to the change +issues: [10510] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [api] diff --git a/cmd/builder/README.md b/cmd/builder/README.md index ec8d18c3808..003a7e93f6b 100644 --- a/cmd/builder/README.md +++ b/cmd/builder/README.md @@ -24,8 +24,6 @@ processors: providers: - gomod: go.opentelemetry.io/collector/confmap/provider/fileprovider v0.99.0 -converters: - - gomod: go.opentelemetry.io/collector/confmap/converter/expandconverter v0.99.0 EOF $ builder --config=otelcol-builder.yaml $ cat > /tmp/otelcol.yaml < ${WORKSPACE_DIR}/config/configtls - go.opentelemetry.io/collector/config/internal => ${WORKSPACE_DIR}/config/internal - go.opentelemetry.io/collector/confmap => ${WORKSPACE_DIR}/confmap - - go.opentelemetry.io/collector/confmap/converter/expandconverter => ${WORKSPACE_DIR}/confmap/converter/expandconverter - go.opentelemetry.io/collector/confmap/provider/envprovider => ${WORKSPACE_DIR}/confmap/provider/envprovider - go.opentelemetry.io/collector/confmap/provider/fileprovider => ${WORKSPACE_DIR}/confmap/provider/fileprovider - go.opentelemetry.io/collector/confmap/provider/httpprovider => ${WORKSPACE_DIR}/confmap/provider/httpprovider diff --git a/cmd/otelcorecol/builder-config.yaml b/cmd/otelcorecol/builder-config.yaml index 3dfa4b40729..35ca62e4538 100644 --- a/cmd/otelcorecol/builder-config.yaml +++ b/cmd/otelcorecol/builder-config.yaml @@ -57,7 +57,6 @@ replaces: - go.opentelemetry.io/collector/config/configtls => ../../config/configtls - go.opentelemetry.io/collector/config/internal => ../../config/internal - go.opentelemetry.io/collector/confmap => ../../confmap - - go.opentelemetry.io/collector/confmap/converter/expandconverter => ../../confmap/converter/expandconverter - go.opentelemetry.io/collector/confmap/provider/envprovider => ../../confmap/provider/envprovider - go.opentelemetry.io/collector/confmap/provider/fileprovider => ../../confmap/provider/fileprovider - go.opentelemetry.io/collector/confmap/provider/httpprovider => ../../confmap/provider/httpprovider diff --git a/cmd/otelcorecol/go.mod b/cmd/otelcorecol/go.mod index 0305b2251b5..c3ef845882b 100644 --- a/cmd/otelcorecol/go.mod +++ b/cmd/otelcorecol/go.mod @@ -9,7 +9,6 @@ toolchain go1.21.12 require ( go.opentelemetry.io/collector/component v0.106.1 go.opentelemetry.io/collector/confmap v0.106.1 - go.opentelemetry.io/collector/confmap/converter/expandconverter v0.106.1 go.opentelemetry.io/collector/confmap/provider/envprovider v0.106.1 go.opentelemetry.io/collector/confmap/provider/fileprovider v0.106.1 go.opentelemetry.io/collector/confmap/provider/httpprovider v0.106.1 @@ -170,8 +169,6 @@ replace go.opentelemetry.io/collector/config/internal => ../../config/internal replace go.opentelemetry.io/collector/confmap => ../../confmap -replace go.opentelemetry.io/collector/confmap/converter/expandconverter => ../../confmap/converter/expandconverter - replace go.opentelemetry.io/collector/confmap/provider/envprovider => ../../confmap/provider/envprovider replace go.opentelemetry.io/collector/confmap/provider/fileprovider => ../../confmap/provider/fileprovider diff --git a/cmd/otelcorecol/main.go b/cmd/otelcorecol/main.go index 3d288ca5156..3ea4852ceef 100644 --- a/cmd/otelcorecol/main.go +++ b/cmd/otelcorecol/main.go @@ -8,7 +8,6 @@ import ( "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/confmap" - "go.opentelemetry.io/collector/confmap/converter/expandconverter" envprovider "go.opentelemetry.io/collector/confmap/provider/envprovider" fileprovider "go.opentelemetry.io/collector/confmap/provider/fileprovider" httpprovider "go.opentelemetry.io/collector/confmap/provider/httpprovider" @@ -36,9 +35,6 @@ func main() { httpsprovider.NewFactory(), yamlprovider.NewFactory(), }, - ConverterFactories: []confmap.ConverterFactory{ - expandconverter.NewFactory(), - }, }, }, } diff --git a/confmap/converter/expandconverter/expand.go b/confmap/converter/expandconverter/expand.go index 2c4af613499..83b8e4b9dd9 100644 --- a/confmap/converter/expandconverter/expand.go +++ b/confmap/converter/expandconverter/expand.go @@ -7,13 +7,11 @@ import ( "context" "fmt" "os" - "regexp" "go.uber.org/zap" "go.opentelemetry.io/collector/confmap" "go.opentelemetry.io/collector/confmap/internal/envvar" - "go.opentelemetry.io/collector/internal/globalgates" ) type converter struct { @@ -25,6 +23,9 @@ type converter struct { // NewFactory returns a factory for a confmap.Converter, // which expands all environment variables for a given confmap.Conf. +// +// Deprecated: [v0.107.0] BASH-style env var expansion is deprecated. Use the `envprovider` instead to expand `${FOO}` and `${env:FOO}`. +// Using the expandconverter with `confmap.Resolver` will cause double escaping, so `$$$$` -> `$` instead of `$$`. func NewFactory() confmap.ConverterFactory { return confmap.NewConverterFactory(newConverter) } @@ -36,6 +37,8 @@ func newConverter(set confmap.ConverterSettings) confmap.Converter { } } +// Deprecated: [v0.107.0] BASH-style env var expansion is deprecated. Use the `envprovider` instead to expand `${FOO}` and `${env:FOO}`. +// Using the expandconverter with `confmap.Resolver` will cause double escaping, so `$$$$` -> `$` instead of `$$`. func (c converter) Convert(_ context.Context, conf *confmap.Conf) error { var err error out := make(map[string]any) @@ -90,19 +93,6 @@ func (c converter) expandEnv(s string) (string, error) { return "$" } - // 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`, regexp.QuoteMeta(str))) - if _, exists := c.loggedDeprecations[str]; !exists && regex.MatchString(s) { - if globalgates.UseUnifiedEnvVarExpansionRules.IsEnabled() { - err = fmt.Errorf("variable substitution using $VAR has been deprecated in favor of ${VAR} and ${env:VAR} - please update $%s or temporarily disable the confmap.unifyEnvVarExpansion feature gate", str) - return "" - } - 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{}{} - } - // For $ENV style environment variables os.Expand returns once it hits a character that isn't an underscore or // an alphanumeric character - so we cannot detect those malformed environment variables. // For ${ENV} style variables we can detect those kinds of env var names! diff --git a/confmap/converter/expandconverter/expand_test.go b/confmap/converter/expandconverter/expand_test.go new file mode 100644 index 00000000000..4934bb5a008 --- /dev/null +++ b/confmap/converter/expandconverter/expand_test.go @@ -0,0 +1,290 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package expandconverter + +import ( + "context" + "fmt" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "go.uber.org/zap" + "go.uber.org/zap/zapcore" + "go.uber.org/zap/zaptest/observer" + + "go.opentelemetry.io/collector/confmap" + "go.opentelemetry.io/collector/confmap/confmaptest" + "go.opentelemetry.io/collector/confmap/internal/envvar" +) + +func TestNewExpandConverter(t *testing.T) { + var testCases = []struct { + name string // test case name (also file name containing config yaml) + }{ + {name: "expand-with-no-env.yaml"}, + {name: "expand-with-partial-env.yaml"}, + {name: "expand-with-all-env.yaml"}, + } + + const valueExtra = "some string" + const valueExtraMapValue = "some map value" + const valueExtraListMapValue = "some list map value" + const valueExtraListElement = "some list value" + t.Setenv("EXTRA", valueExtra) + t.Setenv("EXTRA_MAP_VALUE_1", valueExtraMapValue+"_1") + t.Setenv("EXTRA_MAP_VALUE_2", valueExtraMapValue+"_2") + t.Setenv("EXTRA_LIST_MAP_VALUE_1", valueExtraListMapValue+"_1") + t.Setenv("EXTRA_LIST_MAP_VALUE_2", valueExtraListMapValue+"_2") + t.Setenv("EXTRA_LIST_VALUE_1", valueExtraListElement+"_1") + t.Setenv("EXTRA_LIST_VALUE_2", valueExtraListElement+"_2") + + expectedCfgMap, errExpected := confmaptest.LoadConf(filepath.Join("testdata", "expand-with-no-env.yaml")) + require.NoError(t, errExpected, "Unable to get expected config") + + for _, test := range testCases { + t.Run(test.name, func(t *testing.T) { + conf, err := confmaptest.LoadConf(filepath.Join("testdata", test.name)) + require.NoError(t, err, "Unable to get config") + + // Test that expanded configs are the same with the simple config with no env vars. + require.NoError(t, createConverter().Convert(context.Background(), conf)) + assert.Equal(t, expectedCfgMap.ToStringMap(), conf.ToStringMap()) + }) + } +} + +func TestNewExpandConverter_EscapedMaps(t *testing.T) { + const receiverExtraMapValue = "some map value" + t.Setenv("MAP_VALUE", receiverExtraMapValue) + + conf := confmap.NewFromStringMap( + map[string]any{ + "test_string_map": map[string]any{ + "recv": "$MAP_VALUE", + }, + "test_interface_map": map[any]any{ + "recv": "$MAP_VALUE", + }}, + ) + require.NoError(t, createConverter().Convert(context.Background(), conf)) + + expectedMap := map[string]any{ + "test_string_map": map[string]any{ + "recv": receiverExtraMapValue, + }, + "test_interface_map": map[string]any{ + "recv": receiverExtraMapValue, + }} + assert.Equal(t, expectedMap, conf.ToStringMap()) +} + +func TestNewExpandConverter_EscapedEnvVars(t *testing.T) { + const receiverExtraMapValue = "some map value" + t.Setenv("MAP_VALUE_2", receiverExtraMapValue) + + // Retrieve the config + conf, err := confmaptest.LoadConf(filepath.Join("testdata", "expand-escaped-env.yaml")) + require.NoError(t, err, "Unable to get config") + + expectedMap := map[string]any{ + "test_map": map[string]any{ + // $$ -> escaped $ + "recv.1": "$MAP_VALUE_1", + // $$$ -> escaped $ + substituted env var + "recv.2": "$" + receiverExtraMapValue, + // $$$$ -> two escaped $ + "recv.3": "$$MAP_VALUE_3", + // escaped $ in the middle + "recv.4": "some${MAP_VALUE_4}text", + // $$$$ -> two escaped $ + "recv.5": "${ONE}${TWO}", + // trailing escaped $ + "recv.6": "text$", + // escaped $ alone + "recv.7": "$", + }} + require.NoError(t, createConverter().Convert(context.Background(), conf)) + assert.Equal(t, expectedMap, conf.ToStringMap()) +} + +func TestNewExpandConverterHostPort(t *testing.T) { + t.Setenv("HOST", "127.0.0.1") + t.Setenv("PORT", "4317") + + var testCases = []struct { + name string + input map[string]any + expected map[string]any + }{ + { + name: "brackets", + input: map[string]any{ + "test": "${HOST}:${PORT}", + }, + expected: map[string]any{ + "test": "127.0.0.1:4317", + }, + }, + { + name: "no brackets", + input: map[string]any{ + "test": "$HOST:$PORT", + }, + expected: map[string]any{ + "test": "127.0.0.1:4317", + }, + }, + { + name: "mix", + input: map[string]any{ + "test": "${HOST}:$PORT", + }, + expected: map[string]any{ + "test": "127.0.0.1:4317", + }, + }, + { + name: "reverse mix", + input: map[string]any{ + "test": "$HOST:${PORT}", + }, + expected: map[string]any{ + "test": "127.0.0.1:4317", + }, + }, + } + for _, tt := range testCases { + t.Run(tt.name, func(t *testing.T) { + conf := confmap.NewFromStringMap(tt.input) + require.NoError(t, createConverter().Convert(context.Background(), conf)) + assert.Equal(t, tt.expected, conf.ToStringMap()) + }) + } +} + +func NewTestConverter() (confmap.Converter, *observer.ObservedLogs) { + core, logs := observer.New(zapcore.InfoLevel) + conv := converter{loggedDeprecations: make(map[string]struct{}), logger: zap.New(core)} + return conv, logs +} + +func TestDeprecatedWarning(t *testing.T) { + msgTemplate := `Variable substitution using $VAR will be deprecated in favor of ${VAR} and ${env:VAR}, please update $%s` + t.Setenv("HOST", "127.0.0.1") + t.Setenv("PORT", "4317") + + t.Setenv("HOST_NAME", "127.0.0.2") + t.Setenv("HOSTNAME", "127.0.0.3") + + t.Setenv("BAD!HOST", "127.0.0.2") + + var testCases = []struct { + name string + input map[string]any + expectedOutput map[string]any + expectedWarnings []string + expectedError error + }{ + { + name: "no warning", + input: map[string]any{ + "test": "${HOST}:${PORT}", + }, + expectedOutput: map[string]any{ + "test": "127.0.0.1:4317", + }, + expectedWarnings: []string{}, + expectedError: nil, + }, + { + name: "malformed environment variable", + input: map[string]any{ + "test": "${BAD!HOST}", + }, + expectedOutput: map[string]any{ + "test": "blah", + }, + expectedWarnings: []string{}, + expectedError: fmt.Errorf("environment variable \"BAD!HOST\" has invalid name: must match regex %s", envvar.ValidationRegexp), + }, + { + name: "malformed environment variable number", + input: map[string]any{ + "test": "${2BADHOST}", + }, + expectedOutput: map[string]any{ + "test": "blah", + }, + expectedWarnings: []string{}, + expectedError: fmt.Errorf("environment variable \"2BADHOST\" has invalid name: must match regex %s", envvar.ValidationRegexp), + }, + { + name: "malformed environment variable unicode", + input: map[string]any{ + "test": "${😊BADHOST}", + }, + expectedOutput: map[string]any{ + "test": "blah", + }, + expectedWarnings: []string{}, + expectedError: fmt.Errorf("environment variable \"😊BADHOST\" has invalid name: must match regex %s", envvar.ValidationRegexp), + }, + } + + for _, tt := range testCases { + t.Run(tt.name, func(t *testing.T) { + conf := confmap.NewFromStringMap(tt.input) + conv, logs := NewTestConverter() + err := conv.Convert(context.Background(), conf) + assert.Equal(t, tt.expectedError, err) + if tt.expectedError == nil { + assert.Equal(t, tt.expectedOutput, conf.ToStringMap()) + } + assert.Equal(t, len(tt.expectedWarnings), len(logs.All())) + for i, variable := range tt.expectedWarnings { + errorMsg := fmt.Sprintf(msgTemplate, variable) + assert.Equal(t, errorMsg, logs.All()[i].Message) + } + }) + } +} + +func TestNewExpandConverterWithErrors(t *testing.T) { + var testCases = []struct { + name string // test case name (also file name containing config yaml) + expectedError error + }{ + { + name: "expand-list-error.yaml", + expectedError: fmt.Errorf("environment variable \"EXTRA_LIST_^VALUE_2\" has invalid name: must match regex %s", envvar.ValidationRegexp), + }, + { + name: "expand-list-map-error.yaml", + expectedError: fmt.Errorf("environment variable \"EXTRA_LIST_MAP_V#ALUE_2\" has invalid name: must match regex %s", envvar.ValidationRegexp), + }, + { + name: "expand-map-error.yaml", + expectedError: fmt.Errorf("environment variable \"EX#TRA\" has invalid name: must match regex %s", envvar.ValidationRegexp), + }, + } + + for _, test := range testCases { + t.Run(test.name, func(t *testing.T) { + conf, err := confmaptest.LoadConf(filepath.Join("testdata", "errors", test.name)) + require.NoError(t, err, "Unable to get config") + + // Test that expanded configs are the same with the simple config with no env vars. + err = createConverter().Convert(context.Background(), conf) + + assert.Equal(t, test.expectedError, err) + }) + } +} + +func createConverter() confmap.Converter { + // nolint + return NewFactory().Create(confmap.ConverterSettings{Logger: zap.NewNop()}) +} diff --git a/confmap/converter/expandconverter/go.mod b/confmap/converter/expandconverter/go.mod index a0b3216d4b4..60b4d680b5b 100644 --- a/confmap/converter/expandconverter/go.mod +++ b/confmap/converter/expandconverter/go.mod @@ -1,15 +1,18 @@ +// Deprecated: [v0.107.0] BASH-style env var expansion is deprecated. Use the `envprovider` instead to expand `${FOO}` and `${env:FOO}`. +// Using the expandconverter with `confmap.Resolver` will cause double escaping, so `$$$$` -> `$` instead of `$$`. module go.opentelemetry.io/collector/confmap/converter/expandconverter go 1.21.0 require ( + github.com/stretchr/testify v1.9.0 go.opentelemetry.io/collector/confmap v0.106.1 - go.opentelemetry.io/collector/internal/globalgates v0.106.1 go.uber.org/goleak v1.3.0 go.uber.org/zap v1.27.0 ) require ( + github.com/davecgh/go-spew v1.1.1 // indirect github.com/go-viper/mapstructure/v2 v2.0.0 // indirect github.com/hashicorp/go-version v1.7.0 // indirect github.com/knadh/koanf/maps v0.1.1 // indirect @@ -17,7 +20,9 @@ require ( github.com/knadh/koanf/v2 v2.1.1 // 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 go.opentelemetry.io/collector/featuregate v1.12.0 // indirect + go.opentelemetry.io/collector/internal/globalgates v0.106.1 // indirect go.uber.org/multierr v1.11.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/otelcol/otelcoltest/config.go b/otelcol/otelcoltest/config.go index 299ee647c91..c47adc3e951 100644 --- a/otelcol/otelcoltest/config.go +++ b/otelcol/otelcoltest/config.go @@ -7,7 +7,6 @@ import ( "context" "go.opentelemetry.io/collector/confmap" - "go.opentelemetry.io/collector/confmap/converter/expandconverter" "go.opentelemetry.io/collector/confmap/provider/envprovider" "go.opentelemetry.io/collector/confmap/provider/fileprovider" "go.opentelemetry.io/collector/confmap/provider/httpprovider" @@ -26,7 +25,6 @@ func LoadConfig(fileName string, factories otelcol.Factories) (*otelcol.Config, yamlprovider.NewFactory(), httpprovider.NewFactory(), }, - ConverterFactories: []confmap.ConverterFactory{expandconverter.NewFactory()}, }, }) if err != nil { diff --git a/otelcol/otelcoltest/go.mod b/otelcol/otelcoltest/go.mod index c32e9f8cbf3..ce8515383c2 100644 --- a/otelcol/otelcoltest/go.mod +++ b/otelcol/otelcoltest/go.mod @@ -6,7 +6,6 @@ require ( github.com/stretchr/testify v1.9.0 go.opentelemetry.io/collector/component v0.106.1 go.opentelemetry.io/collector/confmap v0.106.1 - go.opentelemetry.io/collector/confmap/converter/expandconverter v0.106.1 go.opentelemetry.io/collector/confmap/provider/envprovider v0.106.1 go.opentelemetry.io/collector/confmap/provider/fileprovider v0.106.1 go.opentelemetry.io/collector/confmap/provider/httpprovider v0.106.1 @@ -119,8 +118,6 @@ replace go.opentelemetry.io/collector/otelcol => ../ replace go.opentelemetry.io/collector/confmap/provider/yamlprovider => ../../confmap/provider/yamlprovider -replace go.opentelemetry.io/collector/confmap/converter/expandconverter => ../../confmap/converter/expandconverter - replace go.opentelemetry.io/collector => ../.. replace go.opentelemetry.io/collector/service => ../../service