-
Notifications
You must be signed in to change notification settings - Fork 24
/
checks_extra.go
1365 lines (1080 loc) · 32.4 KB
/
checks_extra.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2020 Grafana Labs
// 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 synthetic_monitoring provides access to types and methods
// that allow for the production and consumption of protocol buffer
// messages used to communicate with synthetic-monitoring-api.
package synthetic_monitoring
//go:generate go run github.com/dmarkham/enumer -type=CheckType,CheckClass -trimprefix=CheckType,CheckClass -transform=lower -output=string.go
//go:generate go run github.com/dmarkham/enumer -type=MultiHttpEntryAssertionType,MultiHttpEntryAssertionSubjectVariant,MultiHttpEntryAssertionConditionVariant,MultiHttpEntryVariableType -trimprefix=MultiHttpEntryAssertionType_,MultiHttpEntryAssertionSubjectVariant_,MultiHttpEntryAssertionConditionVariant_,MultiHttpEntryVariableType_ -transform=upper -output=multihttp_string.go
import (
"errors"
"fmt"
"mime"
"net"
"net/url"
"strconv"
"strings"
"time"
"golang.org/x/net/http/httpguts"
)
var (
ErrInvalidTenantId = errors.New("invalid tenant ID")
ErrInvalidCheckProbes = errors.New("invalid check probes")
ErrInvalidCheckTarget = errors.New("invalid check target")
ErrInvalidCheckJob = errors.New("invalid check job")
ErrInvalidCheckFrequency = errors.New("invalid check frequency")
ErrInvalidCheckTimeout = errors.New("invalid check timeout")
ErrInvalidCheckLabelName = errors.New("invalid check label name")
ErrTooManyCheckLabels = errors.New("too many check labels")
ErrInvalidCheckLabelValue = errors.New("invalid check label value")
ErrInvalidLabelName = errors.New("invalid label name")
ErrInvalidLabelValue = errors.New("invalid label value")
ErrDuplicateLabelName = errors.New("duplicate label name")
ErrInvalidTargetValue = errors.New("invalid target value")
ErrInvalidCheckSettings = errors.New("invalid check settings")
ErrInvalidFQDNLength = errors.New("invalid FQHN length")
ErrInvalidFQHNElements = errors.New("invalid number of elements in FQHN")
ErrInvalidFQDNElementLength = errors.New("invalid FQHN element length")
ErrInvalidFQHNElement = errors.New("invalid FQHN element")
ErrInvalidPingHostname = errors.New("invalid ping hostname")
ErrInvalidPingPayloadSize = errors.New("invalid ping payload size")
ErrInvalidPingPacketCount = errors.New("invalid ping packet count")
ErrInvalidDnsName = errors.New("invalid DNS name")
ErrInvalidDnsNameElement = errors.New("invalid DNS name element")
ErrInvalidDnsServer = errors.New("invalid DNS server")
ErrInvalidDnsPort = errors.New("invalid DNS port")
ErrInvalidDnsProtocolString = errors.New("invalid DNS protocol string")
ErrInvalidDnsProtocolValue = errors.New("invalid DNS protocol value")
ErrInvalidDnsRecordTypeString = errors.New("invalid DNS record type string")
ErrInvalidDnsRecordTypeValue = errors.New("invalid DNS record type value")
ErrInvalidHttpUrl = errors.New("invalid HTTP URL")
ErrInvalidHttpMethodString = errors.New("invalid HTTP method string")
ErrInvalidHttpMethodValue = errors.New("invalid HTTP method value")
ErrInvalidHttpUrlHost = errors.New("invalid HTTP URL host")
ErrInvalidHttpHeaders = errors.New("invalid HTTP headers")
ErrHttpUrlContainsPassword = errors.New("HTTP URL contains username and password")
ErrHttpUrlContainsUsername = errors.New("HTTP URL contains username")
ErrInvalidProxyConnectHeaders = errors.New("invalid HTTP proxy connect headers")
ErrInvalidProxyUrl = errors.New("invalid proxy URL")
ErrInvalidProxySettings = errors.New("invalid proxy settings")
ErrInvalidTracerouteHostname = errors.New("invalid traceroute hostname")
ErrInvalidK6Script = errors.New("invalid K6 script")
ErrInvalidMultiHttpTargets = errors.New("invalid multi-http targets")
ErrTooManyMultiHttpTargets = errors.New("too many multi-http targets")
ErrTooManyMultiHttpAssertions = errors.New("too many multi-http assertions")
ErrTooManyMultiHttpVariables = errors.New("too many multi-http variables")
ErrMultiHttpVariableNamesNotUnique = errors.New("multi-http variable names must be unique")
ErrInvalidHostname = errors.New("invalid hostname")
ErrInvalidPort = errors.New("invalid port")
ErrInvalidIpVersionString = errors.New("invalid ip version string")
ErrInvalidIpVersionValue = errors.New("invalid ip version value")
ErrInvalidCompressionAlgorithmString = errors.New("invalid compression algorithm string")
ErrInvalidCompressionAlgorithmValue = errors.New("invalid compression algorithm value")
ErrInvalidProbeName = errors.New("invalid probe name")
ErrInvalidProbeReservedLabelName = errors.New("invalid probe, reserved label name")
ErrInvalidProbeLabelName = errors.New("invalid probe label name")
ErrInvalidProbeLabelValue = errors.New("invalid probe label value")
ErrTooManyProbeLabels = errors.New("too many probe labels")
ErrInvalidProbeLatitude = errors.New("invalid probe latitude")
ErrInvalidProbeLongitude = errors.New("invalid probe longitude")
ErrInvalidHttpRequestBodyContentType = errors.New("invalid HTTP request body content type")
ErrInvalidHttpRequestBodyPayload = errors.New("invalid HTTP request body payload")
ErrInvalidQueryFieldName = errors.New("invalid query field name")
ErrInvalidMultiHttpAssertion = errors.New("invalid multi-http assertion")
ErrInvalidMultiHttpEntryVariable = errors.New("invalid multi-http variable")
ErrInvalidMultiHttpAssertionMissingValue = errors.New("invalid multi-http assertion, missing value")
ErrInvalidMultiHttpAssertionExpressionNotAllowed = errors.New("invalid multi-http assertion, expression not allowed")
ErrInvalidMultiHttpAssertionMissingHeaderName = errors.New("invalid multi-http assertion, missing header name")
)
const (
HealthCheckInterval = 90 * time.Second
HealthCheckTimeout = 30 * time.Second
)
const (
MaxMetricLabels = 20 // Prometheus allows for 32 labels, but limit to 20.
MaxLogLabels = 15 // Loki allows a maximum of 15 labels.
MaxCheckLabels = 10 // Allow 10 user labels for checks,
MaxProbeLabels = 3 // 3 for probes, leaving 7 for internal use.
MaxLabelValueLength = 128 // Keep this number low so that the UI remains usable.
MaxPingPackets = 10 // Allow 10 packets per ping.
MaxMultiHttpTargets = 10 // Max targets per multi-http check.
MaxMultiHttpAssertions = 5 // Max assertions per multi-http target.
MaxMultiHttpVariables = 5 // Max variables per multi-http target.
)
type validatable interface {
Validate() error
}
func validateCollection[T validatable](collection []T) error {
for _, item := range collection {
if err := item.Validate(); err != nil {
return err
}
}
return nil
}
// CheckType represents the type of the associated check
type CheckType int32
const (
CheckTypeDns CheckType = 0
CheckTypeHttp CheckType = 1
CheckTypePing CheckType = 2
CheckTypeTcp CheckType = 3
CheckTypeTraceroute CheckType = 4
CheckTypeK6 CheckType = 5
CheckTypeMultiHttp CheckType = 6
CheckTypeGrpc CheckType = 7
)
type CheckClass int32
const (
CheckClassProtocol CheckClass = 0
CheckClassScripted CheckClass = 1
)
func CheckTypeFromString(in string) (CheckType, bool) {
ct, err := CheckTypeString(in)
if err != nil {
return 0, false
}
return ct, true
}
func (c Check) Type() CheckType {
switch {
case c.Settings.Dns != nil:
return CheckTypeDns
case c.Settings.Http != nil:
return CheckTypeHttp
case c.Settings.Ping != nil:
return CheckTypePing
case c.Settings.Tcp != nil:
return CheckTypeTcp
case c.Settings.Traceroute != nil:
return CheckTypeTraceroute
case c.Settings.K6 != nil:
return CheckTypeK6
case c.Settings.Multihttp != nil:
return CheckTypeMultiHttp
case c.Settings.Grpc != nil:
return CheckTypeGrpc
default:
panic("unhandled check type")
}
}
func (c Check) Class() CheckClass {
return c.Type().Class()
}
func (c CheckType) Class() CheckClass {
switch c {
case CheckTypeDns, CheckTypeHttp, CheckTypePing, CheckTypeTcp, CheckTypeTraceroute, CheckTypeGrpc:
return CheckClassProtocol
case CheckTypeK6, CheckTypeMultiHttp:
return CheckClassScripted
default:
panic("unhandled check class")
}
}
func (c Check) Validate() error {
if c.TenantId == BadID {
return ErrInvalidTenantId
}
if len(c.Probes) == 0 {
return ErrInvalidCheckProbes
}
if len(c.Target) == 0 {
return ErrInvalidCheckTarget
}
if len(c.Job) == 0 {
return ErrInvalidCheckJob
}
if err := c.validateFrequency(); err != nil {
return err
}
if err := c.validateTimeout(); err != nil {
return err
}
if err := validateLabels(c.Labels); err != nil {
return err
}
if err := c.Settings.Validate(); err != nil {
return err
}
if err := c.validateTarget(); err != nil {
return err
}
return nil
}
func (c Check) validateTarget() error {
// All targets must be valid label values.
if err := validateLabelValue(c.Target); err != nil {
return ErrInvalidTargetValue
}
switch c.Type() {
case CheckTypeDns:
if err := validateDnsTarget(c.Target); err != nil {
return ErrInvalidDnsName
}
case CheckTypeHttp:
return validateHttpUrl(c.Target)
case CheckTypePing:
if err := validateHost(c.Target); err != nil {
return ErrInvalidPingHostname
}
case CheckTypeTcp:
return validateHostPort(c.Target)
case CheckTypeTraceroute:
if err := validateHost(c.Target); err != nil {
return ErrInvalidTracerouteHostname
}
case CheckTypeK6:
return nil
case CheckTypeMultiHttp:
// TODO(mem): checks MUST have a target, but in this case it's
// not true that the target must be a valid URL.
// validation of URLs is the responsibility of the MultihttpEntryRequest
return nil
case CheckTypeGrpc:
return validateHostPort(c.Target)
default:
panic("unhandled check type")
}
return nil
}
func (c Check) validateFrequency() error {
// frequency must be in [1, 120] seconds
switch {
case c.Settings.Traceroute != nil:
if c.Frequency != 120*1000 {
return ErrInvalidCheckFrequency
}
case c.Settings.K6 != nil || c.Settings.Multihttp != nil:
// TODO(mem): k6 and multihttp checks should allow for a lower
// frequency (a higher number), but that needs that we keep the
// metrics alive on the Prometheus side, i.e. we need to cache
// results and push them to Prometheus on a periodic basis.
if c.Frequency < 60*1000 || c.Frequency > 120*1000 {
return ErrInvalidCheckFrequency
}
default:
if c.Frequency < 1*1000 || c.Frequency > 120*1000 {
return ErrInvalidCheckFrequency
}
}
return nil
}
func (c Check) validateTimeout() error {
switch {
case c.Settings.Traceroute != nil:
// We are hardcoding traceroute frequency and timeout until we can get data on what the boundaries should be
if c.Timeout != 30*1000 {
return ErrInvalidCheckTimeout
}
case c.Settings.K6 != nil || c.Settings.Multihttp != nil:
// This is expirimental. A 30 second timeout means we have more
// checks lingering around. timeout must be in [1, 30] seconds,
// and it must be less than frequency (otherwise we can end up
// running overlapping checks)
if c.Timeout < 1*1000 || c.Timeout > 30*1000 || c.Timeout > c.Frequency {
return ErrInvalidCheckTimeout
}
default:
// timeout must be in [1, 10] seconds, and it must be less than
// frequency (otherwise we can end up running overlapping
// checks)
if c.Timeout < 1*1000 || c.Timeout > 10*1000 || c.Timeout > c.Frequency {
return ErrInvalidCheckTimeout
}
}
return nil
}
func validateLabels(labels []Label) error {
if len(labels) > MaxCheckLabels {
return ErrTooManyCheckLabels
}
seenLabels := make(map[string]struct{})
for _, label := range labels {
if _, found := seenLabels[label.Name]; found {
return fmt.Errorf("label name %s: %w", label.Name, ErrDuplicateLabelName)
}
seenLabels[label.Name] = struct{}{}
if err := label.Validate(); err != nil {
return err
}
}
return nil
}
func (c Check) ConfigVersion() string {
return strconv.FormatInt(int64(c.Modified*1000000000), 10)
}
func (c AdHocCheck) Type() CheckType {
switch {
case c.Settings.Dns != nil:
return CheckTypeDns
case c.Settings.Http != nil:
return CheckTypeHttp
case c.Settings.Ping != nil:
return CheckTypePing
case c.Settings.Tcp != nil:
return CheckTypeTcp
case c.Settings.Traceroute != nil:
return CheckTypeTraceroute
case c.Settings.K6 != nil:
return CheckTypeK6
case c.Settings.Multihttp != nil:
return CheckTypeMultiHttp
case c.Settings.Grpc != nil:
return CheckTypeGrpc
default:
panic("unhandled check type")
}
}
func (c AdHocCheck) Validate() error {
if c.TenantId < 0 {
return ErrInvalidTenantId
}
if len(c.Probes) == 0 {
return ErrInvalidCheckProbes
}
if len(c.Target) == 0 {
return ErrInvalidCheckTarget
}
if err := c.validateTimeout(); err != nil {
return err
}
if err := c.Settings.Validate(); err != nil {
return err
}
if err := c.validateTarget(); err != nil {
return err
}
return nil
}
func (c AdHocCheck) validateTimeout() error {
switch {
case c.Settings.Traceroute != nil:
// We are hardcoding traceroute frequency and timeout until we can get data on what the boundaries should be
if c.Timeout != 30*1000 {
return ErrInvalidCheckTimeout
}
case c.Settings.K6 != nil || c.Settings.Multihttp != nil:
// This is expirimental. A 30 second timeout means we have more
// checks lingering around. timeout must be in [1, 30] seconds,
// and it must be less than frequency (otherwise we can end up
// running overlapping checks)
if c.Timeout < 1*1000 || c.Timeout > 30*1000 {
return ErrInvalidCheckTimeout
}
default:
// timeout must be in [1, 10] seconds, and it must be less than
// frequency (otherwise we can end up running overlapping
// checks)
if c.Timeout < 1*1000 || c.Timeout > 10*1000 {
return ErrInvalidCheckTimeout
}
}
return nil
}
func (c AdHocCheck) validateTarget() error {
switch c.Type() {
case CheckTypeDns:
if err := validateDnsTarget(c.Target); err != nil {
return ErrInvalidDnsName
}
case CheckTypeHttp:
return validateHttpUrl(c.Target)
case CheckTypePing:
if err := validateHost(c.Target); err != nil {
return ErrInvalidPingHostname
}
case CheckTypeTcp:
return validateHostPort(c.Target)
case CheckTypeTraceroute:
if err := validateHost(c.Target); err != nil {
return ErrInvalidTracerouteHostname
}
case CheckTypeK6:
return nil
case CheckTypeMultiHttp:
return nil
case CheckTypeGrpc:
return validateHostPort(c.Target)
default:
panic("unhandled check type")
}
return nil
}
func (s CheckSettings) Validate() error {
var validateFn func() error
settingsCount := 0
if s.Ping != nil {
settingsCount++
validateFn = s.Ping.Validate
}
if s.Http != nil {
settingsCount++
validateFn = s.Http.Validate
}
if s.Dns != nil {
settingsCount++
validateFn = s.Dns.Validate
}
if s.Tcp != nil {
settingsCount++
validateFn = s.Tcp.Validate
}
if s.Traceroute != nil {
settingsCount++
validateFn = s.Traceroute.Validate
}
if s.K6 != nil {
settingsCount++
validateFn = s.K6.Validate
}
if s.Multihttp != nil {
settingsCount++
validateFn = s.Multihttp.Validate
}
if s.Grpc != nil {
settingsCount++
validateFn = s.Grpc.Validate
}
if settingsCount != 1 {
return ErrInvalidCheckSettings
}
return validateFn()
}
func (s *PingSettings) Validate() error {
if s.PayloadSize < 0 || s.PayloadSize > 65499 {
return ErrInvalidPingPayloadSize
}
if s.PacketCount < 0 || s.PacketCount > MaxPingPackets {
return ErrInvalidPingPacketCount
}
return nil
}
func (s *HttpSettings) Validate() error {
for _, h := range s.Headers {
fields := strings.SplitN(h, ":", 2)
if len(fields) < 2 {
return ErrInvalidHttpHeaders
}
// remove optional leading and trailing whitespace
fields[1] = strings.TrimSpace(fields[1])
if !httpguts.ValidHeaderFieldName(fields[0]) {
return ErrInvalidHttpHeaders
}
if !httpguts.ValidHeaderFieldValue(fields[1]) {
return ErrInvalidHttpHeaders
}
}
if len(s.ProxyURL) > 0 {
u, err := url.Parse(s.ProxyURL)
if err != nil {
return ErrInvalidProxyUrl
}
if !(u.Scheme == "http" || u.Scheme == "https") {
return ErrInvalidProxyUrl
}
}
if len(s.ProxyConnectHeaders) > 0 && len(s.ProxyURL) == 0 {
return ErrInvalidProxySettings
}
for _, h := range s.ProxyConnectHeaders {
fields := strings.SplitN(h, ":", 2)
if len(fields) < 2 {
return ErrInvalidProxyConnectHeaders
}
// remove optional leading and trailing whitespace
fields[1] = strings.TrimSpace(fields[1])
if !httpguts.ValidHeaderFieldName(fields[0]) {
return ErrInvalidProxyConnectHeaders
}
if !httpguts.ValidHeaderFieldValue(fields[1]) {
return ErrInvalidProxyConnectHeaders
}
}
return nil
}
func (s *DnsSettings) Validate() error {
if len(s.Server) == 0 || validateHost(s.Server) != nil {
return ErrInvalidDnsServer
}
if s.Port < 0 || s.Port > 65535 {
return ErrInvalidDnsPort
}
return nil
}
func (s *TcpSettings) Validate() error {
return nil
}
func (s *TracerouteSettings) Validate() error {
return nil
}
func (s *K6Settings) Validate() error {
if len(s.Script) == 0 {
return ErrInvalidK6Script
}
return nil
}
func (s *MultiHttpSettings) Validate() error {
if len(s.Entries) == 0 {
return ErrInvalidMultiHttpTargets
}
if len(s.Entries) > MaxMultiHttpTargets {
return ErrTooManyMultiHttpTargets
}
if err := validateCollection(s.Entries); err != nil {
return err
}
return nil
}
func (s *GrpcSettings) Validate() error {
return nil
}
func hasUniqueValues[U any, V comparable](slice []U, fn func(U) V) bool {
set := make(map[V]struct{})
for _, elem := range slice {
value := fn(elem)
if _, found := set[value]; found {
return false
}
set[value] = struct{}{}
}
return true
}
func (e *MultiHttpEntry) Validate() error {
if e.Request == nil {
return ErrInvalidMultiHttpTargets
}
if err := e.Request.Validate(); err != nil {
return err
}
if len(e.Assertions) > MaxMultiHttpAssertions {
return ErrTooManyMultiHttpAssertions
}
if len(e.Variables) > MaxMultiHttpVariables {
return ErrTooManyMultiHttpVariables
}
if err := validateCollection(e.Assertions); err != nil {
return err
}
if err := validateCollection(e.Variables); err != nil {
return err
}
if !hasUniqueValues(e.Variables, func(v *MultiHttpEntryVariable) string { return v.Name }) {
return ErrMultiHttpVariableNamesNotUnique
}
return nil
}
func (h HttpHeader) Validate() error {
if !httpguts.ValidHeaderFieldName(h.Name) {
return ErrInvalidHttpHeaders
}
if !httpguts.ValidHeaderFieldValue(h.Value) {
return ErrInvalidHttpHeaders
}
return nil
}
func (f QueryField) Validate() error {
if len(f.Name) == 0 {
return ErrInvalidQueryFieldName
}
// the value might be empty
// The name can be anything. TODO(mem): is this true?
return nil
}
func (r *MultiHttpEntryRequest) Validate() error {
if r == nil {
return nil
}
if err := r.Method.Validate(); err != nil {
return err
}
if !strings.Contains(r.Url, "${") {
if err := validateHttpUrl(r.Url); err != nil {
return err
}
}
// TODO(mem): do something with HttpVersion?
if err := r.Body.Validate(); err != nil {
return err
}
if err := validateCollection(r.Headers); err != nil {
return err
}
if err := validateCollection(r.QueryFields); err != nil {
return err
}
return nil
}
// Validate verifies that the MultiHttpEntryAssertion is valid.
//
// Because of the structure represents multiple orthogonal variants, this
// function has to branch based on the type.
//
//nolint:gocyclo
func (a *MultiHttpEntryAssertion) Validate() error {
if a == nil {
return nil
}
if _, found := MultiHttpEntryAssertionType_name[int32(a.Type)]; !found {
// this should never happen
return ErrInvalidMultiHttpAssertion
}
if _, found := MultiHttpEntryAssertionSubjectVariant_name[int32(a.Subject)]; !found {
// this should never happen
return ErrInvalidMultiHttpAssertion
}
if _, found := MultiHttpEntryAssertionConditionVariant_name[int32(a.Condition)]; !found {
// this should never happen
return ErrInvalidMultiHttpAssertion
}
switch a.Type {
case MultiHttpEntryAssertionType_TEXT:
// Value is required
if len(a.Value) == 0 {
return ErrInvalidMultiHttpAssertionMissingValue
}
// Expression is not allowed for subjects other than response headers.
if a.Subject != MultiHttpEntryAssertionSubjectVariant_RESPONSE_HEADERS && len(a.Expression) != 0 {
return ErrInvalidMultiHttpAssertionExpressionNotAllowed
}
case MultiHttpEntryAssertionType_JSON_PATH_VALUE:
// Subject must not be set
if a.Subject != 0 {
return ErrInvalidMultiHttpAssertion
}
// Value is required
if len(a.Value) == 0 {
return ErrInvalidMultiHttpAssertion
}
// Expression is required
if len(a.Expression) == 0 {
return ErrInvalidMultiHttpAssertion
}
// Condition is covered above
case MultiHttpEntryAssertionType_JSON_PATH_ASSERTION:
// Subject must not be set
if a.Subject != 0 {
return ErrInvalidMultiHttpAssertion
}
// Condition must not be set
if a.Condition != 0 {
return ErrInvalidMultiHttpAssertion
}
// Value must not be set
if len(a.Value) != 0 {
return ErrInvalidMultiHttpAssertion
}
// Expression is required
if len(a.Expression) == 0 {
return ErrInvalidMultiHttpAssertion
}
case MultiHttpEntryAssertionType_REGEX_ASSERTION:
// Condition must not be set
if a.Condition != 0 {
return ErrInvalidMultiHttpAssertion
}
// Value must not be set
if len(a.Value) != 0 {
return ErrInvalidMultiHttpAssertion
}
// Expression is required
if len(a.Expression) == 0 {
return ErrInvalidMultiHttpAssertion
}
}
return nil
}
func (v *MultiHttpEntryVariable) Validate() error {
// 1. Type is valid
if _, found := MultiHttpEntryVariableType_name[int32(v.Type)]; !found {
return ErrInvalidMultiHttpEntryVariable
}
// 2. Name is not empty
if len(v.Name) == 0 {
return ErrInvalidMultiHttpEntryVariable
}
// 3. Expression is not empty
if len(v.Expression) == 0 {
return ErrInvalidMultiHttpEntryVariable
}
switch v.Type {
case MultiHttpEntryVariableType_JSON_PATH:
// 4. attribute must be empty
if len(v.Attribute) != 0 {
return ErrInvalidMultiHttpEntryVariable
}
case MultiHttpEntryVariableType_REGEX:
// 4. attribute must be empty
if len(v.Attribute) != 0 {
return ErrInvalidMultiHttpEntryVariable
}
case MultiHttpEntryVariableType_CSS_SELECTOR:
// 4. attribute might be empty
}
return nil
}
func (b *HttpRequestBody) Validate() error {
if b == nil {
return nil
}
if len(b.ContentType) == 0 {
return ErrInvalidHttpRequestBodyContentType
}
if !httpguts.ValidHeaderFieldValue(b.ContentType) {
return ErrInvalidHttpRequestBodyContentType
}
for _, v := range strings.Split(b.ContentType, ",") {
_, _, err := mime.ParseMediaType(v)
if err != nil {
return ErrInvalidHttpRequestBodyContentType
}
}
if len(b.ContentEncoding) > 0 && !httpguts.ValidHeaderFieldValue(b.ContentEncoding) {
return ErrInvalidHttpRequestBodyContentType
}
// Payload can be empty, since Content-Length can be 0.
// https://datatracker.ietf.org/doc/html/rfc9110#section-8.6
return nil
}
func (p *Probe) Validate() error {
if p.TenantId < 0 {
return ErrInvalidTenantId
}
if p.Name == "" {
return ErrInvalidProbeName
}
if len(p.Labels) > MaxProbeLabels {
return ErrTooManyProbeLabels
}
for _, label := range p.Labels {
if err := label.Validate(); err != nil {
return err
}
}
if p.Latitude < -90 || p.Latitude > 90 {
return ErrInvalidProbeLatitude
}
if p.Longitude < -180 || p.Longitude > 180 {
return ErrInvalidProbeLongitude
}
return nil
}
func (l Label) Validate() error {
if err := validateLabelValue(l.Name); err != nil {
return ErrInvalidLabelName
}
// This bit is lifted from Prometheus code, except that
// Prometheus accepts /^[a-zA-Z_][a-zA-Z0-9_]*$/ and we accept
// /^[a-zA-Z0-9_]+$/ because these names are going to be
// prefixed with "label_".
for _, b := range l.Name {
if !((b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') || b == '_' || (b >= '0' && b <= '9')) {
return ErrInvalidLabelName
}
}
return validateLabelValue(l.Value)
}
func validateLabelValue(v string) error {
if len(v) == 0 || len(v) > MaxLabelValueLength {
return ErrInvalidLabelValue
}
return nil