-
Notifications
You must be signed in to change notification settings - Fork 1
/
validator.go
334 lines (280 loc) · 7.41 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
package fluentbitconfig
import (
"fmt"
"regexp"
"strconv"
"strings"
"github.com/calyptia/go-fluentbit-config/v2/property"
)
// Validate with the default schema.
func (c Config) Validate() error {
return c.ValidateWithSchema(DefaultSchema)
}
// ValidateWithSchema validates that the config satisfies
// all properties defined on the schema.
// That is if the schema says the the input plugin "cpu"
// has a property named "pid" that is of integer type,
// it must be a valid integer.
func (c Config) ValidateWithSchema(schema Schema) error {
validate := func(kind SectionKind, plugins Plugins) error {
for _, plugin := range plugins {
if err := ValidateSectionWithSchema(kind, plugin.Properties, schema); err != nil {
return err
}
}
return nil
}
if err := validate(SectionKindCustom, c.Customs); err != nil {
return err
}
if err := validate(SectionKindInput, c.Pipeline.Inputs); err != nil {
return err
}
if err := validate(SectionKindFilter, c.Pipeline.Filters); err != nil {
return err
}
if err := validate(SectionKindOutput, c.Pipeline.Outputs); err != nil {
return err
}
// valid by default
return nil
}
func ValidateSection(kind SectionKind, props property.Properties) error {
return ValidateSectionWithSchema(kind, props, DefaultSchema)
}
func ValidateSectionWithSchema(kind SectionKind, props property.Properties, schema Schema) error {
name := Name(props)
if name == "" {
return ErrMissingName
}
// If the name takes a cloud variable, it won't be on the schema,
// so we allow it.
// TODO: review whether is valid that the `name` property can take a
// cloud variable syntax.
//
// Example:
// [INPUT]
// Name {{files.myinput}}
//
// Maybe we can pass-over the actual value.
if isCloudVariable(name) {
return nil
}
section, ok := schema.findSection(kind, name)
if !ok {
return NewUnknownPluginError(kind, name)
}
for _, p := range props {
if isCommonProperty(p.Key) || isCloudVariable(p.Key) || isCloudVariable(p.Value) || isCoreProperty(p.Key) {
continue
}
if areProcessors(p.Key) {
if err := validateProcessors(p.Value); err != nil {
return err
}
continue
}
opts, ok := section.findOptions(p.Key)
if !ok {
return fmt.Errorf("%s: %s: unknown property %q", kind, name, p.Key)
}
if !valid(opts, p.Value) {
return fmt.Errorf("%s: %s: expected %q to be a valid %s, got %v", kind, name, p.Key, opts.Type, p.Value)
}
}
return nil
}
func isCommonProperty(name string) bool {
for _, got := range [...]string{"name", "alias", "tag", "match", "match_Regex"} {
if strings.EqualFold(name, got) {
return true
}
}
return false
}
var (
// reCloudSecretVariable matches `{{ secrets.example }}`
reCloudSecretVariable = regexp.MustCompile(`{{\s*secrets\.\w+\s*}}`)
// reCloudFileVariable matches `{{ files.example }}`
reCloudFileVariable = regexp.MustCompile(`{{\s*files\.[0-9A-Za-z]+(?:-[A-Za-z]{3,4}|)+\s*}}`)
)
func isCloudVariable(val any) bool {
s, ok := val.(string)
if !ok {
return false
}
return reCloudSecretVariable.MatchString(s) ||
reCloudFileVariable.MatchString(s)
}
func areProcessors(key string) bool {
return strings.ToLower(key) == "processors"
}
// isCoreProperty tells whether the property is from Core.
// Core properties start with `core.` and are ignored.
// See https://github.com/chronosphereio/calyptia-core-fluent-bit/blob/71795c472bcb1f7e09a7ea121366d5f91df4263f/patches/pwhelan-flb_config-ignore-core-namespaced-properties.patch#L22-L25
func isCoreProperty(key string) bool {
return strings.HasPrefix(key, "core.")
}
func validateProcessorsSectionMaps(sectionMaps []interface{}) error {
for _, section := range sectionMaps {
props := property.Properties{}
for key, val := range section.(map[string]interface{}) {
props.Set(key, val)
}
if err := ValidateSection(SectionKindFilter, props); err != nil {
return err
}
}
return nil
}
func validateProcessors(processors any) error {
if procMap, ok := processors.(map[string]interface{}); !ok {
return fmt.Errorf("not a list of processors")
} else {
for key, sections := range procMap {
sectionMaps, ok := sections.([]interface{})
if !ok {
return fmt.Errorf("not a list of processors")
}
switch key {
case "logs", "metrics", "traces":
if err := validateProcessorsSectionMaps(sectionMaps); err != nil {
return err
}
default:
return fmt.Errorf("unknown processor type: %s", key)
}
}
}
return nil
}
func valid(opts SchemaOptions, val any) bool {
switch opts.Type {
case "deprecated":
// TODO: review whether "deprecated" is still valid and just ignored,
// or totally invalid.
return true
case "string":
// numbers and booleans are valid strings too.
if validInteger(val) || validDouble(val) {
return true
}
if _, ok := val.(bool); ok {
return true
}
_, ok := val.(string)
return ok
case "boolean":
return validBoolean(val)
case "integer":
return validInteger(val)
case "double":
return validDouble(val)
case "time":
return validTime(val)
case "size":
return validByteSize(val)
case "prefixed string":
// TODO: stricter validator once we know the full rules.
s, ok := val.(string)
return ok && s != ""
case "multiple comma delimited strings":
if _, ok := val.([]any); ok {
for _, v := range val.([]any) {
if _, ok := v.(string); !ok {
return false
}
}
return true
}
_, ok := val.(string)
return ok
case "space delimited strings (minimum 1)":
return validSpaceDelimitedString(val, 1)
case "space delimited strings (minimum 2)":
return validSpaceDelimitedString(val, 2)
case "space delimited strings (minimum 3)":
return validSpaceDelimitedString(val, 3)
case "space delimited strings (minimum 4)":
return validSpaceDelimitedString(val, 4)
}
// valid by default
return true
}
func validBoolean(val any) bool {
if s, ok := val.(string); ok {
return strings.EqualFold(s, "true") ||
strings.EqualFold(s, "false") ||
strings.EqualFold(s, "on") ||
strings.EqualFold(s, "off")
}
_, ok := val.(bool)
return ok
}
func validInteger(val any) bool {
if s, ok := val.(string); ok {
if _, err := strconv.ParseInt(s, 10, 64); err == nil {
return true
}
_, err := strconv.ParseUint(s, 10, 64)
return err == nil
}
switch val.(type) {
case int,
int8, int16, int32, int64,
uint8, uint16, uint32, uint64:
return true
}
return false
}
func validDouble(val any) bool {
if s, ok := val.(string); ok {
_, err := strconv.ParseFloat(s, 64)
return err == nil
}
switch val.(type) {
case float32, float64:
return true
}
return false
}
func validTime(val any) bool {
if validInteger(val) || validDouble(val) {
return true
}
// TODO: stricter time validator once we know the full rules.
if s, ok := val.(string); ok {
return s != ""
}
return false
}
// reByteSize example -45.32kB
var reByteSize = regexp.MustCompile(`^(?:\+|-)?[0-9]+(?:\.?[0-9]*)\s?(?:(?:k|K)|(?:m|M)|(?:g|G))?(?:b|B)?$`)
func validByteSize(val any) bool {
// numeric bytes without unit.
if validInteger(val) || validDouble(val) {
return true
}
if s, ok := val.(string); ok {
return reByteSize.MatchString(s)
}
return false
}
func validSpaceDelimitedString(val any, min int) bool {
valid := func(val any, min int) bool {
s, ok := val.(string)
if !ok {
return false
}
return len(strings.Fields(s)) >= min
}
if v, ok := val.([]any); ok {
for _, s := range v {
if !valid(s, min) {
return false
}
}
return true
}
return valid(val, min)
}