Skip to content

Commit

Permalink
Add config check to replace processor (#40047)
Browse files Browse the repository at this point in the history
* add config check to replace processor

* add changelog

* use config validator
  • Loading branch information
fearful-symmetry authored Jul 3, 2024
1 parent b872d5e commit 48e4af4
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ https:/elastic/beats/compare/v8.8.1\...main[Check the HEAD diff]
- Fix cache processor expiries infinite growth when large a large TTL is used and recurring keys are cached. {pull}38561[38561]
- Fix parsing of RFC 3164 process IDs in syslog processor. {issue}38947[38947] {pull}38982[38982]
- Rename the field "apache2.module.error" to "apache.module.error" in Apache error visualization. {issue}39480[39480] {pull}39481[39481]
- Validate config of the `replace` processor {pull}40047[40047]

*Auditbeat*

Expand Down
6 changes: 3 additions & 3 deletions libbeat/processors/actions/replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ type replaceStringConfig struct {
}

type replaceConfig struct {
Field string `config:"field"`
Pattern *regexp.Regexp `config:"pattern"`
Replacement string `config:"replacement"`
Field string `config:"field" validate:"required"`
Pattern *regexp.Regexp `config:"pattern" validate:"required"`
Replacement string `config:"replacement" validate:"required"`
}

func init() {
Expand Down
56 changes: 56 additions & 0 deletions libbeat/processors/actions/replace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,70 @@ import (
"regexp"
"testing"

conf "github.com/elastic/elastic-agent-libs/config"
"github.com/elastic/elastic-agent-libs/logp"

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

"github.com/elastic/beats/v7/libbeat/beat"
"github.com/elastic/elastic-agent-libs/mapstr"
)

func TestBadConfig(t *testing.T) {
var cases = []struct {
name string
cfg replaceStringConfig
shouldError bool
}{
{
name: "field-only",
cfg: replaceStringConfig{Fields: []replaceConfig{{Field: "message"}}},
shouldError: true,
},
{
name: "no-regex",
cfg: replaceStringConfig{Fields: []replaceConfig{{Field: "message", Replacement: "new_message"}}},
shouldError: true,
},
{
name: "no-replacement",
cfg: replaceStringConfig{Fields: []replaceConfig{{Field: "message", Pattern: regexp.MustCompile(`message`)}}},
shouldError: true,
},
{
name: "valid-then-invalid",
cfg: replaceStringConfig{Fields: []replaceConfig{
{Field: "message", Pattern: regexp.MustCompile(`message`), Replacement: "new_message"},
{Field: "message", Pattern: regexp.MustCompile(`message`)},
},
},
shouldError: true,
},
{
name: "no-error",
cfg: replaceStringConfig{Fields: []replaceConfig{{Field: "message", Replacement: "new_message", Pattern: regexp.MustCompile(`message`)}}},
shouldError: false,
},
}

for _, testCase := range cases {
t.Run(testCase.name, func(t *testing.T) {
cfg, err := conf.NewConfigFrom(testCase.cfg)
require.NoError(t, err)
unpacked := replaceStringConfig{}
err = cfg.Unpack(&unpacked)
if testCase.shouldError {
require.Error(t, err)
} else {
require.NoError(t, err)
}

})
}

}

func TestReplaceRun(t *testing.T) {
var tests = []struct {
description string
Expand Down

0 comments on commit 48e4af4

Please sign in to comment.