From da73d01eee690c28a050d16591717699f4aaf169 Mon Sep 17 00:00:00 2001 From: mtojek Date: Tue, 20 Oct 2020 17:36:44 +0200 Subject: [PATCH 1/4] Prepend module names to imported Kibana dashboards --- dev/import-beats/kibana.go | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/dev/import-beats/kibana.go b/dev/import-beats/kibana.go index d38eb06777e..0945d762490 100644 --- a/dev/import-beats/kibana.go +++ b/dev/import-beats/kibana.go @@ -234,29 +234,41 @@ func convertToKibanaObjects(dashboardFile []byte, moduleName string, dataStreamN return nil, errors.Wrapf(err, "retrieving type failed") } + id, err := object.getValue("id") + if err != nil { + return nil, errors.Wrapf(err, "retrieving id failed") + } + + dashboardName := id.(string) + if aType == "dashboard" && !strings.HasPrefix(dashboardName, moduleName + "-") { + dashboardName = moduleName + "-" + dashboardName + } + + _, err = object.put("id", dashboardName) + if err != nil { + return nil, errors.Wrapf(err, "putting new ID failed") + } + data, err := json.MarshalIndent(object, "", " ") if err != nil { return nil, errors.Wrapf(err, "marshalling object failed") } - data = replaceBlacklistedWords( - replaceFieldEventDataStreamWithStreamDataStream( - data)) + data = prependModuleNameToDashboard(replaceBlacklistedWords( + replaceFieldEventDatasetWithDataStreamDataset( + data)), moduleName) err = verifyKibanaObjectConvertion(data) if err != nil { return nil, errors.Wrapf(err, "Kibana object convertion failed") } - id, err := object.getValue("id") - if err != nil { - return nil, errors.Wrapf(err, "retrieving id failed") - } if _, ok := extracted[aType.(string)]; !ok { extracted[aType.(string)] = map[string][]byte{} } - extracted[aType.(string)][id.(string)+".json"] = data + + extracted[aType.(string)][dashboardName + ".json"] = data } return extracted, nil @@ -449,7 +461,7 @@ func stripReferencesToEventModuleInQuery(object mapStr, objectKey, moduleName st return object, nil } -func replaceFieldEventDataStreamWithStreamDataStream(data []byte) []byte { +func replaceFieldEventDatasetWithDataStreamDataset(data []byte) []byte { return bytes.ReplaceAll(data, []byte("event.dataset"), []byte("data_stream.dataset")) } @@ -463,6 +475,12 @@ func replaceBlacklistedWords(data []byte) []byte { return data } +func prependModuleNameToDashboard(data []byte, moduleName string) []byte { + data = bytes.ReplaceAll(data, []byte("/app/kibana#/dashboard/" + moduleName + "-"), []byte("/app/kibana#/dashboard/")) + data = bytes.ReplaceAll(data, []byte("/app/kibana#/dashboard/"), []byte("/app/kibana#/dashboard/" + moduleName + "-")) + return data +} + func verifyKibanaObjectConvertion(data []byte) error { i := bytes.Index(data, []byte("event.module")) if i > 0 { From cf869a4e990f7314c1897c03c648ab2c88b2ab87 Mon Sep 17 00:00:00 2001 From: mtojek Date: Tue, 20 Oct 2020 17:40:01 +0200 Subject: [PATCH 2/4] Fix: bug, sample event may not be defined --- dev/import-beats/packages.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/dev/import-beats/packages.go b/dev/import-beats/packages.go index 67f8190026c..14e4d50cf81 100644 --- a/dev/import-beats/packages.go +++ b/dev/import-beats/packages.go @@ -428,6 +428,9 @@ func writeDoc(docsPath string, doc docContent, aPackage packageContent) error { t = template.Must(t.Parse("TODO")) } else { t, err = t.Funcs(template.FuncMap{ + "event": func(dataStream string) (string, error) { + return "TODO", nil + }, "fields": func(dataStream string) (string, error) { return renderExportedFields(dataStream, aPackage.dataStreams) }, From b3bf5444cb41a00caeab1e506da28309e050666a Mon Sep 17 00:00:00 2001 From: mtojek Date: Tue, 20 Oct 2020 17:40:22 +0200 Subject: [PATCH 3/4] Fix: mage format --- dev/import-beats/kibana.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/dev/import-beats/kibana.go b/dev/import-beats/kibana.go index 0945d762490..ea9f9a817ea 100644 --- a/dev/import-beats/kibana.go +++ b/dev/import-beats/kibana.go @@ -240,7 +240,7 @@ func convertToKibanaObjects(dashboardFile []byte, moduleName string, dataStreamN } dashboardName := id.(string) - if aType == "dashboard" && !strings.HasPrefix(dashboardName, moduleName + "-") { + if aType == "dashboard" && !strings.HasPrefix(dashboardName, moduleName+"-") { dashboardName = moduleName + "-" + dashboardName } @@ -263,12 +263,11 @@ func convertToKibanaObjects(dashboardFile []byte, moduleName string, dataStreamN return nil, errors.Wrapf(err, "Kibana object convertion failed") } - if _, ok := extracted[aType.(string)]; !ok { extracted[aType.(string)] = map[string][]byte{} } - extracted[aType.(string)][dashboardName + ".json"] = data + extracted[aType.(string)][dashboardName+".json"] = data } return extracted, nil @@ -476,8 +475,8 @@ func replaceBlacklistedWords(data []byte) []byte { } func prependModuleNameToDashboard(data []byte, moduleName string) []byte { - data = bytes.ReplaceAll(data, []byte("/app/kibana#/dashboard/" + moduleName + "-"), []byte("/app/kibana#/dashboard/")) - data = bytes.ReplaceAll(data, []byte("/app/kibana#/dashboard/"), []byte("/app/kibana#/dashboard/" + moduleName + "-")) + data = bytes.ReplaceAll(data, []byte("/app/kibana#/dashboard/"+moduleName+"-"), []byte("/app/kibana#/dashboard/")) + data = bytes.ReplaceAll(data, []byte("/app/kibana#/dashboard/"), []byte("/app/kibana#/dashboard/"+moduleName+"-")) return data } From b1de4e867d1bab35605890d919d08207e6f3d4eb Mon Sep 17 00:00:00 2001 From: mtojek Date: Tue, 20 Oct 2020 18:00:19 +0200 Subject: [PATCH 4/4] Address PR comments --- dev/import-beats/kibana.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dev/import-beats/kibana.go b/dev/import-beats/kibana.go index ea9f9a817ea..3dac33318b3 100644 --- a/dev/import-beats/kibana.go +++ b/dev/import-beats/kibana.go @@ -254,7 +254,7 @@ func convertToKibanaObjects(dashboardFile []byte, moduleName string, dataStreamN return nil, errors.Wrapf(err, "marshalling object failed") } - data = prependModuleNameToDashboard(replaceBlacklistedWords( + data = prependModuleNameToDashboardLinks(replaceBlacklistedWords( replaceFieldEventDatasetWithDataStreamDataset( data)), moduleName) @@ -474,7 +474,7 @@ func replaceBlacklistedWords(data []byte) []byte { return data } -func prependModuleNameToDashboard(data []byte, moduleName string) []byte { +func prependModuleNameToDashboardLinks(data []byte, moduleName string) []byte { data = bytes.ReplaceAll(data, []byte("/app/kibana#/dashboard/"+moduleName+"-"), []byte("/app/kibana#/dashboard/")) data = bytes.ReplaceAll(data, []byte("/app/kibana#/dashboard/"), []byte("/app/kibana#/dashboard/"+moduleName+"-")) return data