Skip to content

Commit

Permalink
[confmap] Fix double-expansion of escaped environment variables
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dmitryax committed Jul 24, 2024
1 parent 4e44e32 commit 33e745e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 24 deletions.
27 changes: 27 additions & 0 deletions .chloggen/fix-env-var-double-escaping.yaml
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 double-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 double-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]
1 change: 1 addition & 0 deletions confmap/converter/expandconverter/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func (c converter) expandEnv(s string) (string, error) {
// - $FOO will be substituted with env var FOO
// - $$FOO will be replaced with $FOO
// - $$$FOO will be replaced with $ + substituted env var FOO
// TODO: Move the escaping of $$ out from the expand converter to the resolver.
if str == "$" {
return "$"
}
Expand Down
30 changes: 15 additions & 15 deletions confmap/expand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,21 +583,21 @@ func Test_EscapedEnvVars(t *testing.T) {

expectedMap := map[string]any{
"test_map": map[string]any{
"recv.1": "$MAP_VALUE_1",
"recv.2": "$$MAP_VALUE_2",
"recv.3": "$$MAP_VALUE_3",
"recv.4": "$" + mapValue2,
"recv.5": "some${MAP_VALUE_4}text",
"recv.6": "${ONE}${TWO}",
"recv.7": "text$",
"recv.8": "$",
"recv.9": "${1}${env:2}",
"recv.10": "some${env:MAP_VALUE_4}text",
"recv.11": "${env:" + mapValue2 + "}",
"recv.12": "${env:${MAP_VALUE_2}}",
"recv.13": "env:MAP_VALUE_2}${MAP_VALUE_2}{",
"recv.14": "${env:MAP_VALUE_2${MAP_VALUE_2}",
"recv.15": "$" + mapValue2,
"recv.1": "$$MAP_VALUE_1",
"recv.2": "$$$MAP_VALUE_2",
"recv.3": "$$$$MAP_VALUE_3",
"recv.4": "$$" + mapValue2,
"recv.5": "some$${MAP_VALUE_4}text",
"recv.6": "$${ONE}$${TWO}",
"recv.7": "text$$",
"recv.8": "$$",
"recv.9": "$${1}$${env:2}",
"recv.10": "some$${env:MAP_VALUE_4}text",
"recv.11": "$${env:" + mapValue2 + "}",
"recv.12": "$${env:$${MAP_VALUE_2}}",
"recv.13": "env:MAP_VALUE_2}$${MAP_VALUE_2}{",
"recv.14": "${env:MAP_VALUE_2$${MAP_VALUE_2}",
"recv.15": "$$" + mapValue2,
}}

fileProvider := newFakeProvider("file", func(_ context.Context, uri string, _ WatcherFunc) (*Retrieved, error) {
Expand Down
10 changes: 1 addition & 9 deletions confmap/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (

"go.uber.org/multierr"
"go.uber.org/zap"

"go.opentelemetry.io/collector/internal/globalgates"
)

// follows drive-letter specification:
Expand Down Expand Up @@ -173,13 +171,7 @@ func (mr *Resolver) Resolve(ctx context.Context) (*Conf, error) {
if err != nil {
return nil, err
}

if v, ok := val.(string); ok && globalgates.UseUnifiedEnvVarExpansionRules.IsEnabled() {
cfgMap[k] = strings.ReplaceAll(v, "$$", "$")
} else {
cfgMap[k] = val
}

cfgMap[k] = val
}
retMap = NewFromStringMap(cfgMap)

Expand Down

0 comments on commit 33e745e

Please sign in to comment.