coredns/plugin/dnstap/handler.go
Miek Gieben dbd1c047cb
Run gostaticheck (#3325)
* Run gostaticheck

Run gostaticcheck on the codebase and fix almost all flagged items.

Only keep

* coremain/run.go:192:2: var appVersion is unused (U1000)
* plugin/chaos/setup.go:54:3: the surrounding loop is unconditionally terminated (SA4004)
* plugin/etcd/setup.go:103:3: the surrounding loop is unconditionally terminated (SA4004)
* plugin/pkg/replacer/replacer.go:274:13: argument should be pointer-like to avoid allocations (SA6002)
* plugin/route53/setup.go:124:28: session.New is deprecated: Use NewSession functions to create sessions instead. NewSession has the same functionality as New except an error can be returned when the func is called instead of waiting to receive an error until a request is made.  (SA1019)
* test/grpc_test.go:25:69: grpc.WithTimeout is deprecated: use DialContext and context.WithTimeout instead.  Will be supported throughout 1.x.  (SA1019)

The first one isn't true, as this is set via ldflags. The rest is
minor. The deprecation should be fixed at some point; I'll file some
issues.

Signed-off-by: Miek Gieben <miek@miek.nl>

* Make sure to plug in the plugins

import the plugins, that file that did this was removed, put it in the
reload test as this requires an almost complete coredns server.

Signed-off-by: Miek Gieben <miek@miek.nl>
2019-10-01 07:41:29 +01:00

88 lines
2.1 KiB
Go

package dnstap
import (
"context"
"time"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/dnstap/taprw"
tap "github.com/dnstap/golang-dnstap"
"github.com/miekg/dns"
)
// Dnstap is the dnstap handler.
type Dnstap struct {
Next plugin.Handler
IO IORoutine
// Set to true to include the relevant raw DNS message into the dnstap messages.
JoinRawMessage bool
}
type (
// IORoutine is the dnstap I/O thread as defined by: <http://dnstap.info/Architecture>.
IORoutine interface {
Dnstap(tap.Dnstap)
}
// Tapper is implemented by the Context passed by the dnstap handler.
Tapper interface {
TapMessage(message *tap.Message)
Pack() bool
}
)
// ContextKey defines the type of key that is used to save data into the context.
type ContextKey string
const (
// DnstapSendOption specifies the Dnstap message to be send. Default is sent all.
DnstapSendOption ContextKey = "dnstap-send-option"
)
// TapMessage implements Tapper.
func (h Dnstap) TapMessage(m *tap.Message) {
t := tap.Dnstap_MESSAGE
h.IO.Dnstap(tap.Dnstap{
Type: &t,
Message: m,
})
}
// Pack returns true if the raw DNS message should be included into the dnstap messages.
func (h Dnstap) Pack() bool {
return h.JoinRawMessage
}
// 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) {
// Add send option into context so other plugin can decide on which DNSTap
// message to be sent out
sendOption := taprw.SendOption{Cq: true, Cr: true}
newCtx := context.WithValue(ctx, DnstapSendOption, &sendOption)
newCtx = ContextWithTapper(newCtx, h)
rw := &taprw.ResponseWriter{
ResponseWriter: w,
Tapper: &h,
Query: r,
Send: &sendOption,
QueryEpoch: time.Now(),
}
code, err := plugin.NextOrFailure(h.Name(), h.Next, newCtx, rw, r)
if err != nil {
// ignore dnstap errors
return code, err
}
if err = rw.DnstapError(); err != nil {
return code, plugin.Error("dnstap", err)
}
return code, nil
}
// Name returns dnstap.
func (h Dnstap) Name() string { return "dnstap" }