Loosen label and annotation validation and related tests

This commit is contained in:
gmarek
2015-02-27 16:08:02 +01:00
parent 54b2b47caa
commit 726a5af075
6 changed files with 110 additions and 115 deletions

View File

@@ -18,19 +18,34 @@ package util
import (
"regexp"
"strings"
)
// IsDNSLabel tests for a string that conforms to the definition of a label in
// DNS (RFC 1123).
func IsDNSLabel(value string) bool {
return IsDNS1123Label(value)
const LabelValueFmt string = "[A-Za-z0-9_\\-\\.,]*"
var labelValueRegexp = regexp.MustCompile("^" + LabelValueFmt + "$")
const LabelValueMaxLength int = 63
func IsValidLabelValue(value string) bool {
return (len(value) <= LabelValueMaxLength && labelValueRegexp.MatchString(value))
}
// IsDNSSubdomain tests for a string that conforms to the definition of a
// subdomain in DNS (RFC 1123).
func IsDNSSubdomain(value string) bool {
return IsDNS1123Subdomain(value)
const AnnotationValueFmt string = ".*"
var annotationValueRegexp = regexp.MustCompile("^" + AnnotationValueFmt + "$")
const TotalAnnotationSizeB int = 64 * (1 << 10) // 64 kB
func IsValidAnnotationValue(value string) bool {
return annotationValueRegexp.MatchString(value)
}
const QualifiedNameFmt string = "([A-Za-z0-9_\\-\\.]+/)?[A-Za-z0-9_\\-\\.]*[A-Za-z0-9]"
var qualifiedNameRegexp = regexp.MustCompile("^" + QualifiedNameFmt + "$")
func IsQualifiedName(value string) bool {
return (len(value) <= DNS1123SubdomainMaxLength && qualifiedNameRegexp.MatchString(value))
}
const DNS1123LabelFmt string = "[a-z0-9]([-a-z0-9]*[a-z0-9])?"
@@ -57,6 +72,18 @@ func IsDNS1123Subdomain(value string) bool {
return len(value) <= DNS1123SubdomainMaxLength && dns1123SubdomainRegexp.MatchString(value)
}
// IsDNSLabel tests for a string that conforms to the definition of a label in
// DNS (RFC 1123).
func IsDNSLabel(value string) bool {
return IsDNS1123Label(value)
}
// IsDNSSubdomain tests for a string that conforms to the definition of a
// subdomain in DNS (RFC 1123).
func IsDNSSubdomain(value string) bool {
return IsDNS1123Subdomain(value)
}
const DNS952LabelFmt string = "[a-z]([-a-z0-9]*[a-z0-9])?"
var dns952LabelRegexp = regexp.MustCompile("^" + DNS952LabelFmt + "$")
@@ -83,33 +110,3 @@ 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
}
const LabelValueFmt string = "([A-Za-z0-9_\\-\\\\.]*)"
var labelValueRegexp = regexp.MustCompile("^" + LabelValueFmt + "$")
const labelValueMaxLength int = 63
func IsValidLabelValue(value string) bool {
return (len(value) <= labelValueMaxLength && labelValueRegexp.MatchString(value))
}