forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.go
172 lines (146 loc) · 4.98 KB
/
common.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package common
import (
"fmt"
"io/ioutil"
"net"
"net/url"
"os"
"strconv"
"unicode"
"unicode/utf8"
utilvalidation "k8s.io/apimachinery/pkg/util/validation"
"k8s.io/apimachinery/pkg/util/validation/field"
"github.com/openshift/origin/pkg/cmd/server/apis/config"
)
func ValidateStringSource(s config.StringSource, fieldPath *field.Path) ValidationResults {
validationResults := ValidationResults{}
methods := 0
if len(s.Value) > 0 {
methods++
}
if len(s.File) > 0 {
methods++
fileErrors := ValidateFile(s.File, fieldPath.Child("file"))
validationResults.AddErrors(fileErrors...)
// If the file was otherwise ok, and its value will be used verbatim, warn about trailing whitespace
if len(fileErrors) == 0 && len(s.KeyFile) == 0 {
if data, err := ioutil.ReadFile(s.File); err != nil {
validationResults.AddErrors(field.Invalid(fieldPath.Child("file"), s.File, fmt.Sprintf("could not read file: %v", err)))
} else if len(data) > 0 {
r, _ := utf8.DecodeLastRune(data)
if unicode.IsSpace(r) {
validationResults.AddWarnings(field.Invalid(fieldPath.Child("file"), s.File, "contains trailing whitespace which will be included in the value"))
}
}
}
}
if len(s.Env) > 0 {
methods++
}
if methods > 1 {
validationResults.AddErrors(field.Invalid(fieldPath, "", "only one of value, file, and env can be specified"))
}
if len(s.KeyFile) > 0 {
validationResults.AddErrors(ValidateFile(s.KeyFile, fieldPath.Child("keyFile"))...)
}
return validationResults
}
func ValidateSpecifiedIP(ipString string, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
ip := net.ParseIP(ipString)
if ip == nil {
allErrs = append(allErrs, field.Invalid(fldPath, ipString, "must be a valid IP"))
} else if ip.IsUnspecified() {
allErrs = append(allErrs, field.Invalid(fldPath, ipString, "cannot be an unspecified IP"))
}
return allErrs
}
func ValidateSpecifiedIPPort(ipPortString string, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
ipString, portString, err := net.SplitHostPort(ipPortString)
if err != nil {
allErrs = append(allErrs, field.Invalid(fldPath, ipPortString, "must be a valid IP:PORT"))
return allErrs
}
ip := net.ParseIP(ipString)
if ip == nil {
allErrs = append(allErrs, field.Invalid(fldPath, ipString, "must be a valid IP"))
} else if ip.IsUnspecified() {
allErrs = append(allErrs, field.Invalid(fldPath, ipString, "cannot be an unspecified IP"))
}
port, err := strconv.Atoi(portString)
if err != nil {
allErrs = append(allErrs, field.Invalid(fldPath, portString, "must be a valid port"))
} else {
for _, msg := range utilvalidation.IsValidPortNum(port) {
allErrs = append(allErrs, field.Invalid(fldPath, port, msg))
}
}
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 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 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
}
// TODO: this should just be two return arrays, no need to be clever
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...)
}