Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for Issue #5227 #5461

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 73 additions & 18 deletions azurerm/helpers/validate/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@ package validate
import (
"fmt"
"net"
"regexp"
"strings"
)

func IPv6Address(i interface{}, k string) (warnings []string, errors []error) {
return validateIpv6Address(i, k, false)
// IPv4Address validation returns true if the given address is a valid IPv4 address
func IPv4Address(i interface{}, k string) (warnings []string, errors []error) {
return validateIpv4Address(i, k, false)
}

func validateIpv6Address(i interface{}, k string, allowEmpty bool) (warnings []string, errors []error) { // nolint: unparam
// IPv4AddressOrEmpty validation returns true if the given address is a valid IPv4 address or empty
func IPv4AddressOrEmpty(i interface{}, k string) (warnings []string, errors []error) {
return validateIpv4Address(i, k, true)
}

func validateIpv4Address(i interface{}, k string, allowEmpty bool) (warnings []string, errors []error) {
v, ok := i.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %q to be string", k))
Expand All @@ -22,33 +28,53 @@ func validateIpv6Address(i interface{}, k string, allowEmpty bool) (warnings []s
}

ip := net.ParseIP(v)
if six := ip.To16(); six == nil {
errors = append(errors, fmt.Errorf("%q is not a valid IPv6 address: %q", k, v))
if four := ip.To4(); four == nil {
errors = append(errors, fmt.Errorf("%q is not a valid IPv4 address: %q", k, v))
}

return warnings, errors
}

func CIDR(i interface{}, k string) (warnings []string, errors []error) {
cidr := i.(string)
// IPv6Address validation returns true if the given address is a valid IPv6 address
func IPv6Address(i interface{}, k string) (warnings []string, errors []error) {
return validateIpv6Address(i, k, false)
}

// IPv6AddressOrEmpty validation returns true if the given address is a valid IPv6 address or empty
func IPv6AddressOrEmpty(i interface{}, k string) (warnings []string, errors []error) {
return validateIpv6Address(i, k, true)
}

re := regexp.MustCompile(`^([0-9]{1,3}\.){3}[0-9]{1,3}(\/([0-9]|[1-2][0-9]|3[0-2]))?$`)
if re != nil && !re.MatchString(cidr) {
errors = append(errors, fmt.Errorf("%s must start with IPV4 address and/or slash, number of bits (0-32) as prefix. Example: 127.0.0.1/8. Got %q.", k, cidr))
func validateIpv6Address(i interface{}, k string, allowEmpty bool) (warnings []string, errors []error) { // nolint: unparam
v, ok := i.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %q to be string", k))
return
}

if v == "" && allowEmpty {
return
}

ip := net.ParseIP(v)
if six := ip.To16(); six == nil {
errors = append(errors, fmt.Errorf("%q is not a valid IPv6 address: %q", k, v))
}

return warnings, errors
}

func IPv4Address(i interface{}, k string) (warnings []string, errors []error) {
return validateIpv4Address(i, k, false)
// IPAddress validation returns true if the given address is a valid IPv4 or IPv6 address
func IPAddress(i interface{}, k string) (warnings []string, errors []error) {
return validateIPAddress(i, k, false)
}

func IPv4AddressOrEmpty(i interface{}, k string) (warnings []string, errors []error) {
return validateIpv4Address(i, k, true)
// IPAddressOrEmpty validation returns true if the given address is a valid IPv4 or IPv6 address, or empty
func IPAddressOrEmpty(i interface{}, k string) (warnings []string, errors []error) {
return validateIPAddress(i, k, true)
}

func validateIpv4Address(i interface{}, k string, allowEmpty bool) (warnings []string, errors []error) {
func validateIPAddress(i interface{}, k string, allowEmpty bool) (warnings []string, errors []error) { // nolint: unparam
v, ok := i.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %q to be string", k))
Expand All @@ -60,13 +86,42 @@ func validateIpv4Address(i interface{}, k string, allowEmpty bool) (warnings []s
}

ip := net.ParseIP(v)
if four := ip.To4(); four == nil {
errors = append(errors, fmt.Errorf("%q is not a valid IPv4 address: %q", k, v))
if ip == nil {
errors = append(errors, fmt.Errorf("%q is not a valid IPv4 or IPv6 address: %q", k, v))
}

return warnings, errors
}

// CIDR validations allow IPv4 and IPv6 CIDR notations as defined in RFC 4632 and RFC 4291
func CIDR(i interface{}, k string) (warnings []string, errors []error) {
return cIDR(i, k, false)
}

// CIDRAllowNoSuffix validations allow IPv4 and IPv6 CIDR notations as defined in RFC 4632 and RFC 4291
// Additionally invalid CIDR notations without suffix are allowed.
func CIDRAllowNoSuffix(i interface{}, k string) (warnings []string, errors []error) {
return cIDR(i, k, true)
}

func cIDR(i interface{}, k string, allowNoSuffix bool) (warnings []string, errors []error) {
cidr := i.(string)

// A CIDR without the suffix is invalid, but for compatibility reasons we need
// to be able to validate those as valid, too.
if allowNoSuffix && !strings.Contains(cidr, "/") {
cidr = cidr + "/0"
}
_, _, err := net.ParseCIDR(cidr)
if err != nil {
errors = append(errors, fmt.Errorf("%s must be a valid IPv4 or IPv6 address and slash, number of bits (0-32) as prefix. Example: 127.0.0.1/8. Got %q", k, cidr))
}

return warnings, errors
}

// MACAddress validates the input to be a valid MAC address
// Valid are addresses according to IEEE 802 MAC-48, EUI-48, EUI-64, or a 20-octet IP over InfiniBand link-layer address
func MACAddress(i interface{}, k string) (warnings []string, errors []error) {
v, ok := i.(string)
if !ok {
Expand Down
176 changes: 175 additions & 1 deletion azurerm/helpers/validate/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"
)

func TestCIDR(t *testing.T) {
func TestCIDRallowNoSuffix(t *testing.T) {
cases := []struct {
CIDR string
Errors int
Expand All @@ -30,6 +30,64 @@ func TestCIDR(t *testing.T) {
CIDR: "127.0.0.1/-1",
Errors: 1,
},
{
CIDR: "2001:db8:a0b:12f0::1/32",
Errors: 0,
},
{
CIDR: "2001:db8:a0b:12f0::1/222",
Errors: 1,
},
{
CIDR: "2001:db8:a0b:12f0::1",
Errors: 0,
},
}

for _, tc := range cases {
t.Run(tc.CIDR, func(t *testing.T) {
_, errors := CIDRAllowNoSuffix(tc.CIDR, "test")

if len(errors) != tc.Errors {
t.Fatalf("Expected CIDRAllowNoSuffix to return %d error(s) not %d", tc.Errors, len(errors))
}
})
}
}

func TestCIDR(t *testing.T) {
cases := []struct {
CIDR string
Errors int
}{
{
CIDR: "",
Errors: 1,
},
{
CIDR: "0.0.0.0",
Errors: 1,
},
{
CIDR: "127.0.0.1/8",
Errors: 0,
},
{
CIDR: "127.0.0.1/33",
Errors: 1,
},
{
CIDR: "127.0.0.1/-1",
Errors: 1,
},
{
CIDR: "2001:db8:a0b:12f0::1/32",
Errors: 0,
},
{
CIDR: "2001:db8:a0b:12f0::1/222",
Errors: 1,
},
}

for _, tc := range cases {
Expand Down Expand Up @@ -193,6 +251,122 @@ func TestIPv4AddressOrEmpty(t *testing.T) {
}
}

func TestIPAddress(t *testing.T) {
cases := []struct {
IP string
Errors int
}{
{
IP: "",
Errors: 1,
},
{
IP: "0.0.0.0",
Errors: 0,
},
{
IP: "1.2.3.no",
Errors: 1,
},
{
IP: "text",
Errors: 1,
},
{
IP: "1.2.3.4",
Errors: 0,
},
{
IP: "12.34.43.21",
Errors: 0,
},
{
IP: "100.123.199.0",
Errors: 0,
},
{
IP: "255.255.255.255",
Errors: 0,
},
{
IP: "2001:0db8:85a3:0:0:8a2e:0370:7334",
Errors: 0,
},
{
IP: "2001:0db8:85a3:0000:0000:8a2e:0370:7334",
Errors: 0,
},
}

for _, tc := range cases {
t.Run(tc.IP, func(t *testing.T) {
_, errors := IPAddress(tc.IP, "test")

if len(errors) != tc.Errors {
t.Fatalf("Expected IPAddress to return %d error(s) not %d", len(errors), tc.Errors)
}
})
}
}

func TestIPAddressOrEmpty(t *testing.T) {
cases := []struct {
IP string
Errors int
}{
{
IP: "",
Errors: 0,
},
{
IP: "0.0.0.0",
Errors: 0,
},
{
IP: "1.2.3.no",
Errors: 1,
},
{
IP: "text",
Errors: 1,
},
{
IP: "1.2.3.4",
Errors: 0,
},
{
IP: "12.34.43.21",
Errors: 0,
},
{
IP: "100.123.199.0",
Errors: 0,
},
{
IP: "255.255.255.255",
Errors: 0,
},
{
IP: "2001:0db8:85a3:0:0:8a2e:0370:7334",
Errors: 0,
},
{
IP: "2001:0db8:85a3:0000:0000:8a2e:0370:7334",
Errors: 0,
},
}

for _, tc := range cases {
t.Run(tc.IP, func(t *testing.T) {
_, errors := IPAddressOrEmpty(tc.IP, "test")

if len(errors) != tc.Errors {
t.Fatalf("Expected IPAddressOrEmpty to return %d error(s) not %d", len(errors), tc.Errors)
}
})
}
}

func TestMACAddress(t *testing.T) {
cases := []struct {
MAC string
Expand Down