-
Notifications
You must be signed in to change notification settings - Fork 76
/
validators.go
341 lines (296 loc) · 9.92 KB
/
validators.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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package mesh
import (
"fmt"
"regexp"
"sort"
"strings"
)
import (
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/durationpb"
"sigs.k8s.io/yaml"
)
import (
mesh_proto "github.com/apache/dubbo-kubernetes/api/mesh/v1alpha1"
"github.com/apache/dubbo-kubernetes/pkg/core/validators"
util_proto "github.com/apache/dubbo-kubernetes/pkg/util/proto"
)
const dnsLabel = `[a-z0-9]([-a-z0-9]*[a-z0-9])?`
var (
nameCharacterSet = regexp.MustCompile(`^[0-9a-z.\-_]*$`)
tagNameCharacterSet = regexp.MustCompile(`^[a-zA-Z0-9.\-_:/]*$`)
tagValueCharacterSet = regexp.MustCompile(`^[a-zA-Z0-9.\-_:]*$`)
selectorCharacterSet = regexp.MustCompile(`^([a-zA-Z0-9.\-_:/]*|\*)$`)
domainRegexp = regexp.MustCompile("^" + dnsLabel + "(\\." + dnsLabel + ")*" + "$")
)
type (
TagsValidatorFunc func(path validators.PathBuilder, selector map[string]string) validators.ValidationError
TagKeyValidatorFunc func(path validators.PathBuilder, key string) validators.ValidationError
TagValueValidatorFunc func(path validators.PathBuilder, key, value string) validators.ValidationError
)
type ValidateTagsOpts struct {
RequireAtLeastOneTag bool
RequireService bool
ExtraTagsValidators []TagsValidatorFunc
ExtraTagKeyValidators []TagKeyValidatorFunc
ExtraTagValueValidators []TagValueValidatorFunc
}
type ValidateSelectorsOpts struct {
ValidateTagsOpts
RequireAtMostOneSelector bool
RequireAtLeastOneSelector bool
}
func ValidateSelector(path validators.PathBuilder, tags map[string]string, opts ValidateTagsOpts) validators.ValidationError {
opts.ExtraTagValueValidators = append([]TagValueValidatorFunc{
func(path validators.PathBuilder, key, value string) validators.ValidationError {
var err validators.ValidationError
if !selectorCharacterSet.MatchString(value) {
err.AddViolationAt(path.Key(key), `tag value must consist of alphanumeric characters, dots, dashes, slashes and underscores or be "*"`)
}
return err
},
}, opts.ExtraTagValueValidators...)
return validateTagKeyValues(path, tags, opts)
}
func ValidateTags(path validators.PathBuilder, tags map[string]string, opts ValidateTagsOpts) validators.ValidationError {
opts.ExtraTagValueValidators = append([]TagValueValidatorFunc{
func(path validators.PathBuilder, key, value string) validators.ValidationError {
var err validators.ValidationError
if !tagValueCharacterSet.MatchString(value) {
err.AddViolationAt(path.Key(key), "tag value must consist of alphanumeric characters, dots, dashes and underscores")
}
return err
},
}, opts.ExtraTagValueValidators...)
return validateTagKeyValues(path, tags, opts)
}
func validateTagKeyValues(path validators.PathBuilder, keyValues map[string]string, opts ValidateTagsOpts) validators.ValidationError {
var err validators.ValidationError
if opts.RequireAtLeastOneTag && len(keyValues) == 0 {
err.AddViolationAt(path, "must have at least one tag")
}
for _, validate := range opts.ExtraTagsValidators {
err.Add(validate(path, keyValues))
}
for _, key := range Keys(keyValues) {
if key == "" {
err.AddViolationAt(path, "tag name must be non-empty")
}
if !tagNameCharacterSet.MatchString(key) {
err.AddViolationAt(path.Key(key), "tag name must consist of alphanumeric characters, dots, dashes, slashes and underscores")
}
for _, validate := range opts.ExtraTagKeyValidators {
err.Add(validate(path, key))
}
value := keyValues[key]
if value == "" {
err.AddViolationAt(path.Key(key), "tag value must be non-empty")
}
for _, validate := range opts.ExtraTagValueValidators {
err.Add(validate(path, key, value))
}
}
_, defined := keyValues[mesh_proto.ServiceTag]
if opts.RequireService && !defined {
err.AddViolationAt(path, fmt.Sprintf("mandatory tag %q is missing", mesh_proto.ServiceTag))
}
return err
}
var OnlyServiceTagAllowed = ValidateSelectorsOpts{
RequireAtLeastOneSelector: true,
ValidateTagsOpts: ValidateTagsOpts{
RequireService: true,
ExtraTagsValidators: []TagsValidatorFunc{
func(path validators.PathBuilder, selector map[string]string) validators.ValidationError {
var err validators.ValidationError
_, defined := selector[mesh_proto.ServiceTag]
if len(selector) != 1 || !defined {
err.AddViolationAt(path, fmt.Sprintf("must consist of exactly one tag %q", mesh_proto.ServiceTag))
}
return err
},
},
ExtraTagKeyValidators: []TagKeyValidatorFunc{
func(path validators.PathBuilder, key string) validators.ValidationError {
var err validators.ValidationError
if key != mesh_proto.ServiceTag {
err.AddViolationAt(path.Key(key), fmt.Sprintf("tag %q is not allowed", key))
}
return err
},
},
},
}
func Keys(tags map[string]string) []string {
// sort keys for consistency
var keys []string
for key := range tags {
keys = append(keys, key)
}
sort.Strings(keys)
return keys
}
func ValidateDuration(path validators.PathBuilder, duration *durationpb.Duration) validators.ValidationError {
var errs validators.ValidationError
if duration == nil {
errs.AddViolationAt(path, "must have a positive value")
return errs
}
if err := duration.CheckValid(); err != nil {
errs.AddViolationAt(path, "must have a valid value")
return errs
}
if duration.AsDuration() == 0 {
errs.AddViolationAt(path, "must have a positive value")
}
return errs
}
func ValidateThreshold(path validators.PathBuilder, threshold uint32) validators.ValidationError {
var err validators.ValidationError
if threshold == 0 {
err.AddViolationAt(path, "must have a positive value")
}
return err
}
// ValidatePort validates that port is a valid TCP or UDP port number.
func ValidatePort(path validators.PathBuilder, port uint32) validators.ValidationError {
err := validators.ValidationError{}
if port == 0 || port > 65535 {
err.AddViolationAt(path, "port must be in the range [1, 65535]")
}
return err
}
// ValidateHostname validates a gateway hostname field. A hostname may be one of
// - '*'
// - '*.domain.name'
// - 'domain.name'
func ValidateHostname(path validators.PathBuilder, hostname string) validators.ValidationError {
if hostname == "*" {
return validators.ValidationError{}
}
err := validators.ValidationError{}
if strings.HasPrefix(hostname, "*.") {
if !domainRegexp.MatchString(strings.TrimPrefix(hostname, "*.")) {
err.AddViolationAt(path, "invalid wildcard domain")
}
return err
}
if !domainRegexp.MatchString(hostname) {
err.AddViolationAt(path, "invalid hostname")
}
return err
}
func AllowedValuesHint(values ...string) string {
options := strings.Join(values, ", ")
if len(values) == 0 {
options = "(none)"
}
return fmt.Sprintf("Allowed values: %s", options)
}
func ProtocolValidator(protocols ...string) TagsValidatorFunc {
return func(path validators.PathBuilder, selector map[string]string) validators.ValidationError {
var err validators.ValidationError
v, defined := selector[mesh_proto.ProtocolTag]
if !defined {
err.AddViolationAt(path, "protocol must be specified")
return err
}
for _, protocol := range protocols {
if v == protocol {
return err
}
}
err.AddViolationAt(path.Key(mesh_proto.ProtocolTag), fmt.Sprintf("must be one of the [%s]",
strings.Join(protocols, ", ")))
return err
}
}
// Resource is considered valid if it pass validation of any message
func ValidateAnyResourceYAML(resYAML string, msgs ...proto.Message) error {
var err error
for _, msg := range msgs {
err = ValidateResourceYAML(msg, resYAML)
if err == nil {
return nil
}
}
return err
}
// Resource is considered valid if it pass validation of any message
func ValidateAnyResourceYAMLPatch(resYAML string, msgs ...proto.Message) error {
var err error
for _, msg := range msgs {
err = ValidateResourceYAMLPatch(msg, resYAML)
if err == nil {
return nil
}
}
return err
}
func ValidateResourceYAML(msg proto.Message, resYAML string) error {
json, err := yaml.YAMLToJSON([]byte(resYAML))
if err != nil {
json = []byte(resYAML)
}
if err := util_proto.FromJSON(json, msg); err != nil {
return err
}
if v, ok := msg.(interface{ Validate() error }); ok {
if err := v.Validate(); err != nil {
return err
}
}
return nil
}
func ValidateResourceYAMLPatch(msg proto.Message, resYAML string) error {
json, err := yaml.YAMLToJSON([]byte(resYAML))
if err != nil {
json = []byte(resYAML)
}
return util_proto.FromJSON(json, msg)
}
// SelectorKeyNotInSet returns a TagKeyValidatorFunc that checks the tag key
// is not any one of the given names.
func SelectorKeyNotInSet(keyName ...string) TagKeyValidatorFunc {
set := map[string]struct{}{}
for _, k := range keyName {
set[k] = struct{}{}
}
return TagKeyValidatorFunc(
func(path validators.PathBuilder, key string) validators.ValidationError {
err := validators.ValidationError{}
if _, ok := set[key]; ok {
err.AddViolationAt(
path.Key(key),
fmt.Sprintf("tag name must not be %q", key),
)
}
return err
})
}
func validateName(value string) validators.ValidationError {
var err validators.ValidationError
if !nameCharacterSet.MatchString(value) {
err.AddViolation(
"name",
"invalid characters: must consist of lower case alphanumeric characters, '-', '.' and '_'.",
)
}
return err
}