Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix custom index name in setup #12457

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Fix Central Management enroll under Windows {issue}12797[12797] {pull}12799[12799]
- ILM: Use GET instead of HEAD when checking for alias to expose detailed error message. {pull}12886[12886]
- Fix seccomp policy preventing some features to function properly on 32bit Linux systems. {issue}12990[12990] {pull}13008[13008]
- Fix custom index name in setup. {pull}12457[12457]

*Auditbeat*

Expand Down
43 changes: 27 additions & 16 deletions libbeat/dashboards/modify_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,36 @@ func ReplaceIndexInIndexPattern(index string, content common.MapStr) common.MapS
return content
}

objects, ok := content["objects"].([]interface{})
if !ok {
return content
}

// change index pattern name
for i, object := range objects {
objectMap, ok := object.(map[string]interface{})
if !ok {
continue
switch objects := content["objects"].(type) {
case []interface{}:
// change index pattern name
for i, object := range objects {
objectMap, ok := object.(map[string]interface{})
if !ok {
continue
}

objectMap["id"] = index
if attributes, ok := objectMap["attributes"].(map[string]interface{}); ok {
attributes["title"] = index
}
objects[i] = objectMap
}

objectMap["id"] = index
if attributes, ok := objectMap["attributes"].(map[string]interface{}); ok {
attributes["title"] = index
content["objects"] = objects
case []common.MapStr:
// change index pattern name
for i, objectMap := range objects {

objectMap["id"] = index
if attributes, ok := objectMap["attributes"].(common.MapStr); ok {
attributes["title"] = index
}
objects[i] = objectMap
}
objects[i] = objectMap
content["objects"] = objects
default:
logp.Err("Unexpected object type %T, expected list of objects. Got: %#v", content["objects"], content["objects"])
}
content["objects"] = objects
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When dealing with external input and some implicitely assumed types, then we should validate and log if something doesn't match our expectations.

Maybe use this pattern:

  switch objects := content["objects"].(type) {
    case []interface{}:
        ...
    case []common.MapStr;
        ...
    default:
        // log error with type + contents e.g.
        logp.Errf("Unexpected object type %T, expected list of objects. Got: %#v", content["objects"], content["objects"])
  }

This if-else construct seems to normalize the object contents for further processing. Maybe we should error if we can't do so, but the field is present? How about moving the normalization into a separate function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I've changed to a switch.

I initially tried separating the normalization from the rest of the logic. Unfortunately, it's not just a single type cast. The whole structure is nested, with each nested object being either []interface{} or []common.MapStr. So we'd need to recursively cast everything from one to the other, and maybe that's out of scope for this PR (which is essentially just fixing a regression bug)?


return content
}
Expand Down