Skip to content

Commit

Permalink
[dns] validate query_class and query_type config values
Browse files Browse the repository at this point in the history
This allows to catch simple config errors earlier.
  • Loading branch information
Annih committed Jun 13, 2020
1 parent 6b88618 commit 7b7c466
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
12 changes: 12 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

yaml "gopkg.in/yaml.v3"

"github.com/miekg/dns"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/config"
)
Expand Down Expand Up @@ -233,6 +234,17 @@ func (s *DNSProbe) UnmarshalYAML(unmarshal func(interface{}) error) error {
if s.QueryName == "" {
return errors.New("query name must be set for DNS module")
}
if s.QueryClass != "" {
if _, ok := dns.StringToClass[s.QueryClass]; !ok {
return fmt.Errorf("query class '%s' is not valid", s.QueryClass)
}
}
if s.QueryType != "" {
if _, ok := dns.StringToType[s.QueryType]; !ok {
return fmt.Errorf("query type '%s' is not valid", s.QueryType)
}
}

return nil
}

Expand Down
8 changes: 8 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ func TestLoadBadConfigs(t *testing.T) {
ConfigFile: "testdata/invalid-dns-module.yml",
ExpectedError: "error parsing config file: query name must be set for DNS module",
},
{
ConfigFile: "testdata/invalid-dns-class.yml",
ExpectedError: "error parsing config file: query class 'X' is not valid",
},
{
ConfigFile: "testdata/invalid-dns-type.yml",
ExpectedError: "error parsing config file: query type 'X' is not valid",
},
{
ConfigFile: "testdata/invalid-http-header-match.yml",
ExpectedError: "error parsing config file: regexp must be set for HTTP header matchers",
Expand Down
8 changes: 8 additions & 0 deletions config/testdata/invalid-dns-class.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
modules:
dns_test:
prober: dns
timeout: 5s
dns:
query_name: example.com
query_class: X
query_type: A
8 changes: 8 additions & 0 deletions config/testdata/invalid-dns-type.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
modules:
dns_test:
prober: dns
timeout: 5s
dns:
query_name: example.com
query_class: CH
query_type: X

0 comments on commit 7b7c466

Please sign in to comment.