forked from openshift/library-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
general.go
130 lines (109 loc) · 3.76 KB
/
general.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package validation
import (
"fmt"
"net"
"net/url"
"os"
"strings"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/validation/field"
)
type ValidationResults struct {
Warnings field.ErrorList
Errors field.ErrorList
}
func (r *ValidationResults) Append(additionalResults ValidationResults) {
r.AddErrors(additionalResults.Errors...)
r.AddWarnings(additionalResults.Warnings...)
}
func (r *ValidationResults) AddErrors(errors ...*field.Error) {
if len(errors) == 0 {
return
}
r.Errors = append(r.Errors, errors...)
}
func (r *ValidationResults) AddWarnings(warnings ...*field.Error) {
if len(warnings) == 0 {
return
}
r.Warnings = append(r.Warnings, warnings...)
}
func ValidateHostPort(value string, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if len(value) == 0 {
allErrs = append(allErrs, field.Required(fldPath, ""))
} else if _, _, err := net.SplitHostPort(value); err != nil {
allErrs = append(allErrs, field.Invalid(fldPath, value, "must be a host:port"))
}
return allErrs
}
func ValidateFile(path string, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if len(path) == 0 {
allErrs = append(allErrs, field.Required(fldPath, ""))
} else if _, err := os.Stat(path); err != nil {
allErrs = append(allErrs, field.Invalid(fldPath, path, fmt.Sprintf("could not read file: %v", err)))
}
return allErrs
}
func ValidateSecureURL(urlString string, fldPath *field.Path) (*url.URL, field.ErrorList) {
url, urlErrs := ValidateURL(urlString, fldPath)
if len(urlErrs) == 0 && url.Scheme != "https" {
urlErrs = append(urlErrs, field.Invalid(fldPath, urlString, "must use https scheme"))
}
return url, urlErrs
}
func ValidateURL(urlString string, fldPath *field.Path) (*url.URL, field.ErrorList) {
allErrs := field.ErrorList{}
urlObj, err := url.Parse(urlString)
if err != nil {
allErrs = append(allErrs, field.Invalid(fldPath, urlString, "must be a valid URL"))
return nil, allErrs
}
if len(urlObj.Scheme) == 0 {
allErrs = append(allErrs, field.Invalid(fldPath, urlString, "must contain a scheme (e.g. https://)"))
}
if len(urlObj.Host) == 0 {
allErrs = append(allErrs, field.Invalid(fldPath, urlString, "must contain a host"))
}
return urlObj, allErrs
}
func ValidateDir(path string, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if len(path) == 0 {
allErrs = append(allErrs, field.Required(fldPath, ""))
} else {
fileInfo, err := os.Stat(path)
if err != nil {
allErrs = append(allErrs, field.Invalid(fldPath, path, fmt.Sprintf("could not read info: %v", err)))
} else if !fileInfo.IsDir() {
allErrs = append(allErrs, field.Invalid(fldPath, path, "not a directory"))
}
}
return allErrs
}
// HostnameMatchSpecCandidates returns a list of match specs that would match the provided hostname
// Returns nil if len(hostname) == 0
func HostnameMatchSpecCandidates(hostname string) []string {
if len(hostname) == 0 {
return nil
}
// Exact match has priority
candidates := []string{hostname}
// Replace successive labels in the name with wildcards, to require an exact match on number of
// path segments, because certificates cannot wildcard multiple levels of subdomains
//
// This is primarily to be consistent with tls.Config#getCertificate implementation
//
// It using a cert signed for *.foo.example.com and *.bar.example.com by specifying the name *.*.example.com
labels := strings.Split(hostname, ".")
for i := range labels {
labels[i] = "*"
candidates = append(candidates, strings.Join(labels, "."))
}
return candidates
}
// HostnameMatches returns true if the given hostname is matched by the given matchSpec
func HostnameMatches(hostname string, matchSpec string) bool {
return sets.NewString(HostnameMatchSpecCandidates(hostname)...).Has(matchSpec)
}