Skip to content

Commit

Permalink
dnstap collector: decode query zone field (#621)
Browse files Browse the repository at this point in the history
* Decode query zone
* Update README.md
  • Loading branch information
dmachard authored Feb 25, 2024
1 parent 2ef2163 commit d198ad5
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 4 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<p align="center">
<img src="https://goreportcard.com/badge/github.com/dmachard/go-dns-collector" alt="Go Report"/>
<img src="https://img.shields.io/badge/go%20version-min%201.20-green" alt="Go version"/>
<img src="https://img.shields.io/badge/go%20tests-414-green" alt="Go tests"/>
<img src="https://img.shields.io/badge/go%20lines-38159-green" alt="Go lines"/>
<img src="https://img.shields.io/badge/go%20tests-417-green" alt="Go tests"/>
<img src="https://img.shields.io/badge/go%20lines-38329-green" alt="Go lines"/>
</p>

<p align="center">
Expand Down Expand Up @@ -50,7 +50,7 @@
- [`Stdout`](docs/loggers/logger_stdout.md) console in text or binary output
- [`File`](docs/loggers/logger_file.md) with automatic rotation and compression
- *Provide metrics and API*
- [`Prometheus`](docs/loggers/logger_prometheus.md) metrics
- [`Prometheus`](docs/loggers/logger_prometheus.md) exporter
- [`Statsd`](docs/loggers/logger_statsd.md) support
- [`REST API`](docs/loggers/logger_restapi.md) with [swagger](https://generator.swagger.io/?url=https://raw.githubusercontent.com/dmachard/go-dnscollector/main/docs/swagger.yml) to search DNS domains
- *Send to remote host with generic transport protocol*
Expand Down
6 changes: 6 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ global:
# - version: dnstap version
# - extra: dnstap extra as string
# - operation: dnstap operation
# - policy-rule: dnstap policy rule
# - policy-type: dnstap policy type
# - policy-action: dnstap policy action
# - policy-match: dnstap policy match
# - policy-value: dnstap policy value
# - quey-zone: dnstap query zone
# - opcode: dns opcode (integer)
# - rcode: dns return code
# - queryip: dns query ip
Expand Down
4 changes: 4 additions & 0 deletions dnsutils/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ type DNSTap struct {
PolicyAction string `json:"policy-action"`
PolicyValue string `json:"policy-value"`
PeerName string `json:"peer-name"`
QueryZone string `json:"query-zone"`
}

type PowerDNS struct {
Expand Down Expand Up @@ -267,6 +268,7 @@ func (dm *DNSMessage) Init() {
PolicyAction: "-",
PolicyValue: "-",
PeerName: "-",
QueryZone: "-",
}

dm.DNS = DNS{
Expand Down Expand Up @@ -628,6 +630,8 @@ func (dm *DNSMessage) ToTextLine(format []string, fieldDelimiter string, fieldBo
s.WriteString(dm.DNSTap.PolicyMatch)
case directive == "policy-value":
s.WriteString(dm.DNSTap.PolicyValue)
case directive == "query-zone":
s.WriteString(dm.DNSTap.QueryZone)
case directive == "operation":
s.WriteString(dm.DNSTap.Operation)
case directive == "rcode":
Expand Down
9 changes: 8 additions & 1 deletion dnsutils/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ func TestDnsMessage_Json_Reference(t *testing.T) {
"policy-match": "-",
"policy-value": "-",
"policy-rule": "-",
"peer-name": "-"
"peer-name": "-",
"query-zone": "-"
}
}
`
Expand Down Expand Up @@ -262,6 +263,7 @@ func TestDnsMessage_JsonFlatten_Reference(t *testing.T) {
"dnstap.policy-match": "-",
"dnstap.policy-value": "-",
"dnstap.peer-name": "-",
"dnstap.query-zone": "-",
"edns.dnssec-ok": 0,
"edns.options": [],
"edns.rcode": 0,
Expand Down Expand Up @@ -599,6 +601,11 @@ func TestDnsMessage_TextFormat_DefaultDirectives(t *testing.T) {
dm: DNSMessage{DNSTap: DNSTap{PeerName: "testpeer"}},
expected: "testpeer",
},
{
format: "query-zone",
dm: DNSMessage{DNSTap: DNSTap{QueryZone: "queryzone.test"}},
expected: "queryzone.test",
},
}

for _, tc := range testcases {
Expand Down
1 change: 1 addition & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ Default directives:
- `policy-action`: dnstap policy action
- `policy-match`: dnstap policy match
- `policy-value`: dnstap policy value
- `query-zone`: dnstap query zone
- `opcode`: dns opcode (integer)
- `rcode`: dns return code
- `queryip`: dns query ip
Expand Down
2 changes: 2 additions & 0 deletions docs/dnsjson.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ Example:
"policy-action": "-",
"policy-match": "-",
"policy-value": "-",
"query-zone": "-",
}
}
```
Expand Down Expand Up @@ -125,6 +126,7 @@ Using flat-json requires more processing on the host running go-dnscollector but
"dnstap.policy-action": "-",
"dnstap.policy-match": "-",
"dnstap.policy-value": "-",
"dnstap.query-zone": "-",
"edns.dnssec-ok": 0,
"edns.options.0.code": 10,
"edns.options.0.data": "-",
Expand Down
9 changes: 9 additions & 0 deletions processors/dnstap.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,15 @@ RUN_LOOP:
dm.DNSTap.PolicyValue = policyValue
}

queryZone := dt.GetMessage().GetQueryZone()
if len(queryZone) > 0 {
qz, _, err := dnsutils.ParseLabels(0, queryZone)
if err != nil {
d.LogError("invalid query zone: %v - %v", err, queryZone)
}
dm.DNSTap.QueryZone = qz
}

// compute timestamp
ts := time.Unix(int64(dm.DNSTap.TimeSec), int64(dm.DNSTap.TimeNsec))
dm.DNSTap.Timestamp = ts.UnixNano()
Expand Down

0 comments on commit d198ad5

Please sign in to comment.