Skip to content

Commit

Permalink
Use IgnoreCommas in default config options (#4436)
Browse files Browse the repository at this point in the history
* use IgnoreCommas in default config options

* add config line

* add notice

* add changelog

* change test to make output more helpful
  • Loading branch information
fearful-symmetry authored Mar 27, 2024
1 parent 0dc98db commit ce229d2
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
32 changes: 32 additions & 0 deletions changelog/fragments/1710868294-ucfg-parser-fix.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Kind can be one of:
# - breaking-change: a change to previously-documented behavior
# - deprecation: functionality that is being removed in a later release
# - bug-fix: fixes a problem in a previous version
# - enhancement: extends functionality but does not break or fix existing behavior
# - feature: new functionality
# - known-issue: problems that we are aware of in a given version
# - security: impacts on the security of a product or a user’s deployment.
# - upgrade: important information for someone upgrading from a prior version
# - other: does not fit into any of the other categories
kind: feature

# Change summary; a 80ish characters long description of the change.
summary: ucfg-parser-fix

# Long description; in case the summary is not enough to describe the change
# this field accommodate a description without length limits.
# NOTE: This field will be rendered only for breaking-change and known-issue kinds at the moment.
description: Fix config unpacking when config strings contain list-like passages

# Affected component; usually one of "elastic-agent", "fleet-server", "filebeat", "metricbeat", "auditbeat", "all", etc.
component: elastic-agent

# PR URL; optional; the PR number that added the changeset.
# If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added.
# NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number.
# Please provide it if you are adding a fragment for a different PR.
pr: https://github.com/elastic/elastic-agent/pull/4436

# Issue URL; optional; the GitHub issue related to this changeset (either closes or is part of).
# If not present is automatically filled by the tooling with the issue linked to the PR number.
#issue: https://github.com/owner/repo/1234
4 changes: 2 additions & 2 deletions internal/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var DefaultOptions = []interface{}{
ucfg.ResolveEnv,
ucfg.VarExp,
VarSkipKeys("inputs"),
ucfg.IgnoreCommas,
}

// Config custom type over a ucfg.Config to add new methods on the object.
Expand Down Expand Up @@ -155,9 +156,8 @@ func (c *Config) ToMapStr(opts ...interface{}) (map[string]interface{}, error) {
}
ucfgOpts, local, err := getOptions(opts...)
if err != nil {
return nil, fmt.Errorf("error unpacking logs: %w", err)
return nil, fmt.Errorf("error unpacking config: %w", err)
}

// remove and unpack each skip keys into its own map with no resolve
// so that variables are not substituted
skippedKeys := map[string]interface{}{}
Expand Down
16 changes: 16 additions & 0 deletions internal/pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ func testToMapStr(t *testing.T) {
assert.True(t, reflect.DeepEqual(m, nm))
}

func TestCommaParsing(t *testing.T) {
_ = os.Setenv("testname", "motmot")
// test to make sure that we don't blow up the parsers when we have a `,` in a string
inMap := map[string]interface{}{
"test": "startsWith('${testname}','motmot')",
}
outMap := map[string]interface{}{
"test": "startsWith('motmot','motmot')",
}
c := MustNewConfigFrom(inMap)
parsedMap, err := c.ToMapStr()
require.NoError(t, err)
t.Logf("got :%#v", parsedMap)
require.Equal(t, outMap, parsedMap)
}

func testLoadFiles(t *testing.T) {
tmp, err := os.MkdirTemp("", "watch")
require.NoError(t, err)
Expand Down

0 comments on commit ce229d2

Please sign in to comment.