diff options
author | Eric Multanen <eric.w.multanen@intel.com> | 2020-04-08 12:01:24 -0700 |
---|---|---|
committer | Eric Multanen <eric.w.multanen@intel.com> | 2020-04-08 12:01:24 -0700 |
commit | eafe59ee894f6bb42d090a2aad42323e67243b4f (patch) | |
tree | 4cf2c8d1ecb7e0b06c6f5949d3b8b9ae86a857ca /src/orchestrator/pkg/infra/validation | |
parent | ee5748eca6350222051125b5e2313f78da00efbe (diff) |
Add validation routines
Splitting out new orchestration validation routines
from other patch for ncm which uses them - so these
changes can go in first. Should fix build issues.
Issue-ID: MULTICLOUD-1029
Signed-off-by: Eric Multanen <eric.w.multanen@intel.com>
Change-Id: I89c4163e1204d444e48a4786acececa20fce1844
Diffstat (limited to 'src/orchestrator/pkg/infra/validation')
-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 { |