More cleanup - needs to think a little about NewSOA()

This commit is contained in:
Miek Gieben 2016-03-22 10:29:48 +00:00
parent 22dade9e12
commit 1a7f0deadd
5 changed files with 138 additions and 114 deletions

View file

@ -147,6 +147,14 @@ func (g Etcd) Ttl(node *etcdc.Node, serv *msg.Service) uint32 {
return serv.Ttl
}
// etcNameError checks if the error is ErrorCodeKeyNotFound from etcd.
func isEtcdNameError(err error) bool {
if e, ok := err.(etcdc.Error); ok && e.Code == etcdc.ErrorCodeKeyNotFound {
return true
}
return false
}
const (
priority = 10 // default priority when nothing is set
ttl = 300 // default ttl when nothing is set

View file

@ -16,90 +16,59 @@ func (e Etcd) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (i
m.RecursionAvailable = true
m.Compress = true
return 0, nil
}
// TODO(miek): get current zone when serving multiple
zone := "."
// only needs state and current zone name we are auth for.
/*
func (s *server) ServeDNS(w dns.ResponseWriter, req *dns.Msg) {
q := req.Question[0]
name := strings.ToLower(q.Name)
switch q.Qtype {
case dns.TypeNS:
records, extra, err := s.NSRecords(q, s.config.dnsDomain)
if isEtcdNameError(err, s) {
m = s.NameError(req)
return
switch state.Type() {
case "A":
records, err := e.A(zone, state, nil)
case "AAAA":
records, err := e.AAAA(zone, state, nil)
fallthrough
case "TXT":
records, err := e.TXT(zone, state)
fallthrough
case "CNAME":
records, err := e.CNAME(zone, state)
fallthrough
case "MX":
records, extra, err := e.MX(zone, state)
fallthrough
case "SRV":
records, extra, err := e.SRV(zone, state)
if isEtcdNameError(err) {
NameError(zone, state)
return dns.RcodeNameError, nil
}
m.Answer = append(m.Answer, records...)
m.Extra = append(m.Extra, extra...)
case dns.TypeA, dns.TypeAAAA:
records, err := s.AddressRecords(q, name, nil, bufsize, dnssec, false)
if isEtcdNameError(err, s) {
m = s.NameError(req)
return
}
m.Answer = append(m.Answer, records...)
case dns.TypeTXT:
records, err := s.TXTRecords(q, name)
if isEtcdNameError(err, s) {
m = s.NameError(req)
return
}
m.Answer = append(m.Answer, records...)
case dns.TypeCNAME:
records, err := s.CNAMERecords(q, name)
if isEtcdNameError(err, s) {
m = s.NameError(req)
return
}
m.Answer = append(m.Answer, records...)
case dns.TypeMX:
records, extra, err := s.MXRecords(q, name, bufsize, dnssec)
if isEtcdNameError(err, s) {
m = s.NameError(req)
return
}
m.Answer = append(m.Answer, records...)
m.Extra = append(m.Extra, extra...)
default:
fallthrough // also catch other types, so that they return NODATA
case dns.TypeSRV:
records, extra, err := s.SRVRecords(q, name, bufsize, dnssec)
if err != nil {
if isEtcdNameError(err, s) {
m = s.NameError(req)
return
}
logf("got error from backend: %s", err)
if q.Qtype == dns.TypeSRV { // Otherwise NODATA
m = s.ServerFailure(req)
return
}
// TODO(miek): err or nil in this case?
return dns.RcodeServerFailure, err
}
// if we are here again, check the types, because an answer may only
// be given for SRV. All other types should return NODATA, the
// NXDOMAIN part is handled in the above code. TODO(miek): yes this
// can be done in a more elegant manor.
if q.Qtype == dns.TypeSRV {
if len(records) > 0 {
m.Answer = append(m.Answer, records...)
}
if len(extra) > 0 {
m.Extra = append(m.Extra, extra...)
}
default:
// Nodata response
// also catch other types, so that they return NODATA
}
if len(m.Answer) == 0 { // NODATA response
m.Ns = []dns.RR{s.NewSOA()}
m.Ns[0].Header().Ttl = s.config.MinTtl
}
return e.Next.ServeDNS(ctx, w, r)
}
// etcNameError checks if the error is ErrorCodeKeyNotFound from etcd.
func isEtcdNameError(err error, s *server) bool {
if e, ok := err.(etcd.Error); ok && e.Code == etcd.ErrorCodeKeyNotFound {
return true
}
return false
// NameError writes a name error to the client.
func NameError(zone string, state middleware.State) {
m := new(dns.Msg)
m.SetRcode(state.Req, dns.RcodeNameError)
m.Ns = []dns.RR{NewSOA()}
m.Ns[0].Header().Ttl = minTtl
state.W.WriteMsg(m)
}
// NoData write a nodata response to the client.
func NoData(zone string, state middleware.State) {
// TODO(miek): write it
}
*/

View file

@ -11,8 +11,7 @@ import (
)
// need current zone argument.
func (e Etcd) AddressRecords(zone string, state middleware.State, previousRecords []dns.RR) (records []dns.RR, err error) {
func (e Etcd) A(zone string, state middleware.State, previousRecords []dns.RR) (records []dns.RR, err error) {
services, err := e.Records(state.Name(), false)
if err != nil {
return nil, err
@ -41,11 +40,75 @@ func (e Etcd) AddressRecords(zone string, state middleware.State, previousRecord
}
state1 := copyState(state, serv.Host, state.QType())
nextRecords, err := e.AddressRecords(zone, state1, append(previousRecords, newRecord))
nextRecords, err := e.A(zone, state1, append(previousRecords, newRecord))
if err == nil {
// Only have we found something we should add the CNAME and the IP addresses.
// Not only have we found something we should add the CNAME and the IP addresses.
if len(nextRecords) > 0 {
// TODO(miek): sorting here?
records = append(records, newRecord)
records = append(records, nextRecords...)
}
continue
}
// This means we can not complete the CNAME, try to look else where.
target := newRecord.Target
if dns.IsSubDomain(zone, target) {
// We should already have found it
continue
}
m1, e1 := e.Proxy.Lookup(state, target, state.QType())
if e1 != nil {
continue
}
// Len(m1.Answer) > 0 here is well?
records = append(records, newRecord)
records = append(records, m1.Answer...)
continue
case ip.To4() != nil:
records = append(records, serv.NewA(state.QName(), ip.To4()))
case ip.To4() == nil:
// noda?
}
}
return records, nil
}
func (e Etcd) AAAA(zone string, state middleware.State, previousRecords []dns.RR) (records []dns.RR, err error) {
services, err := e.Records(state.Name(), false)
if err != nil {
return nil, err
}
services = msg.Group(services)
for _, serv := range services {
ip := net.ParseIP(serv.Host)
switch {
case ip == nil:
// Try to resolve as CNAME if it's not an IP, but only if we don't create loops.
// TODO(miek): lowercasing, use Match in middleware/
if state.Name() == dns.Fqdn(serv.Host) {
// x CNAME x is a direct loop, don't add those
continue
}
newRecord := serv.NewCNAME(state.QName(), dns.Fqdn(serv.Host))
if len(previousRecords) > 7 {
// don't add it, and just continue
continue
}
if isDuplicateCNAME(newRecord, previousRecords) {
continue
}
state1 := copyState(state, serv.Host, state.QType())
nextRecords, err := e.AAAA(zone, state1, append(previousRecords, newRecord))
if err == nil {
// Not only have we found something we should add the CNAME and the IP addresses.
if len(nextRecords) > 0 {
// TODO(miek): sorting here?
records = append(records, newRecord)
records = append(records, nextRecords...)
}
@ -66,18 +129,18 @@ func (e Etcd) AddressRecords(zone string, state middleware.State, previousRecord
records = append(records, m1.Answer...)
continue
// both here again
case ip.To4() != nil && (state.QType() == dns.TypeA):
records = append(records, serv.NewA(state.QName(), ip.To4()))
case ip.To4() == nil && (state.QType() == dns.TypeAAAA):
case ip.To4() != nil:
// nada?
case ip.To4() == nil:
records = append(records, serv.NewAAAA(state.QName(), ip.To16()))
}
}
return records, nil
}
// SRVRecords returns SRV records from etcd.
// SRV returns SRV records from etcd.
// If the Target is not a name but an IP address, a name is created on the fly.
func (e Etcd) SRVRecords(zone string, state middleware.State) (records []dns.RR, extra []dns.RR, err error) {
func (e Etcd) SRV(zone string, state middleware.State) (records []dns.RR, extra []dns.RR, err error) {
services, err := e.Records(state.Name(), false)
if err != nil {
return nil, nil, err
@ -140,10 +203,11 @@ func (e Etcd) SRVRecords(zone string, state middleware.State) (records []dns.RR,
// view.
state1 := copyState(state, srv.Target, dns.TypeA)
// TODO(both is true here!
addr, e1 := e.AddressRecords(zone, state1, nil)
addr, e1 := e.A(zone, state1, nil)
if e1 == nil {
extra = append(extra, addr...)
}
// e.AAA(zone, state1, nil) as well...
case ip.To4() != nil:
serv.Host = e.Domain(serv.Key)
srv := serv.NewSRV(state.QName(), weight)
@ -161,9 +225,9 @@ func (e Etcd) SRVRecords(zone string, state middleware.State) (records []dns.RR,
return records, extra, nil
}
// MXRecords returns MX records from etcd.
// MX returns MX records from etcd.
// If the Target is not a name but an IP address, a name is created on the fly.
func (e Etcd) MXRecords(zone string, state middleware.State) (records []dns.RR, extra []dns.RR, err error) {
func (e Etcd) MX(zone string, state middleware.State) (records []dns.RR, extra []dns.RR, err error) {
services, err := e.Records(state.Name(), false)
if err != nil {
return nil, nil, err
@ -204,10 +268,11 @@ func (e Etcd) MXRecords(zone string, state middleware.State) (records []dns.RR,
// Internal name
// both is true here as well
state1 := copyState(state, mx.Mx, dns.TypeA)
addr, e1 := e.AddressRecords(zone, state1, nil)
addr, e1 := e.A(zone, state1, nil)
if e1 == nil {
extra = append(extra, addr...)
}
// e.AAAA as well
case ip.To4() != nil:
serv.Host = e.Domain(serv.Key)
records = append(records, serv.NewMX(state.QName()))
@ -221,7 +286,7 @@ func (e Etcd) MXRecords(zone string, state middleware.State) (records []dns.RR,
return records, extra, nil
}
func (e Etcd) CNAMERecords(zone string, state middleware.State) (records []dns.RR, err error) {
func (e Etcd) CNAME(zone string, state middleware.State) (records []dns.RR, err error) {
services, err := e.Records(state.Name(), true)
if err != nil {
return nil, err
@ -238,7 +303,7 @@ func (e Etcd) CNAMERecords(zone string, state middleware.State) (records []dns.R
return records, nil
}
func (e Etcd) TXTRecords(zone string, state middleware.State) (records []dns.RR, err error) {
func (e Etcd) TXT(zone string, state middleware.State) (records []dns.RR, err error) {
services, err := e.Records(state.Name(), false)
if err != nil {
return nil, err
@ -255,6 +320,10 @@ func (e Etcd) TXTRecords(zone string, state middleware.State) (records []dns.RR,
return records, nil
}
func (e Etcd) SOA(zone string, state middleware.State) *dns.SOA {
return nil
}
func isDuplicateCNAME(r *dns.CNAME, records []dns.RR) bool {
for _, rec := range records {
if v, ok := rec.(*dns.CNAME); ok {
@ -271,14 +340,3 @@ func copyState(state middleware.State, target string, typ uint16) middleware.Sta
state1.Req.Question[0] = dns.Question{dns.Fqdn(target), dns.ClassINET, typ}
return state1
}
/*
// Move to state.go somehow?
func (s *server) NameError(req *dns.Msg) *dns.Msg {
m := new(dns.Msg)
m.SetRcode(req, dns.RcodeNameError)
m.Ns = []dns.RR{s.NewSOA()}
m.Ns[0].Header().Ttl = s.config.MinTtl
return m
}
*/

View file

@ -66,21 +66,11 @@ func (s *Service) NewCNAME(name string, target string) *dns.CNAME {
return &dns.CNAME{Hdr: dns.RR_Header{Name: name, Rrtype: dns.TypeCNAME, Class: dns.ClassINET, Ttl: s.Ttl}, Target: target}
}
// NewNS returns a new NS record based on the Service.
func (s *Service) NewNS(name string, target string) *dns.NS {
return &dns.NS{Hdr: dns.RR_Header{Name: name, Rrtype: dns.TypeNS, Class: dns.ClassINET, Ttl: s.Ttl}, Ns: target}
}
// NewTXT returns a new TXT record based on the Service.
func (s *Service) NewTXT(name string) *dns.TXT {
return &dns.TXT{Hdr: dns.RR_Header{Name: name, Rrtype: dns.TypeTXT, Class: dns.ClassINET, Ttl: s.Ttl}, Txt: split255(s.Text)}
}
// NewPTR returns a new PTR record based on the Service.
func (s *Service) NewPTR(name string, ttl uint32) *dns.PTR {
return &dns.PTR{Hdr: dns.RR_Header{Name: name, Rrtype: dns.TypePTR, Class: dns.ClassINET, Ttl: ttl}, Ptr: dns.Fqdn(s.Host)}
}
// Group checks the services in sx, it looks for a Group attribute on the shortest
// keys. If there are multiple shortest keys *and* the group attribute disagrees (and
// is not empty), we don't consider it a group.

View file

@ -16,8 +16,7 @@ func TestLookupProxy(t *testing.T) {
defer log.SetOutput(os.Stderr)
p := New([]string{"8.8.8.8:53"})
fakestate := fakeState()
resp, err := p.Lookup(fakestate, "example.org.", dns.TypeA)
resp, err := p.Lookup(fakeState(), "example.org.", dns.TypeA)
if err != nil {
t.Error("Expected to receive reply, but didn't")
}