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

fix: (dpluger) add aggregation key type checking #381

Merged
merged 8 commits into from
Feb 20, 2022
5 changes: 4 additions & 1 deletion internal/pkg/dpluger/es5client.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ func (es *es5Client) CollectPair(plugin Plugin, confFile, sidSource, esFilter, t
}
for _, lvl2Bucket := range subterm.Buckets {
sKey := lvl1Bucket.Key.(string)
nKey := int(lvl2Bucket.Key.(float64))
nKey, err := toInt(lvl2Bucket.Key)
if err != nil {
return c, fmt.Errorf("invalid sid aggregation key, %s", err.Error())
}
// fmt.Println("item1:", sKey, "item2:", nKey)
if shouldCollectCategory {
subSubTerm, found2 := lvl1Bucket.Terms("subSubTerm")
Expand Down
5 changes: 4 additions & 1 deletion internal/pkg/dpluger/es6client.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ func (es *es6Client) CollectPair(plugin Plugin, confFile, sidSource, esFilter, t
}
for _, lvl2Bucket := range subterm.Buckets {
sKey := lvl1Bucket.Key.(string)
nKey := int(lvl2Bucket.Key.(float64))
nKey, err := toInt(lvl2Bucket.Key)
if err != nil {
return c, fmt.Errorf("invalid sid aggregation key, %s", err.Error())
}
// fmt.Println("item1:", sKey, "item2:", nKey)
if shouldCollectCategory {
subSubTerm, found2 := lvl1Bucket.Terms("subSubTerm")
Expand Down
9 changes: 6 additions & 3 deletions internal/pkg/dpluger/es7client.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,11 @@ func (es *es7Client) CollectPair(plugin Plugin, confFile, sidSource, esFilter, t
}
for _, lvl2Bucket := range subterm.Buckets {
sKey := lvl1Bucket.Key.(string)
nKey := int(lvl2Bucket.Key.(float64))
// fmt.Println("item1:", sKey, "item2:", nKey)
nKey, err := toInt(lvl2Bucket.Key)
if err != nil {
return c, fmt.Errorf("invalid sid aggregation key, %s", err.Error())
}

if shouldCollectCategory {
subSubTerm, found2 := lvl1Bucket.Terms("subSubTerm")
if !found2 {
Expand Down Expand Up @@ -133,7 +136,7 @@ func (es *es7Client) Collect(plugin Plugin, confFile, sidSource, esFilter, categ
for _, v := range coll {
s := strings.Split(v, "=")
if len(s) != 2 {
err = errors.New("Cannot split the ES filter term")
err = errors.New("cannot split the ES filter term")
return
}
query = query.Must(elastic7.NewTermQuery(s[0], s[1]))
Expand Down
44 changes: 44 additions & 0 deletions internal/pkg/dpluger/util.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package dpluger

import (
"errors"
"fmt"
"math"
"reflect"
"strconv"
"strings"

"github.com/defenxor/dsiem/internal/pkg/dsiem/rule"
Expand Down Expand Up @@ -216,3 +219,44 @@ func ruleEqual(rule1, rule2 rule.DirectiveRule) []error {

return errors
}

var (
ErrIntValueExceedBoundary = errors.New("integer value exceeds maximum value boundary")
)

// toInt safely convert interface into int.
func toInt(v interface{}) (int, error) {
if v == nil {
return 0, nil
}

switch t := v.(type) {
case int:
return t, nil
case float64:
if t > 0 && t < math.MaxInt32 {
return int(t), nil
}

return 0, ErrIntValueExceedBoundary
case int64:
if t > 0 && t < math.MaxInt32 {
return int(t), nil
}

return 0, ErrIntValueExceedBoundary
case string:
n, err := strconv.ParseInt(t, 10, 64)
if err != nil {
return 0, err
}

if n > 0 && n < math.MaxInt32 {
return int(n), nil
}

return 0, ErrIntValueExceedBoundary
}

return 0, fmt.Errorf("invalid value type, %T", v)
}