Skip to content

Commit

Permalink
Osquerybeat: Fix missing result values when the value can not be conv…
Browse files Browse the repository at this point in the history
…erted into expected type (#33587)
  • Loading branch information
aleksmaus authored and chrisberkhout committed Jun 1, 2023
1 parent 2882bf6 commit 466b95d
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 5 deletions.
11 changes: 6 additions & 5 deletions x-pack/osquerybeat/internal/osqdcli/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func resolveTypes(hits []map[string]string, colTypes map[string]string) []map[st
}

// Best effort to convert value types and replace values in the
// If conversion fails the value is kept as string
// If type conversion fails the value is preserved as string
func resolveHitTypes(hit, colTypes map[string]string) map[string]interface{} {
m := make(map[string]interface{})
for k, v := range hit {
Expand All @@ -266,25 +266,26 @@ func resolveHitTypes(hit, colTypes map[string]string) map[string]interface{} {
n, err = strconv.ParseInt(v, 10, 64)
if err == nil {
m[k] = n
continue
}
case "UNSIGNED_BIGINT":
var n uint64
n, err = strconv.ParseUint(v, 10, 64)
if err == nil {
m[k] = n
continue
}
case "DOUBLE":
var n float64
n, err = strconv.ParseFloat(v, 64)
if err == nil {
m[k] = n
continue
}
default:
m[k] = v
}
} else {
m[k] = v
}
// Keep the original string value if the value can not be converted
m[k] = v
}
return m
}
71 changes: 71 additions & 0 deletions x-pack/osquerybeat/internal/osqdcli/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package osqdcli

import (
"testing"

"github.com/google/go-cmp/cmp"
)

func TestResolveHitTypes(t *testing.T) {

tests := []struct {
name string
hit, colTypes map[string]string
res map[string]interface{}
}{
{
name: "empty",
res: map[string]interface{}{},
},
{
name: "resolvable",
hit: map[string]string{
"pid": "5551",
"pid_int": "5552",
"pid_uint": "5553",
"pid_text": "5543",
"foo": "bar",
},
colTypes: map[string]string{
"pid": "BIGINT",
"pid_int": "INTEGER",
"pid_uint": "UNSIGNED_BIGINT",
"pid_text": "TEXT",
},
res: map[string]interface{}{
"pid": int64(5551),
"pid_int": int64(5552),
"pid_uint": uint64(5553),
"pid_text": "5543",
"foo": "bar",
},
},
{
// Should preserve the field if it can not be parsed into the type
name: "wrong type",
hit: map[string]string{
"data": "0,22,137,138,29754,49154,49155",
"foo": "bar",
},
colTypes: map[string]string{"data": "BIGINT"},
res: map[string]interface{}{
"data": "0,22,137,138,29754,49154,49155",
"foo": "bar",
},
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
res := resolveHitTypes(tc.hit, tc.colTypes)
diff := cmp.Diff(tc.res, res)
if diff != "" {
t.Fatal(diff)
}
})
}
}

0 comments on commit 466b95d

Please sign in to comment.