From 5ecce5d60c9d89bc176daeff7eea508b63b08699 Mon Sep 17 00:00:00 2001 From: Tim Hockin Date: Tue, 25 Nov 2014 00:16:23 -0800 Subject: [PATCH] Add util to validate namespaced names --- pkg/api/validation/validation.go | 14 +------------ pkg/util/validation.go | 21 +++++++++++++++++++ pkg/util/validation_test.go | 36 ++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 13 deletions(-) diff --git a/pkg/api/validation/validation.go b/pkg/api/validation/validation.go index a4460d89c87..9eeccc7edef 100644 --- a/pkg/api/validation/validation.go +++ b/pkg/api/validation/validation.go @@ -385,19 +385,7 @@ func ValidatePodSpec(spec *api.PodSpec) errs.ValidationErrorList { func validateLabels(labels map[string]string, field string) errs.ValidationErrorList { allErrs := errs.ValidationErrorList{} for k := range labels { - var n, ns string - parts := strings.Split(k, "/") - switch len(parts) { - case 1: - n = parts[0] - case 2: - ns = parts[0] - n = parts[1] - default: - allErrs = append(allErrs, errs.NewFieldInvalid(field, k, "")) - continue - } - if (ns != "" && !util.IsDNSSubdomain(ns)) || !util.IsDNSLabel(n) { + if !util.IsQualifiedName(k) { allErrs = append(allErrs, errs.NewFieldInvalid(field, k, "")) } } diff --git a/pkg/util/validation.go b/pkg/util/validation.go index a46e6636f81..7bf1d70895c 100644 --- a/pkg/util/validation.go +++ b/pkg/util/validation.go @@ -18,6 +18,7 @@ package util import ( "regexp" + "strings" ) // IsDNSLabel tests for a string that conforms to the definition of a label in @@ -82,3 +83,23 @@ func IsCIdentifier(value string) bool { func IsValidPortNum(port int) bool { return 0 < port && port < 65536 } + +// IsQualifiedName tests whether a string fits the "optionally-namespaced +// name" pattern: [ DNS_SUBDOMAIN "/" ] DNS_LABEL +func IsQualifiedName(value string) bool { + var n, ns string + parts := strings.Split(value, "/") + switch len(parts) { + case 1: + n = parts[0] + case 2: + ns = parts[0] + n = parts[1] + default: + return false + } + if (ns != "" && !IsDNSSubdomain(ns)) || !IsDNSLabel(n) { + return false + } + return true +} diff --git a/pkg/util/validation_test.go b/pkg/util/validation_test.go index 48254c553ee..556e071af1a 100644 --- a/pkg/util/validation_test.go +++ b/pkg/util/validation_test.go @@ -153,3 +153,39 @@ func TestIsValidPortNum(t *testing.T) { } } } + +func TestIsQualifiedName(t *testing.T) { + successCases := []string{ + "simple", + "now-with-dashes", + "1-starts-with-num", + "1234", + "simple/simple", + "now-with-dashes/simple", + "now-with-dashes/now-with-dashes", + "now.with.dots/simple", + "now-with.dashes-and.dots/simple", + "1-num.2-num/3-num", + "1234/5678", + "1.2.3.4/5678", + } + for i := range successCases { + if !IsQualifiedName(successCases[i]) { + t.Errorf("case[%d] expected success", i) + } + } + + errorCases := []string{ + "NoUppercase123", + "nospecialchars%^=@", + "cantendwithadash-", + "-cantstartwithadash", + "only/one/slash", + strings.Repeat("a", 254), + } + for i := range errorCases { + if IsQualifiedName(errorCases[i]) { + t.Errorf("case[%d] expected failure", i) + } + } +}