From 1e1741aa45788169760c743c45653af2e7fd6048 Mon Sep 17 00:00:00 2001 From: AlteredCoder <64792091+AlteredCoder@users.noreply.github.com> Date: Thu, 19 May 2022 16:28:25 +0200 Subject: [PATCH] Allow to set static to a pointer and add IsIPV6 helper (#1540) * Allow to set static to a pointer and add IsIPV6 helper --- pkg/exprhelpers/exprlib.go | 12 ++++++++++++ pkg/parser/runtime.go | 11 ++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/pkg/exprhelpers/exprlib.go b/pkg/exprhelpers/exprlib.go index c16e11f1d..0c22cf33f 100644 --- a/pkg/exprhelpers/exprlib.go +++ b/pkg/exprhelpers/exprlib.go @@ -58,6 +58,7 @@ func GetExprEnv(ctx map[string]interface{}) map[string]interface{} { "XMLGetAttributeValue": XMLGetAttributeValue, "XMLGetNodeValue": XMLGetNodeValue, "IpToRange": IpToRange, + "IsIPV6": IsIPV6, } for k, v := range ctx { ExprLib[k] = v @@ -180,6 +181,17 @@ func IpInRange(ip string, ipRange string) bool { return false } +func IsIPV6(ip string) bool { + ipParsed := net.ParseIP(ip) + if ipParsed == nil { + log.Debugf("'%s' is not a valid IP", ip) + return false + } + + // If it's a valid IP and can't be converted to IPv4 then it is an IPv6 + return ipParsed.To4() == nil +} + func IpToRange(ip string, cidr string) string { cidr = strings.TrimPrefix(cidr, "/") mask, err := strconv.Atoi(cidr) diff --git a/pkg/parser/runtime.go b/pkg/parser/runtime.go index da50da909..620cb0195 100644 --- a/pkg/parser/runtime.go +++ b/pkg/parser/runtime.go @@ -17,7 +17,6 @@ import ( "strconv" - "github.com/davecgh/go-spew/spew" "github.com/mohae/deepcopy" "github.com/prometheus/client_golang/prometheus" log "github.com/sirupsen/logrus" @@ -38,7 +37,7 @@ func SetTargetByName(target string, value string, evt *types.Event) bool { log.Debugf("setting target %s to %s", target, value) defer func() { if r := recover(); r != nil { - log.Errorf("Runtime error while trying to set '%s' in %s : %+v", target, spew.Sdump(evt), r) + log.Errorf("Runtime error while trying to set '%s': %+v", target, r) return } }() @@ -65,11 +64,17 @@ func SetTargetByName(target string, value string, evt *types.Event) bool { case reflect.Struct: tmp := iter.FieldByName(f) if !tmp.IsValid() { - log.Debugf("%s IsValid false", f) + log.Debugf("'%s' is not a valid target because '%s' is not valid", target, f) return false } + if tmp.Kind() == reflect.Ptr { + tmp = reflect.Indirect(tmp) + } iter = tmp break + case reflect.Ptr: + tmp := iter.Elem() + iter = reflect.Indirect(tmp.FieldByName(f)) default: log.Errorf("unexpected type %s in '%s'", iter.Kind(), target) return false