Identity and version support for the dnstap plugin (#5555)

* Added identity and version support to dnstap plugin

Signed-off-by: Daniel Jolly <code@danieljolly.com>

* Added missing commas

Signed-off-by: Daniel Jolly <code@danieljolly.com>

* Moved byte slice conversions to setup rather than handler.
Fixed indentation issue.

Signed-off-by: Daniel Jolly <code@danieljolly.com>

* Improved setup config parsing and added tests to detect various configurations

Signed-off-by: Daniel Jolly <code@danieljolly.com>

Signed-off-by: Daniel Jolly <code@danieljolly.com>
Co-authored-by: Daniel Jolly <code@danieljolly.com>
This commit is contained in:
Daniel Jolly 2022-09-07 09:22:38 -04:00 committed by GitHub
parent a740ed7536
commit 0511ca2e4d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 67 additions and 10 deletions

View file

@ -15,11 +15,16 @@ Every message is sent to the socket as soon as it comes in, the *dnstap* plugin
## Syntax ## Syntax
~~~ txt ~~~ txt
dnstap SOCKET [full] dnstap SOCKET [full] {
[identity IDENTITY]
[version VERSION]
}
~~~ ~~~
* **SOCKET** is the socket (path) supplied to the dnstap command line tool. * **SOCKET** is the socket (path) supplied to the dnstap command line tool.
* `full` to include the wire-format DNS message. * `full` to include the wire-format DNS message.
* **IDENTITY** to override the identity of the server. Defaults to the hostname.
* **VERSION** to override the version field. Defaults to the CoreDNS version.
## Examples ## Examples
@ -47,6 +52,15 @@ Log to a remote endpoint by FQDN.
dnstap tcp://example.com:6000 full dnstap tcp://example.com:6000 full
~~~ ~~~
Log to a socket, overriding the default identity and version.
~~~ txt
dnstap /tmp/dnstap.sock {
identity my-dns-server1
version MyDNSServer-1.2.3
}
~~~
## Command Line Tool ## Command Line Tool
Dnstap has a command line tool that can be used to inspect the logging. The tool can be found Dnstap has a command line tool that can be used to inspect the logging. The tool can be found

View file

@ -18,12 +18,14 @@ type Dnstap struct {
// IncludeRawMessage will include the raw DNS message into the dnstap messages if true. // IncludeRawMessage will include the raw DNS message into the dnstap messages if true.
IncludeRawMessage bool IncludeRawMessage bool
Identity []byte
Version []byte
} }
// TapMessage sends the message m to the dnstap interface. // TapMessage sends the message m to the dnstap interface.
func (h Dnstap) TapMessage(m *tap.Message) { func (h Dnstap) TapMessage(m *tap.Message) {
t := tap.Dnstap_MESSAGE t := tap.Dnstap_MESSAGE
h.io.Dnstap(&tap.Dnstap{Type: &t, Message: m}) h.io.Dnstap(&tap.Dnstap{Type: &t, Message: m, Identity: h.Identity, Version: h.Version})
} }
func (h Dnstap) tapQuery(w dns.ResponseWriter, query *dns.Msg, queryTime time.Time) { func (h Dnstap) tapQuery(w dns.ResponseWriter, query *dns.Msg, queryTime time.Time) {

View file

@ -2,6 +2,7 @@ package dnstap
import ( import (
"net/url" "net/url"
"os"
"strings" "strings"
"github.com/coredns/caddy" "github.com/coredns/caddy"
@ -19,10 +20,14 @@ func parseConfig(c *caddy.Controller) (Dnstap, error) {
d := Dnstap{} d := Dnstap{}
endpoint := "" endpoint := ""
if !c.Args(&endpoint) { args := c.RemainingArgs()
if len(args) == 0 {
return d, c.ArgErr() return d, c.ArgErr()
} }
endpoint = args[0]
if strings.HasPrefix(endpoint, "tcp://") { if strings.HasPrefix(endpoint, "tcp://") {
// remote network endpoint // remote network endpoint
endpointURL, err := url.Parse(endpoint) endpointURL, err := url.Parse(endpoint)
@ -37,7 +42,30 @@ func parseConfig(c *caddy.Controller) (Dnstap, error) {
d = Dnstap{io: dio} d = Dnstap{io: dio}
} }
d.IncludeRawMessage = c.NextArg() && c.Val() == "full" d.IncludeRawMessage = len(args) == 2 && args[1] == "full"
hostname, _ := os.Hostname()
d.Identity = []byte(hostname)
d.Version = []byte(caddy.AppName + "-" + caddy.AppVersion)
for c.NextBlock() {
switch c.Val() {
case "identity":
{
if !c.NextArg() {
return d, c.ArgErr()
}
d.Identity = []byte(c.Val())
}
case "version":
{
if !c.NextArg() {
return d, c.ArgErr()
}
d.Version = []byte(c.Val())
}
}
}
return d, nil return d, nil
} }

View file

@ -1,25 +1,32 @@
package dnstap package dnstap
import ( import (
"os"
"testing" "testing"
"github.com/coredns/caddy" "github.com/coredns/caddy"
) )
func TestConfig(t *testing.T) { func TestConfig(t *testing.T) {
hostname, _ := os.Hostname()
tests := []struct { tests := []struct {
in string in string
endpoint string endpoint string
full bool full bool
proto string proto string
fail bool fail bool
identity []byte
version []byte
}{ }{
{"dnstap dnstap.sock full", "dnstap.sock", true, "unix", false}, {"dnstap dnstap.sock full", "dnstap.sock", true, "unix", false, []byte(hostname), []byte("-")},
{"dnstap unix://dnstap.sock", "dnstap.sock", false, "unix", false}, {"dnstap unix://dnstap.sock", "dnstap.sock", false, "unix", false, []byte(hostname), []byte("-")},
{"dnstap tcp://127.0.0.1:6000", "127.0.0.1:6000", false, "tcp", false}, {"dnstap tcp://127.0.0.1:6000", "127.0.0.1:6000", false, "tcp", false, []byte(hostname), []byte("-")},
{"dnstap tcp://[::1]:6000", "[::1]:6000", false, "tcp", false}, {"dnstap tcp://[::1]:6000", "[::1]:6000", false, "tcp", false, []byte(hostname), []byte("-")},
{"dnstap tcp://example.com:6000", "example.com:6000", false, "tcp", false}, {"dnstap tcp://example.com:6000", "example.com:6000", false, "tcp", false, []byte(hostname), []byte("-")},
{"dnstap", "fail", false, "tcp", true}, {"dnstap", "fail", false, "tcp", true, []byte(hostname), []byte("-")},
{"dnstap dnstap.sock full {\nidentity NAME\nversion VER\n}\n", "dnstap.sock", true, "unix", false, []byte("NAME"), []byte("VER")},
{"dnstap dnstap.sock {\nidentity NAME\nversion VER\n}\n", "dnstap.sock", false, "unix", false, []byte("NAME"), []byte("VER")},
{"dnstap {\nidentity NAME\nversion VER\n}\n", "fail", false, "tcp", true, []byte("NAME"), []byte("VER")},
} }
for i, tc := range tests { for i, tc := range tests {
c := caddy.NewTestController("dns", tc.in) c := caddy.NewTestController("dns", tc.in)
@ -43,5 +50,11 @@ func TestConfig(t *testing.T) {
if x := tap.IncludeRawMessage; x != tc.full { if x := tap.IncludeRawMessage; x != tc.full {
t.Errorf("Test %d: expected IncludeRawMessage %t, got %t", i, tc.full, x) t.Errorf("Test %d: expected IncludeRawMessage %t, got %t", i, tc.full, x)
} }
if x := string(tap.Identity); x != string(tc.identity) {
t.Errorf("Test %d: expected identity %s, got %s", i, tc.identity, x)
}
if x := string(tap.Version); x != string(tc.version) {
t.Errorf("Test %d: expected version %s, got %s", i, tc.version, x)
}
} }
} }