Skip to content

Commit

Permalink
Check dashboards index pattern (#13303)
Browse files Browse the repository at this point in the history
If a dashboard is created using an index pattern manually created,
it can contain incorrect references, check for index patterns to avoid
this mistake.

Fix #13300
  • Loading branch information
jsoriano committed Aug 22, 2019
1 parent ba8a45e commit bffe7aa
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 18 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Syslog input will now omit the `process` object from events if it is empty. {pull}12700[12700]
- Fix multiline pattern in Postgres which was too permissive {issue}12078[12078] {pull}13069[13069]
- Allow path variables to be used in files loaded from modules.d. {issue}13184[13184]
- Fix incorrect references to index patterns in AWS and CoreDNS dashboards. {pull}13303[13303]

*Heartbeat*

Expand Down Expand Up @@ -168,7 +169,8 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Fix incoherent behaviour in redis key metricset when keyspace is specified both in host URL and key pattern {pull}12913[12913]
- Fix connections leak in redis module {pull}12914[12914] {pull}12950[12950]
- Fix wrong uptime reporting by system/uptime metricset under Windows. {pull}12915[12915]
- Print errors that were being omitted in vSphere metricsets {pull}12816[12816]
- Print errors that were being omitted in vSphere metricsets. {pull}12816[12816]
- Fix redis key metricset dashboard references to index pattern. {pull}13303[13303]

*Packetbeat*

Expand Down
46 changes: 44 additions & 2 deletions dev-tools/mage/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,25 @@ type Dashboard struct {
type dashboardObject struct {
Type string `json:"type"`
Attributes struct {
Description string `json:"description"`
Title string `json:"title"`
Description string `json:"description"`
Title string `json:"title"`
KibanaSavedObjectMeta *struct {
SearchSourceJSON struct {
Index string `json:"index"`
} `json:"searchSourceJSON,omitempty"`
} `json:"kibanaSavedObjectMeta"`
VisState *struct {
Params struct {
Controls []struct {
IndexPattern string
} `json:"controls"`
} `json:"params"`
} `json:"visState,omitempty"`
} `json:"attributes"`
References []struct {
Type string `json:"type"`
ID string `json:"id"`
} `json:"references"`
}

var (
Expand All @@ -271,6 +287,11 @@ func (d *Dashboard) CheckFormat(module string) []error {
return errors.Wrapf(err, "expected title with format 'Some title [%s Module]', found '%s'", strings.Title(BeatName), o.Attributes.Title)
}
}

expectedIndexPattern := strings.ToLower(BeatName) + "-*"
if err := checkDashboardIndexPattern(expectedIndexPattern, o); err != nil {
return errors.Wrapf(err, "expected index pattern reference '%s'", expectedIndexPattern)
}
return nil
}
var errs []error
Expand Down Expand Up @@ -301,3 +322,24 @@ func checkTitle(re *regexp.Regexp, title string, module string) error {
}
return nil
}

func checkDashboardIndexPattern(expectedIndex string, o *dashboardObject) error {
if objectMeta := o.Attributes.KibanaSavedObjectMeta; objectMeta != nil {
if index := objectMeta.SearchSourceJSON.Index; index != "" && index != expectedIndex {
return errors.Errorf("unexpected index pattern reference found in object meta: %s", index)
}
}
if visState := o.Attributes.VisState; visState != nil {
for _, control := range visState.Params.Controls {
if index := control.IndexPattern; index != "" && index != expectedIndex {
return errors.Errorf("unexpected index pattern reference found in visualization state: %s", index)
}
}
}
for _, reference := range o.References {
if reference.Type == "index-pattern" && reference.ID != expectedIndex {
return errors.Errorf("unexpected reference to index pattern %s", reference.ID)
}
}
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
{
"fieldName": "redis.keyspace.id",
"id": "1545388837304",
"indexPattern": "bc2e8280-0509-11e9-9c60-d582a238e2c5",
"indexPattern": "metricbeat-*",
"label": "Keyspace",
"options": {
"dynamicOptions": true,
Expand Down Expand Up @@ -61,7 +61,7 @@
"meta": {
"alias": null,
"disabled": false,
"index": "bc2e8280-0509-11e9-9c60-d582a238e2c5",
"index": "metricbeat-*",
"key": "redis.key.type",
"negate": false,
"params": {
Expand All @@ -81,7 +81,7 @@
}
}
],
"index": "bc2e8280-0509-11e9-9c60-d582a238e2c5",
"index": "metricbeat-*",
"query": {
"language": "kuery",
"query": ""
Expand Down Expand Up @@ -243,7 +243,7 @@
"kibanaSavedObjectMeta": {
"searchSourceJSON": {
"filter": [],
"index": "bc2e8280-0509-11e9-9c60-d582a238e2c5",
"index": "metricbeat-*",
"query": {
"language": "kuery",
"query": ""
Expand Down Expand Up @@ -399,7 +399,7 @@
"meta": {
"alias": null,
"disabled": false,
"index": "bc2e8280-0509-11e9-9c60-d582a238e2c5",
"index": "metricbeat-*",
"key": "redis.key.type",
"negate": false,
"params": {
Expand All @@ -419,7 +419,7 @@
}
}
],
"index": "bc2e8280-0509-11e9-9c60-d582a238e2c5",
"index": "metricbeat-*",
"query": {
"language": "kuery",
"query": ""
Expand Down Expand Up @@ -570,7 +570,7 @@
"meta": {
"alias": null,
"disabled": false,
"index": "bc2e8280-0509-11e9-9c60-d582a238e2c5",
"index": "metricbeat-*",
"key": "redis.key.expire.ttl",
"negate": false,
"params": {
Expand All @@ -588,7 +588,7 @@
}
}
],
"index": "bc2e8280-0509-11e9-9c60-d582a238e2c5",
"index": "metricbeat-*",
"query": {
"language": "kuery",
"query": ""
Expand Down Expand Up @@ -758,7 +758,7 @@
"alias": null,
"controlledBy": "1545388837304",
"disabled": false,
"index": "bc2e8280-0509-11e9-9c60-d582a238e2c5",
"index": "metricbeat-*",
"key": "redis.keyspace.id",
"negate": false,
"params": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@
},
"references": [
{
"id": "46074e60-babd-11e9-9f70-1f7bda85a5eb",
"id": "filebeat-*",
"name": "kibanaSavedObjectMeta.searchSourceJSON.index",
"type": "index-pattern"
}
Expand Down Expand Up @@ -401,12 +401,12 @@
},
"references": [
{
"id": "46074e60-babd-11e9-9f70-1f7bda85a5eb",
"id": "filebeat-*",
"name": "kibanaSavedObjectMeta.searchSourceJSON.index",
"type": "index-pattern"
},
{
"id": "46074e60-babd-11e9-9f70-1f7bda85a5eb",
"id": "filebeat-*",
"name": "kibanaSavedObjectMeta.searchSourceJSON.filter[0].meta.index",
"type": "index-pattern"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@
},
"references": [
{
"id": "8d890080-413c-11e9-8548-ab7fbe04f038",
"id": "filebeat-*",
"name": "kibanaSavedObjectMeta.searchSourceJSON.index",
"type": "index-pattern"
}
Expand Down Expand Up @@ -245,7 +245,7 @@
},
"references": [
{
"id": "8d890080-413c-11e9-8548-ab7fbe04f038",
"id": "filebeat-*",
"name": "kibanaSavedObjectMeta.searchSourceJSON.index",
"type": "index-pattern"
}
Expand Down Expand Up @@ -344,7 +344,7 @@
},
"references": [
{
"id": "8d890080-413c-11e9-8548-ab7fbe04f038",
"id": "filebeat-*",
"name": "kibanaSavedObjectMeta.searchSourceJSON.index",
"type": "index-pattern"
}
Expand Down

0 comments on commit bffe7aa

Please sign in to comment.