github.com/aclisp/heapster@v0.19.2-0.20160613100040-51756f899a96/Godeps/_workspace/src/k8s.io/kubernetes/pkg/util/validation/validation.go (about)

     1  /*
     2  Copyright 2014 The Kubernetes Authors All rights reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package validation
    18  
    19  import (
    20  	"math"
    21  	"net"
    22  	"regexp"
    23  	"strings"
    24  )
    25  
    26  const qnameCharFmt string = "[A-Za-z0-9]"
    27  const qnameExtCharFmt string = "[-A-Za-z0-9_.]"
    28  const QualifiedNameFmt string = "(" + qnameCharFmt + qnameExtCharFmt + "*)?" + qnameCharFmt
    29  const QualifiedNameMaxLength int = 63
    30  
    31  var qualifiedNameRegexp = regexp.MustCompile("^" + QualifiedNameFmt + "$")
    32  
    33  func IsQualifiedName(value string) bool {
    34  	parts := strings.Split(value, "/")
    35  	var name string
    36  	switch len(parts) {
    37  	case 1:
    38  		name = parts[0]
    39  	case 2:
    40  		var prefix string
    41  		prefix, name = parts[0], parts[1]
    42  		if prefix == "" || !IsDNS1123Subdomain(prefix) {
    43  			return false
    44  		}
    45  	default:
    46  		return false
    47  	}
    48  
    49  	return name != "" && len(name) <= QualifiedNameMaxLength && qualifiedNameRegexp.MatchString(name)
    50  }
    51  
    52  const LabelValueFmt string = "(" + QualifiedNameFmt + ")?"
    53  const LabelValueMaxLength int = 63
    54  
    55  var labelValueRegexp = regexp.MustCompile("^" + LabelValueFmt + "$")
    56  
    57  func IsValidLabelValue(value string) bool {
    58  	return (len(value) <= LabelValueMaxLength && labelValueRegexp.MatchString(value))
    59  }
    60  
    61  const DNS1123LabelFmt string = "[a-z0-9]([-a-z0-9]*[a-z0-9])?"
    62  const DNS1123LabelMaxLength int = 63
    63  
    64  var dns1123LabelRegexp = regexp.MustCompile("^" + DNS1123LabelFmt + "$")
    65  
    66  // IsDNS1123Label tests for a string that conforms to the definition of a label in
    67  // DNS (RFC 1123).
    68  func IsDNS1123Label(value string) bool {
    69  	return len(value) <= DNS1123LabelMaxLength && dns1123LabelRegexp.MatchString(value)
    70  }
    71  
    72  const DNS1123SubdomainFmt string = DNS1123LabelFmt + "(\\." + DNS1123LabelFmt + ")*"
    73  const DNS1123SubdomainMaxLength int = 253
    74  
    75  var dns1123SubdomainRegexp = regexp.MustCompile("^" + DNS1123SubdomainFmt + "$")
    76  
    77  // IsDNS1123Subdomain tests for a string that conforms to the definition of a
    78  // subdomain in DNS (RFC 1123).
    79  func IsDNS1123Subdomain(value string) bool {
    80  	return len(value) <= DNS1123SubdomainMaxLength && dns1123SubdomainRegexp.MatchString(value)
    81  }
    82  
    83  const DNS952LabelFmt string = "[a-z]([-a-z0-9]*[a-z0-9])?"
    84  const DNS952LabelMaxLength int = 24
    85  
    86  var dns952LabelRegexp = regexp.MustCompile("^" + DNS952LabelFmt + "$")
    87  
    88  // IsDNS952Label tests for a string that conforms to the definition of a label in
    89  // DNS (RFC 952).
    90  func IsDNS952Label(value string) bool {
    91  	return len(value) <= DNS952LabelMaxLength && dns952LabelRegexp.MatchString(value)
    92  }
    93  
    94  const CIdentifierFmt string = "[A-Za-z_][A-Za-z0-9_]*"
    95  
    96  var cIdentifierRegexp = regexp.MustCompile("^" + CIdentifierFmt + "$")
    97  
    98  // IsCIdentifier tests for a string that conforms the definition of an identifier
    99  // in C. This checks the format, but not the length.
   100  func IsCIdentifier(value string) bool {
   101  	return cIdentifierRegexp.MatchString(value)
   102  }
   103  
   104  // IsValidPortNum tests that the argument is a valid, non-zero port number.
   105  func IsValidPortNum(port int) bool {
   106  	return 0 < port && port < 65536
   107  }
   108  
   109  // Now in libcontainer UID/GID limits is 0 ~ 1<<31 - 1
   110  // TODO: once we have a type for UID/GID we should make these that type.
   111  const (
   112  	minUserID  = 0
   113  	maxUserID  = math.MaxInt32
   114  	minGroupID = 0
   115  	maxGroupID = math.MaxInt32
   116  )
   117  
   118  // IsValidGroupId tests that the argument is a valid gids.
   119  func IsValidGroupId(gid int64) bool {
   120  	return minGroupID <= gid && gid <= maxGroupID
   121  }
   122  
   123  // IsValidUserId tests that the argument is a valid uids.
   124  func IsValidUserId(uid int64) bool {
   125  	return minUserID <= uid && uid <= maxUserID
   126  }
   127  
   128  const doubleHyphensFmt string = ".*(--).*"
   129  
   130  var doubleHyphensRegexp = regexp.MustCompile("^" + doubleHyphensFmt + "$")
   131  
   132  const IdentifierNoHyphensBeginEndFmt string = "[a-z0-9]([a-z0-9-]*[a-z0-9])*"
   133  
   134  var identifierNoHyphensBeginEndRegexp = regexp.MustCompile("^" + IdentifierNoHyphensBeginEndFmt + "$")
   135  
   136  const atLeastOneLetterFmt string = ".*[a-z].*"
   137  
   138  var atLeastOneLetterRegexp = regexp.MustCompile("^" + atLeastOneLetterFmt + "$")
   139  
   140  // IsValidPortName check that the argument is valid syntax. It must be non empty and no more than 15 characters long
   141  // It must contains at least one letter [a-z] and it must contains only [a-z0-9-].
   142  // Hypens ('-') cannot be leading or trailing character of the string and cannot be adjacent to other hyphens.
   143  // Although RFC 6335 allows upper and lower case characters but case is ignored for comparison purposes: (HTTP
   144  // and http denote the same service).
   145  func IsValidPortName(port string) bool {
   146  	if len(port) < 1 || len(port) > 15 {
   147  		return false
   148  	}
   149  	if doubleHyphensRegexp.MatchString(port) {
   150  		return false
   151  	}
   152  	if identifierNoHyphensBeginEndRegexp.MatchString(port) && atLeastOneLetterRegexp.MatchString(port) {
   153  		return true
   154  	}
   155  	return false
   156  }
   157  
   158  // IsValidIP tests that the argument is a valid IP address.
   159  func IsValidIP(value string) bool {
   160  	return net.ParseIP(value) != nil
   161  }
   162  
   163  const percentFmt string = "[0-9]+%"
   164  
   165  var percentRegexp = regexp.MustCompile("^" + percentFmt + "$")
   166  
   167  func IsValidPercent(percent string) bool {
   168  	return percentRegexp.MatchString(percent)
   169  }
   170  
   171  const HTTPHeaderNameFmt string = "[-A-Za-z0-9]+"
   172  
   173  var httpHeaderNameRegexp = regexp.MustCompile("^" + HTTPHeaderNameFmt + "$")
   174  
   175  // IsHTTPHeaderName checks that a string conforms to the Go HTTP library's
   176  // definition of a valid header field name (a stricter subset than RFC7230).
   177  func IsHTTPHeaderName(value string) bool {
   178  	return httpHeaderNameRegexp.MatchString(value)
   179  }