[BUGFIX] disable ip protocol fallback and tests (#540)
authorgenofire <geno+dev@fireorbit.de>
Wed, 23 Oct 2019 12:03:30 +0000 (14:03 +0200)
committerBrian Brazil <brian.brazil@robustperception.io>
Wed, 23 Oct 2019 12:03:30 +0000 (13:03 +0100)
Signed-off-by: Martin/Geno <geno+dev@fireorbit.de>
prober/dns_test.go
prober/tcp_test.go
prober/utils.go
prober/utils_test.go

index 767a31c781bfa9cefc3f34d0a23b4589d88f4bb1..9b7b4a6f177674c52c3793c4e7f0536c76b67e11 100644 (file)
@@ -96,21 +96,24 @@ func TestRecursiveDNSResponse(t *testing.T) {
        }{
                {
                        config.DNSProbe{
-                               IPProtocol: "ipv4",
-                               QueryName:  "example.com",
+                               IPProtocol:         "ip4",
+                               IPProtocolFallback: true,
+                               QueryName:          "example.com",
                        }, true,
                },
                {
                        config.DNSProbe{
-                               IPProtocol:  "ipv4",
-                               QueryName:   "example.com",
-                               ValidRcodes: []string{"SERVFAIL", "NXDOMAIN"},
+                               IPProtocol:         "ip4",
+                               IPProtocolFallback: true,
+                               QueryName:          "example.com",
+                               ValidRcodes:        []string{"SERVFAIL", "NXDOMAIN"},
                        }, false,
                },
                {
                        config.DNSProbe{
-                               IPProtocol: "ipv4",
-                               QueryName:  "example.com",
+                               IPProtocol:         "ip4",
+                               IPProtocolFallback: true,
+                               QueryName:          "example.com",
                                ValidateAnswer: config.DNSRRValidator{
                                        FailIfMatchesRegexp:    []string{".*7200.*"},
                                        FailIfNotMatchesRegexp: []string{".*3600.*"},
@@ -119,8 +122,9 @@ func TestRecursiveDNSResponse(t *testing.T) {
                },
                {
                        config.DNSProbe{
-                               IPProtocol: "ipv4",
-                               QueryName:  "example.com",
+                               IPProtocol:         "ip4",
+                               IPProtocolFallback: true,
+                               QueryName:          "example.com",
                                ValidateAuthority: config.DNSRRValidator{
                                        FailIfMatchesRegexp: []string{".*7200.*"},
                                },
@@ -128,8 +132,9 @@ func TestRecursiveDNSResponse(t *testing.T) {
                },
                {
                        config.DNSProbe{
-                               IPProtocol: "ipv4",
-                               QueryName:  "example.com",
+                               IPProtocol:         "ip4",
+                               IPProtocolFallback: true,
+                               QueryName:          "example.com",
                                ValidateAdditional: config.DNSRRValidator{
                                        FailIfNotMatchesRegexp: []string{".*3600.*"},
                                },
@@ -226,27 +231,31 @@ func TestAuthoritativeDNSResponse(t *testing.T) {
        }{
                {
                        config.DNSProbe{
-                               IPProtocol: "ipv4",
-                               QueryName:  "example.com",
+                               IPProtocol:         "ip4",
+                               IPProtocolFallback: true,
+                               QueryName:          "example.com",
                        }, true,
                },
                {
                        config.DNSProbe{
-                               IPProtocol: "ipv4",
-                               QueryName:  "example.com",
-                               QueryType:  "SOA",
+                               IPProtocol:         "ip4",
+                               IPProtocolFallback: true,
+                               QueryName:          "example.com",
+                               QueryType:          "SOA",
                        }, true,
                }, {
                        config.DNSProbe{
-                               IPProtocol:  "ipv4",
-                               QueryName:   "example.com",
-                               ValidRcodes: []string{"SERVFAIL", "NXDOMAIN"},
+                               IPProtocol:         "ip4",
+                               IPProtocolFallback: true,
+                               QueryName:          "example.com",
+                               ValidRcodes:        []string{"SERVFAIL", "NXDOMAIN"},
                        }, false,
                },
                {
                        config.DNSProbe{
-                               IPProtocol: "ipv4",
-                               QueryName:  "example.com",
+                               IPProtocol:         "ip4",
+                               IPProtocolFallback: true,
+                               QueryName:          "example.com",
                                ValidateAnswer: config.DNSRRValidator{
                                        FailIfMatchesRegexp:    []string{".*3600.*"},
                                        FailIfNotMatchesRegexp: []string{".*3600.*"},
@@ -255,8 +264,9 @@ func TestAuthoritativeDNSResponse(t *testing.T) {
                },
                {
                        config.DNSProbe{
-                               IPProtocol: "ipv4",
-                               QueryName:  "example.com",
+                               IPProtocol:         "ip4",
+                               IPProtocolFallback: true,
+                               QueryName:          "example.com",
                                ValidateAnswer: config.DNSRRValidator{
                                        FailIfMatchesRegexp:    []string{".*7200.*"},
                                        FailIfNotMatchesRegexp: []string{".*7200.*"},
@@ -265,8 +275,9 @@ func TestAuthoritativeDNSResponse(t *testing.T) {
                },
                {
                        config.DNSProbe{
-                               IPProtocol: "ipv4",
-                               QueryName:  "example.com",
+                               IPProtocol:         "ip4",
+                               IPProtocolFallback: true,
+                               QueryName:          "example.com",
                                ValidateAuthority: config.DNSRRValidator{
                                        FailIfNotMatchesRegexp: []string{"ns.*.isp.net"},
                                },
@@ -274,8 +285,9 @@ func TestAuthoritativeDNSResponse(t *testing.T) {
                },
                {
                        config.DNSProbe{
-                               IPProtocol: "ipv4",
-                               QueryName:  "example.com",
+                               IPProtocol:         "ip4",
+                               IPProtocolFallback: true,
+                               QueryName:          "example.com",
                                ValidateAdditional: config.DNSRRValidator{
                                        FailIfNotMatchesRegexp: []string{"^ns.*.isp"},
                                },
@@ -283,8 +295,9 @@ func TestAuthoritativeDNSResponse(t *testing.T) {
                },
                {
                        config.DNSProbe{
-                               IPProtocol: "ipv4",
-                               QueryName:  "example.com",
+                               IPProtocol:         "ip4",
+                               IPProtocolFallback: true,
+                               QueryName:          "example.com",
                                ValidateAdditional: config.DNSRRValidator{
                                        FailIfMatchesRegexp: []string{"^ns.*.isp"},
                                },
@@ -334,29 +347,33 @@ func TestServfailDNSResponse(t *testing.T) {
        }{
                {
                        config.DNSProbe{
-                               IPProtocol: "ipv4",
-                               QueryName:  "example.com",
+                               IPProtocol:         "ip4",
+                               IPProtocolFallback: true,
+                               QueryName:          "example.com",
                        }, false,
                },
                {
                        config.DNSProbe{
-                               IPProtocol:  "ipv4",
-                               QueryName:   "example.com",
-                               ValidRcodes: []string{"SERVFAIL", "NXDOMAIN"},
+                               IPProtocol:         "ip4",
+                               IPProtocolFallback: true,
+                               QueryName:          "example.com",
+                               ValidRcodes:        []string{"SERVFAIL", "NXDOMAIN"},
                        }, true,
                },
                {
                        config.DNSProbe{
-                               IPProtocol: "ipv4",
-                               QueryName:  "example.com",
-                               QueryType:  "NOT_A_VALID_QUERY_TYPE",
+                               IPProtocol:         "ip4",
+                               IPProtocolFallback: true,
+                               QueryName:          "example.com",
+                               QueryType:          "NOT_A_VALID_QUERY_TYPE",
                        }, false,
                },
                {
                        config.DNSProbe{
-                               IPProtocol:  "ipv4",
-                               QueryName:   "example.com",
-                               ValidRcodes: []string{"NOT_A_VALID_RCODE"},
+                               IPProtocol:         "ip4",
+                               IPProtocolFallback: true,
+                               QueryName:          "example.com",
+                               ValidRcodes:        []string{"NOT_A_VALID_RCODE"},
                        }, false,
                },
        }
index e2fcbd4d3a970ff45f8e9c083c3c960f6ecc18b9..02401f618282d6c7f5aadcae9cbedc660fe5a690 100644 (file)
@@ -135,8 +135,9 @@ func TestTCPConnectionWithTLS(t *testing.T) {
        // Expect name-verified TLS connection.
        module := config.Module{
                TCP: config.TCPProbe{
-                       IPProtocol: "ipv4",
-                       TLS:        true,
+                       IPProtocol:         "ip4",
+                       IPProtocolFallback: true,
+                       TLS:                true,
                        TLSConfig: pconfig.TLSConfig{
                                CAFile:             tmpCaFile.Name(),
                                InsecureSkipVerify: false,
index 238f65c7e3b4e4eac4ea8e1eea7854c23186e4ce..eab20044d79e97598c7ce693e7e729e038976708 100644 (file)
@@ -91,7 +91,7 @@ func chooseProtocol(ctx context.Context, IPProtocol string, fallbackIPProtocol b
        }
 
        // Unable to find ip and no fallback set.
-       if fallback == nil {
+       if fallback == nil || !fallbackIPProtocol {
                return nil, 0.0, fmt.Errorf("unable to find ip; no fallback")
        }
 
index 326da5d81af0d9532a53bfb2fd20e385cf9fda59..dc0e8f96c5f3c59da59f13695e45376e19bdea0a 100644 (file)
@@ -14,6 +14,7 @@
 package prober
 
 import (
+       "context"
        "crypto/rand"
        "crypto/rsa"
        "crypto/x509"
@@ -22,9 +23,13 @@ import (
        "fmt"
        "math/big"
        "net"
+       "os"
        "testing"
        "time"
 
+       "github.com/go-kit/kit/log"
+
+       "github.com/prometheus/client_golang/prometheus"
        dto "github.com/prometheus/client_model/go"
 )
 
@@ -110,3 +115,30 @@ func generateTestCertificate(expiry time.Time, IPAddressSAN bool) ([]byte, []byt
        pemKey := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privatekey)})
        return pemCert, pemKey
 }
+
+func TestChooseProtocol(t *testing.T) {
+       ctx := context.Background()
+       registry := prometheus.NewPedanticRegistry()
+       w := log.NewSyncWriter(os.Stderr)
+       logger := log.NewLogfmtLogger(w)
+
+       ip, _, err := chooseProtocol(ctx, "ip4", true, "ipv6.google.com", registry, logger)
+       if err != nil {
+               t.Error(err)
+       }
+       if ip == nil || ip.IP.To4() != nil {
+               t.Error("with fallback it should answer")
+       }
+
+       registry = prometheus.NewPedanticRegistry()
+
+       ip, _, err = chooseProtocol(ctx, "ip4", false, "ipv6.google.com", registry, logger)
+       if err != nil && err.Error() != "unable to find ip; no fallback" {
+               t.Error(err)
+       } else if err == nil {
+               t.Error("should set error")
+       }
+       if ip != nil {
+               t.Error("without fallback it should not answer")
+       }
+}