Skip to content

Commit

Permalink
libbeat: fix decode_json_fields config validation
Browse files Browse the repository at this point in the history
Add "expand_keys" to the list of allowed config fields,
and add a test that ensures all fields defined in the
config struct are allowed.
  • Loading branch information
axw committed Mar 31, 2021
1 parent 2b00b83 commit 63c86c7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Fix ILM alias not being created if `setup.ilm.check_exists: false` and `setup.ilm.overwrite: true` has been configured. {pull}24480[24480]
- Fix issue discovering docker containers and metadata after reconnections {pull}24318[24318]
- Allow cgroup self-monitoring to see alternate `hostfs` paths {pull}24334[24334]
- Add `expand_keys` to the list of permitted config fields for `decode_json_fields` {24862}[24862]

*Auditbeat*

Expand Down
2 changes: 1 addition & 1 deletion libbeat/processors/actions/decode_json_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func init() {
processors.RegisterPlugin("decode_json_fields",
checks.ConfigChecked(NewDecodeJSONFields,
checks.RequireFields("fields"),
checks.AllowedFields("fields", "max_depth", "overwrite_keys", "add_error_key", "process_array", "target", "when", "document_id")))
checks.AllowedFields("fields", "max_depth", "overwrite_keys", "add_error_key", "process_array", "target", "when", "document_id", "expand_keys")))

jsprocessor.RegisterPlugin("DecodeJSONFields", NewDecodeJSONFields)
}
Expand Down
24 changes: 24 additions & 0 deletions libbeat/processors/actions/decode_json_fields_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/elastic/beats/v7/libbeat/beat"
"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/libbeat/logp"
"github.com/elastic/beats/v7/libbeat/processors"
)

var fields = [1]string{"msg"}
Expand All @@ -35,6 +36,29 @@ var testConfig, _ = common.NewConfigFrom(map[string]interface{}{
"processArray": false,
})

func TestDecodeJSONFieldsCheckConfig(t *testing.T) {
// All fields defined in config should be allowed.
cfg := common.MustNewConfigFrom(map[string]interface{}{
"decode_json_fields": &config{
// Rely on zero values for all fields that don't have validation.
MaxDepth: 1,
},
})
_, err := processors.New(processors.PluginConfig([]*common.Config{cfg}))
assert.NoError(t, err)

// Unknown fields should not be allowed.
cfg = common.MustNewConfigFrom(map[string]interface{}{
"decode_json_fields": map[string]interface{}{
"fields": []string{"required"},
"extraneous": "field",
},
})
_, err = processors.New(processors.PluginConfig([]*common.Config{cfg}))
assert.Error(t, err)
assert.EqualError(t, err, "unexpected extraneous option in decode_json_fields")
}

func TestMissingKey(t *testing.T) {
input := common.MapStr{
"pipeline": "us1",
Expand Down

0 comments on commit 63c86c7

Please sign in to comment.