Skip to content

Commit

Permalink
Fix nil panic when overwriting metadata (#24741) (#25180)
Browse files Browse the repository at this point in the history
Fix panic when overwriting metadata using the  decode_json_fields processor.

(cherry picked from commit ce680e1)

Co-authored-by: Chao <19381524+sincejune@users.noreply.github.com>
  • Loading branch information
jsoriano and sincejune committed Apr 20, 2021
1 parent 9e04a75 commit 82e1226
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Fix inode removal tracking code when files are replaced by files with the same name {pull}25002[25002]
- Fix negative Kafka partition bug {pull}25048[25048]
- Fix bug with annotations dedot config on k8s not used {pull}25111[25111]
- Fix panic when overwriting metadata {pull}24741[24741]

*Auditbeat*

Expand Down
6 changes: 6 additions & 0 deletions libbeat/common/jsontransform/jsonhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,17 @@ func WriteJSONKeys(event *beat.Event, keys map[string]interface{}, expandKeys, o
case "@metadata":
switch m := v.(type) {
case map[string]string:
if event.Meta == nil && len(m) > 0 {
event.Meta = common.MapStr{}
}
for meta, value := range m {
event.Meta[meta] = value
}

case map[string]interface{}:
if event.Meta == nil {
event.Meta = common.MapStr{}
}
event.Meta.DeepUpdate(common.MapStr(m))

default:
Expand Down
19 changes: 19 additions & 0 deletions libbeat/processors/actions/decode_json_fields_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,25 @@ func TestExpandKeysError(t *testing.T) {
assert.Equal(t, expected, actual)
}

func TestOverwriteMetadata(t *testing.T) {
testConfig := common.MustNewConfigFrom(map[string]interface{}{
"fields": fields,
"target": "",
"overwrite_keys": true,
})

input := common.MapStr{
"msg": "{\"@metadata\":{\"beat\":\"libbeat\"},\"msg\":\"overwrite metadata test\"}",
}

expected := common.MapStr{
"msg": "overwrite metadata test",
}
actual := getActualValue(t, testConfig, input)

assert.Equal(t, expected, actual)
}

func getActualValue(t *testing.T, config *common.Config, input common.MapStr) common.MapStr {
log := logp.NewLogger("decode_json_fields_test")

Expand Down

0 comments on commit 82e1226

Please sign in to comment.