mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #2394 from arkodg/dns-lookup-case-insensitive
Make DNS records and queries case-insensitive
This commit is contained in:
commit
bdd0b7bb40
3 changed files with 29 additions and 20 deletions
|
@ -1381,14 +1381,18 @@ func delIPToName(ipMap setmatrix.SetMatrix, name, serviceID string, ip net.IP) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func addNameToIP(svcMap setmatrix.SetMatrix, name, serviceID string, epIP net.IP) {
|
func addNameToIP(svcMap setmatrix.SetMatrix, name, serviceID string, epIP net.IP) {
|
||||||
svcMap.Insert(name, svcMapEntry{
|
// Since DNS name resolution is case-insensitive, Use the lower-case form
|
||||||
|
// of the name as the key into svcMap
|
||||||
|
lowerCaseName := strings.ToLower(name)
|
||||||
|
svcMap.Insert(lowerCaseName, svcMapEntry{
|
||||||
ip: epIP.String(),
|
ip: epIP.String(),
|
||||||
serviceID: serviceID,
|
serviceID: serviceID,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func delNameToIP(svcMap setmatrix.SetMatrix, name, serviceID string, epIP net.IP) {
|
func delNameToIP(svcMap setmatrix.SetMatrix, name, serviceID string, epIP net.IP) {
|
||||||
svcMap.Remove(name, svcMapEntry{
|
lowerCaseName := strings.ToLower(name)
|
||||||
|
svcMap.Remove(lowerCaseName, svcMapEntry{
|
||||||
ip: epIP.String(),
|
ip: epIP.String(),
|
||||||
serviceID: serviceID,
|
serviceID: serviceID,
|
||||||
})
|
})
|
||||||
|
@ -1956,6 +1960,7 @@ func (n *network) ResolveName(req string, ipType int) ([]net.IP, bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
req = strings.TrimSuffix(req, ".")
|
req = strings.TrimSuffix(req, ".")
|
||||||
|
req = strings.ToLower(req)
|
||||||
ipSet, ok := sr.svcMap.Get(req)
|
ipSet, ok := sr.svcMap.Get(req)
|
||||||
|
|
||||||
if ipType == types.IPv6 {
|
if ipType == types.IPv6 {
|
||||||
|
|
|
@ -366,8 +366,8 @@ func (r *resolver) ServeDNS(w dns.ResponseWriter, query *dns.Msg) {
|
||||||
if query == nil || len(query.Question) == 0 {
|
if query == nil || len(query.Question) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
name := query.Question[0].Name
|
|
||||||
|
|
||||||
|
name := query.Question[0].Name
|
||||||
switch query.Question[0].Qtype {
|
switch query.Question[0].Qtype {
|
||||||
case dns.TypeA:
|
case dns.TypeA:
|
||||||
resp, err = r.handleIPQuery(name, query, types.IPv4)
|
resp, err = r.handleIPQuery(name, query, types.IPv4)
|
||||||
|
|
|
@ -126,29 +126,33 @@ func TestDNSIPQuery(t *testing.T) {
|
||||||
r := NewResolver(resolverIPSandbox, false, sb.Key(), sb.(*sandbox))
|
r := NewResolver(resolverIPSandbox, false, sb.Key(), sb.(*sandbox))
|
||||||
|
|
||||||
// test name1's IP is resolved correctly with the default A type query
|
// test name1's IP is resolved correctly with the default A type query
|
||||||
q := new(dns.Msg)
|
// Also make sure DNS lookups are case insensitive
|
||||||
q.SetQuestion("name1", dns.TypeA)
|
names := []string{"name1", "NaMe1"}
|
||||||
r.(*resolver).ServeDNS(w, q)
|
for _, name := range names {
|
||||||
resp := w.GetResponse()
|
q := new(dns.Msg)
|
||||||
checkNonNullResponse(t, resp)
|
q.SetQuestion(name, dns.TypeA)
|
||||||
t.Log("Response: ", resp.String())
|
r.(*resolver).ServeDNS(w, q)
|
||||||
checkDNSResponseCode(t, resp, dns.RcodeSuccess)
|
resp := w.GetResponse()
|
||||||
checkDNSAnswersCount(t, resp, 1)
|
checkNonNullResponse(t, resp)
|
||||||
checkDNSRRType(t, resp.Answer[0].Header().Rrtype, dns.TypeA)
|
t.Log("Response: ", resp.String())
|
||||||
if answer, ok := resp.Answer[0].(*dns.A); ok {
|
checkDNSResponseCode(t, resp, dns.RcodeSuccess)
|
||||||
if !bytes.Equal(answer.A, net.ParseIP("192.168.0.1")) {
|
checkDNSAnswersCount(t, resp, 1)
|
||||||
t.Fatalf("IP response in Answer %v does not match 192.168.0.1", answer.A)
|
checkDNSRRType(t, resp.Answer[0].Header().Rrtype, dns.TypeA)
|
||||||
|
if answer, ok := resp.Answer[0].(*dns.A); ok {
|
||||||
|
if !bytes.Equal(answer.A, net.ParseIP("192.168.0.1")) {
|
||||||
|
t.Fatalf("IP response in Answer %v does not match 192.168.0.1", answer.A)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
t.Fatal("Answer of type A not found")
|
||||||
}
|
}
|
||||||
} else {
|
w.ClearResponse()
|
||||||
t.Fatal("Answer of type A not found")
|
|
||||||
}
|
}
|
||||||
w.ClearResponse()
|
|
||||||
|
|
||||||
// test MX query with name1 results in Success response with 0 answer records
|
// test MX query with name1 results in Success response with 0 answer records
|
||||||
q = new(dns.Msg)
|
q := new(dns.Msg)
|
||||||
q.SetQuestion("name1", dns.TypeMX)
|
q.SetQuestion("name1", dns.TypeMX)
|
||||||
r.(*resolver).ServeDNS(w, q)
|
r.(*resolver).ServeDNS(w, q)
|
||||||
resp = w.GetResponse()
|
resp := w.GetResponse()
|
||||||
checkNonNullResponse(t, resp)
|
checkNonNullResponse(t, resp)
|
||||||
t.Log("Response: ", resp.String())
|
t.Log("Response: ", resp.String())
|
||||||
checkDNSResponseCode(t, resp, dns.RcodeSuccess)
|
checkDNSResponseCode(t, resp, dns.RcodeSuccess)
|
||||||
|
|
Loading…
Reference in a new issue