From 4066561b9c75ecdc2f8cee2240cf81b72f157f10 Mon Sep 17 00:00:00 2001 From: fearful-symmetry Date: Wed, 25 Sep 2024 10:30:45 -0700 Subject: [PATCH 1/3] fix nil pointer deref if the fileout lacks a path --- libbeat/outputs/fileout/config.go | 1 + libbeat/outputs/fileout/pathformatstring.go | 4 ++++ libbeat/outputs/fileout/pathformatstring_test.go | 11 +++++++++++ 3 files changed, 16 insertions(+) diff --git a/libbeat/outputs/fileout/config.go b/libbeat/outputs/fileout/config.go index 69af40e4289b..9228430d6d9b 100644 --- a/libbeat/outputs/fileout/config.go +++ b/libbeat/outputs/fileout/config.go @@ -38,6 +38,7 @@ type fileOutConfig struct { func defaultConfig() fileOutConfig { return fileOutConfig{ + Path: &PathFormatString{}, NumberOfFiles: 7, RotateEveryKb: 10 * 1024, Permissions: 0600, diff --git a/libbeat/outputs/fileout/pathformatstring.go b/libbeat/outputs/fileout/pathformatstring.go index acd2a7605fe6..fdaff2177872 100644 --- a/libbeat/outputs/fileout/pathformatstring.go +++ b/libbeat/outputs/fileout/pathformatstring.go @@ -18,6 +18,7 @@ package fileout import ( + "fmt" "os" "strings" "time" @@ -44,6 +45,9 @@ func (fs *PathFormatString) Run(timestamp time.Time) (string, error) { placeholderEvent := &beat.Event{ Timestamp: timestamp, } + if fs.efs == nil { + return "", fmt.Errorf("path format string is nil; check if `path` option is configured correctly") + } return fs.efs.Run(placeholderEvent) } diff --git a/libbeat/outputs/fileout/pathformatstring_test.go b/libbeat/outputs/fileout/pathformatstring_test.go index b8eee4e44eaa..6826b6ff5ed0 100644 --- a/libbeat/outputs/fileout/pathformatstring_test.go +++ b/libbeat/outputs/fileout/pathformatstring_test.go @@ -21,9 +21,20 @@ import ( "testing" "time" + "github.com/elastic/elastic-agent-libs/config" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) +func TestCheckNilDoesntPanic(t *testing.T) { + newCfg := config.NewConfig() + handler, err := readConfig(newCfg) + require.NoError(t, err) + _, err = handler.Path.Run(time.Now()) + require.Error(t, err) + +} + func TestPathFormatString(t *testing.T) { tests := []struct { title string From 90d50f7ed536135a9d131fe7a2e8bc47befbf333 Mon Sep 17 00:00:00 2001 From: fearful-symmetry Date: Wed, 25 Sep 2024 12:15:14 -0700 Subject: [PATCH 2/3] format --- libbeat/outputs/fileout/pathformatstring_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libbeat/outputs/fileout/pathformatstring_test.go b/libbeat/outputs/fileout/pathformatstring_test.go index 6826b6ff5ed0..fa3c9ee08fe0 100644 --- a/libbeat/outputs/fileout/pathformatstring_test.go +++ b/libbeat/outputs/fileout/pathformatstring_test.go @@ -21,9 +21,10 @@ import ( "testing" "time" - "github.com/elastic/elastic-agent-libs/config" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + + "github.com/elastic/elastic-agent-libs/config" ) func TestCheckNilDoesntPanic(t *testing.T) { From cff121aebbff10f8ef6d6bedf2a04c054d16b1a6 Mon Sep 17 00:00:00 2001 From: fearful-symmetry Date: Wed, 25 Sep 2024 13:47:13 -0700 Subject: [PATCH 3/3] fix tests --- libbeat/outputs/fileout/config_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/libbeat/outputs/fileout/config_test.go b/libbeat/outputs/fileout/config_test.go index 7e149173f6df..0bf0d6f7cc13 100644 --- a/libbeat/outputs/fileout/config_test.go +++ b/libbeat/outputs/fileout/config_test.go @@ -37,6 +37,7 @@ func TestConfig(t *testing.T) { config: config.MustNewConfigFrom([]byte(`{ }`)), assertion: func(t *testing.T, actual *fileOutConfig, err error) { expectedConfig := &fileOutConfig{ + Path: &PathFormatString{}, NumberOfFiles: 7, RotateEveryKb: 10 * 1024, Permissions: 0600,