Skip to content

Commit 33e745e

Browse files
committed
[confmap] Fix double-expansion of escaped environment variables
This change fixes the issue where environment variables escaped with $$ were double-expanded. The collector now converts `$${ENV_VAR}` to `${ENV_VAR}` and `$$ENV_VAR` to `$ENV_VAR` without further expansion.
1 parent 4e44e32 commit 33e745e

File tree

4 files changed

+44
-24
lines changed

4 files changed

+44
-24
lines changed
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: bug_fix
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
7+
component: confmap
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Fix double-expansion of environment variables escaped with `$$`, e.g. `$${ENV_VAR}` and `$$ENV_VAR`.
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [10713]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext: |
19+
This change fixes the issue where environment variables escaped with $$ were double-expanded.
20+
The collector now converts `$${ENV_VAR}` to `${ENV_VAR}` and `$$ENV_VAR` to `$ENV_VAR` without further expansion.
21+
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: [api]

confmap/converter/expandconverter/expand.go

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ func (c converter) expandEnv(s string) (string, error) {
8585
// - $FOO will be substituted with env var FOO
8686
// - $$FOO will be replaced with $FOO
8787
// - $$$FOO will be replaced with $ + substituted env var FOO
88+
// TODO: Move the escaping of $$ out from the expand converter to the resolver.
8889
if str == "$" {
8990
return "$"
9091
}

confmap/expand_test.go

+15-15
Original file line numberDiff line numberDiff line change
@@ -583,21 +583,21 @@ func Test_EscapedEnvVars(t *testing.T) {
583583

584584
expectedMap := map[string]any{
585585
"test_map": map[string]any{
586-
"recv.1": "$MAP_VALUE_1",
587-
"recv.2": "$$MAP_VALUE_2",
588-
"recv.3": "$$MAP_VALUE_3",
589-
"recv.4": "$" + mapValue2,
590-
"recv.5": "some${MAP_VALUE_4}text",
591-
"recv.6": "${ONE}${TWO}",
592-
"recv.7": "text$",
593-
"recv.8": "$",
594-
"recv.9": "${1}${env:2}",
595-
"recv.10": "some${env:MAP_VALUE_4}text",
596-
"recv.11": "${env:" + mapValue2 + "}",
597-
"recv.12": "${env:${MAP_VALUE_2}}",
598-
"recv.13": "env:MAP_VALUE_2}${MAP_VALUE_2}{",
599-
"recv.14": "${env:MAP_VALUE_2${MAP_VALUE_2}",
600-
"recv.15": "$" + mapValue2,
586+
"recv.1": "$$MAP_VALUE_1",
587+
"recv.2": "$$$MAP_VALUE_2",
588+
"recv.3": "$$$$MAP_VALUE_3",
589+
"recv.4": "$$" + mapValue2,
590+
"recv.5": "some$${MAP_VALUE_4}text",
591+
"recv.6": "$${ONE}$${TWO}",
592+
"recv.7": "text$$",
593+
"recv.8": "$$",
594+
"recv.9": "$${1}$${env:2}",
595+
"recv.10": "some$${env:MAP_VALUE_4}text",
596+
"recv.11": "$${env:" + mapValue2 + "}",
597+
"recv.12": "$${env:$${MAP_VALUE_2}}",
598+
"recv.13": "env:MAP_VALUE_2}$${MAP_VALUE_2}{",
599+
"recv.14": "${env:MAP_VALUE_2$${MAP_VALUE_2}",
600+
"recv.15": "$$" + mapValue2,
601601
}}
602602

603603
fileProvider := newFakeProvider("file", func(_ context.Context, uri string, _ WatcherFunc) (*Retrieved, error) {

confmap/resolver.go

+1-9
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import (
1212

1313
"go.uber.org/multierr"
1414
"go.uber.org/zap"
15-
16-
"go.opentelemetry.io/collector/internal/globalgates"
1715
)
1816

1917
// follows drive-letter specification:
@@ -173,13 +171,7 @@ func (mr *Resolver) Resolve(ctx context.Context) (*Conf, error) {
173171
if err != nil {
174172
return nil, err
175173
}
176-
177-
if v, ok := val.(string); ok && globalgates.UseUnifiedEnvVarExpansionRules.IsEnabled() {
178-
cfgMap[k] = strings.ReplaceAll(v, "$$", "$")
179-
} else {
180-
cfgMap[k] = val
181-
}
182-
174+
cfgMap[k] = val
183175
}
184176
retMap = NewFromStringMap(cfgMap)
185177

0 commit comments

Comments
 (0)