Skip to content

Commit ce680e1

Browse files
authored
Fix nil panic when overwriting metadata (elastic#24741)
Fix panic when overwriting metadata using the decode_json_fields processor.
1 parent 6a0dc39 commit ce680e1

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
@@ -249,6 +249,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
249249
- Fix `mage GenerateCustomBeat` instructions for a new beat {pull}17679[17679]
250250
- Fix bug with annotations dedot config on k8s not used {pull}25111[25111]
251251
- Fix negative Kafka partition bug {pull}25048[25048]
252+
- Fix panic when overwriting metadata {pull}24741[24741]
252253

253254
*Auditbeat*
254255

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)