github.com/google/cadvisor@v0.49.1/container/containerd/identifiers/validate.go (about) 1 // Copyright 2017 Google Inc. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 /* 15 Copyright The containerd Authors. 16 17 Licensed under the Apache License, Version 2.0 (the "License"); 18 you may not use this file except in compliance with the License. 19 You may obtain a copy of the License at 20 21 http://www.apache.org/licenses/LICENSE-2.0 22 23 Unless required by applicable law or agreed to in writing, software 24 distributed under the License is distributed on an "AS IS" BASIS, 25 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 26 See the License for the specific language governing permissions and 27 limitations under the License. 28 */ 29 30 // Package identifiers provides common validation for identifiers and keys 31 // across containerd. 32 // 33 // Identifiers in containerd must be a alphanumeric, allowing limited 34 // underscores, dashes and dots. 35 // 36 // While the character set may be expanded in the future, identifiers 37 // are guaranteed to be safely used as filesystem path components. 38 package identifiers 39 40 import ( 41 "regexp" 42 43 "github.com/google/cadvisor/container/containerd/errdefs" 44 "github.com/pkg/errors" 45 ) 46 47 const ( 48 maxLength = 76 49 alphanum = `[A-Za-z0-9]+` 50 separators = `[._-]` 51 ) 52 53 var ( 54 // identifierRe defines the pattern for valid identifiers. 55 identifierRe = regexp.MustCompile(reAnchor(alphanum + reGroup(separators+reGroup(alphanum)) + "*")) 56 ) 57 58 // Validate returns nil if the string s is a valid identifier. 59 // 60 // identifiers are similar to the domain name rules according to RFC 1035, section 2.3.1. However 61 // rules in this package are relaxed to allow numerals to follow period (".") and mixed case is 62 // allowed. 63 // 64 // In general identifiers that pass this validation should be safe for use as filesystem path components. 65 func Validate(s string) error { 66 if len(s) == 0 { 67 return errors.Wrapf(errdefs.ErrInvalidArgument, "identifier must not be empty") 68 } 69 70 if len(s) > maxLength { 71 return errors.Wrapf(errdefs.ErrInvalidArgument, "identifier %q greater than maximum length (%d characters)", s, maxLength) 72 } 73 74 if !identifierRe.MatchString(s) { 75 return errors.Wrapf(errdefs.ErrInvalidArgument, "identifier %q must match %v", s, identifierRe) 76 } 77 return nil 78 } 79 80 func reGroup(s string) string { 81 return `(?:` + s + `)` 82 } 83 84 func reAnchor(s string) string { 85 return `^` + s + `$` 86 }