forked from cloudfoundry-attic/bosh-init
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator.go
368 lines (306 loc) · 11.7 KB
/
validator.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package manifest
import (
"net"
"regexp"
"strings"
binet "github.com/cloudfoundry/bosh-init/common/net"
bosherr "github.com/cloudfoundry/bosh-init/internal/github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-init/internal/github.com/cloudfoundry/bosh-utils/logger"
birel "github.com/cloudfoundry/bosh-init/release"
birelsetmanifest "github.com/cloudfoundry/bosh-init/release/set/manifest"
)
type Validator interface {
Validate(Manifest, birelsetmanifest.Manifest) error
ValidateReleaseJobs(Manifest, birel.Manager) error
}
type validator struct {
logger boshlog.Logger
}
func NewValidator(logger boshlog.Logger) Validator {
return &validator{
logger: logger,
}
}
func (v *validator) Validate(deploymentManifest Manifest, releaseSetManifest birelsetmanifest.Manifest) error {
errs := []error{}
if v.isBlank(deploymentManifest.Name) {
errs = append(errs, bosherr.Error("name must be provided"))
}
networksErrors := v.validateNetworks(deploymentManifest.Networks)
errs = append(errs, networksErrors...)
for idx, resourcePool := range deploymentManifest.ResourcePools {
if v.isBlank(resourcePool.Name) {
errs = append(errs, bosherr.Errorf("resource_pools[%d].name must be provided", idx))
}
if v.isBlank(resourcePool.Network) {
errs = append(errs, bosherr.Errorf("resource_pools[%d].network must be provided", idx))
} else if _, ok := v.networkNames(deploymentManifest)[resourcePool.Network]; !ok {
errs = append(errs, bosherr.Errorf("resource_pools[%d].network must be the name of a network", idx))
}
if v.isBlank(resourcePool.Stemcell.URL) {
errs = append(errs, bosherr.Errorf("resource_pools[%d].stemcell.url must be provided", idx))
}
matched, err := regexp.MatchString("^(file|http|https)://", resourcePool.Stemcell.URL)
if err != nil || !matched {
errs = append(errs, bosherr.Errorf("resource_pools[%d].stemcell.url must be a valid URL (file:// or http(s)://)", idx))
}
if strings.HasPrefix(resourcePool.Stemcell.URL, "http") && v.isBlank(resourcePool.Stemcell.SHA1) {
errs = append(errs, bosherr.Errorf("resource_pools[%d].stemcell.sha1 must be provided for http URL", idx))
}
}
for idx, diskPool := range deploymentManifest.DiskPools {
if v.isBlank(diskPool.Name) {
errs = append(errs, bosherr.Errorf("disk_pools[%d].name must be provided", idx))
}
if diskPool.DiskSize <= 0 {
errs = append(errs, bosherr.Errorf("disk_pools[%d].disk_size must be > 0", idx))
}
}
if len(deploymentManifest.Jobs) > 1 {
errs = append(errs, bosherr.Error("jobs must be of size 1"))
}
for idx, job := range deploymentManifest.Jobs {
if v.isBlank(job.Name) {
errs = append(errs, bosherr.Errorf("jobs[%d].name must be provided", idx))
}
if job.PersistentDisk < 0 {
errs = append(errs, bosherr.Errorf("jobs[%d].persistent_disk must be >= 0", idx))
}
if job.PersistentDiskPool != "" {
if _, ok := v.diskPoolNames(deploymentManifest)[job.PersistentDiskPool]; !ok {
errs = append(errs, bosherr.Errorf("jobs[%d].persistent_disk_pool must be the name of a disk pool", idx))
}
}
if job.Instances < 0 {
errs = append(errs, bosherr.Errorf("jobs[%d].instances must be >= 0", idx))
}
if len(job.Networks) == 0 {
errs = append(errs, bosherr.Errorf("jobs[%d].networks must be a non-empty array", idx))
}
if v.isBlank(job.ResourcePool) {
errs = append(errs, bosherr.Errorf("jobs[%d].resource_pool must be provided", idx))
} else {
if _, ok := v.resourcePoolNames(deploymentManifest)[job.ResourcePool]; !ok {
errs = append(errs, bosherr.Errorf("jobs[%d].resource_pool must be the name of a resource pool", idx))
}
}
errs = append(errs, v.validateJobNetworks(job.Networks, deploymentManifest.Networks, idx)...)
if job.Lifecycle != "" && job.Lifecycle != JobLifecycleService {
errs = append(errs, bosherr.Errorf("jobs[%d].lifecycle must be 'service' ('%s' not supported)", idx, job.Lifecycle))
}
templateNames := map[string]struct{}{}
for templateIdx, template := range job.Templates {
if v.isBlank(template.Name) {
errs = append(errs, bosherr.Errorf("jobs[%d].templates[%d].name must be provided", idx, templateIdx))
}
if _, found := templateNames[template.Name]; found {
errs = append(errs, bosherr.Errorf("jobs[%d].templates[%d].name '%s' must be unique", idx, templateIdx, template.Name))
}
templateNames[template.Name] = struct{}{}
if v.isBlank(template.Release) {
errs = append(errs, bosherr.Errorf("jobs[%d].templates[%d].release must be provided", idx, templateIdx))
} else {
_, found := releaseSetManifest.FindByName(template.Release)
if !found {
errs = append(errs, bosherr.Errorf("jobs[%d].templates[%d].release '%s' must refer to release in releases", idx, templateIdx, template.Release))
}
}
}
}
if len(errs) > 0 {
return bosherr.NewMultiError(errs...)
}
return nil
}
func (v *validator) ValidateReleaseJobs(deploymentManifest Manifest, releaseManager birel.Manager) error {
errs := []error{}
for idx, job := range deploymentManifest.Jobs {
for templateIdx, template := range job.Templates {
release, found := releaseManager.Find(template.Release)
if !found {
errs = append(errs, bosherr.Errorf("jobs[%d].templates[%d].release '%s' must refer to release in releases", idx, templateIdx, template.Release))
} else {
_, found := release.FindJobByName(template.Name)
if !found {
errs = append(errs, bosherr.Errorf("jobs[%d].templates[%d] must refer to a job in '%s', but there is no job named '%s'", idx, templateIdx, release.Name(), template.Name))
}
}
}
}
if len(errs) > 0 {
return bosherr.NewMultiError(errs...)
}
return nil
}
func (v *validator) isBlank(str string) bool {
return str == "" || strings.TrimSpace(str) == ""
}
func (v *validator) networkNames(deploymentManifest Manifest) map[string]struct{} {
names := make(map[string]struct{})
for _, network := range deploymentManifest.Networks {
names[network.Name] = struct{}{}
}
return names
}
func (v *validator) diskPoolNames(deploymentManifest Manifest) map[string]struct{} {
names := make(map[string]struct{})
for _, diskPool := range deploymentManifest.DiskPools {
names[diskPool.Name] = struct{}{}
}
return names
}
func (v *validator) resourcePoolNames(deploymentManifest Manifest) map[string]struct{} {
names := make(map[string]struct{})
for _, resourcePool := range deploymentManifest.ResourcePools {
names[resourcePool.Name] = struct{}{}
}
return names
}
func (v *validator) isValidIP(ip string) bool {
parsedIP := net.ParseIP(ip)
return parsedIP != nil
}
type maybeIPNet interface {
Try(func(*net.IPNet) error) error
}
type nothingIpNet struct{}
func (in *nothingIpNet) Try(fn func(*net.IPNet) error) error {
return nil
}
type somethingIpNet struct {
ipNet *net.IPNet
}
func (in *somethingIpNet) Try(fn func(*net.IPNet) error) error {
return fn(in.ipNet)
}
func (v *validator) validateRange(idx int, ipRange string) ([]error, maybeIPNet) {
if v.isBlank(ipRange) {
return []error{bosherr.Errorf("networks[%d].subnets[0].range must be provided", idx)}, ¬hingIpNet{}
} else {
_, ipNet, err := net.ParseCIDR(ipRange)
if err != nil {
return []error{bosherr.Errorf("networks[%d].subnets[0].range must be an ip range", idx)}, ¬hingIpNet{}
}
return []error{}, &somethingIpNet{ipNet: ipNet}
}
}
func (v *validator) validateNetworks(networks []Network) []error {
errs := []error{}
for idx, network := range networks {
networkErrors := v.validateNetwork(network, idx)
errs = append(errs, networkErrors...)
}
return errs
}
func (v *validator) validateNetwork(network Network, networkIdx int) []error {
errs := []error{}
if v.isBlank(network.Name) {
errs = append(errs, bosherr.Errorf("networks[%d].name must be provided", networkIdx))
}
if network.Type != Dynamic && network.Type != Manual && network.Type != VIP {
errs = append(errs, bosherr.Errorf("networks[%d].type must be 'manual', 'dynamic', or 'vip'", networkIdx))
}
if network.Type == Manual {
if len(network.Subnets) != 1 {
errs = append(errs, bosherr.Errorf("networks[%d].subnets must be of size 1", networkIdx))
} else {
ipRange := network.Subnets[0].Range
rangeErrors, maybeIpNet := v.validateRange(networkIdx, ipRange)
errs = append(errs, rangeErrors...)
gateway := network.Subnets[0].Gateway
gatewayErrors := v.validateGateway(networkIdx, gateway, maybeIpNet)
errs = append(errs, gatewayErrors...)
}
}
return errs
}
func (v *validator) validateJobNetworks(jobNetworks []JobNetwork, networks []Network, jobIdx int) []error {
errs := []error{}
defaultCounts := make(map[NetworkDefault]int)
for networkIdx, jobNetwork := range jobNetworks {
if v.isBlank(jobNetwork.Name) {
errs = append(errs, bosherr.Errorf("jobs[%d].networks[%d].name must be provided", jobIdx, networkIdx))
}
var matchingNetwork Network
found := false
for _, network := range networks {
if network.Name == jobNetwork.Name {
found = true
matchingNetwork = network
}
}
if !found {
errs = append(errs, bosherr.Errorf("jobs[%d].networks[%d] not found in networks", jobIdx, networkIdx))
}
for ipIdx, ip := range jobNetwork.StaticIPs {
staticIPErrors := v.validateStaticIP(ip, jobNetwork, matchingNetwork, jobIdx, networkIdx, ipIdx)
errs = append(errs, staticIPErrors...)
}
for defaultIdx, value := range jobNetwork.Defaults {
if value != NetworkDefaultDNS && value != NetworkDefaultGateway {
errs = append(errs, bosherr.Errorf("jobs[%d].networks[%d].default[%d] must be 'dns' or 'gateway'", jobIdx, networkIdx, defaultIdx))
}
}
for _, dflt := range jobNetwork.Defaults {
count, present := defaultCounts[dflt]
if present {
defaultCounts[dflt] = count + 1
} else {
defaultCounts[dflt] = 1
}
}
}
for _, dflt := range []NetworkDefault{"dns", "gateway"} {
count, found := defaultCounts[dflt]
if len(jobNetworks) > 1 && !found {
errs = append(errs, bosherr.Errorf("with multiple networks, a default for '%s' must be specified", dflt))
} else if count > 1 {
errs = append(errs, bosherr.Errorf("only one network can be the default for '%s'", dflt))
}
}
return errs
}
func (v *validator) validateStaticIP(ip string, jobNetwork JobNetwork, network Network, jobIdx, networkIdx, ipIdx int) []error {
if !v.isValidIP(ip) {
return []error{bosherr.Errorf("jobs[%d].networks[%d].static_ips[%d] must be a valid IP", jobIdx, networkIdx, ipIdx)}
}
if network.Type != Manual {
return []error{}
}
foundInSubnetRange := false
for _, subnet := range network.Subnets {
_, rangeNet, err := net.ParseCIDR(subnet.Range)
if err == nil && rangeNet.Contains(net.ParseIP(ip)) {
foundInSubnetRange = true
}
}
if foundInSubnetRange {
return []error{}
} else {
return []error{bosherr.Errorf("jobs[%d].networks[%d] static ip '%s' must be within subnet range", jobIdx, networkIdx, ip)}
}
}
func (v *validator) validateGateway(idx int, gateway string, ipNet maybeIPNet) []error {
if v.isBlank(gateway) {
return []error{bosherr.Errorf("networks[%d].subnets[0].gateway must be provided", idx)}
} else {
errors := []error{}
_ = ipNet.Try(func(ipNet *net.IPNet) error {
gatewayIp := net.ParseIP(gateway)
if gatewayIp == nil {
errors = append(errors, bosherr.Errorf("networks[%d].subnets[0].gateway must be an ip", idx))
}
if !ipNet.Contains(gatewayIp) {
errors = append(errors, bosherr.Errorf("subnet gateway '%s' must be within the specified range '%s'", gateway, ipNet))
}
if ipNet.IP.Equal(gatewayIp) {
errors = append(errors, bosherr.Errorf("subnet gateway can't be the network address '%s'", gatewayIp))
}
if binet.LastAddress(ipNet).Equal(gatewayIp) {
errors = append(errors, bosherr.Errorf("subnet gateway can't be the broadcast address '%s'", gatewayIp))
}
return nil
})
return errors
}
}