From b33ba277bf369497ce8318bb335fff4f594c3124 Mon Sep 17 00:00:00 2001 From: bui Date: Thu, 20 Jul 2023 17:10:01 +0200 Subject: [PATCH] add flatten to manipulate arrays of arrays --- pkg/exprhelpers/expr_lib.go | 5 +++++ pkg/exprhelpers/helpers.go | 23 ++++++++++++++++++++++- pkg/types/constants.go | 2 +- pkg/types/event.go | 1 + 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/pkg/exprhelpers/expr_lib.go b/pkg/exprhelpers/expr_lib.go index f4e1f4722..7880233a3 100644 --- a/pkg/exprhelpers/expr_lib.go +++ b/pkg/exprhelpers/expr_lib.go @@ -20,6 +20,11 @@ var exprFuncs = []exprCustomFunc{ new(func(string) (*cticlient.SmokeItem, error)), }, }, + { + name: "Flatten", + function: Flatten, + signature: []interface{}{}, + }, { name: "Distance", function: Distance, diff --git a/pkg/exprhelpers/helpers.go b/pkg/exprhelpers/helpers.go index a5f45c4b0..4122aad84 100644 --- a/pkg/exprhelpers/helpers.go +++ b/pkg/exprhelpers/helpers.go @@ -8,6 +8,7 @@ import ( "net/url" "os" "path" + "reflect" "regexp" "strconv" "strings" @@ -170,7 +171,27 @@ func FileInit(fileFolder string, filename string, fileType string) error { return nil } -//Expr helpers +// Expr helpers + +func Flatten(params ...any) (any, error) { + return flatten(nil, reflect.ValueOf(params)), nil +} + +func flatten(args []interface{}, v reflect.Value) []interface{} { + if v.Kind() == reflect.Interface { + v = v.Elem() + } + + if v.Kind() == reflect.Array || v.Kind() == reflect.Slice { + for i := 0; i < v.Len(); i++ { + args = flatten(args, v.Index(i)) + } + } else { + args = append(args, v.Interface()) + } + + return args +} // func Get(arr []string, index int) string { func Get(params ...any) (any, error) { diff --git a/pkg/types/constants.go b/pkg/types/constants.go index fa50b64f3..c82e9a06c 100644 --- a/pkg/types/constants.go +++ b/pkg/types/constants.go @@ -9,7 +9,7 @@ const PAPIVersion = "v1" const PAPIPollUrl = "/decisions/stream/poll" const PAPIPermissionsUrl = "/permissions" -const CAPIBaseURL = "https://api.crowdsec.net/" +const CAPIBaseURL = "https://api.dev.crowdsec.net/" const CscliOrigin = "cscli" const CrowdSecOrigin = "crowdsec" diff --git a/pkg/types/event.go b/pkg/types/event.go index a0741c660..b51d12916 100644 --- a/pkg/types/event.go +++ b/pkg/types/event.go @@ -155,6 +155,7 @@ func (w WaapEvent) ByTagRx(rx string) WaapEvent { } for _, rule := range w { for _, tag := range rule["tags"].([]string) { + log.Infof("ByTagRx: %s = %s -> %t", rx, tag, re.MatchString(tag)) if re.MatchString(tag) { waap = append(waap, rule) break