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,
|
||||
"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)
|
||||
|
|
|
@ -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"
|
||||
|
@ -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