Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expand env var when loading acquis #3375

Merged
merged 3 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion pkg/acquisition/acquisition.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
tomb "gopkg.in/tomb.v2"
"gopkg.in/yaml.v2"

"github.com/crowdsecurity/go-cs-lib/csstring"
"github.com/crowdsecurity/go-cs-lib/trace"

"github.com/crowdsecurity/crowdsec/pkg/acquisition/configuration"
Expand Down Expand Up @@ -232,7 +233,16 @@
return nil, err
}

dec := yaml.NewDecoder(yamlFile)
defer yamlFile.Close()

acquisContent, err := io.ReadAll(yamlFile)
if err != nil {
return nil, fmt.Errorf("failed to read %s: %w", acquisFile, err)
}

Check warning on line 241 in pkg/acquisition/acquisition.go

View check run for this annotation

Codecov / codecov/patch

pkg/acquisition/acquisition.go#L240-L241

Added lines #L240 - L241 were not covered by tests

expandedAcquis := csstring.StrictExpand(string(acquisContent), os.LookupEnv)

dec := yaml.NewDecoder(strings.NewReader(expandedAcquis))
dec.SetStrict(true)

idx := -1
Expand Down
15 changes: 15 additions & 0 deletions pkg/acquisition/acquisition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ wowo: ajsajasjas

func TestLoadAcquisitionFromFile(t *testing.T) {
appendMockSource()
t.Setenv("TEST_ENV", "test_value2")

tests := []struct {
TestName string
Expand Down Expand Up @@ -282,6 +283,13 @@ func TestLoadAcquisitionFromFile(t *testing.T) {
},
ExpectedError: "while configuring datasource of type file from test_files/bad_filetype.yaml",
},
{
TestName: "from_env",
Config: csconfig.CrowdsecServiceCfg{
AcquisitionFiles: []string{"test_files/env.yaml"},
},
ExpectedLen: 1,
},
}
for _, tc := range tests {
t.Run(tc.TestName, func(t *testing.T) {
Expand All @@ -293,6 +301,13 @@ func TestLoadAcquisitionFromFile(t *testing.T) {
}

assert.Len(t, dss, tc.ExpectedLen)
if tc.TestName == "from_env" {
mock := dss[0].Dump().(*MockSource)
assert.Equal(t, "test_value2", mock.Toto)
assert.Equal(t, "foobar", mock.Labels["test"])
assert.Equal(t, "${NON_EXISTING}", mock.Labels["non_existing"])
assert.Equal(t, log.InfoLevel, mock.logger.Logger.Level)
}
})
}
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/acquisition/test_files/env.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
labels:
test: foobar
non_existing: ${NON_EXISTING}
log_level: info
source: mock
toto: ${TEST_ENV}
Loading