Add IsIPV4()
and IsIP()
helpers (#2050)
This commit is contained in:
parent
0f5560b62a
commit
812b87ab48
|
@ -72,6 +72,8 @@ func GetExprEnv(ctx map[string]interface{}) map[string]interface{} {
|
||||||
"XMLGetNodeValue": XMLGetNodeValue,
|
"XMLGetNodeValue": XMLGetNodeValue,
|
||||||
"IpToRange": IpToRange,
|
"IpToRange": IpToRange,
|
||||||
"IsIPV6": IsIPV6,
|
"IsIPV6": IsIPV6,
|
||||||
|
"IsIPV4": IsIPV4,
|
||||||
|
"IsIP": IsIP,
|
||||||
"LookupHost": LookupHost,
|
"LookupHost": LookupHost,
|
||||||
"GetDecisionsCount": GetDecisionsCount,
|
"GetDecisionsCount": GetDecisionsCount,
|
||||||
"GetDecisionsSinceCount": GetDecisionsSinceCount,
|
"GetDecisionsSinceCount": GetDecisionsSinceCount,
|
||||||
|
@ -234,6 +236,24 @@ func IsIPV6(ip string) bool {
|
||||||
return ipParsed.To4() == nil
|
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 {
|
func IpToRange(ip string, cidr string) string {
|
||||||
cidr = strings.TrimPrefix(cidr, "/")
|
cidr = strings.TrimPrefix(cidr, "/")
|
||||||
mask, err := strconv.Atoi(cidr)
|
mask, err := strconv.Atoi(cidr)
|
||||||
|
|
|
@ -9,10 +9,10 @@ import (
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
||||||
|
"github.com/crowdsecurity/crowdsec/pkg/cstest"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/database"
|
"github.com/crowdsecurity/crowdsec/pkg/database"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/models"
|
"github.com/crowdsecurity/crowdsec/pkg/models"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cstest"
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -981,25 +981,25 @@ func TestParseUnixTime(t *testing.T) {
|
||||||
expectedErr string
|
expectedErr string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "ParseUnix() test: valid value with milli",
|
name: "ParseUnix() test: valid value with milli",
|
||||||
value: "1672239773.3590894",
|
value: "1672239773.3590894",
|
||||||
expected: time.Date(2022, 12, 28, 15, 02, 53, 0, time.UTC),
|
expected: time.Date(2022, 12, 28, 15, 02, 53, 0, time.UTC),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "ParseUnix() test: valid value without milli",
|
name: "ParseUnix() test: valid value without milli",
|
||||||
value: "1672239773",
|
value: "1672239773",
|
||||||
expected: time.Date(2022, 12, 28, 15, 02, 53, 0, time.UTC),
|
expected: time.Date(2022, 12, 28, 15, 02, 53, 0, time.UTC),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "ParseUnix() test: invalid input",
|
name: "ParseUnix() test: invalid input",
|
||||||
value: "AbcDefG!#",
|
value: "AbcDefG!#",
|
||||||
expected: time.Time{},
|
expected: time.Time{},
|
||||||
expectedErr: "unable to parse AbcDefG!# as unix timestamp",
|
expectedErr: "unable to parse AbcDefG!# as unix timestamp",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "ParseUnix() test: negative value",
|
name: "ParseUnix() test: negative value",
|
||||||
value: "-1000",
|
value: "-1000",
|
||||||
expected: time.Time{},
|
expected: time.Time{},
|
||||||
expectedErr: "unable to parse -1000 as unix timestamp",
|
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)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue