Skip to content

Commit

Permalink
Overwrite index name in index template correctly (#29299)
Browse files Browse the repository at this point in the history
  • Loading branch information
kvch authored Dec 8, 2021
1 parent 8796caa commit e5a6631
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 115 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Skip configuration checks in autodiscover for configurations that are already running {pull}29048[29048]
- Fix `decode_json_processor` to always respect `add_error_key` {pull}29107[29107]
- Fix `add_labels` flattening of array values. {pull}29211[29211]
- Overwrite index name in index template correctly. {issue}28571[28571] {pull}29299[29299]

*Auditbeat*

Expand Down
44 changes: 6 additions & 38 deletions libbeat/dashboards/modify_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import (
"fmt"
"regexp"

"github.com/pkg/errors"

"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/libbeat/logp"
)
Expand Down Expand Up @@ -53,44 +51,14 @@ func ReplaceIndexInIndexPattern(index string, content common.MapStr) (err error)
return nil
}

list, ok := content["objects"]
if !ok {
return errors.New("empty index pattern")
}

updateObject := func(obj common.MapStr) {
// This uses Put instead of DeepUpdate to avoid modifying types for
// inner objects. (DeepUpdate will replace maps with MapStr).
obj.Put("id", index)
// Only overwrite title if it exists.
if _, err := obj.GetValue("attributes.title"); err == nil {
obj.Put("attributes.title", index)
}
// This uses Put instead of DeepUpdate to avoid modifying types for
// inner objects. (DeepUpdate will replace maps with MapStr).
content.Put("id", index)
// Only overwrite title if it exists.
if _, err := content.GetValue("attributes.title"); err == nil {
content.Put("attributes.title", index)
}

switch v := list.(type) {
case []interface{}:
for _, objIf := range v {
switch obj := objIf.(type) {
case common.MapStr:
updateObject(obj)
case map[string]interface{}:
updateObject(obj)
default:
return errors.Errorf("index pattern object has unexpected type %T", v)
}
}
case []map[string]interface{}:
for _, obj := range v {
updateObject(obj)
}
case []common.MapStr:
for _, obj := range v {
updateObject(obj)
}
default:
return errors.Errorf("index pattern objects have unexpected type %T", v)
}
return nil
}

Expand Down
104 changes: 27 additions & 77 deletions libbeat/dashboards/modify_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,118 +124,68 @@ func TestReplaceIndexInIndexPattern(t *testing.T) {
index string
expected common.MapStr
}{
{
title: "Replace in []interface(map).map",
input: common.MapStr{"objects": []interface{}{map[string]interface{}{
"id": "phonybeat-*",
"type": "index-pattern",
"attributes": map[string]interface{}{
"title": "phonybeat-*",
"timeFieldName": "@timestamp",
}}}},
index: "otherindex-*",
expected: common.MapStr{"objects": []interface{}{map[string]interface{}{
"id": "otherindex-*",
"type": "index-pattern",
"attributes": map[string]interface{}{
"title": "otherindex-*",
"timeFieldName": "@timestamp",
}}}},
},
{
title: "Replace in []interface(map).mapstr",
input: common.MapStr{"objects": []interface{}{map[string]interface{}{
"id": "phonybeat-*",
"type": "index-pattern",
"attributes": common.MapStr{
"title": "phonybeat-*",
"timeFieldName": "@timestamp",
}}}},
index: "otherindex-*",
expected: common.MapStr{"objects": []interface{}{map[string]interface{}{
"id": "otherindex-*",
"type": "index-pattern",
"attributes": common.MapStr{
"title": "otherindex-*",
"timeFieldName": "@timestamp",
}}}},
},
{
title: "Replace in []map.mapstr",
input: common.MapStr{"objects": []map[string]interface{}{{
"id": "phonybeat-*",
"type": "index-pattern",
"attributes": common.MapStr{
"title": "phonybeat-*",
"timeFieldName": "@timestamp",
}}}},
index: "otherindex-*",
expected: common.MapStr{"objects": []map[string]interface{}{{
"id": "otherindex-*",
"type": "index-pattern",
"attributes": common.MapStr{
"title": "otherindex-*",
"timeFieldName": "@timestamp",
}}}},
},
{
title: "Replace in []mapstr.mapstr",
input: common.MapStr{"objects": []common.MapStr{{
input: common.MapStr{
"id": "phonybeat-*",
"type": "index-pattern",
"attributes": common.MapStr{
"title": "phonybeat-*",
"timeFieldName": "@timestamp",
}}}},
}},
index: "otherindex-*",
expected: common.MapStr{"objects": []common.MapStr{{
expected: common.MapStr{
"id": "otherindex-*",
"type": "index-pattern",
"attributes": common.MapStr{
"title": "otherindex-*",
"timeFieldName": "@timestamp",
}}}},
}},
},
{
title: "Replace in []mapstr.interface(mapstr)",
input: common.MapStr{"objects": []common.MapStr{{
input: common.MapStr{
"id": "phonybeat-*",
"type": "index-pattern",
"attributes": interface{}(common.MapStr{
"title": "phonybeat-*",
"timeFieldName": "@timestamp",
})}}},
})},
index: "otherindex-*",
expected: common.MapStr{"objects": []common.MapStr{{
expected: common.MapStr{
"id": "otherindex-*",
"type": "index-pattern",
"attributes": interface{}(common.MapStr{
"title": "otherindex-*",
"timeFieldName": "@timestamp",
})}}},
})},
},
{
title: "Do not create missing attributes",
input: common.MapStr{"objects": []common.MapStr{{
"id": "phonybeat-*",
"type": "index-pattern",
}}},
input: common.MapStr{
"attributes": common.MapStr{},
"id": "phonybeat-*",
"type": "index-pattern",
},
index: "otherindex-*",
expected: common.MapStr{"objects": []common.MapStr{{
"id": "otherindex-*",
"type": "index-pattern",
}}},
expected: common.MapStr{
"attributes": common.MapStr{},
"id": "otherindex-*",
"type": "index-pattern",
},
},
{
title: "Create missing id",
input: common.MapStr{"objects": []common.MapStr{{
"type": "index-pattern",
}}},
input: common.MapStr{
"attributes": common.MapStr{},
"type": "index-pattern",
},
index: "otherindex-*",
expected: common.MapStr{"objects": []common.MapStr{{
"id": "otherindex-*",
"type": "index-pattern",
}}},
expected: common.MapStr{
"attributes": common.MapStr{},
"id": "otherindex-*",
"type": "index-pattern",
},
},
}

Expand Down

0 comments on commit e5a6631

Please sign in to comment.