From 3e7abcd14cd35526aaca9bb40b9ea561693208b4 Mon Sep 17 00:00:00 2001 From: Sebastien Blot Date: Fri, 20 Dec 2024 09:20:16 +0100 Subject: [PATCH 1/3] expand env var when loading acquis --- pkg/acquisition/acquisition.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/pkg/acquisition/acquisition.go b/pkg/acquisition/acquisition.go index 4e233aad616..db00e00862e 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 From 034c721ebfc1e074604c4f9f6b2042f3e26ae9a1 Mon Sep 17 00:00:00 2001 From: Sebastien Blot Date: Fri, 20 Dec 2024 09:28:31 +0100 Subject: [PATCH 2/3] test --- pkg/acquisition/acquisition_test.go | 16 ++++++++++++++++ pkg/acquisition/test_files/env.yaml | 6 ++++++ 2 files changed, 22 insertions(+) create mode 100644 pkg/acquisition/test_files/env.yaml diff --git a/pkg/acquisition/acquisition_test.go b/pkg/acquisition/acquisition_test.go index cfe1e74c612..7c85147aada 100644 --- a/pkg/acquisition/acquisition_test.go +++ b/pkg/acquisition/acquisition_test.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "os" "strings" "testing" "time" @@ -217,6 +218,7 @@ wowo: ajsajasjas func TestLoadAcquisitionFromFile(t *testing.T) { appendMockSource() + os.Setenv("TEST_ENV", "test_value2") tests := []struct { TestName string @@ -282,6 +284,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 +302,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 From 2dde732cc65799a1b75e6fd07813cb828c37217e Mon Sep 17 00:00:00 2001 From: Sebastien Blot Date: Fri, 20 Dec 2024 09:38:40 +0100 Subject: [PATCH 3/3] lint --- pkg/acquisition/acquisition_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/acquisition/acquisition_test.go b/pkg/acquisition/acquisition_test.go index 7c85147aada..1ea8f11c22a 100644 --- a/pkg/acquisition/acquisition_test.go +++ b/pkg/acquisition/acquisition_test.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "os" "strings" "testing" "time" @@ -218,7 +217,7 @@ wowo: ajsajasjas func TestLoadAcquisitionFromFile(t *testing.T) { appendMockSource() - os.Setenv("TEST_ENV", "test_value2") + t.Setenv("TEST_ENV", "test_value2") tests := []struct { TestName string