From 812b87ab487518075fdd43a885ef6deea8cd4746 Mon Sep 17 00:00:00 2001 From: blotus Date: Fri, 10 Feb 2023 14:44:42 +0100 Subject: [PATCH] Add `IsIPV4()` and `IsIP()` helpers (#2050) --- pkg/exprhelpers/exprlib.go | 20 +++++++ pkg/exprhelpers/exprlib_test.go | 93 +++++++++++++++++++++++++++++---- 2 files changed, 102 insertions(+), 11 deletions(-) diff --git a/pkg/exprhelpers/exprlib.go b/pkg/exprhelpers/exprlib.go index db61ef4b6..b830507af 100644 --- a/pkg/exprhelpers/exprlib.go +++ b/pkg/exprhelpers/exprlib.go @@ -72,6 +72,8 @@ func GetExprEnv(ctx map[string]interface{}) map[string]interface{} { "XMLGetNodeValue": XMLGetNodeValue, "IpToRange": IpToRange, "IsIPV6": IsIPV6, + "IsIPV4": IsIPV4, + "IsIP": IsIP, "LookupHost": LookupHost, "GetDecisionsCount": GetDecisionsCount, "GetDecisionsSinceCount": GetDecisionsSinceCount, @@ -234,6 +236,24 @@ func IsIPV6(ip string) bool { return ipParsed.To4() == nil } +func IsIPV4(ip string) bool { + ipParsed := net.ParseIP(ip) + if ipParsed == nil { + log.Debugf("'%s' is not a valid IP", ip) + return false + } + return ipParsed.To4() != nil +} + +func IsIP(ip string) bool { + ipParsed := net.ParseIP(ip) + if ipParsed == nil { + log.Debugf("'%s' is not a valid IP", ip) + return false + } + return true +} + func IpToRange(ip string, cidr string) string { cidr = strings.TrimPrefix(cidr, "/") mask, err := strconv.Atoi(cidr) diff --git a/pkg/exprhelpers/exprlib_test.go b/pkg/exprhelpers/exprlib_test.go index 97a274695..2f7f9de4e 100644 --- a/pkg/exprhelpers/exprlib_test.go +++ b/pkg/exprhelpers/exprlib_test.go @@ -9,10 +9,10 @@ import ( "github.com/pkg/errors" "github.com/crowdsecurity/crowdsec/pkg/csconfig" + "github.com/crowdsecurity/crowdsec/pkg/cstest" "github.com/crowdsecurity/crowdsec/pkg/database" "github.com/crowdsecurity/crowdsec/pkg/models" "github.com/crowdsecurity/crowdsec/pkg/types" - "github.com/crowdsecurity/crowdsec/pkg/cstest" log "github.com/sirupsen/logrus" "testing" @@ -981,25 +981,25 @@ func TestParseUnixTime(t *testing.T) { expectedErr string }{ { - name: "ParseUnix() test: valid value with milli", - value: "1672239773.3590894", + name: "ParseUnix() test: valid value with milli", + value: "1672239773.3590894", expected: time.Date(2022, 12, 28, 15, 02, 53, 0, time.UTC), }, { - name: "ParseUnix() test: valid value without milli", - value: "1672239773", + name: "ParseUnix() test: valid value without milli", + value: "1672239773", expected: time.Date(2022, 12, 28, 15, 02, 53, 0, time.UTC), }, { - name: "ParseUnix() test: invalid input", - value: "AbcDefG!#", - expected: time.Time{}, + name: "ParseUnix() test: invalid input", + value: "AbcDefG!#", + expected: time.Time{}, expectedErr: "unable to parse AbcDefG!# as unix timestamp", }, { - name: "ParseUnix() test: negative value", - value: "-1000", - expected: time.Time{}, + name: "ParseUnix() test: negative value", + value: "-1000", + expected: time.Time{}, expectedErr: "unable to parse -1000 as unix timestamp", }, } @@ -1016,3 +1016,74 @@ func TestParseUnixTime(t *testing.T) { }) } } + +func TestIsIp(t *testing.T) { + tests := []struct { + name string + method func(string) bool + value string + expected bool + }{ + { + name: "IsIPV4() test: valid IPv4", + method: IsIPV4, + value: "1.2.3.4", + expected: true, + }, + { + name: "IsIPV6() test: valid IPv6", + method: IsIPV6, + value: "1.2.3.4", + expected: false, + }, + { + name: "IsIPV6() test: valid IPv6", + method: IsIPV6, + value: "2001:0db8:85a3:0000:0000:8a2e:0370:7334", + expected: true, + }, + { + name: "IsIPV4() test: valid IPv6", + method: IsIPV4, + value: "2001:0db8:85a3:0000:0000:8a2e:0370:7334", + expected: false, + }, + { + name: "IsIP() test: invalid IP", + method: IsIP, + value: "foo.bar", + expected: false, + }, + { + name: "IsIP() test: valid IPv4", + method: IsIP, + value: "1.2.3.4", + expected: true, + }, + { + name: "IsIP() test: valid IPv6", + method: IsIP, + value: "2001:0db8:85a3:0000:0000:8a2e:0370:7334", + expected: true, + }, + { + name: "IsIPV4() test: invalid IPv4", + method: IsIPV4, + value: "foo.bar", + expected: false, + }, + { + name: "IsIPV6() test: invalid IPv6", + method: IsIPV6, + value: "foo.bar", + expected: false, + }, + } + for _, tc := range tests { + tc := tc + t.Run(tc.name, func(t *testing.T) { + output := tc.method(tc.value) + require.Equal(t, tc.expected, output) + }) + } +}