Skip to content

Commit

Permalink
Hotfix checking ip strategy (#16)
Browse files Browse the repository at this point in the history
Co-authored-by: Sergey Kvartnikov <[email protected]>
  • Loading branch information
ownercoder and Sergey Kvartnikov authored Sep 8, 2022
1 parent c8e79f1 commit 59cf112
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
29 changes: 24 additions & 5 deletions pkg/strategies/type_ip_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,31 @@ package strategies
import (
"fmt"
"net"
"strings"
)

// TypeIPAddress is for ip-address values (eg "1.2.3.4" or "10.0.0.0/16"):
const TypeIPAddress = "IP_ADDRESS"

// parseIP get ip address from CIDR or simple ip (eg "1.2.3.4" or "10.0.0.0/16"):
func parseIP(value string) (net.IP, error) {
// Check for ip contain CIDR
isCIDR := strings.Contains(value, "/")

if isCIDR {
ip, _, err := net.ParseCIDR(value)
return ip, err
}

// Try to parse simple ip address
ip := net.ParseIP(value)
if len(ip) == 0 {
return net.IP{}, fmt.Errorf("unknown ip: %s", value)
}

return ip, nil
}

// IPAddress asserts the given parameters then passes on for evaluation:
func IPAddress(conditional string, options []interface{}, value interface{}) (bool, error) {

Expand All @@ -17,8 +37,7 @@ func IPAddress(conditional string, options []interface{}, value interface{}) (bo
return false, fmt.Errorf("Unable to assert value (%v) as string", value)
}

// The value should be a valid IP address:
ip, _, err := net.ParseCIDR(assertedValue)
ip, err := parseIP(assertedValue)
if err != nil {
return false, err
}
Expand All @@ -35,7 +54,7 @@ func IPAddress(conditional string, options []interface{}, value interface{}) (bo
}

// The string evaluations are fine for TypeIPAddress:
return evaluateString(conditional, assertedOptions, assertedValue), nil
return evaluateIPAddress(conditional, assertedOptions, assertedValue), nil
}

// evaluateIPAddress makes evaluations for TypeIPAddress values:
Expand Down Expand Up @@ -69,7 +88,7 @@ func evaluateIPAddress(conditional string, options []string, value string) bool
case ConditionalExcludes:

// Parse the value IP:
valueIP, _, err := net.ParseCIDR(value)
valueIP, err := parseIP(value)
if err != nil {
return false
}
Expand All @@ -92,7 +111,7 @@ func evaluateIPAddress(conditional string, options []string, value string) bool
case ConditionalIncludes:

// Parse the value IP:
valueIP, _, err := net.ParseCIDR(value)
valueIP, err := parseIP(value)
if err != nil {
return false
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/strategies/type_ip_address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ func TestIPAddressExcludes(t *testing.T) {
func TestIPAddressIncludes(t *testing.T) {
assert.False(t, evaluateIPAddress(ConditionalIncludes, []string{"10.2.0.0/24"}, "1.3.3.4/32"))
assert.False(t, evaluateIPAddress(ConditionalIncludes, []string{"10.0.0.0/16"}, "10.1.1.6/32"))
assert.False(t, evaluateIPAddress(ConditionalIncludes, []string{"10.0.0.0/16"}, "10.1.1.6"))
assert.True(t, evaluateIPAddress(ConditionalIncludes, []string{"10.1.0.0/16"}, "10.1.1.6/32"))
assert.True(t, evaluateIPAddress(ConditionalIncludes, []string{"10.1.0.0/16"}, "10.1.1.6"))
assert.True(t, evaluateIPAddress(ConditionalIncludes, []string{"10.1.0.0/16"}, "10.1.2.0/24"))
}

0 comments on commit 59cf112

Please sign in to comment.