Skip to content

decode_json_field: do not process arrays when flag not set #11318

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

Merged
merged 8 commits into from
Mar 25, 2019
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 CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Include ip and boolean type when generating index pattern. {pull}10995[10995]
- Cancelling enrollment of a beat will not enroll the beat. {issue}10150[10150]
- Add missing fields and test cases for libbeat add_kubernetes_metadata processor. {issue}11133[11133], {pull}11134[11134]
- decode_json_field: do not process arrays when flag not set. {pull}11318[11318]
- Report faulting file when config reload fails. {pull}[11304]11304

*Auditbeat*
Expand Down
5 changes: 3 additions & 2 deletions libbeat/processors/actions/decode_json_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ var (
MaxDepth: 1,
ProcessArray: false,
}
errProcessingSkipped = errors.New("processing skipped")
)

var debug = logp.MakeDebug("filters")
Expand Down Expand Up @@ -150,7 +151,7 @@ func unmarshal(maxDepth int, text string, fields *interface{}, processArray bool
var tmp interface{}
err := unmarshal(maxDepth, str, &tmp, processArray)
if err != nil {
return v, false
return v, err == errProcessingSkipped
}

return tmp, true
Expand All @@ -167,7 +168,7 @@ func unmarshal(maxDepth int, text string, fields *interface{}, processArray bool
// We want to process arrays here
case []interface{}:
if !processArray {
break
return errProcessingSkipped
}

for i, v := range O {
Expand Down
47 changes: 47 additions & 0 deletions libbeat/processors/actions/decode_json_fields_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,53 @@ func TestTargetRootOption(t *testing.T) {
assert.Equal(t, expected.String(), actual.String())
}

func TestArrayWithArraysDisabled(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind adding test cases for decoding array when process_array is set to true? Also, it would be nice to see tests for max_depth. The coverage of decode_json_fields is pretty thin. :(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

input := common.MapStr{
"msg": `{
"arrayOfMap": "[{\"a\":\"b\"}]"
}`,
}

testConfig, _ = common.NewConfigFrom(map[string]interface{}{
"fields": fields,
"max_depth": 10,
"process_array": false,
})

actual := getActualValue(t, testConfig, input)

expected := common.MapStr{
"msg": common.MapStr{
"arrayOfMap": "[{\"a\":\"b\"}]",
},
}

assert.Equal(t, expected.String(), actual.String())
}
func TestArrayWithArraysEnabled(t *testing.T) {
input := common.MapStr{
"msg": `{
"arrayOfMap": "[{\"a\":\"b\"}]"
}`,
}

testConfig, _ = common.NewConfigFrom(map[string]interface{}{
"fields": fields,
"max_depth": 10,
"process_array": true,
})

actual := getActualValue(t, testConfig, input)

expected := common.MapStr{
"msg": common.MapStr{
"arrayOfMap": []common.MapStr{common.MapStr{"a": "b"}},
},
}

assert.Equal(t, expected.String(), actual.String())
}

func getActualValue(t *testing.T, config *common.Config, input common.MapStr) common.MapStr {
logp.TestingSetup()

Expand Down