-
Notifications
You must be signed in to change notification settings - Fork 17
/
config.go
519 lines (477 loc) · 14.8 KB
/
config.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/*************************************************************************
* 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 config provides a common base for Gravwell ingester config files.
// The ingester will typically need to extend the config struct to allow configuration of data sources.
// An ingester might implement something like the following:
//
// type cfgType struct {
// Global config.IngestConfig
// Listener map[string]*lst
// Preprocessor processors.ProcessorConfig
// }
//
// func GetConfig(path string) (*cfgType, error) {
// var cr cfgType
// if err := config.LoadConfigFile(&cr, path); err != nil {
// return nil, err
// }
// if err := cr.Global.Verify(); err != nil {
// return nil, err
// }
// // Verify and set UUID
// if _, ok := cr.Global.IngesterUUID(); !ok {
// id := uuid.New()
// if err := cr.Global.SetIngesterUUID(id, path); err != nil {
// return nil, err
// }
// if id2, ok := cr.Global.IngesterUUID(); !ok || id != id2 {
// return nil, errors.New("Failed to set a new ingester UUID")
// }
// }
// return c, nil
// }
package config
import (
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"path/filepath"
"strings"
"time"
"github.com/google/uuid"
"github.com/gravwell/gravwell/v4/ingest/log"
"github.com/gravwell/gravwell/v4/ingest/log/rotate"
"github.com/gravwell/gravwell/v4/timegrinder"
)
const (
defaultLogLevel = `ERROR`
minThrottle = (1024 * 1024) / 8
)
const (
envSecret string = `GRAVWELL_INGEST_SECRET`
envLogLevel string = `GRAVWELL_LOG_LEVEL`
envClearTarget string = `GRAVWELL_CLEARTEXT_TARGETS`
envEncTarget string = `GRAVWELL_ENCRYPTED_TARGETS`
envPipeTarget string = `GRAVWELL_PIPE_TARGETS`
envCompressionTarget string = `GRAVWELL_ENABLE_COMPRESSION`
envCacheMode string = `GRAVWELL_CACHE_MODE`
envCachePath string = `GRAVWELL_CACHE_PATH`
envMaxCache string = `GRAVWELL_CACHE_SIZE`
envDisableSelfIngest string = `GRAVWELL_DISABLE_SELF_INGEST`
DefaultCleartextPort uint16 = 4023
DefaultTLSPort uint16 = 4024
commentValue = `#`
globalHeader = `[global]`
headerStart = `[`
uuidParam = `Ingester-UUID`
CACHE_MODE_DEFAULT = "always"
CACHE_DEPTH_DEFAULT = 128
CACHE_SIZE_DEFAULT = 1000
)
var (
ErrNoConnections = errors.New("No connections specified")
ErrMissingIngestSecret = errors.New("Ingest-Secret value missing")
ErrInvalidLogLevel = errors.New("Invalid Log Level")
ErrInvalidConnectionTimeout = errors.New("Invalid connection timeout")
ErrGlobalSectionNotFound = errors.New("Global config section not found")
ErrInvalidLineLocation = errors.New("Invalid line location")
ErrInvalidUpdateLineParameter = errors.New("Update line location does not contain the specified paramter")
)
type IngestConfig struct {
IngestStreamConfig
Ingester_Name string `json:",omitempty"`
Ingest_Secret string `json:"-"` // DO NOT send this when marshalling
Ingest_Secret_File string `json:"-"` // DO NOT send this when marshalling
Connection_Timeout string `json:",omitempty"`
Verify_Remote_Certificates bool `json:"-"` //legacy, will be removed
Insecure_Skip_TLS_Verify bool `json:",omitempty"`
Cleartext_Backend_Target []string `json:",omitempty"`
Encrypted_Backend_Target []string `json:",omitempty"`
Pipe_Backend_Target []string `json:",omitempty"`
Log_Level string `json:",omitempty"`
Log_File string `json:",omitempty"`
Disable_Self_Ingest bool //do not ship logs via the gravwell tag
Source_Override string `json:",omitempty"` // override normal source if desired
Rate_Limit string `json:",omitempty"`
Ingester_UUID string `json:",omitempty"`
Cache_Depth int `json:",omitempty"`
Cache_Mode string `json:",omitempty"`
Ingest_Cache_Path string `json:",omitempty"`
Max_Ingest_Cache int `json:",omitempty"`
Log_Source_Override string `json:",omitempty"` // override log messages only
Label string `json:",omitempty"` //arbitrary label that can be attached to an ingester
Disable_Multithreading bool //basically set GOMAXPROCS(1)
Stats_Sample_Interval string `json:",omitempty"` // if set to > 0 duration then we periodically throw stats
}
type IngestStreamConfig struct {
Enable_Compression bool `json:",omitempty"`
}
type TimeFormat struct {
Format string
Regex string
Extraction_Regex string
}
type CustomTimeFormat map[string]*TimeFormat
func (ic *IngestConfig) loadDefaults() error {
//arrange the logic to be secure by default or when there is ambiguity
if ic.Verify_Remote_Certificates {
ic.Insecure_Skip_TLS_Verify = false
}
//Ingest secret
if err := LoadEnvVar(&ic.Ingest_Secret, envSecret, ``); err != nil {
return err
}
//Log level
if err := LoadEnvVar(&ic.Log_Level, envLogLevel, defaultLogLevel); err != nil {
return err
}
//Cleartext targets
if err := LoadEnvVar(&ic.Cleartext_Backend_Target, envClearTarget, nil); err != nil {
return err
}
//Encrypted targets
if err := LoadEnvVar(&ic.Encrypted_Backend_Target, envEncTarget, nil); err != nil {
return err
}
//Pipe targets
if err := LoadEnvVar(&ic.Pipe_Backend_Target, envPipeTarget, nil); err != nil {
return err
}
//Compression
if err := LoadEnvVar(&ic.Enable_Compression, envCompressionTarget, false); err != nil {
return err
}
// Cache
if err := LoadEnvVar(&ic.Cache_Mode, envCacheMode, nil); err != nil {
return err
}
if err := LoadEnvVar(&ic.Ingest_Cache_Path, envCachePath, nil); err != nil {
return err
}
if err := LoadEnvVar(&ic.Max_Ingest_Cache, envMaxCache, nil); err != nil {
return err
}
if err := LoadEnvVar(&ic.Disable_Self_Ingest, envDisableSelfIngest, false); err != nil {
return err
}
return nil
}
// Verify checks the configuration parameters of the IngestConfig, verifying
// that there is at least one indexer target, creating directories as necessary,
// and generally making sure values are sensible.
func (ic *IngestConfig) Verify() error {
if err := ic.loadDefaults(); err != nil {
return err
}
if ic.Ingester_UUID != `` {
if _, err := uuid.Parse(ic.Ingester_UUID); err != nil {
return fmt.Errorf("Malformed ingester UUID %v: %v", ic.Ingester_UUID, err)
}
}
ic.Log_Level = strings.ToUpper(strings.TrimSpace(ic.Log_Level))
if ic.Max_Ingest_Cache == 0 && len(ic.Ingest_Cache_Path) != 0 {
ic.Max_Ingest_Cache = CACHE_SIZE_DEFAULT
}
if to, err := ic.parseTimeout(); err != nil || to < 0 {
if err != nil {
return err
}
return ErrInvalidConnectionTimeout
}
// we always use Ingest-Secret over Ingest-Secret-File. If both are populated the direct reference is used
if len(ic.Ingest_Secret) == 0 {
//check if ic.Ingest_Secret_File is not empty
if len(ic.Ingest_Secret_File) == 0 {
return ErrMissingIngestSecret
} else {
if err := loadStringFromFile(ic.Ingest_Secret_File, &ic.Ingest_Secret); err != nil {
return fmt.Errorf("Failed to load Ingest-Secret from Ingest-Secret-File %q %w", ic.Ingest_Secret_File, err)
}
}
}
//ensure there is at least one target
if (len(ic.Cleartext_Backend_Target) + len(ic.Encrypted_Backend_Target) + len(ic.Pipe_Backend_Target)) == 0 {
return ErrNoConnections
}
//normalize the log level and check it
if err := ic.checkLogLevel(); err != nil {
return err
}
// Make sure the log directory exists.
logdir := filepath.Dir(ic.Log_File)
fi, err := os.Stat(logdir)
if err != nil {
if os.IsNotExist(err) {
//try to make the directory
err = os.MkdirAll(logdir, 0700)
if err != nil {
return err
}
} else {
return err
}
} else if !fi.IsDir() {
return errors.New("Log Location is not a directory")
}
if ic.Source_Override != `` {
if net.ParseIP(ic.Source_Override) == nil {
return errors.New("Failed to parse Source_Override")
}
}
if ic.Log_Source_Override != `` {
if net.ParseIP(ic.Log_Source_Override) == nil {
return errors.New("Failed to parse Log_Source_Override")
}
}
// cache checks and defaults
switch strings.ToLower(ic.Cache_Mode) {
case "":
ic.Cache_Mode = CACHE_MODE_DEFAULT
case "always", "fail":
default:
return errors.New("Cache-Mode must be [always,fail]")
}
if ic.Cache_Depth == 0 {
ic.Cache_Depth = CACHE_DEPTH_DEFAULT
}
// there are no defaults for the cache_size.
//if Stats_Sample_Interval is populated, check that we can parse as a duration
if ic.Stats_Sample_Interval != `` {
if _, err := time.ParseDuration(ic.Stats_Sample_Interval); err != nil {
return fmt.Errorf("invalid Stats-Sample-Interval %s %w", ic.Stats_Sample_Interval, err)
}
}
return nil
}
// Targets returns a list of indexer targets, including TCP, TLS, and Unix pipes.
// Each target will be prepended with the connection type, e.g.:
//
// tcp://10.0.0.1:4023
func (ic *IngestConfig) Targets() ([]string, error) {
var conns []string
for _, v := range ic.Cleartext_Backend_Target {
conns = append(conns, "tcp://"+AppendDefaultPort(v, DefaultCleartextPort))
}
for _, v := range ic.Encrypted_Backend_Target {
conns = append(conns, "tls://"+AppendDefaultPort(v, DefaultTLSPort))
}
for _, v := range ic.Pipe_Backend_Target {
conns = append(conns, "pipe://"+v)
}
if len(conns) == 0 {
return nil, ErrNoConnections
}
return conns, nil
}
// InsecureSkipTLSVerification returns true if the Insecure-Skip-TLS-Verify
// config parameter was set.
func (ic *IngestConfig) InsecureSkipTLSVerification() bool {
return ic.Insecure_Skip_TLS_Verify
}
// Timeout returns the timeout for an ingester connection to go live before
// giving up.
func (ic *IngestConfig) Timeout() time.Duration {
if tos, _ := ic.parseTimeout(); tos > 0 {
return tos
}
return 0
}
// Secret returns the value of the Ingest-Secret parameter, used to authenticate to the indexer.
func (ic *IngestConfig) Secret() string {
return ic.Ingest_Secret
}
// Return the specified log level
func (ic *IngestConfig) LogLevel() string {
return ic.Log_Level
}
func (ic *IngestConfig) AddLocalLogging(lg *log.Logger) {
if lg == nil {
return
}
//set the log level
if ll := ic.LogLevel(); len(ll) > 0 {
if err := lg.SetLevelString(ll); err != nil {
lg.FatalCode(0, "invalid Log Level", log.KV("loglevel", ic.Log_Level), log.KVErr(err))
}
}
if len(ic.Log_File) > 0 {
// attach a .log extension for everything but /dev/null, this is a special case
// for when you want a file follower to recursively watch something like /opt/gravwell/log but you don't
// want to go through the hassle of setting up exclusions. So point the log file at dev null and just act like it didn't happen
if ext := filepath.Ext(ic.Log_File); ext == `` && ic.Log_File != `/dev/null` {
ic.Log_File = ic.Log_File + `.log`
}
if fout, err := rotate.Open(ic.Log_File, 0640); err != nil {
lg.FatalCode(0, "failed to open log file", log.KV("path", ic.Log_File), log.KVErr(err))
} else if err = lg.AddWriter(fout); err != nil {
lg.Fatal("failed to add a writer", log.KVErr(err))
}
}
}
func (ic *IngestConfig) SelfIngest() bool {
return ic.Disable_Self_Ingest == false
}
func (ic *IngestConfig) checkLogLevel() error {
if len(ic.Log_Level) == 0 {
ic.Log_Level = defaultLogLevel
return nil
}
switch ic.Log_Level {
case `OFF`:
fallthrough
case `INFO`:
fallthrough
case `WARN`:
fallthrough
case `ERROR`:
return nil
}
return ErrInvalidLogLevel
}
func (ic *IngestConfig) parseTimeout() (time.Duration, error) {
tos := strings.TrimSpace(ic.Connection_Timeout)
if len(tos) == 0 {
return 0, nil
}
return time.ParseDuration(tos)
}
// RateLimit returns the bandwidth limit, in bits per second, which
// should be applied to the indexer connection.
func (ic *IngestConfig) RateLimit() (bps int64, err error) {
if ic.Rate_Limit == `` {
return
}
bps, err = ParseRate(ic.Rate_Limit)
if bps < minThrottle {
err = errors.New("Ingest cannot be limited below 1mbit")
}
return
}
// returns whether the supplied uuid is all zeros
func zeroUUID(id uuid.UUID) bool {
for _, v := range id {
if v != 0 {
return false
}
}
return true
}
// IngesterUUID returns the UUID of this ingester, set with the `Ingester-UUID`
// parameter. If the UUID is not set, the UUID is invalid, or the UUID is all
// zeroes, the function will return ok = false. If the UUID is valid, it returns
// the UUID and ok = true.
func (ic *IngestConfig) IngesterUUID() (id uuid.UUID, ok bool) {
if ic.Ingester_UUID == `` {
return
}
var err error
if id, err = uuid.Parse(ic.Ingester_UUID); err == nil {
ok = true
}
if zeroUUID(id) {
ok = false
}
return
}
func reloadContent(loc string) (content string, err error) {
if loc == `` {
err = errors.New("not loaded from file")
return
}
var bts []byte
bts, err = ioutil.ReadFile(loc)
content = string(bts)
return
}
func (ic *IngestConfig) GetLogger() (l *log.Logger, err error) {
var ll log.Level
if ll, err = log.LevelFromString(ic.Log_Level); err != nil {
return
}
if ic.Log_File == `` {
l = log.NewDiscardLogger()
} else {
l, err = log.NewFile(ic.Log_File)
}
if err == nil {
err = l.SetLevel(ll)
}
return
}
func (ic *IngestConfig) StatsSampleInterval() (dur time.Duration) {
if ic == nil || ic.Stats_Sample_Interval == `` {
return // disabled, no duration
}
var err error
if dur, err = time.ParseDuration(ic.Stats_Sample_Interval); err != nil {
// bad parses are just zero, validate should prevent this though
dur = 0
}
return
}
func writeFull(w io.Writer, b []byte) error {
var written int
for written < len(b) {
if n, err := w.Write(b[written:]); err != nil {
return err
} else if n == 0 {
return errors.New("empty write")
} else {
written += n
}
}
return nil
}
func (ctf CustomTimeFormat) Validate() (err error) {
if len(ctf) == 0 {
return
}
for k, v := range ctf {
if v == nil {
continue
}
cf := timegrinder.CustomFormat{
Name: k,
Format: v.Format,
Regex: v.Regex,
Extraction_Regex: v.Extraction_Regex,
}
if err = cf.Validate(); err != nil {
return
}
}
return
}
func (ctf CustomTimeFormat) LoadFormats(tg *timegrinder.TimeGrinder) (err error) {
if len(ctf) == 0 {
return
} else if err = ctf.Validate(); err != nil {
return
}
for k, v := range ctf {
var p timegrinder.Processor
if v == nil {
continue
}
cf := timegrinder.CustomFormat{
Name: k,
Format: v.Format,
Regex: v.Regex,
Extraction_Regex: v.Extraction_Regex,
}
if p, err = timegrinder.NewCustomProcessor(cf); err != nil {
return
} else if _, err = tg.AddProcessor(p); err != nil {
return
}
}
return
}