mirror of
https://github.com/coredns/coredns.git
synced 2025-11-28 06:34:08 -05:00
Remove the word middleware (#1067)
* Rename middleware to plugin first pass; mostly used 'sed', few spots where I manually changed text. This still builds a coredns binary. * fmt error * Rename AddMiddleware to AddPlugin * Readd AddMiddleware to remain backwards compat
This commit is contained in:
73
plugin/dnstap/taprw/writer.go
Normal file
73
plugin/dnstap/taprw/writer.go
Normal file
@@ -0,0 +1,73 @@
|
||||
// Package taprw takes a query and intercepts the response.
|
||||
// It will log both after the response is written.
|
||||
package taprw
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/coredns/coredns/plugin/dnstap/msg"
|
||||
|
||||
tap "github.com/dnstap/golang-dnstap"
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
// Tapper is what ResponseWriter needs to log to dnstap.
|
||||
type Tapper interface {
|
||||
TapMessage(m *tap.Message) error
|
||||
TapBuilder() msg.Builder
|
||||
}
|
||||
|
||||
// ResponseWriter captures the client response and logs the query to dnstap.
|
||||
// Single request use.
|
||||
type ResponseWriter struct {
|
||||
queryEpoch uint64
|
||||
Query *dns.Msg
|
||||
dns.ResponseWriter
|
||||
Tapper
|
||||
err error
|
||||
}
|
||||
|
||||
// DnstapError check if a dnstap error occurred during Write and returns it.
|
||||
func (w ResponseWriter) DnstapError() error {
|
||||
return w.err
|
||||
}
|
||||
|
||||
// QueryEpoch sets the query epoch as reported by dnstap.
|
||||
func (w *ResponseWriter) QueryEpoch() {
|
||||
w.queryEpoch = msg.Epoch()
|
||||
}
|
||||
|
||||
// WriteMsg writes back the response to the client and THEN works on logging the request
|
||||
// and response to dnstap.
|
||||
// Dnstap errors are to be checked by DnstapError.
|
||||
func (w *ResponseWriter) WriteMsg(resp *dns.Msg) (writeErr error) {
|
||||
writeErr = w.ResponseWriter.WriteMsg(resp)
|
||||
writeEpoch := msg.Epoch()
|
||||
|
||||
b := w.TapBuilder()
|
||||
b.TimeSec = w.queryEpoch
|
||||
if err := func() (err error) {
|
||||
err = b.AddrMsg(w.ResponseWriter.RemoteAddr(), w.Query)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return w.TapMessage(b.ToClientQuery())
|
||||
}(); err != nil {
|
||||
w.err = fmt.Errorf("client query: %s", err)
|
||||
// don't forget to call DnstapError later
|
||||
}
|
||||
|
||||
if writeErr == nil {
|
||||
if err := func() (err error) {
|
||||
b.TimeSec = writeEpoch
|
||||
if err = b.Msg(resp); err != nil {
|
||||
return
|
||||
}
|
||||
return w.TapMessage(b.ToClientResponse())
|
||||
}(); err != nil {
|
||||
w.err = fmt.Errorf("client response: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
82
plugin/dnstap/taprw/writer_test.go
Normal file
82
plugin/dnstap/taprw/writer_test.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package taprw
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/coredns/coredns/plugin/dnstap/msg"
|
||||
"github.com/coredns/coredns/plugin/dnstap/test"
|
||||
mwtest "github.com/coredns/coredns/plugin/test"
|
||||
|
||||
tap "github.com/dnstap/golang-dnstap"
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
type TapFailer struct {
|
||||
}
|
||||
|
||||
func (TapFailer) TapMessage(*tap.Message) error {
|
||||
return errors.New("failed")
|
||||
}
|
||||
func (TapFailer) TapBuilder() msg.Builder {
|
||||
return msg.Builder{Full: true}
|
||||
}
|
||||
|
||||
func TestDnstapError(t *testing.T) {
|
||||
rw := ResponseWriter{
|
||||
Query: new(dns.Msg),
|
||||
ResponseWriter: &mwtest.ResponseWriter{},
|
||||
Tapper: TapFailer{},
|
||||
}
|
||||
if err := rw.WriteMsg(new(dns.Msg)); err != nil {
|
||||
t.Errorf("dnstap error during Write: %s", err)
|
||||
}
|
||||
if rw.DnstapError() == nil {
|
||||
t.Fatal("no dnstap error")
|
||||
}
|
||||
}
|
||||
|
||||
func testingMsg() (m *dns.Msg) {
|
||||
m = new(dns.Msg)
|
||||
m.SetQuestion("example.com.", dns.TypeA)
|
||||
m.SetEdns0(4097, true)
|
||||
return
|
||||
}
|
||||
|
||||
func TestClientQueryResponse(t *testing.T) {
|
||||
trapper := test.TrapTapper{Full: true}
|
||||
m := testingMsg()
|
||||
rw := ResponseWriter{
|
||||
Query: m,
|
||||
Tapper: &trapper,
|
||||
ResponseWriter: &mwtest.ResponseWriter{},
|
||||
}
|
||||
d := test.TestingData()
|
||||
|
||||
// will the wire-format msg be reported?
|
||||
bin, err := m.Pack()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
return
|
||||
}
|
||||
d.Packed = bin
|
||||
|
||||
if err := rw.WriteMsg(m); err != nil {
|
||||
t.Fatal(err)
|
||||
return
|
||||
}
|
||||
if l := len(trapper.Trap); l != 2 {
|
||||
t.Fatalf("%d msg trapped", l)
|
||||
return
|
||||
}
|
||||
want := d.ToClientQuery()
|
||||
have := trapper.Trap[0]
|
||||
if !test.MsgEqual(want, have) {
|
||||
t.Fatalf("query: want: %v\nhave: %v", want, have)
|
||||
}
|
||||
want = d.ToClientResponse()
|
||||
have = trapper.Trap[1]
|
||||
if !test.MsgEqual(want, have) {
|
||||
t.Fatalf("response: want: %v\nhave: %v", want, have)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user