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 fields in Kibana objects if not encoded #506

Merged
merged 3 commits into from
Jun 10, 2020
Merged
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.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Generate package index.json files. [#479](https://github.com/elastic/package-registry/pull/479)
* Add validation for dataset type. [#501](https://github.com/elastic/package-registry/pull/501)
* Add -dry-run flag. [#503](https://github.com/elastic/package-registry/pull/503)
* Encode fields in Kibana objects if not encoded. [#506](https://github.com/elastic/package-registry/pull/506)

### Deprecated

Expand Down
31 changes: 17 additions & 14 deletions dev/generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,16 @@ func buildPackage(packagesBasePath string, p util.Package) error {
if err != nil {
return err
}
output, err := encodedSavedObject(data)
output, changed, err := encodedSavedObject(data)
if err != nil {
return err
}

err = ioutil.WriteFile(file, []byte(output), 0644)
if err != nil {
return err
if changed {
err = ioutil.WriteFile(file, output, 0644)
if err != nil {
return err
}
}
}
return nil
Expand Down Expand Up @@ -309,38 +311,39 @@ var (
// 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) {
func encodedSavedObject(data []byte) ([]byte, bool, error) {
savedObject := MapStr{}
err := json.Unmarshal(data, &savedObject)
if err != nil {
return "", errors.Wrapf(err, "unmarshalling saved object failed")
return nil, false, errors.Wrapf(err, "unmarshalling saved object failed")
}

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

// It may happen that some objects existing in example directory might be already encoded.
// In this case skip the encoding.
// In this case skip encoding the field and move to the next one.
_, isString := out.(string)
if isString {
return "", fmt.Errorf("expect non-string field type (fieldName: %s)", v)
continue
}

// Marshal the value to encode it properly
// Marshal the value to encode it properly.
r, err := json.Marshal(&out)
if err != nil {
return "", err
return nil, false, err
}
_, err = savedObject.Put(v, string(r))
if err != nil {
return "", errors.Wrapf(err, "can't put value to the saved object")
return nil, false, errors.Wrapf(err, "can't put value to the saved object")
}

changed = true
}

return savedObject.StringToPrint(), nil
return []byte(savedObject.StringToPrint()), changed, nil
}
3 changes: 2 additions & 1 deletion dev/generator/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func TestEncodeSavedObject(t *testing.T) {
data, err := ioutil.ReadFile(file)
assert.NoError(t, err)

_, err = encodedSavedObject(data)
_, changed, err := encodedSavedObject(data)
assert.NoError(t, err)
assert.True(t, changed)
}
2 changes: 1 addition & 1 deletion docs/api/example-0.0.2.tar.gz-preview.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
0 example-0.0.2/kibana/index-pattern/
91348 example-0.0.2/kibana/index-pattern/filebeat.json
0 example-0.0.2/kibana/infrastructure-ui-source/
797 example-0.0.2/kibana/infrastructure-ui-source/default.json
1013 example-0.0.2/kibana/infrastructure-ui-source/default.json
0 example-0.0.2/kibana/visualization/
1863 example-0.0.2/kibana/visualization/0a994af0-5c9d-11e9-8477-077ec9664dbd.json
1982 example-0.0.2/kibana/visualization/36f872a0-5c03-11e9-85b4-19d0072eb4f2.json
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
{
"infrastructure-ui-source": {
"description": "",
"fields": {
"container": "container.id",
"host": "host.name",
"pod": "kubernetes.pod.uid",
"tiebreaker": "_doc",
"timestamp": "@timestamp"
"infrastructure-ui-source" : {
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder why it reordered this one 🤔

Copy link
Contributor Author

@mtojek mtojek Jun 10, 2020

Choose a reason for hiding this comment

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

Accidentally cleaned up the source here. There are no differences in the content.

"name" : "Default",
"description" : "",
"metricAlias" : "example-*",
"logAlias" : "example-*",
"fields" : {
"container" : "container.id",
"host" : "host.name",
"pod" : "kubernetes.pod.uid",
"tiebreaker" : "_doc",
"timestamp" : "@timestamp"
},
"logColumns" : [
{
"timestampColumn" : {
"id" : "5e7f964a-be8a-40d8-88d2-fbcfbdca0e2f"
}
},
{
"fieldColumn" : {
"id" : " eb9777a8-fcd3-420e-ba7d-172fff6da7a2",
"field" : "event.dataset"
}
},
{
"messageColumn" : {
"id" : "b645d6da-824b-4723-9a2a-e8cece1645c0"
}
}
]
},
"logAlias": "example-*",
"logColumns": [
{
"timestampColumn": {
"id": "5e7f964a-be8a-40d8-88d2-fbcfbdca0e2f"
}
},
{
"fieldColumn": {
"field": "event.dataset",
"id": " eb9777a8-fcd3-420e-ba7d-172fff6da7a2"
}
},
{
"messageColumn": {
"id": "b645d6da-824b-4723-9a2a-e8cece1645c0"
}
}
],
"metricAlias": "example-*",
"name": "Default"
},
"type": "infrastructure-ui-source",
"updated_at": "2019-09-05T09:42:51.592Z"
}
"type" : "infrastructure-ui-source",
"updated_at" : "2019-09-05T09:42:51.592Z"
}