name.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. Copyright 2015 The Kubernetes 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 validation
  14. import (
  15. "fmt"
  16. "strings"
  17. )
  18. // NameMayNotBe specifies strings that cannot be used as names specified as path segments (like the REST API or etcd store)
  19. var NameMayNotBe = []string{".", ".."}
  20. // NameMayNotContain specifies substrings that cannot be used in names specified as path segments (like the REST API or etcd store)
  21. var NameMayNotContain = []string{"/", "%"}
  22. // IsValidPathSegmentName validates the name can be safely encoded as a path segment
  23. func IsValidPathSegmentName(name string) []string {
  24. for _, illegalName := range NameMayNotBe {
  25. if name == illegalName {
  26. return []string{fmt.Sprintf(`may not be '%s'`, illegalName)}
  27. }
  28. }
  29. for _, illegalContent := range NameMayNotContain {
  30. if strings.Contains(name, illegalContent) {
  31. return []string{fmt.Sprintf(`may not contain '%s'`, illegalContent)}
  32. }
  33. }
  34. return nil
  35. }
  36. // IsValidPathSegmentPrefix validates the name can be used as a prefix for a name which will be encoded as a path segment
  37. // It does not check for exact matches with disallowed names, since an arbitrary suffix might make the name valid
  38. func IsValidPathSegmentPrefix(name string) []string {
  39. for _, illegalContent := range NameMayNotContain {
  40. if strings.Contains(name, illegalContent) {
  41. return []string{fmt.Sprintf(`may not contain '%s'`, illegalContent)}
  42. }
  43. }
  44. return nil
  45. }
  46. // ValidatePathSegmentName validates the name can be safely encoded as a path segment
  47. func ValidatePathSegmentName(name string, prefix bool) []string {
  48. if prefix {
  49. return IsValidPathSegmentPrefix(name)
  50. } else {
  51. return IsValidPathSegmentName(name)
  52. }
  53. }