blob: a50cd089dfb390563e4df0eed78919cdaac82ba2 [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001/*
2Copyright 2015 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package path
18
19import (
20 "fmt"
21 "strings"
22)
23
24// NameMayNotBe specifies strings that cannot be used as names specified as path segments (like the REST API or etcd store)
25var NameMayNotBe = []string{".", ".."}
26
27// NameMayNotContain specifies substrings that cannot be used in names specified as path segments (like the REST API or etcd store)
28var NameMayNotContain = []string{"/", "%"}
29
30// IsValidPathSegmentName validates the name can be safely encoded as a path segment
31func IsValidPathSegmentName(name string) []string {
32 for _, illegalName := range NameMayNotBe {
33 if name == illegalName {
34 return []string{fmt.Sprintf(`may not be '%s'`, illegalName)}
35 }
36 }
37
38 var errors []string
39 for _, illegalContent := range NameMayNotContain {
40 if strings.Contains(name, illegalContent) {
41 errors = append(errors, fmt.Sprintf(`may not contain '%s'`, illegalContent))
42 }
43 }
44
45 return errors
46}
47
48// IsValidPathSegmentPrefix validates the name can be used as a prefix for a name which will be encoded as a path segment
49// It does not check for exact matches with disallowed names, since an arbitrary suffix might make the name valid
50func IsValidPathSegmentPrefix(name string) []string {
51 var errors []string
52 for _, illegalContent := range NameMayNotContain {
53 if strings.Contains(name, illegalContent) {
54 errors = append(errors, fmt.Sprintf(`may not contain '%s'`, illegalContent))
55 }
56 }
57
58 return errors
59}
60
61// ValidatePathSegmentName validates the name can be safely encoded as a path segment
62func ValidatePathSegmentName(name string, prefix bool) []string {
63 if prefix {
64 return IsValidPathSegmentPrefix(name)
65 } else {
66 return IsValidPathSegmentName(name)
67 }
68}