Added check for empty query name and handle case where targetAddr may empty
authorconorbroderick <cjayjayb@gmail.com>
Mon, 14 Aug 2017 11:51:22 +0000 (12:51 +0100)
committerconorbroderick <cjayjayb@gmail.com>
Mon, 14 Aug 2017 13:28:46 +0000 (14:28 +0100)
config.go
config_test.go
dns.go
testdata/blackbox-good.yml
testdata/invalid-dns-module.yml [new file with mode: 0644]

index 91880daf1c7a8ad491c87b05ca6720d65bd2d335..3d1d324f9baab1850d5b32a72eff61c2c63c2a67 100644 (file)
--- a/config.go
+++ b/config.go
@@ -1,6 +1,7 @@
 package main
 
 import (
+       "errors"
        "fmt"
        "strings"
        "sync"
@@ -155,6 +156,9 @@ func (s *DNSProbe) UnmarshalYAML(unmarshal func(interface{}) error) error {
        if err := checkOverflow(s.XXX, "dns probe"); err != nil {
                return err
        }
+       if s.QueryName == "" {
+               return errors.New("Query name must be set for DNS module")
+       }
        return nil
 }
 
index cea82c46e85c5f1e80d283e76c063e1a1e60aac0..1379098507eacc020be67bb2abc28f30ea827c06 100644 (file)
@@ -18,21 +18,32 @@ func TestLoadConfig(t *testing.T) {
        }
 }
 
-func TestLoadBadConfig(t *testing.T) {
+func TestLoadBadConfigs(t *testing.T) {
        sc := &SafeConfig{
                C: &Config{},
        }
-
-       expected := "unknown fields in dns probe: invalid_extra_field"
-
-       err := sc.reloadConfig("testdata/blackbox-bad.yml")
-       if err.Error() != expected {
-               t.Errorf("\nexpected:\n%v\ngot:\n%v", expected, err.Error())
+       tests := []struct {
+               ConfigFile    string
+               ExpectedError string
+       }{
+               {
+                       ConfigFile:    "testdata/blackbox-bad.yml",
+                       ExpectedError: "unknown fields in dns probe: invalid_extra_field",
+               },
+               {
+                       ConfigFile:    "testdata/invalid-dns-module.yml",
+                       ExpectedError: "Query name must be set for DNS module",
+               },
+       }
+       for i, test := range tests {
+               err := sc.reloadConfig(test.ConfigFile)
+               if err.Error() != test.ExpectedError {
+                       t.Errorf("In case %v:\nExpected:\n%v\nGot:\n%v", i, test.ExpectedError, err.Error())
+               }
        }
 }
 
 func TestHideConfigSecrets(t *testing.T) {
-
        sc := &SafeConfig{
                C: &Config{},
        }
diff --git a/dns.go b/dns.go
index 6718faf44c0473e5bd24c664258fca70b1376468..97c5ff22177290fa06b472b004192d7a6061fa01 100644 (file)
--- a/dns.go
+++ b/dns.go
@@ -110,15 +110,15 @@ func probeDNS(ctx context.Context, target string, module Module, registry *prome
        }()
 
        var ip *net.IPAddr
-       var err error
-
        if module.DNS.TransportProtocol == "" {
                module.DNS.TransportProtocol = "udp"
        }
        if module.DNS.TransportProtocol == "udp" || module.DNS.TransportProtocol == "tcp" {
-               targetAddr, port, _ := net.SplitHostPort(target)
-               if port == "" {
+               targetAddr, port, err := net.SplitHostPort(target)
+               if err != nil {
+                       // Target only contains host so fallback to default port and set targetAddr as target.
                        port = "53"
+                       targetAddr = target
                }
                ip, err = chooseProtocol(module.DNS.PreferredIPProtocol, targetAddr, registry)
                if err != nil {
@@ -153,7 +153,6 @@ func probeDNS(ctx context.Context, target string, module Module, registry *prome
 
        timeoutDeadline, _ := ctx.Deadline()
        client.Timeout = timeoutDeadline.Sub(time.Now())
-
        response, _, err := client.Exchange(msg, target)
        if err != nil {
                log.Warnf("Error while sending a DNS query: %s", err)
index 9025394f3d8145cd6bfed10338a9b7a1d7c3d349..1f145d2f07ceff08b492c38ecdc2ba493d79dacd 100644 (file)
@@ -47,6 +47,7 @@ modules:
     prober: dns
     timeout: 5s
     dns:
-      preferred_ip_protocol: ip6
+      query_name: example.com
+      preferred_ip_protocol: ip4
       validate_answer_rrs:
         fail_if_matches_regexp: [test]
diff --git a/testdata/invalid-dns-module.yml b/testdata/invalid-dns-module.yml
new file mode 100644 (file)
index 0000000..ae91e66
--- /dev/null
@@ -0,0 +1,8 @@
+modules:
+  dns_test:
+    prober: dns
+    timeout: 5s
+    dns:
+      preferred_ip_protocol: ip6
+      validate_answer_rrs:
+        fail_if_matches_regexp: [test]