Merge pull request #4328 from hashicorp/bugfix/prevent-multi-c

Only allow 1 CNAME when querying for a service.
This commit is contained in:
Matt Keeler 2018-07-10 10:39:03 -04:00 committed by GitHub
commit ad3d5e3738
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 200 additions and 2 deletions

View File

@ -1143,6 +1143,7 @@ func (d *DNSServer) serviceNodeRecords(dc string, nodes structs.CheckServiceNode
qType := req.Question[0].Qtype
handled := make(map[string]struct{})
edns := req.IsEdns0() != nil
var answerCNAME []dns.RR = nil
count := 0
for _, node := range nodes {
@ -1170,8 +1171,17 @@ func (d *DNSServer) serviceNodeRecords(dc string, nodes structs.CheckServiceNode
had_answer := false
records, meta := d.formatNodeRecord(node.Node, addr, qName, qType, ttl, edns)
if records != nil {
resp.Answer = append(resp.Answer, records...)
had_answer = true
switch records[0].(type) {
case *dns.CNAME:
// keep track of the first CNAME + associated RRs but don't add to the resp.Answer yet
// this will only be added if no non-CNAME RRs are found
if len(answerCNAME) == 0 {
answerCNAME = records
}
default:
resp.Answer = append(resp.Answer, records...)
had_answer = true
}
}
if meta != nil && (qType == dns.TypeANY || qType == dns.TypeTXT) {
@ -1189,6 +1199,10 @@ func (d *DNSServer) serviceNodeRecords(dc string, nodes structs.CheckServiceNode
}
}
}
if len(resp.Answer) == 0 && len(answerCNAME) > 0 {
resp.Answer = answerCNAME
}
}
// serviceARecords is used to add the SRV records for a service lookup

View File

@ -982,6 +982,190 @@ func TestDNS_ServiceReverseLookupNodeAddress(t *testing.T) {
}
}
func TestDNS_ServiceLookupNoMultiCNAME(t *testing.T) {
t.Parallel()
a := NewTestAgent(t.Name(), "")
defer a.Shutdown()
// Register a node with a service.
{
args := &structs.RegisterRequest{
Datacenter: "dc1",
Node: "foo",
Address: "198.18.0.1",
Service: &structs.NodeService{
Service: "db",
Port: 12345,
Address: "foo.node.consul",
},
}
var out struct{}
require.NoError(t, a.RPC("Catalog.Register", args, &out))
}
// Register a second node node with the same service.
{
args := &structs.RegisterRequest{
Datacenter: "dc1",
Node: "bar",
Address: "198.18.0.2",
Service: &structs.NodeService{
Service: "db",
Port: 12345,
Address: "bar.node.consul",
},
}
var out struct{}
if err := a.RPC("Catalog.Register", args, &out); err != nil {
t.Fatalf("err: %v", err)
}
}
m := new(dns.Msg)
m.SetQuestion("db.service.consul.", dns.TypeANY)
c := new(dns.Client)
in, _, err := c.Exchange(m, a.DNSAddr())
require.NoError(t, err)
// expect a CNAME and an A RR
require.Len(t, in.Answer, 2)
require.IsType(t, &dns.CNAME{}, in.Answer[0])
require.IsType(t, &dns.A{}, in.Answer[1])
}
func TestDNS_ServiceLookupPreferNoCNAME(t *testing.T) {
t.Parallel()
a := NewTestAgent(t.Name(), "")
defer a.Shutdown()
// Register a node with a service.
{
args := &structs.RegisterRequest{
Datacenter: "dc1",
Node: "foo",
Address: "198.18.0.1",
Service: &structs.NodeService{
Service: "db",
Port: 12345,
Address: "198.18.0.1",
},
}
var out struct{}
require.NoError(t, a.RPC("Catalog.Register", args, &out))
}
// Register a second node node with the same service.
{
args := &structs.RegisterRequest{
Datacenter: "dc1",
Node: "bar",
Address: "198.18.0.2",
Service: &structs.NodeService{
Service: "db",
Port: 12345,
Address: "bar.node.consul",
},
}
var out struct{}
if err := a.RPC("Catalog.Register", args, &out); err != nil {
t.Fatalf("err: %v", err)
}
}
m := new(dns.Msg)
m.SetQuestion("db.service.consul.", dns.TypeANY)
c := new(dns.Client)
in, _, err := c.Exchange(m, a.DNSAddr())
require.NoError(t, err)
// expect a CNAME and an A RR
require.Len(t, in.Answer, 1)
aRec, ok := in.Answer[0].(*dns.A)
require.Truef(t, ok, "Not an A RR")
require.Equal(t, "db.service.consul.", aRec.Hdr.Name)
require.Equal(t, "198.18.0.1", aRec.A.String())
}
func TestDNS_ServiceLookupMultiAddrNoCNAME(t *testing.T) {
t.Parallel()
a := NewTestAgent(t.Name(), "")
defer a.Shutdown()
// Register a node with a service.
{
args := &structs.RegisterRequest{
Datacenter: "dc1",
Node: "foo",
Address: "198.18.0.1",
Service: &structs.NodeService{
Service: "db",
Port: 12345,
Address: "198.18.0.1",
},
}
var out struct{}
require.NoError(t, a.RPC("Catalog.Register", args, &out))
}
// Register a second node node with the same service.
{
args := &structs.RegisterRequest{
Datacenter: "dc1",
Node: "bar",
Address: "198.18.0.2",
Service: &structs.NodeService{
Service: "db",
Port: 12345,
Address: "bar.node.consul",
},
}
var out struct{}
if err := a.RPC("Catalog.Register", args, &out); err != nil {
t.Fatalf("err: %v", err)
}
}
// Register a second node node with the same service.
{
args := &structs.RegisterRequest{
Datacenter: "dc1",
Node: "baz",
Address: "198.18.0.3",
Service: &structs.NodeService{
Service: "db",
Port: 12345,
Address: "198.18.0.3",
},
}
var out struct{}
if err := a.RPC("Catalog.Register", args, &out); err != nil {
t.Fatalf("err: %v", err)
}
}
m := new(dns.Msg)
m.SetQuestion("db.service.consul.", dns.TypeANY)
c := new(dns.Client)
in, _, err := c.Exchange(m, a.DNSAddr())
require.NoError(t, err)
// expect a CNAME and an A RR
require.Len(t, in.Answer, 2)
require.IsType(t, &dns.A{}, in.Answer[0])
require.IsType(t, &dns.A{}, in.Answer[1])
}
func TestDNS_ServiceLookup(t *testing.T) {
t.Parallel()
a := NewTestAgent(t.Name(), "")