Skip to content

Commit

Permalink
config: validate module probe on unmarshal
Browse files Browse the repository at this point in the history
Signed-off-by: Jakob Hahn <[email protected]>
  • Loading branch information
Jakob3xD committed Oct 22, 2024
1 parent af13c69 commit 69f21a1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
12 changes: 10 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"reflect"
"regexp"
"runtime"
"slices"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -350,7 +351,7 @@ func (s *Module) UnmarshalYAML(unmarshal func(interface{}) error) error {
if err := unmarshal((*plain)(s)); err != nil {
return err
}
return nil
return s.validate()
}

// UnmarshalJSON implements the json.Unmarshaler interface.
Expand Down Expand Up @@ -390,7 +391,7 @@ func (s *Module) UnmarshalJSON(data []byte) error {
return err
}
default:
return fmt.Errorf("invalid duration: %#v", tmp)
return fmt.Errorf("invalid duration '%#v'", tmp)
}
}
*s = Module{
Expand All @@ -402,6 +403,13 @@ func (s *Module) UnmarshalJSON(data []byte) error {
DNS: tmp.DNS,
GRPC: tmp.GRPC,
}
return s.validate()
}

func (s *Module) validate() error {
if !slices.Contains([]string{"http", "tcp", "icmp", "dns", "grpc"}, s.Prober) {
return fmt.Errorf("prober '%s' is invalid", s.Prober)
}
return nil
}

Expand Down
5 changes: 5 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,11 @@ func TestLoadBadConfigs(t *testing.T) {
want: `error parsing config file: setting body and body_file both are not allowed`,
format: []string{"yml", "json"},
},
{
input: "testdata/invalid-module-prober",
want: `error parsing config file: invalid prober 'hTTp'`,
format: []string{"yml", "json"},
},
}
for _, test := range tests {
require.NoError(t, yamlToJson(t, test.input))
Expand Down
5 changes: 5 additions & 0 deletions config/testdata/invalid-module-prober.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
modules:
http_2xx:
prober: hTTp
timeout: 5s
http:

0 comments on commit 69f21a1

Please sign in to comment.