Fix #4395, fix out of order messages and fix forward perspective. (#4396)

Signed-off-by: Frank Riley <fhriley@gmail.com>
This commit is contained in:
Frank Riley 2021-01-21 02:00:27 -07:00 committed by GitHub
parent 8b2ff6c388
commit eba74389c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 14 deletions

View file

@ -5,6 +5,7 @@ import (
"time" "time"
"github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/dnstap/msg"
tap "github.com/dnstap/golang-dnstap" tap "github.com/dnstap/golang-dnstap"
"github.com/miekg/dns" "github.com/miekg/dns"
@ -25,6 +26,19 @@ func (h Dnstap) TapMessage(m *tap.Message) {
h.io.Dnstap(tap.Dnstap{Type: &t, Message: m}) h.io.Dnstap(tap.Dnstap{Type: &t, Message: m})
} }
func (h Dnstap) tapQuery(w dns.ResponseWriter, query *dns.Msg, queryTime time.Time) {
q := new(tap.Message)
msg.SetQueryTime(q, queryTime)
msg.SetQueryAddress(q, w.RemoteAddr())
if h.IncludeRawMessage {
buf, _ := query.Pack()
q.QueryMessage = buf
}
msg.SetType(q, tap.Message_CLIENT_QUERY)
h.TapMessage(q)
}
// ServeDNS logs the client query and response to dnstap and passes the dnstap Context. // ServeDNS logs the client query and response to dnstap and passes the dnstap Context.
func (h Dnstap) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) { func (h Dnstap) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
rw := &ResponseWriter{ rw := &ResponseWriter{
@ -34,6 +48,10 @@ func (h Dnstap) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg)
queryTime: time.Now(), queryTime: time.Now(),
} }
// The query tap message should be sent before sending the query to the
// forwarder. Otherwise, the tap messages will come out out of order.
h.tapQuery(w, r, rw.queryTime)
return plugin.NextOrFailure(h.Name(), h.Next, ctx, rw, r) return plugin.NextOrFailure(h.Name(), h.Next, ctx, rw, r)
} }

View file

@ -19,18 +19,6 @@ type ResponseWriter struct {
// WriteMsg writes back the response to the client and THEN works on logging the request and response to dnstap. // WriteMsg writes back the response to the client and THEN works on logging the request and response to dnstap.
func (w *ResponseWriter) WriteMsg(resp *dns.Msg) error { func (w *ResponseWriter) WriteMsg(resp *dns.Msg) error {
err := w.ResponseWriter.WriteMsg(resp) err := w.ResponseWriter.WriteMsg(resp)
q := new(tap.Message)
msg.SetQueryTime(q, w.queryTime)
msg.SetQueryAddress(q, w.RemoteAddr())
if w.IncludeRawMessage {
buf, _ := w.query.Pack()
q.QueryMessage = buf
}
msg.SetType(q, tap.Message_CLIENT_QUERY)
w.TapMessage(q)
if err != nil { if err != nil {
return err return err
} }

View file

@ -34,7 +34,10 @@ func toDnstap(f *Forward, host string, state request.Request, opts options, repl
ta = &net.TCPAddr{IP: ip, Port: int(port)} ta = &net.TCPAddr{IP: ip, Port: int(port)}
} }
msg.SetQueryAddress(q, ta) // Forwarder dnstap messages are from the perspective of the downstream server
// (upstream is the forward server)
msg.SetQueryAddress(q, state.W.RemoteAddr())
msg.SetResponseAddress(q, ta)
if f.tapPlugin.IncludeRawMessage { if f.tapPlugin.IncludeRawMessage {
buf, _ := state.Req.Pack() buf, _ := state.Req.Pack()
@ -51,7 +54,8 @@ func toDnstap(f *Forward, host string, state request.Request, opts options, repl
r.ResponseMessage = buf r.ResponseMessage = buf
} }
msg.SetQueryTime(r, start) msg.SetQueryTime(r, start)
msg.SetQueryAddress(r, ta) msg.SetQueryAddress(r, state.W.RemoteAddr())
msg.SetResponseAddress(r, ta)
msg.SetResponseTime(r, time.Now()) msg.SetResponseTime(r, time.Now())
msg.SetType(r, tap.Message_FORWARDER_RESPONSE) msg.SetType(r, tap.Message_FORWARDER_RESPONSE)
f.tapPlugin.TapMessage(r) f.tapPlugin.TapMessage(r)