crowdsec/cmd/crowdsec-cli/clialert/sanitize.go
mmetc bc6be99b97
cscli refact: package clialert, clidecision (#3203)
* cscli refact: package clialert, clidecision

* refact: function SanitizeScope()

* lint
2024-09-03 12:37:38 +02:00

26 lines
545 B
Go

package clialert
import (
"fmt"
"net"
"github.com/crowdsecurity/crowdsec/pkg/types"
)
// SanitizeScope validates ip and range and sets the scope accordingly to our case convention.
func SanitizeScope(scope, ip, ipRange string) (string, error) {
if ipRange != "" {
_, _, err := net.ParseCIDR(ipRange)
if err != nil {
return "", fmt.Errorf("%s is not a valid range", ipRange)
}
}
if ip != "" {
if net.ParseIP(ip) == nil {
return "", fmt.Errorf("%s is not a valid ip", ip)
}
}
return types.NormalizeScope(scope), nil
}