diff --git a/pkg/acquisition/acquisition.go b/pkg/acquisition/acquisition.go index 291bc369c3e..06a4918592b 100644 --- a/pkg/acquisition/acquisition.go +++ b/pkg/acquisition/acquisition.go @@ -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" @@ -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 diff --git a/pkg/acquisition/acquisition_test.go b/pkg/acquisition/acquisition_test.go index cfe1e74c612..1ea8f11c22a 100644 --- a/pkg/acquisition/acquisition_test.go +++ b/pkg/acquisition/acquisition_test.go @@ -217,6 +217,7 @@ wowo: ajsajasjas func TestLoadAcquisitionFromFile(t *testing.T) { appendMockSource() + t.Setenv("TEST_ENV", "test_value2") tests := []struct { TestName string @@ -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) { @@ -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) + } }) } } diff --git a/pkg/acquisition/test_files/env.yaml b/pkg/acquisition/test_files/env.yaml new file mode 100644 index 00000000000..8abd4b16ca5 --- /dev/null +++ b/pkg/acquisition/test_files/env.yaml @@ -0,0 +1,6 @@ +labels: + test: foobar + non_existing: ${NON_EXISTING} +log_level: info +source: mock +toto: ${TEST_ENV} \ No newline at end of file