forked from openshift/origin
/
validation.go
182 lines (140 loc) · 5.91 KB
/
validation.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
173
174
175
176
177
178
179
180
181
182
package validation
import (
"net"
"net/url"
"os"
kvalidation "github.com/GoogleCloudPlatform/kubernetes/pkg/api/validation"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/fielderrors"
"github.com/openshift/origin/pkg/cmd/server/api"
)
func ValidateHostPort(value string, field string) fielderrors.ValidationErrorList {
allErrs := fielderrors.ValidationErrorList{}
if len(value) == 0 {
allErrs = append(allErrs, fielderrors.NewFieldRequired(field))
} else if _, _, err := net.SplitHostPort(value); err != nil {
allErrs = append(allErrs, fielderrors.NewFieldInvalid(field, value, "must be a host:port"))
}
return allErrs
}
func ValidateCertInfo(certInfo api.CertInfo, required bool) fielderrors.ValidationErrorList {
allErrs := fielderrors.ValidationErrorList{}
if required || len(certInfo.CertFile) > 0 || len(certInfo.KeyFile) > 0 {
if len(certInfo.CertFile) == 0 {
allErrs = append(allErrs, fielderrors.NewFieldRequired("certFile"))
}
if len(certInfo.KeyFile) == 0 {
allErrs = append(allErrs, fielderrors.NewFieldRequired("keyFile"))
}
}
if len(certInfo.CertFile) > 0 {
allErrs = append(allErrs, ValidateFile(certInfo.CertFile, "certFile")...)
}
if len(certInfo.KeyFile) > 0 {
allErrs = append(allErrs, ValidateFile(certInfo.KeyFile, "keyFile")...)
}
return allErrs
}
func ValidateServingInfo(info api.ServingInfo) fielderrors.ValidationErrorList {
allErrs := fielderrors.ValidationErrorList{}
allErrs = append(allErrs, ValidateHostPort(info.BindAddress, "bindAddress")...)
allErrs = append(allErrs, ValidateCertInfo(info.ServerCert, false)...)
if len(info.ServerCert.CertFile) > 0 {
if len(info.ClientCA) > 0 {
allErrs = append(allErrs, ValidateFile(info.ClientCA, "clientCA")...)
}
} else {
if len(info.ClientCA) > 0 {
allErrs = append(allErrs, fielderrors.NewFieldInvalid("clientCA", info.ClientCA, "cannot specify a clientCA without a certFile"))
}
}
return allErrs
}
func ValidateHTTPServingInfo(info api.HTTPServingInfo) fielderrors.ValidationErrorList {
allErrs := ValidateServingInfo(info.ServingInfo)
if info.MaxRequestsInFlight < 0 {
allErrs = append(allErrs, fielderrors.NewFieldInvalid("maxRequestsInFlight", info.MaxRequestsInFlight, "must be zero (no limit) or greater"))
}
if info.RequestTimeoutSeconds < -1 {
allErrs = append(allErrs, fielderrors.NewFieldInvalid("requestTimeoutSeconds", info.RequestTimeoutSeconds, "must be -1 (no timeout), 0 (default timeout), or greater"))
}
return allErrs
}
func ValidateKubeConfig(path string, field string) fielderrors.ValidationErrorList {
allErrs := fielderrors.ValidationErrorList{}
allErrs = append(allErrs, ValidateFile(path, field)...)
// TODO: load and parse
return allErrs
}
func ValidateRemoteConnectionInfo(remoteConnectionInfo api.RemoteConnectionInfo) fielderrors.ValidationErrorList {
allErrs := fielderrors.ValidationErrorList{}
if len(remoteConnectionInfo.URL) == 0 {
allErrs = append(allErrs, fielderrors.NewFieldRequired("url"))
} else {
_, urlErrs := ValidateURL(remoteConnectionInfo.URL, "url")
allErrs = append(allErrs, urlErrs...)
}
if len(remoteConnectionInfo.CA) > 0 {
allErrs = append(allErrs, ValidateFile(remoteConnectionInfo.CA, "ca")...)
}
allErrs = append(allErrs, ValidateCertInfo(remoteConnectionInfo.ClientCert, false)...)
return allErrs
}
func ValidatePodManifestConfig(podManifestConfig *api.PodManifestConfig) fielderrors.ValidationErrorList {
allErrs := fielderrors.ValidationErrorList{}
// the Path can be a file or a directory
allErrs = append(allErrs, ValidateFile(podManifestConfig.Path, "path")...)
if podManifestConfig.FileCheckIntervalSeconds < 1 {
allErrs = append(allErrs, fielderrors.NewFieldInvalid("fileCheckIntervalSeconds", podManifestConfig.FileCheckIntervalSeconds, "interval has to be positive"))
}
return allErrs
}
func ValidateSpecifiedIP(ipString string, field string) fielderrors.ValidationErrorList {
allErrs := fielderrors.ValidationErrorList{}
ip := net.ParseIP(ipString)
if ip == nil {
allErrs = append(allErrs, fielderrors.NewFieldInvalid(field, ipString, "must be a valid IP"))
} else if ip.IsUnspecified() {
allErrs = append(allErrs, fielderrors.NewFieldInvalid(field, ipString, "cannot be an unspecified IP"))
}
return allErrs
}
func ValidateSecureURL(urlString string, field string) (*url.URL, fielderrors.ValidationErrorList) {
url, urlErrs := ValidateURL(urlString, field)
if len(urlErrs) == 0 && url.Scheme != "https" {
urlErrs = append(urlErrs, fielderrors.NewFieldInvalid(field, urlString, "must use https scheme"))
}
return url, urlErrs
}
func ValidateURL(urlString string, field string) (*url.URL, fielderrors.ValidationErrorList) {
allErrs := fielderrors.ValidationErrorList{}
urlObj, err := url.Parse(urlString)
if err != nil {
allErrs = append(allErrs, fielderrors.NewFieldInvalid(field, urlString, "must be a valid URL"))
return nil, allErrs
}
if len(urlObj.Scheme) == 0 {
allErrs = append(allErrs, fielderrors.NewFieldInvalid(field, urlString, "must contain a scheme (e.g. https://)"))
}
if len(urlObj.Host) == 0 {
allErrs = append(allErrs, fielderrors.NewFieldInvalid(field, urlString, "must contain a host"))
}
return urlObj, allErrs
}
func ValidateNamespace(namespace, field string) fielderrors.ValidationErrorList {
allErrs := fielderrors.ValidationErrorList{}
if len(namespace) == 0 {
allErrs = append(allErrs, fielderrors.NewFieldRequired(field))
} else if ok, _ := kvalidation.ValidateNamespaceName(namespace, false); !ok {
allErrs = append(allErrs, fielderrors.NewFieldInvalid(field, namespace, "must be a valid namespace"))
}
return allErrs
}
func ValidateFile(path string, field string) fielderrors.ValidationErrorList {
allErrs := fielderrors.ValidationErrorList{}
if len(path) == 0 {
allErrs = append(allErrs, fielderrors.NewFieldRequired(field))
} else if _, err := os.Stat(path); err != nil {
allErrs = append(allErrs, fielderrors.NewFieldInvalid(field, path, "could not read file"))
}
return allErrs
}