-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
validation.go
350 lines (304 loc) · 9.6 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
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
// Copyright 2021 The prometheus-operator Authors
//
// Licensed 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 v1alpha1
import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"
)
func (hc *HTTPConfig) Validate() error {
if hc == nil {
return nil
}
if (hc.BasicAuth != nil || hc.OAuth2 != nil) && (hc.BearerTokenSecret != nil) {
return fmt.Errorf("at most one of basicAuth, oauth2, bearerTokenSecret must be configured")
}
if hc.Authorization != nil {
if hc.BearerTokenSecret != nil {
return fmt.Errorf("authorization is not compatible with bearerTokenSecret")
}
if hc.BasicAuth != nil || hc.OAuth2 != nil {
return fmt.Errorf("at most one of basicAuth, oauth2 & authorization must be configured")
}
if err := hc.Authorization.Validate(); err != nil {
return err
}
}
if hc.OAuth2 != nil {
if hc.BasicAuth != nil {
return fmt.Errorf("at most one of basicAuth, oauth2 & authorization must be configured")
}
if err := hc.OAuth2.Validate(); err != nil {
return err
}
}
if hc.TLSConfig != nil {
if err := hc.TLSConfig.Validate(); err != nil {
return err
}
}
return nil
}
// Validate the MuteTimeInterval
func (mti MuteTimeInterval) Validate() error {
if mti.Name == "" {
return errors.New("empty name field for mute time interval")
}
for i, ti := range mti.TimeIntervals {
for _, time := range ti.Times {
if err := time.Validate(); err != nil {
return fmt.Errorf("time range at %d is invalid: %w", i, err)
}
}
for _, weekday := range ti.Weekdays {
if err := weekday.Validate(); err != nil {
return fmt.Errorf("weekday range at %d is invalid: %w", i, err)
}
}
for _, dom := range ti.DaysOfMonth {
if err := dom.Validate(); err != nil {
return fmt.Errorf("mute time interval is invalid - day of month range at %d is invalid: %w", i, err)
}
}
for _, month := range ti.Months {
if err := month.Validate(); err != nil {
return fmt.Errorf("month range at %d is invalid: %w", i, err)
}
}
for _, year := range ti.Years {
if err := year.Validate(); err != nil {
return fmt.Errorf("year range at %d is invalid: %w", i, err)
}
}
}
return nil
}
// Validate the TimeRange
func (tr TimeRange) Validate() error {
_, err := tr.Parse()
return err
}
// Parse returns a ParsedRange on valid input or an error if the fields cannot be parsed
// End of the day is represented as 1440.
func (tr TimeRange) Parse() (*ParsedRange, error) {
if tr.StartTime == "" || tr.EndTime == "" {
return nil, fmt.Errorf("start and end are required")
}
start, err := parseTime(string(tr.StartTime))
if err != nil {
return nil, fmt.Errorf("start time invalid: %w", err)
}
end, err := parseTime(string(tr.EndTime))
if err != nil {
return nil, fmt.Errorf("end time invalid: %w", err)
}
if start >= end {
return nil, fmt.Errorf("start time %d cannot be equal or greater than end time %d", start, end)
}
return &ParsedRange{
Start: start,
End: end,
}, nil
}
// Validate the WeekdayRange
func (wr WeekdayRange) Validate() error {
_, err := wr.Parse()
return err
}
// Parse returns a ParsedRange on valid input or an error if the fields cannot be parsed
// The week starts on Sunday -> 0
func (wr WeekdayRange) Parse() (*ParsedRange, error) {
startStr, endStr, err := parseRange(string(wr))
if err != nil {
return nil, err
}
start, err := Weekday(startStr).Int()
if err != nil {
return nil, fmt.Errorf("failed to parse start day from weekday range: %w", err)
}
end, err := Weekday(endStr).Int()
if err != nil {
return nil, fmt.Errorf("failed to parse end day from weekday range: %w", err)
}
if start > end {
return nil, errors.New("start day cannot be before end day")
}
if start < 0 || start > 6 {
return nil, fmt.Errorf("%s is not a valid day of the week: out of range", startStr)
}
if end < 0 || end > 6 {
return nil, fmt.Errorf("%s is not a valid day of the week: out of range", endStr)
}
return &ParsedRange{Start: start, End: end}, nil
}
// Validate the YearRange
func (yr YearRange) Validate() error {
_, err := yr.Parse()
return err
}
// Parse returns a ParsedRange on valid input or an error if the fields cannot be parsed
func (yr YearRange) Parse() (*ParsedRange, error) {
startStr, endStr, err := parseRange(string(yr))
if err != nil {
return nil, err
}
start, err := strconv.Atoi(startStr)
if err != nil {
return nil, fmt.Errorf("start year cannot be %s parsed: %w", startStr, err)
}
end, err := strconv.Atoi(endStr)
if err != nil {
return nil, fmt.Errorf("end year cannot be %s parsed: %w", endStr, err)
}
if start > end {
return nil, fmt.Errorf("end year %d is before start year %d", end, start)
}
return &ParsedRange{Start: start, End: end}, nil
}
// Int returns an integer, which is the canonical representation
// of the Weekday in upstream types.
// Returns an error if the Weekday is invalid
func (w Weekday) Int() (int, error) {
normaliseWeekday := Weekday(strings.ToLower(string(w)))
day, found := daysOfWeek[normaliseWeekday]
if !found {
i, err := strconv.Atoi(string(normaliseWeekday))
if err != nil {
return day, fmt.Errorf("%s is an invalid weekday", w)
}
day = i
}
return day, nil
}
// Int validates the Month and returns an integer, which is the canonical representation
// of the Month in upstream types.
// Returns an error if the Month is invalid
func (m Month) Int() (int, error) {
normaliseMonth := Month(strings.ToLower(string(m)))
month, found := months[normaliseMonth]
if !found {
i, err := strconv.Atoi(string(normaliseMonth))
if err != nil || i < 1 || i > 12 {
return month, fmt.Errorf("%s is an invalid month", m)
}
month = i
}
return month, nil
}
// Validate the DayOfMonthRange
func (r DayOfMonthRange) Validate() error {
// Note: Validation is copied from UnmarshalYAML for DayOfMonthRange in alertmanager repo
// Check beginning <= end accounting for negatives day of month indices as well.
// Months != 31 days can't be addressed here and are clamped, but at least we can catch blatant errors.
if r.Start == 0 || r.Start < -31 || r.Start > 31 {
return fmt.Errorf("%d is not a valid day of the month: out of range", r.Start)
}
if r.End == 0 || r.End < -31 || r.End > 31 {
return fmt.Errorf("%d is not a valid day of the month: out of range", r.End)
}
// Restricting here prevents errors where begin > end in longer months but not shorter months.
if r.Start < 0 && r.End > 0 {
return fmt.Errorf("end day must be negative if start day is negative")
}
// Check begin <= end. We can't know this for sure when using negative indices,
// but we can prevent cases where its always invalid (using 28 day minimum length).
checkBegin := r.Start
checkEnd := r.End
if r.Start < 0 {
checkBegin = 28 + r.Start
}
if r.End < 0 {
checkEnd = 28 + r.End
}
if checkBegin > checkEnd {
return fmt.Errorf("end day %d is always before start day %d", r.End, r.Start)
}
return nil
}
// Validate the month range
func (mr MonthRange) Validate() error {
_, err := mr.Parse()
return err
}
// Parse returns a ParsedMonthRange or error on invalid input
func (mr MonthRange) Parse() (*ParsedRange, error) {
startStr, endStr, err := parseRange(string(mr))
if err != nil {
return nil, err
}
start, err := Month(startStr).Int()
if err != nil {
return nil, fmt.Errorf("failed to parse start month from month range: %w", err)
}
end, err := Month(endStr).Int()
if err != nil {
return nil, fmt.Errorf("failed to parse start month from month range: %w", err)
}
if start > end {
return nil, fmt.Errorf("end month %s is before start month %s", endStr, startStr)
}
return &ParsedRange{
Start: start,
End: end,
}, nil
}
// ParsedRange is an integer representation of a range
// +kubebuilder:object:generate:=false
type ParsedRange struct {
// Start is the beginning of the range
Start int `json:"start,omitempty"`
// End of the range
End int `json:"end,omitempty"`
}
var (
validTime = "^((([01][0-9])|(2[0-3])):[0-5][0-9])$|(^24:00$)"
validTimeRE = regexp.MustCompile(validTime)
)
// Converts a string of the form "HH:MM" into the number of minutes elapsed in the day.
func parseTime(in string) (mins int, err error) {
if !validTimeRE.MatchString(in) {
return 0, fmt.Errorf("couldn't parse timestamp %s, invalid format", in)
}
timestampComponents := strings.Split(in, ":")
if len(timestampComponents) != 2 {
return 0, fmt.Errorf("invalid timestamp format: %s", in)
}
timeStampHours, err := strconv.Atoi(timestampComponents[0])
if err != nil {
return 0, err
}
timeStampMinutes, err := strconv.Atoi(timestampComponents[1])
if err != nil {
return 0, err
}
if timeStampHours < 0 || timeStampHours > 24 || timeStampMinutes < 0 || timeStampMinutes > 60 {
return 0, fmt.Errorf("timestamp %s out of range", in)
}
// Timestamps are stored as minutes elapsed in the day, so multiply hours by 60.
mins = timeStampHours*60 + timeStampMinutes
return mins, nil
}
// parseRange parses a valid range string into parts
func parseRange(in string) (start, end string, err error) {
if !strings.ContainsRune(in, ':') {
return in, in, nil
}
parts := strings.Split(string(in), ":")
if len(parts) != 2 {
return start, end, fmt.Errorf("invalid range provided %s", in)
}
return parts[0], parts[1], nil
}