/
gravwell.go
368 lines (338 loc) · 8.08 KB
/
gravwell.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*************************************************************************
* Copyright 2017 Gravwell, Inc. All rights reserved.
* Contact: <legal@gravwell.io>
*
* This software may be modified and distributed under the terms of the
* BSD 2-clause license. See the LICENSE file for details.
**************************************************************************/
package gravwellcoredns
import (
"encoding/json"
"errors"
"fmt"
"net"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/coredns/caddy"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
"github.com/gravwell/gravwell/v3/ingest"
"github.com/gravwell/gravwell/v3/ingest/entry"
"github.com/miekg/dns"
"golang.org/x/net/context"
)
const (
coreDNSPackageName string = `gravwell`
)
func init() {
caddy.RegisterPlugin(coreDNSPackageName, caddy.Plugin{
ServerType: `dns`,
Action: setup,
})
}
// Callback functionto encode DNS Request/Response
type encoder interface {
Encode(entry.Timestamp, net.Addr, net.Addr, *introspector) [][]byte
EncodeError(entry.Timestamp, net.Addr, net.Addr, *dns.Msg, error) [][]byte
}
func parseConfig(c *caddy.Controller) (conf ingest.UniformMuxerConfig, tag string, enc encoder, err error) {
conf = ingest.UniformMuxerConfig{
LogLevel: `INFO`,
IngesterName: `coredns`,
VerifyCert: true,
}
for c.Next() {
for c.NextBlock() {
var arg, val string
if arg, val, err = getArgLine(c); err != nil {
return
}
switch arg {
case `log-level`:
if err = testLogLevel(val); err != nil {
return
}
conf.LogLevel = val
case `ingest-cache-path`:
conf.CacheMode = "always"
conf.CachePath = filepath.Clean(val)
case `max-cache-size-mb`:
var v int
if v, err = strconv.Atoi(val); err != nil || v < 0 {
err = fmt.Errorf("Invalid max cache size: %v", err)
}
conf.CacheSize = v * 1024 * 1024
case `ingest-secret`:
conf.Auth = val
case `cleartext-target`:
if _, _, err = net.SplitHostPort(val); err != nil {
return
}
conf.Destinations = append(conf.Destinations, `tcp://`+val)
case `ciphertext-target`:
if _, _, err = net.SplitHostPort(val); err != nil {
return
}
conf.Destinations = append(conf.Destinations, `tls://`+val)
case `insecure-novalidate-tls`:
if val == `true` {
conf.VerifyCert = false
} else if val == `false` {
//do nothing
} else {
err = fmt.Errorf("Unknown gravwell insecure-novalidate-tls argument %s", val)
return
}
case `tag`:
conf.Tags = append(conf.Tags, val)
tag = val
case `encoding`:
if enc, err = getEncoder(val); err != nil {
return
}
default:
err = fmt.Errorf("Unknown gravwell configuration directive %s", arg)
return
}
}
}
conf.CacheDepth = 128
if conf.CacheSize > 0 && conf.CachePath == "" {
err = fmt.Errorf("Max-Cache-Size-MB may not be set without an active cache location")
}
if len(conf.Tags) != 1 || tag == `` {
err = fmt.Errorf("Tag not appropriately defined. Exactly one tag must be specified")
}
if len(conf.Destinations) == 0 {
err = fmt.Errorf("Invalid destination count, > 0 destinations must be specified")
}
if len(conf.Auth) == 0 {
err = fmt.Errorf("Invalid Ingest-Auth. An auth token is required")
}
if enc == nil {
//default to the JSON encoder
enc = &jsonEncoder{}
}
return
}
// setup the plugin
func setup(c *caddy.Controller) error {
conf, tag, enc, err := parseConfig(c)
if err != nil {
return err
}
im, err := ingest.NewUniformMuxer(conf)
if err != nil {
return err
}
if err = im.Start(); err != nil {
return err
}
if err = im.WaitForHot(time.Second); err != nil {
return err
}
tg, err := im.GetTag(tag)
if err != nil {
return err
}
cfg := dnsserver.GetConfig(c)
mid := func(next plugin.Handler) plugin.Handler {
return gwHandler{
Next: next,
im: im,
tag: tg,
enc: enc,
}
}
cfg.AddPlugin(mid)
return nil
}
type gwHandler struct {
Next plugin.Handler
im *ingest.IngestMuxer
tag entry.EntryTag
enc encoder
}
func (gh gwHandler) String() string {
return coreDNSPackageName
}
func (gh gwHandler) Name() string {
return coreDNSPackageName
}
func (gh gwHandler) ServeDNS(ctx context.Context, rw dns.ResponseWriter, r *dns.Msg) (c int, err error) {
var bbs [][]byte
var lerr error
ts := entry.Now()
local := rw.LocalAddr()
remote := rw.RemoteAddr()
is := &introspector{
ResponseWriter: rw,
}
c, err = gh.Next.ServeDNS(ctx, is, r)
if gh.enc == nil {
var bb []byte
if bb, lerr = r.Pack(); err != nil {
bb = []byte(fmt.Sprintf("ERROR: Failed to pack DNS response: %v", err))
}
bbs = append(bbs, bb)
} else if err != nil {
bbs = gh.enc.EncodeError(ts, local, remote, r, err)
} else {
bbs = gh.enc.Encode(ts, local, remote, is)
}
for _, bb := range bbs {
if lerr = gh.im.Write(ts, gh.tag, bb); lerr != nil {
return
}
}
return
}
func testLogLevel(v string) error {
v = strings.TrimSpace(strings.ToLower(v))
switch v {
case `error`:
case `warn`:
case `info`:
case `off`:
default:
return errors.New("Invalid log level")
}
return nil
}
type introspector struct {
dns.ResponseWriter
q []dns.Question
a []dns.RR
}
func (i *introspector) Write(b []byte) (int, error) {
return i.ResponseWriter.Write(b)
}
func (i *introspector) WriteMsg(m *dns.Msg) error {
i.q = m.Question
i.a = m.Answer
return i.ResponseWriter.WriteMsg(m)
}
func getEncoder(t string) (encoder, error) {
t = strings.TrimSpace(strings.ToLower(t))
switch t {
case `binary`:
fallthrough
case `native`:
return nil, nil
case `text`:
return &textEncoder{}, nil
case `json`:
fallthrough
case ``:
return &jsonEncoder{}, nil
}
return nil, fmt.Errorf("Unknown encoding type")
}
type textEncoder struct{}
func (t textEncoder) Encode(ts entry.Timestamp, local, remote net.Addr, tr *introspector) (bb [][]byte) {
var dt string
for i := range tr.q {
if i < len(tr.a) {
dt = tr.a[i].String()
} else {
dt = tr.q[i].String()
}
bb = append(bb, []byte(fmt.Sprintf("%s %s %s %s %v", ts.String(),
local.Network(), local.String(), remote.String(), dt)))
}
return
}
func (t textEncoder) EncodeError(ts entry.Timestamp, l, r net.Addr, msg *dns.Msg, err error) (bb [][]byte) {
for _, q := range msg.Question {
bb = append(bb, []byte(fmt.Sprintf("%s %s %s %s %v", ts.String(),
l.Network(), l.String(), r.String(), q.String())))
}
return
}
type dnsBase struct {
TS entry.Timestamp
Proto string
Local string
Remote string
}
type dnsAnswer struct {
dnsBase
Question dns.RR
}
type dnsQuestion struct {
dnsBase
Question struct {
Hdr dns.Question
}
}
type jsonEncoder struct{}
func (j jsonEncoder) Encode(ts entry.Timestamp, local, remote net.Addr, tr *introspector) (bbs [][]byte) {
var bb []byte
var err error
base := dnsBase{
TS: ts,
Proto: local.Network(),
Local: local.String(),
Remote: remote.String(),
}
for i := range tr.q {
if i >= len(tr.a) {
dnsq := dnsQuestion{
dnsBase: base,
}
dnsq.Question.Hdr = tr.q[i]
bb, err = json.Marshal(dnsq)
} else {
dnsa := dnsAnswer{
dnsBase: base,
Question: tr.a[i],
}
bb, err = json.Marshal(dnsa)
}
if err != nil {
bb = []byte(fmt.Sprintf("%s ERROR JSON marshal: %v", ts, err))
}
bbs = append(bbs, bb)
}
return
}
type errAnswer struct {
TS entry.Timestamp
Proto string
Local string
Remote string
Question dns.Question
Error string
}
func (j jsonEncoder) EncodeError(ts entry.Timestamp, l, r net.Addr, msg *dns.Msg, err error) (bbs [][]byte) {
var bb []byte
a := errAnswer{
TS: ts,
Proto: l.Network(),
Local: l.String(),
Remote: r.String(),
Error: err.Error(),
}
var lerr error
for _, q := range msg.Question {
a.Question = q
if bb, lerr = json.Marshal(a); lerr != nil {
bb = []byte(fmt.Sprintf("%s ERROR JSON marshal: %v", ts, lerr))
}
bbs = append(bbs, bb)
}
return
}
func getArgLine(c *caddy.Controller) (name, value string, err error) {
name = strings.ToLower(c.Val())
if !c.NextArg() {
err = fmt.Errorf("Missing argument to %s", name)
}
value = c.Val()
if c.NextArg() {
err = fmt.Errorf("%s only takes one argument", name)
}
return
}