validate.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. Copyright The containerd Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // Package namespaces provides tools for working with namespaces across
  14. // containerd.
  15. //
  16. // Namespaces collect resources such as containers and images, into a unique
  17. // identifier space. This means that two applications can use the same
  18. // identifiers and not conflict while using containerd.
  19. //
  20. // This package can be used to ensure that client and server functions
  21. // correctly store the namespace on the context.
  22. package namespaces
  23. import (
  24. "regexp"
  25. "github.com/containerd/containerd/errdefs"
  26. "github.com/pkg/errors"
  27. )
  28. const (
  29. maxLength = 76
  30. alpha = `[A-Za-z]`
  31. alphanum = `[A-Za-z0-9]+`
  32. label = alpha + alphanum + `(:?[-]+` + alpha + alphanum + `)*`
  33. )
  34. var (
  35. // namespaceRe validates that a namespace matches valid identifiers.
  36. //
  37. // Rules for domains, defined in RFC 1035, section 2.3.1, are used for
  38. // namespaces.
  39. namespaceRe = regexp.MustCompile(reAnchor(label + reGroup("[.]"+reGroup(label)) + "*"))
  40. )
  41. // Validate returns nil if the string s is a valid namespace.
  42. //
  43. // To allow such namespace identifiers to be used across various contexts
  44. // safely, the character set has been restricted to that defined for domains in
  45. // RFC 1035, section 2.3.1. This will make namespace identifiers safe for use
  46. // across networks, filesystems and other media.
  47. //
  48. // The identifier specification departs from RFC 1035 in that it allows
  49. // "labels" to start with number and only enforces a total length restriction
  50. // of 76 characters.
  51. //
  52. // While the character set may be expanded in the future, namespace identifiers
  53. // are guaranteed to be safely used as filesystem path components.
  54. //
  55. // For the most part, this doesn't need to be called directly when using the
  56. // context-oriented functions.
  57. func Validate(s string) error {
  58. if len(s) > maxLength {
  59. return errors.Wrapf(errdefs.ErrInvalidArgument, "namespace %q greater than maximum length (%d characters)", s, maxLength)
  60. }
  61. if !namespaceRe.MatchString(s) {
  62. return errors.Wrapf(errdefs.ErrInvalidArgument, "namespace %q must match %v", s, namespaceRe)
  63. }
  64. return nil
  65. }
  66. func reGroup(s string) string {
  67. return `(?:` + s + `)`
  68. }
  69. func reAnchor(s string) string {
  70. return `^` + s + `$`
  71. }