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

Encode the Kibana saved objects during packaging #157

Merged
merged 9 commits into from
Nov 26, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Breaking changes

* Change `requirements.kibana.version.min/max` to `requirements.kibana.versions: {semver-range}`
* Encode Kibana objects during packaging. [#157](https://github.com/elastic/integrations-registry/pull/157)

### Bugfixes

Expand Down
1 change: 1 addition & 0 deletions dev/generator/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
generator
68 changes: 68 additions & 0 deletions dev/generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,37 @@ func buildPackage(packagesBasePath string, p util.Package) error {
if err != nil {
return err
}

// Get all Kibana files
savedObjects1, err := filepath.Glob(p.GetPath() + "/dataset/*/kibana/dashboard/*")
if err != nil {
return err
}
savedObjects2, err := filepath.Glob(p.GetPath() + "/kibana/dashboard/*")
if err != nil {
return err
}
savedObjects := append(savedObjects1, savedObjects2...)

// Run each file through the saved object encoder
for _, file := range savedObjects {

data, err := ioutil.ReadFile(file)
if err != nil {
return err
}
output, err := encodedSavedObject(data)
if err != nil {
return err
}

err = ioutil.WriteFile(file, []byte(output), 0644)
if err != nil {
return err
}

}

return nil
}

Expand All @@ -152,3 +183,40 @@ func writeJsonFile(v interface{}, path string) error {

return ioutil.WriteFile(path, data, 0644)
}

var (
fieldsToEncode = []string{
"attributes.uiStateJSON",
"attributes.visState",
"attributes.optionsJSON",
"attributes.panelsJSON",
"attributes.kibanaSavedObjectMeta.searchSourceJSON",
}
)

// encodeSavedObject encodes all the fields inside a saved object
// which are stored in encoded JSON in Kibana.
// The reason is that for versioning it is much nicer to have the full
// json so only on packaging this is changed.
func encodedSavedObject(data []byte) (string, error) {

savedObject := MapStr{}
json.Unmarshal(data, &savedObject)

for _, v := range fieldsToEncode {
out, err := savedObject.GetValue(v)
// This means the key did not exists, no conversion needed
if err != nil {
continue
}

// Marshal the value to encode it properly
r, err := json.Marshal(&out)
if err != nil {
return "", err
}
savedObject.Put(v, string(r))
}

return savedObject.StringToPrint(), nil
}
58 changes: 58 additions & 0 deletions dev/generator/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"testing"

"github.com/stretchr/testify/assert"
)

var (
responseToDecode = []string{
ruflin marked this conversation as resolved.
Show resolved Hide resolved
"attributes.uiStateJSON",
"attributes.visState",
"attributes.optionsJSON",
"attributes.panelsJSON",
"attributes.kibanaSavedObjectMeta.searchSourceJSON",
}
)

// DecodeExported decodes an exported dashboard
//func EncodeKibanaAssets(result MapStr) MapStr {

func TestFoo(t *testing.T) { // Read file from json
Copy link

Choose a reason for hiding this comment

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

If I'm reading this correctly, the test ensures no errors occur when a) reading the test file b) calling encodedSavedObject.

If it doesn't already, it would be useful to test if the output of the function is what's expected; not only that it doesn't fail.

Copy link
Member Author

Choose a reason for hiding this comment

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

Agree. My plan is to have a more static test file and check the generated output to compare it. At the moment I link to an existing file which will probably not be there anymore. Will merge as is and follow up.

file := "../package-examples/auditd-2.0.4/kibana/dashboard/7de391b0-c1ca-11e7-8995-936807a28b16-ecs.json"

out, err := encodedSavedObject(file)
fmt.Println(out)
assert.NoError(t, err)
}

func encodedSavedObject(file string) (string, error) {

data, err := ioutil.ReadFile(file)
if err != nil {
return "", err
}

savedObject := MapStr{}
json.Unmarshal(data, &savedObject)

for _, v := range responseToDecode {
out, err := savedObject.GetValue(v)
// This means the key did not exists, no conversion needed
if err != nil {
continue
}

r, err := json.Marshal(&out)
if err != nil {
return "", err
}
savedObject.Put(v, string(r))
}

return savedObject.StringToPrint(), nil
}
Loading