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

Fix nil pointer deref if the file output lacks a path setting. #40992

Merged
merged 3 commits into from
Sep 27, 2024
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
1 change: 1 addition & 0 deletions libbeat/outputs/fileout/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type fileOutConfig struct {

func defaultConfig() fileOutConfig {
return fileOutConfig{
Path: &PathFormatString{},
NumberOfFiles: 7,
RotateEveryKb: 10 * 1024,
Permissions: 0600,
Expand Down
1 change: 1 addition & 0 deletions libbeat/outputs/fileout/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions libbeat/outputs/fileout/pathformatstring.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package fileout

import (
"fmt"
"os"
"strings"
"time"
Expand All @@ -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)
}

Expand Down
12 changes: 12 additions & 0 deletions libbeat/outputs/fileout/pathformatstring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,20 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/elastic/elastic-agent-libs/config"
)

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
Expand Down
Loading