Plugin dnstap: add support for "extra" field in payload (#6226)
* dnstap: add 'extra' field Signed-off-by: chenyuheng <chenyuheng99@qq.com> * dnstap: add setup_test for 'extra' field Signed-off-by: chenyuheng <chenyuheng99@qq.com> * udnstap: update document and test Signed-off-by: chenyuheng <chenyuheng99@qq.com> * dnstap: update setup_test for more coverage Signed-off-by: chenyuheng <chenyuheng99@qq.com> * dnstap: add TapMessageWithMetadata function to Dnstap Signed-off-by: chenyuheng <chenyuheng99@qq.com> * dnstap: adapt dnstap and forward plugins to use TapMessageWithMetadata Signed-off-by: chenyuheng <chenyuheng99@qq.com> * change TapMessageWithMetadata function Signed-off-by: chenyuheng <chenyuheng99@qq.com> * tab inconsistency fix Signed-off-by: chenyuheng <chenyuheng99@qq.com> * fix replacer to support empty state Signed-off-by: chenyuheng <chenyuheng99@qq.com> * add replacer test for empty status parameter Signed-off-by: chenyuheng <chenyuheng99@qq.com> * dnstap: update unit test for 'extra' field Signed-off-by: chenyuheng <chenyuheng99@qq.com> * clean up code Signed-off-by: chenyuheng <chenyuheng99@qq.com> * gofmt fix & static analysis fix Signed-off-by: chenyuheng <chenyuheng99@qq.com> * dnstap: refactor Signed-off-by: chenyuheng <chenyuheng99@qq.com> --------- Signed-off-by: chenyuheng <chenyuheng99@qq.com>
This commit is contained in:
parent
eec26e59c6
commit
90d55611a2
10 changed files with 196 additions and 64 deletions
|
@ -6,6 +6,8 @@ import (
|
|||
|
||||
"github.com/coredns/coredns/plugin"
|
||||
"github.com/coredns/coredns/plugin/dnstap/msg"
|
||||
"github.com/coredns/coredns/plugin/pkg/replacer"
|
||||
"github.com/coredns/coredns/request"
|
||||
|
||||
tap "github.com/dnstap/golang-dnstap"
|
||||
"github.com/miekg/dns"
|
||||
|
@ -15,20 +17,40 @@ import (
|
|||
type Dnstap struct {
|
||||
Next plugin.Handler
|
||||
io tapper
|
||||
repl replacer.Replacer
|
||||
|
||||
// IncludeRawMessage will include the raw DNS message into the dnstap messages if true.
|
||||
IncludeRawMessage bool
|
||||
Identity []byte
|
||||
Version []byte
|
||||
ExtraFormat string
|
||||
}
|
||||
|
||||
// TapMessage sends the message m to the dnstap interface.
|
||||
// TapMessage sends the message m to the dnstap interface, without populating "Extra" field.
|
||||
func (h Dnstap) TapMessage(m *tap.Message) {
|
||||
t := tap.Dnstap_MESSAGE
|
||||
h.io.Dnstap(&tap.Dnstap{Type: &t, Message: m, Identity: h.Identity, Version: h.Version})
|
||||
if h.ExtraFormat == "" {
|
||||
h.tapWithExtra(m, nil)
|
||||
} else {
|
||||
h.tapWithExtra(m, []byte(h.ExtraFormat))
|
||||
}
|
||||
}
|
||||
|
||||
func (h Dnstap) tapQuery(w dns.ResponseWriter, query *dns.Msg, queryTime time.Time) {
|
||||
// TapMessageWithMetadata sends the message m to the dnstap interface, with "Extra" field being populated.
|
||||
func (h Dnstap) TapMessageWithMetadata(ctx context.Context, m *tap.Message, state request.Request) {
|
||||
if h.ExtraFormat == "" {
|
||||
h.tapWithExtra(m, nil)
|
||||
return
|
||||
}
|
||||
extraStr := h.repl.Replace(ctx, state, nil, h.ExtraFormat)
|
||||
h.tapWithExtra(m, []byte(extraStr))
|
||||
}
|
||||
|
||||
func (h Dnstap) tapWithExtra(m *tap.Message, extra []byte) {
|
||||
t := tap.Dnstap_MESSAGE
|
||||
h.io.Dnstap(&tap.Dnstap{Type: &t, Message: m, Identity: h.Identity, Version: h.Version, Extra: extra})
|
||||
}
|
||||
|
||||
func (h Dnstap) tapQuery(ctx context.Context, w dns.ResponseWriter, query *dns.Msg, queryTime time.Time) {
|
||||
q := new(tap.Message)
|
||||
msg.SetQueryTime(q, queryTime)
|
||||
msg.SetQueryAddress(q, w.RemoteAddr())
|
||||
|
@ -38,7 +60,8 @@ func (h Dnstap) tapQuery(w dns.ResponseWriter, query *dns.Msg, queryTime time.Ti
|
|||
q.QueryMessage = buf
|
||||
}
|
||||
msg.SetType(q, tap.Message_CLIENT_QUERY)
|
||||
h.TapMessage(q)
|
||||
state := request.Request{W: w, Req: query}
|
||||
h.TapMessageWithMetadata(ctx, q, state)
|
||||
}
|
||||
|
||||
// ServeDNS logs the client query and response to dnstap and passes the dnstap Context.
|
||||
|
@ -47,12 +70,13 @@ func (h Dnstap) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg)
|
|||
ResponseWriter: w,
|
||||
Dnstap: h,
|
||||
query: r,
|
||||
ctx: ctx,
|
||||
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)
|
||||
h.tapQuery(ctx, w, r, rw.queryTime)
|
||||
|
||||
return plugin.NextOrFailure(h.Name(), h.Next, ctx, rw, r)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue