Skip to content

Commit fa88da8

Browse files
sincejunejsoriano
authored andcommitted
Fix nil panic when overwriting metadata (#24741)
Fix panic when overwriting metadata using the decode_json_fields processor. (cherry picked from commit ce680e1)
1 parent 22853a1 commit fa88da8

3 files changed

Lines changed: 26 additions & 0 deletions

File tree

CHANGELOG.next.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
155155
- Fix inode removal tracking code when files are replaced by files with the same name {pull}25002[25002]
156156
- Fix negative Kafka partition bug {pull}25048[25048]
157157
- Fix bug with annotations dedot config on k8s not used {pull}25111[25111]
158+
- Fix panic when overwriting metadata {pull}24741[24741]
158159

159160
*Auditbeat*
160161

libbeat/common/jsontransform/jsonhelper.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,17 @@ func WriteJSONKeys(event *beat.Event, keys map[string]interface{}, expandKeys, o
7979
case "@metadata":
8080
switch m := v.(type) {
8181
case map[string]string:
82+
if event.Meta == nil && len(m) > 0 {
83+
event.Meta = common.MapStr{}
84+
}
8285
for meta, value := range m {
8386
event.Meta[meta] = value
8487
}
8588

8689
case map[string]interface{}:
90+
if event.Meta == nil {
91+
event.Meta = common.MapStr{}
92+
}
8793
event.Meta.DeepUpdate(common.MapStr(m))
8894

8995
default:

libbeat/processors/actions/decode_json_fields_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,25 @@ func TestExpandKeysError(t *testing.T) {
472472
assert.Equal(t, expected, actual)
473473
}
474474

475+
func TestOverwriteMetadata(t *testing.T) {
476+
testConfig := common.MustNewConfigFrom(map[string]interface{}{
477+
"fields": fields,
478+
"target": "",
479+
"overwrite_keys": true,
480+
})
481+
482+
input := common.MapStr{
483+
"msg": "{\"@metadata\":{\"beat\":\"libbeat\"},\"msg\":\"overwrite metadata test\"}",
484+
}
485+
486+
expected := common.MapStr{
487+
"msg": "overwrite metadata test",
488+
}
489+
actual := getActualValue(t, testConfig, input)
490+
491+
assert.Equal(t, expected, actual)
492+
}
493+
475494
func getActualValue(t *testing.T, config *common.Config, input common.MapStr) common.MapStr {
476495
log := logp.NewLogger("decode_json_fields_test")
477496

0 commit comments

Comments
 (0)