Skip to content

Commit 682f9bd

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

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
@@ -24,6 +24,7 @@ import (
2424
"reflect"
2525
"regexp"
2626
"runtime"
27+
"slices"
2728
"sort"
2829
"strconv"
2930
"strings"
@@ -350,7 +351,7 @@ func (s *Module) UnmarshalYAML(unmarshal func(interface{}) error) error {
350351
if err := unmarshal((*plain)(s)); err != nil {
351352
return err
352353
}
353-
return nil
354+
return s.validate()
354355
}
355356

356357
// UnmarshalJSON implements the json.Unmarshaler interface.
@@ -390,7 +391,7 @@ func (s *Module) UnmarshalJSON(data []byte) error {
390391
return err
391392
}
392393
default:
393-
return fmt.Errorf("invalid duration: %#v", tmp)
394+
return fmt.Errorf("invalid duration '%#v'", tmp)
394395
}
395396
}
396397
*s = Module{
@@ -402,6 +403,13 @@ func (s *Module) UnmarshalJSON(data []byte) error {
402403
DNS: tmp.DNS,
403404
GRPC: tmp.GRPC,
404405
}
406+
return s.validate()
407+
}
408+
409+
func (s *Module) validate() error {
410+
if !slices.Contains([]string{"http", "tcp", "icmp", "dns", "grpc"}, s.Prober) {
411+
return fmt.Errorf("prober '%s' is invalid", s.Prober)
412+
}
405413
return nil
406414
}
407415

config/config_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,11 @@ func TestLoadBadConfigs(t *testing.T) {
224224
want: `error parsing config file: setting body and body_file both are not allowed`,
225225
format: []string{"yml", "json"},
226226
},
227+
{
228+
input: "testdata/invalid-module-prober",
229+
want: `error parsing config file: prober 'hTTp' is invalid`,
230+
format: []string{"yml", "json"},
231+
},
227232
}
228233
for _, test := range tests {
229234
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)