Skip to content

Commit

Permalink
Merge pull request #995 from 42wim/rfc2308-soa-ttl
Browse files Browse the repository at this point in the history
Send SOA with negative responses (RFC2308)
  • Loading branch information
ryanuber committed Jul 13, 2015
2 parents cd41dab + 3b1bcae commit 5682b71
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
3 changes: 3 additions & 0 deletions command/agent/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ PARSE:
return
INVALID:
d.logger.Printf("[WARN] dns: QName invalid: %s", qName)
d.addSOA(d.domain, resp)
resp.SetRcode(req, dns.RcodeNameError)
}

Expand Down Expand Up @@ -373,6 +374,7 @@ RPC:

// If we have no address, return not found!
if out.NodeServices == nil {
d.addSOA(d.domain, resp)
resp.SetRcode(req, dns.RcodeNameError)
return
}
Expand Down Expand Up @@ -478,6 +480,7 @@ RPC:

// If we have no nodes, return not found!
if len(out.Nodes) == 0 {
d.addSOA(d.domain, resp)
resp.SetRcode(req, dns.RcodeNameError)
return
}
Expand Down
76 changes: 76 additions & 0 deletions command/agent/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,28 @@ func TestDNS_NodeLookup(t *testing.T) {
if aRec.Hdr.Ttl != 0 {
t.Fatalf("Bad: %#v", in.Answer[0])
}

// lookup a non-existing node, we should receive a SOA
m = new(dns.Msg)
m.SetQuestion("nofoo.node.dc1.consul.", dns.TypeANY)

c = new(dns.Client)
in, _, err = c.Exchange(m, addr.String())
if err != nil {
t.Fatalf("err: %v", err)
}

if len(in.Ns) != 1 {
t.Fatalf("Bad: %#v", in, len(in.Answer))
}

soaRec, ok := in.Ns[0].(*dns.SOA)
if !ok {
t.Fatalf("Bad: %#v", in.Ns[0])
}
if soaRec.Hdr.Ttl != 0 {
t.Fatalf("Bad: %#v", in.Ns[0])
}
}

func TestDNS_CaseInsensitiveNodeLookup(t *testing.T) {
Expand Down Expand Up @@ -542,6 +564,29 @@ func TestDNS_ServiceLookup(t *testing.T) {
if aRec.Hdr.Ttl != 0 {
t.Fatalf("Bad: %#v", in.Extra[0])
}

// lookup a non-existing service, we should receive a SOA
m = new(dns.Msg)
m.SetQuestion("nodb.service.consul.", dns.TypeSRV)

c = new(dns.Client)
addr, _ = srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
in, _, err = c.Exchange(m, addr.String())
if err != nil {
t.Fatalf("err: %v", err)
}

if len(in.Ns) != 1 {
t.Fatalf("Bad: %#v", in)
}

soaRec, ok := in.Ns[0].(*dns.SOA)
if !ok {
t.Fatalf("Bad: %#v", in.Ns[0])
}
if soaRec.Hdr.Ttl != 0 {
t.Fatalf("Bad: %#v", in.Ns[0])
}
}

func TestDNS_ServiceLookup_ServiceAddress(t *testing.T) {
Expand Down Expand Up @@ -1771,3 +1816,34 @@ func TestDNS_ServiceLookup_FilterACL(t *testing.T) {
t.Fatalf("Bad: %#v", in)
}
}

func TestDNS_NonExistingLookup(t *testing.T) {
dir, srv := makeDNSServer(t)
defer os.RemoveAll(dir)
defer srv.agent.Shutdown()

addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)

// lookup a non-existing node, we should receive a SOA
m := new(dns.Msg)
m.SetQuestion("nonexisting.consul.", dns.TypeANY)

c := new(dns.Client)
in, _, err := c.Exchange(m, addr.String())
if err != nil {
t.Fatalf("err: %v", err)
}

if len(in.Ns) != 1 {
t.Fatalf("Bad: %#v", in, len(in.Answer))
}

soaRec, ok := in.Ns[0].(*dns.SOA)
if !ok {
t.Fatalf("Bad: %#v", in.Ns[0])
}
if soaRec.Hdr.Ttl != 0 {
t.Fatalf("Bad: %#v", in.Ns[0])
}

}

0 comments on commit 5682b71

Please sign in to comment.