-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[confmap] Fix expansion of escaped environment variables
This change fixes the issue where environment variables escaped with $$ were expanded. The collector now converts `$${ENV_VAR}` to `${ENV_VAR}` and `$$ENV_VAR` to `$ENV_VAR` without further expansion.
- Loading branch information
Showing
9 changed files
with
160 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: bug_fix | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) | ||
component: confmap | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Fix wrong expansion of environment variables escaped with `$$`, e.g. `$${ENV_VAR}` and `$$ENV_VAR`. | ||
|
||
# One or more tracking issues or pull requests related to the change | ||
issues: [10713] | ||
|
||
# (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: | | ||
This change fixes the issue where environment variables escaped with $$ were expanded. | ||
The collector now converts `$${ENV_VAR}` to `${ENV_VAR}` and `$$ENV_VAR` to `$ENV_VAR` without further expansion. | ||
# 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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package e2etest | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"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/yamlprovider" | ||
) | ||
|
||
// Test_EscapedEnvVars tests that the resolver supports escaped env vars working together with expand converter. | ||
func Test_EscapedEnvVars(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
scheme string | ||
}{ | ||
{ | ||
name: "no_default_scheme", | ||
scheme: "", | ||
}, | ||
{ | ||
name: "env", | ||
scheme: "env", | ||
}, | ||
} | ||
|
||
const expandedValue = "some expanded value" | ||
t.Setenv("ENV_VALUE", expandedValue) | ||
|
||
expectedFailures := map[string]string{ | ||
"$ENV_VALUE": "variable substitution using $VAR has been deprecated in favor of ${VAR} and ${env:VAR}", | ||
"$$$ENV_VALUE": "variable substitution using $VAR has been deprecated in favor of ${VAR} and ${env:VAR}", | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
expectedMap := map[string]any{ | ||
"test_map": map[string]any{ | ||
"key1": "$ENV_VALUE", | ||
"key2": "$$ENV_VALUE", | ||
"key3": "$" + expandedValue, | ||
"key4": "some" + expandedValue + "text", | ||
"key5": "some${ENV_VALUE}text", | ||
"key6": "${ONE}${TWO}", | ||
"key7": "text$", | ||
"key8": "$", | ||
"key9": "${1}${env:2}", | ||
"key10": "some${env:ENV_VALUE}text", | ||
"key11": "${env:" + expandedValue + "}", | ||
"key12": "${env:${ENV_VALUE}}", | ||
"key13": "env:MAP_VALUE_2}${ENV_VALUE}{", | ||
"key14": "$" + expandedValue, | ||
}, | ||
} | ||
|
||
resolver, err := confmap.NewResolver(confmap.ResolverSettings{ | ||
URIs: []string{filepath.Join("testdata", "expand-escaped-env.yaml")}, | ||
ProviderFactories: []confmap.ProviderFactory{fileprovider.NewFactory(), envprovider.NewFactory()}, | ||
ConverterFactories: []confmap.ConverterFactory{expandconverter.NewFactory()}, | ||
DefaultScheme: tt.scheme, | ||
}) | ||
require.NoError(t, err) | ||
|
||
// Test that expanded configs are the same with the simple config with no env vars. | ||
cfgMap, err := resolver.Resolve(context.Background()) | ||
require.NoError(t, err) | ||
m := cfgMap.ToStringMap() | ||
assert.Equal(t, expectedMap, m) | ||
|
||
for val, expectedErr := range expectedFailures { | ||
resolver, err = confmap.NewResolver(confmap.ResolverSettings{ | ||
URIs: []string{fmt.Sprintf("yaml: test: %s", val)}, | ||
ProviderFactories: []confmap.ProviderFactory{yamlprovider.NewFactory(), envprovider.NewFactory()}, | ||
ConverterFactories: []confmap.ConverterFactory{expandconverter.NewFactory()}, | ||
DefaultScheme: tt.scheme, | ||
}) | ||
require.NoError(t, err) | ||
_, err := resolver.Resolve(context.Background()) | ||
require.ErrorContains(t, err, expectedErr) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
test_map: | ||
# $$ -> escaped $ | ||
key1: "$$ENV_VALUE" | ||
# $$$$ -> two escaped $ | ||
key2: "$$$$ENV_VALUE" | ||
# $$ -> escaped $ + ${ENV_VALUE} expanded | ||
key3: "$$${ENV_VALUE}" | ||
# expanded in the middle | ||
key4: "some${ENV_VALUE}text" | ||
# escaped $ in the middle | ||
key5: "some$${ENV_VALUE}text" | ||
# two escaped $ | ||
key6: "$${ONE}$${TWO}" | ||
# trailing escaped $ | ||
key7: "text$$" | ||
# escaped $ alone | ||
key8: "$$" | ||
# escaped number and uri | ||
key9: "$${1}$${env:2}" | ||
# escape provider | ||
key10: "some$${env:ENV_VALUE}text" | ||
# can escape outer when nested | ||
key11: "$${env:${ENV_VALUE}}" | ||
# can escape inner and outer when nested | ||
key12: "$${env:$${ENV_VALUE}}" | ||
# can escape partial | ||
key13: "env:MAP_VALUE_2}$${ENV_VALUE}{" | ||
# $$$ -> escaped $ + expanded env var | ||
key14: "$$${env:ENV_VALUE}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.