Skip to content

Commit

Permalink
Fix nil panic when overwriting metadata (#24741)
Browse files Browse the repository at this point in the history
Fix panic when overwriting metadata using the  decode_json_fields processor.
  • Loading branch information
sincejune authored Apr 20, 2021
1 parent 6a0dc39 commit ce680e1
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 @@ -249,6 +249,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Fix `mage GenerateCustomBeat` instructions for a new beat {pull}17679[17679]
- Fix bug with annotations dedot config on k8s not used {pull}25111[25111]
- Fix negative Kafka partition bug {pull}25048[25048]
- 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 ce680e1

Please sign in to comment.