Skip to content

Commit 74394cb

Browse files
committed
config: validate module probe on unmarshal
Signed-off-by: Jakob Hahn <[email protected]>
1 parent e4fc163 commit 74394cb

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

config/config.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"reflect"
2626
"regexp"
2727
"runtime"
28+
"slices"
2829
"sort"
2930
"strconv"
3031
"strings"
@@ -452,7 +453,7 @@ func (s *Module) UnmarshalYAML(unmarshal func(interface{}) error) error {
452453
if err := unmarshal((*plain)(s)); err != nil {
453454
return err
454455
}
455-
return nil
456+
return s.validate()
456457
}
457458

458459
// UnmarshalJSON implements the json.Unmarshaler interface.
@@ -492,7 +493,7 @@ func (s *Module) UnmarshalJSON(data []byte) error {
492493
return err
493494
}
494495
default:
495-
return fmt.Errorf("invalid duration: %#v", tmp)
496+
return fmt.Errorf("invalid duration '%#v'", tmp)
496497
}
497498
}
498499
*s = Module{
@@ -504,6 +505,13 @@ func (s *Module) UnmarshalJSON(data []byte) error {
504505
DNS: tmp.DNS,
505506
GRPC: tmp.GRPC,
506507
}
508+
return s.validate()
509+
}
510+
511+
func (s *Module) validate() error {
512+
if !slices.Contains([]string{"http", "tcp", "icmp", "dns", "grpc"}, s.Prober) {
513+
return fmt.Errorf("prober '%s' is invalid", s.Prober)
514+
}
507515
return nil
508516
}
509517

config/config_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,11 @@ func TestLoadBadConfigs(t *testing.T) {
244244
want: `error parsing config file: setting body and body_file both are not allowed`,
245245
format: []string{"yml", "json"},
246246
},
247+
{
248+
input: "testdata/invalid-module-prober",
249+
want: `error parsing config file: prober 'hTTp' is invalid`,
250+
format: []string{"yml", "json"},
251+
},
247252
}
248253
for _, test := range tests {
249254
require.NoError(t, yamlToJson(t, test.input))
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
modules:
2+
http_2xx:
3+
prober: hTTp
4+
timeout: 5s
5+
http:

0 commit comments

Comments
 (0)