github.com/containerd/containerd@v22.0.0-20200918172823-438c87b8e050+incompatible/identifiers/validate.go (about) 1 /* 2 Copyright The containerd Authors. 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 identifiers provides common validation for identifiers and keys 18 // across containerd. 19 // 20 // Identifiers in containerd must be a alphanumeric, allowing limited 21 // underscores, dashes and dots. 22 // 23 // While the character set may be expanded in the future, identifiers 24 // are guaranteed to be safely used as filesystem path components. 25 package identifiers 26 27 import ( 28 "regexp" 29 30 "github.com/containerd/containerd/errdefs" 31 "github.com/pkg/errors" 32 ) 33 34 const ( 35 maxLength = 76 36 alphanum = `[A-Za-z0-9]+` 37 separators = `[._-]` 38 ) 39 40 var ( 41 // identifierRe defines the pattern for valid identifiers. 42 identifierRe = regexp.MustCompile(reAnchor(alphanum + reGroup(separators+reGroup(alphanum)) + "*")) 43 ) 44 45 // Validate returns nil if the string s is a valid identifier. 46 // 47 // identifiers are similar to the domain name rules according to RFC 1035, section 2.3.1. However 48 // rules in this package are relaxed to allow numerals to follow period (".") and mixed case is 49 // allowed. 50 // 51 // In general identifiers that pass this validation should be safe for use as filesystem path components. 52 func Validate(s string) error { 53 if len(s) == 0 { 54 return errors.Wrapf(errdefs.ErrInvalidArgument, "identifier must not be empty") 55 } 56 57 if len(s) > maxLength { 58 return errors.Wrapf(errdefs.ErrInvalidArgument, "identifier %q greater than maximum length (%d characters)", s, maxLength) 59 } 60 61 if !identifierRe.MatchString(s) { 62 return errors.Wrapf(errdefs.ErrInvalidArgument, "identifier %q must match %v", s, identifierRe) 63 } 64 return nil 65 } 66 67 func reGroup(s string) string { 68 return `(?:` + s + `)` 69 } 70 71 func reAnchor(s string) string { 72 return `^` + s + `$` 73 }