forked from projectatomic/atomic-enterprise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation.go
261 lines (232 loc) · 10.2 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package validation
import (
"net/url"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/validation"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/fielderrors"
buildapi "github.com/openshift/origin/pkg/build/api"
imageapi "github.com/openshift/origin/pkg/image/api"
)
// ValidateBuild tests required fields for a Build.
func ValidateBuild(build *buildapi.Build) fielderrors.ValidationErrorList {
allErrs := fielderrors.ValidationErrorList{}
if len(build.Name) == 0 {
allErrs = append(allErrs, fielderrors.NewFieldRequired("name"))
} else if !util.IsDNS1123Subdomain(build.Name) {
allErrs = append(allErrs, fielderrors.NewFieldInvalid("name", build.Name, "name must be a valid subdomain"))
}
if len(build.Namespace) == 0 {
allErrs = append(allErrs, fielderrors.NewFieldRequired("namespace"))
} else if !util.IsDNS1123Subdomain(build.Namespace) {
allErrs = append(allErrs, fielderrors.NewFieldInvalid("namespace", build.Namespace, "namespace must be a valid subdomain"))
}
allErrs = append(allErrs, validation.ValidateLabels(build.Labels, "labels")...)
allErrs = append(allErrs, validateBuildParameters(&build.Parameters).Prefix("parameters")...)
return allErrs
}
// ValidateBuildConfig tests required fields for a Build.
func ValidateBuildConfig(config *buildapi.BuildConfig) fielderrors.ValidationErrorList {
allErrs := fielderrors.ValidationErrorList{}
if len(config.Name) == 0 {
allErrs = append(allErrs, fielderrors.NewFieldRequired("name"))
} else if !util.IsDNS1123Subdomain(config.Name) {
allErrs = append(allErrs, fielderrors.NewFieldInvalid("name", config.Name, "name must be a valid subdomain"))
}
if len(config.Namespace) == 0 {
allErrs = append(allErrs, fielderrors.NewFieldRequired("namespace"))
} else if !util.IsDNS1123Subdomain(config.Namespace) {
allErrs = append(allErrs, fielderrors.NewFieldInvalid("namespace", config.Namespace, "namespace must be a valid subdomain"))
}
allErrs = append(allErrs, validation.ValidateLabels(config.Labels, "labels")...)
for i := range config.Triggers {
allErrs = append(allErrs, validateTrigger(&config.Triggers[i]).PrefixIndex(i).Prefix("triggers")...)
}
allErrs = append(allErrs, validateBuildParameters(&config.Parameters).Prefix("parameters")...)
allErrs = append(allErrs, validateBuildConfigOutput(&config.Parameters.Output).Prefix("parameters.output")...)
return allErrs
}
// ValidateBuildRequest validates a BuildRequest object
func ValidateBuildRequest(request *buildapi.BuildRequest) fielderrors.ValidationErrorList {
allErrs := fielderrors.ValidationErrorList{}
if len(request.Name) == 0 {
allErrs = append(allErrs, fielderrors.NewFieldRequired("name"))
}
if request.Revision != nil {
allErrs = append(allErrs, validateRevision(request.Revision).Prefix("revision")...)
}
return allErrs
}
func validateBuildParameters(params *buildapi.BuildParameters) fielderrors.ValidationErrorList {
allErrs := fielderrors.ValidationErrorList{}
isCustomBuild := params.Strategy.Type == buildapi.CustomBuildStrategyType
// Validate 'source' and 'output' for all build types except Custom build
// where they are optional and validated only if present.
if !isCustomBuild || (isCustomBuild && len(params.Source.Type) != 0) {
allErrs = append(allErrs, validateSource(¶ms.Source).Prefix("source")...)
if params.Revision != nil {
allErrs = append(allErrs, validateRevision(params.Revision).Prefix("revision")...)
}
}
allErrs = append(allErrs, validateOutput(¶ms.Output).Prefix("output")...)
allErrs = append(allErrs, validateStrategy(¶ms.Strategy).Prefix("strategy")...)
// TODO: validate resource requirements (prereq: https://github.com/GoogleCloudPlatform/kubernetes/pull/7059)
return allErrs
}
func validateSource(input *buildapi.BuildSource) fielderrors.ValidationErrorList {
allErrs := fielderrors.ValidationErrorList{}
if input.Type != buildapi.BuildSourceGit {
allErrs = append(allErrs, fielderrors.NewFieldRequired("type"))
}
if input.Git == nil {
allErrs = append(allErrs, fielderrors.NewFieldRequired("git"))
} else {
allErrs = append(allErrs, validateGitSource(input.Git).Prefix("git")...)
}
return allErrs
}
func validateGitSource(git *buildapi.GitBuildSource) fielderrors.ValidationErrorList {
allErrs := fielderrors.ValidationErrorList{}
if len(git.URI) == 0 {
allErrs = append(allErrs, fielderrors.NewFieldRequired("uri"))
} else if !isValidURL(git.URI) {
allErrs = append(allErrs, fielderrors.NewFieldInvalid("uri", git.URI, "uri is not a valid url"))
}
return allErrs
}
func validateRevision(revision *buildapi.SourceRevision) fielderrors.ValidationErrorList {
allErrs := fielderrors.ValidationErrorList{}
if len(revision.Type) == 0 {
allErrs = append(allErrs, fielderrors.NewFieldRequired("type"))
}
// TODO: validate other stuff
return allErrs
}
func validateOutput(output *buildapi.BuildOutput) fielderrors.ValidationErrorList {
allErrs := fielderrors.ValidationErrorList{}
// TODO: make part of a generic ValidateObjectReference method upstream.
if output.To != nil {
kind, name, namespace := output.To.Kind, output.To.Name, output.To.Namespace
if len(kind) == 0 {
kind = "ImageStream"
output.To.Kind = kind
}
if kind != "ImageStream" {
allErrs = append(allErrs, fielderrors.NewFieldInvalid("to.kind", kind, "the target of build output must be 'ImageStream'"))
}
if len(name) == 0 {
allErrs = append(allErrs, fielderrors.NewFieldRequired("to.name"))
} else if !util.IsDNS1123Subdomain(name) {
allErrs = append(allErrs, fielderrors.NewFieldInvalid("to.name", name, "name must be a valid subdomain"))
}
if len(namespace) != 0 && !util.IsDNS1123Subdomain(namespace) {
allErrs = append(allErrs, fielderrors.NewFieldInvalid("to.namespace", namespace, "namespace must be a valid subdomain"))
}
}
if len(output.DockerImageReference) != 0 {
if _, err := imageapi.ParseDockerImageReference(output.DockerImageReference); err != nil {
allErrs = append(allErrs, fielderrors.NewFieldInvalid("dockerImageReference", output.DockerImageReference, err.Error()))
}
}
return allErrs
}
func validateBuildConfigOutput(output *buildapi.BuildOutput) fielderrors.ValidationErrorList {
allErrs := fielderrors.ValidationErrorList{}
if len(output.DockerImageReference) != 0 && output.To != nil {
allErrs = append(allErrs, fielderrors.NewFieldInvalid("dockerImageReference", output.DockerImageReference, "only one of 'dockerImageReference' and 'to' may be set"))
}
return allErrs
}
func validateStrategy(strategy *buildapi.BuildStrategy) fielderrors.ValidationErrorList {
allErrs := fielderrors.ValidationErrorList{}
switch {
case len(strategy.Type) == 0:
allErrs = append(allErrs, fielderrors.NewFieldRequired("type"))
case strategy.Type == buildapi.SourceBuildStrategyType:
if strategy.SourceStrategy == nil {
allErrs = append(allErrs, fielderrors.NewFieldRequired("stiStrategy"))
} else {
allErrs = append(allErrs, validateSourceStrategy(strategy.SourceStrategy).Prefix("stiStrategy")...)
}
case strategy.Type == buildapi.DockerBuildStrategyType:
// DockerStrategy is currently optional, initialize it to a default state if it's not set.
if strategy.DockerStrategy == nil {
strategy.DockerStrategy = &buildapi.DockerBuildStrategy{}
}
case strategy.Type == buildapi.CustomBuildStrategyType:
if strategy.CustomStrategy == nil {
allErrs = append(allErrs, fielderrors.NewFieldRequired("customStrategy"))
} else {
allErrs = append(allErrs, validateCustomStrategy(strategy.CustomStrategy).Prefix("customStrategy")...)
}
default:
allErrs = append(allErrs, fielderrors.NewFieldInvalid("type", strategy.Type, "type is not in the enumerated list"))
}
return allErrs
}
func validateSourceStrategy(strategy *buildapi.SourceBuildStrategy) fielderrors.ValidationErrorList {
allErrs := fielderrors.ValidationErrorList{}
if strategy.From == nil || len(strategy.From.Name) == 0 {
allErrs = append(allErrs, fielderrors.NewFieldRequired("from"))
}
return allErrs
}
func validateCustomStrategy(strategy *buildapi.CustomBuildStrategy) fielderrors.ValidationErrorList {
allErrs := fielderrors.ValidationErrorList{}
if strategy.From == nil || len(strategy.From.Name) == 0 {
allErrs = append(allErrs, fielderrors.NewFieldRequired("from"))
}
return allErrs
}
func validateTrigger(trigger *buildapi.BuildTriggerPolicy) fielderrors.ValidationErrorList {
allErrs := fielderrors.ValidationErrorList{}
if len(trigger.Type) == 0 {
allErrs = append(allErrs, fielderrors.NewFieldRequired("type"))
return allErrs
}
// Ensure that only parameters for the trigger's type are present
triggerPresence := map[buildapi.BuildTriggerType]bool{
buildapi.GithubWebHookBuildTriggerType: trigger.GithubWebHook != nil,
buildapi.GenericWebHookBuildTriggerType: trigger.GenericWebHook != nil,
}
allErrs = append(allErrs, validateTriggerPresence(triggerPresence, trigger.Type)...)
// Validate each trigger type
switch trigger.Type {
case buildapi.GithubWebHookBuildTriggerType:
if trigger.GithubWebHook == nil {
allErrs = append(allErrs, fielderrors.NewFieldRequired("github"))
} else {
allErrs = append(allErrs, validateWebHook(trigger.GithubWebHook).Prefix("github")...)
}
case buildapi.GenericWebHookBuildTriggerType:
if trigger.GenericWebHook == nil {
allErrs = append(allErrs, fielderrors.NewFieldRequired("generic"))
} else {
allErrs = append(allErrs, validateWebHook(trigger.GenericWebHook).Prefix("generic")...)
}
case buildapi.ImageChangeBuildTriggerType:
if trigger.ImageChange == nil {
allErrs = append(allErrs, fielderrors.NewFieldRequired("imageChange"))
}
}
return allErrs
}
func validateTriggerPresence(params map[buildapi.BuildTriggerType]bool, t buildapi.BuildTriggerType) fielderrors.ValidationErrorList {
allErrs := fielderrors.ValidationErrorList{}
for triggerType, present := range params {
if triggerType != t && present {
allErrs = append(allErrs, fielderrors.NewFieldInvalid(string(triggerType), "", "triggerType wasn't found"))
}
}
return allErrs
}
func validateWebHook(webHook *buildapi.WebHookTrigger) fielderrors.ValidationErrorList {
allErrs := fielderrors.ValidationErrorList{}
if len(webHook.Secret) == 0 {
allErrs = append(allErrs, fielderrors.NewFieldRequired("secret"))
}
return allErrs
}
func isValidURL(uri string) bool {
_, err := url.Parse(uri)
return err == nil
}