diff options
-rw-r--r-- | src/orchestrator/pkg/infra/validation/validation.go | 24 | ||||
-rw-r--r-- | src/orchestrator/pkg/infra/validation/validation_test.go | 37 |
2 files changed, 57 insertions, 4 deletions
diff --git a/src/orchestrator/pkg/infra/validation/validation.go b/src/orchestrator/pkg/infra/validation/validation.go index d744dc3d..03b37fa1 100644 --- a/src/orchestrator/pkg/infra/validation/validation.go +++ b/src/orchestrator/pkg/infra/validation/validation.go @@ -70,21 +70,37 @@ func IsTarGz(r io.Reader) error { } func IsIpv4Cidr(cidr string) error { - _, _, err := net.ParseCIDR(cidr) - if err != nil { - return pkgerrors.Wrapf(err, "could not parse subnet %v", cidr) + ip, _, err := net.ParseCIDR(cidr) + if err != nil || ip.To4() == nil { + return pkgerrors.Wrapf(err, "could not parse ipv4 cidr %v", cidr) } return nil } -func IsIpv4(ip string) error { +func IsIp(ip string) error { addr := net.ParseIP(ip) if addr == nil { + return pkgerrors.Errorf("invalid ip address %v", ip) + } + return nil +} + +func IsIpv4(ip string) error { + addr := net.ParseIP(ip) + if addr == nil || addr.To4() == nil { return pkgerrors.Errorf("invalid ipv4 address %v", ip) } return nil } +func IsMac(mac string) error { + _, err := net.ParseMAC(mac) + if err != nil { + return pkgerrors.Errorf("invalid MAC address %v", mac) + } + return nil +} + // default name check - matches valid label value with addtion that length > 0 func IsValidName(name string) []string { var errs []string diff --git a/src/orchestrator/pkg/infra/validation/validation_test.go b/src/orchestrator/pkg/infra/validation/validation_test.go index 5109b6c7..6a7f504b 100644 --- a/src/orchestrator/pkg/infra/validation/validation_test.go +++ b/src/orchestrator/pkg/infra/validation/validation_test.go @@ -185,6 +185,43 @@ func TestIsIpv4(t *testing.T) { }) } +func TestIsMac(t *testing.T) { + t.Run("Valid MAC", func(t *testing.T) { + validmacs := []string{ + "11:22:33:44:55:66", + "ab-cd-ef-12-34-56", + "AB-CD-EF-12-34-56", + } + for _, mac := range validmacs { + err := IsMac(mac) + if err != nil { + t.Errorf("Valid MAC string failed to pass: %v", mac) + } + } + }) + + t.Run("Invalid MAC", func(t *testing.T) { + invalidmacs := []string{ + "", + "1.2.3.4.5", + "1.2.3.45/32", + "ab:cd:ef:gh:12:34", + "11:22-33-44:55:66", + "11,22,33,44,55,66", + "11|22|33|44|55|66", + "11:22:33:44:55:66:77", + "11-22-33-44-55", + "11-22-33-44-55-66-77", + } + for _, mac := range invalidmacs { + err := IsMac(mac) + if err == nil { + t.Errorf("Invalid MAC passed: %v", mac) + } + } + }) +} + func TestIsValidString(t *testing.T) { t.Run("Valid Strings", func(t *testing.T) { validStrings := []struct { |