Skip to content

Commit

Permalink
expand env var when loading acquis (#3375)
Browse files Browse the repository at this point in the history
  • Loading branch information
blotus authored Jan 10, 2025
1 parent 866b0ad commit 34c0e6a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
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 @@ import (
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 @@ func LoadAcquisitionFromFile(config *csconfig.CrowdsecServiceCfg, prom *csconfig
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)
}

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}

0 comments on commit 34c0e6a

Please sign in to comment.