forked from linkedin/Burrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
587 lines (551 loc) · 17.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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
/* Copyright 2015 LinkedIn Corp. Licensed under the Apache License, Version
* 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*/
package main
import (
"errors"
"fmt"
"gopkg.in/gcfg.v1"
"log"
"net"
"net/url"
"os"
"regexp"
"strconv"
"strings"
)
// Configuration definition
type ClientProfile struct {
ClientID string `gcfg:"client-id"`
TLS bool `gcfg:"tls"`
TLSNoVerify bool `gcfg:"tls-noverify"`
TLSCertFilePath string `gcfg:"tls-certfilepath"`
TLSKeyFilePath string `gcfg:"tls-keyfilepath"`
TLSCAFilePath string `gcfg:"tls-cafilepath"`
}
type BurrowConfig struct {
General struct {
LogDir string `gcfg:"logdir"`
LogConfig string `gcfg:"logconfig"`
PIDFile string `gcfg:"pidfile"`
ClientID string `gcfg:"client-id"`
GroupBlacklist string `gcfg:"group-blacklist"`
GroupWhitelist string `gcfg:"group-whitelist"`
StdoutLogfile string `gcfg:"stdout-logfile"`
}
Zookeeper struct {
Hosts []string `gcfg:"hostname"`
Port int `gcfg:"port"`
Timeout int `gcfg:"timeout"`
LockPath string `gcfg:"lock-path"`
}
Kafka map[string]*struct {
Brokers []string `gcfg:"broker"`
BrokerPort int `gcfg:"broker-port"`
Zookeepers []string `gcfg:"zookeeper"`
ZookeeperPort int `gcfg:"zookeeper-port"`
ZookeeperPath string `gcfg:"zookeeper-path"`
OffsetsTopic string `gcfg:"offsets-topic"`
ZKOffsets bool `gcfg:"zookeeper-offsets"`
Clientprofile string `gcfg:"client-profile"`
}
Storm map[string]*struct {
Zookeepers []string `gcfg:"zookeeper"`
ZookeeperPort int `gcfg:"zookeeper-port"`
ZookeeperPath []string `gcfg:"zookeeper-path"`
}
Tickers struct {
BrokerOffsets int `gcfg:"broker-offsets"`
}
Lagcheck struct {
Intervals int `gcfg:"intervals"`
MinDistance int64 `gcfg:"min-distance"`
ExpireGroup int64 `gcfg:"expire-group"`
ZKCheck int64 `gcfg:"zookeeper-interval"`
ZKGroupRefresh int64 `gcfg:"zk-group-refresh"`
StormCheck int64 `gcfg:"storm-interval"`
StormGroupRefresh int64 `gcfg:"storm-group-refresh"`
}
Httpserver struct {
Enable bool `gcfg:"server"`
Port int `gcfg:"port"`
Listen []string `gcfg:"listen"`
}
Notify struct {
Interval int64 `gcfg:"interval"`
}
Smtp struct {
Server string `gcfg:"server"`
Port int `gcfg:"port"`
AuthType string `gcfg:"auth-type"`
Username string `gcfg:"username"`
Password string `gcfg:"password"`
From string `gcfg:"from"`
Template string `gcfg:"template"`
}
Emailnotifier map[string]*struct {
Enable bool `gcfg:"enable"`
Groups []string `gcfg:"group"`
Interval int64 `gcfg:"interval"`
Threshold int `gcfg:"threshold"`
}
Httpnotifier struct {
Enable bool `gcfg:"enable"`
Groups []string `gcfg:"group"`
UrlOpen string `gcfg:"url"`
UrlClose string `gcfg:"url-delete"`
MethodOpen string `gcfg:"method"`
MethodClose string `gcfg:"method-delete"`
Interval int64 `gcfg:"interval"`
Extras []string `gcfg:"extra"`
TemplateOpen string `gcfg:"template-post"`
TemplateClose string `gcfg:"template-delete"`
SendClose bool `gcfg:"send-delete"`
PostThreshold int `gcfg:"post-threshold"`
Timeout int `gcfg:"timeout"`
Keepalive int `gcfg:"keepalive"`
}
Slacknotifier struct {
Enable bool `gcfg:"enable"`
Groups []string `gcfg:"group"`
Url string `gcfg:"url"`
Interval int64 `gcfg:"interval"`
Channel string `gcfg:"channel"`
Username string `gcfg:"username"`
IconUrl string `gcfg:"icon-url"`
IconEmoji string `gfcg:"icon-emoji"`
Threshold int `gcfg:"threshold"`
Timeout int `gcfg:"timeout"`
Keepalive int `gcfg:"keepalive"`
}
Clientprofile map[string]*ClientProfile
}
func ReadConfig(cfgFile string) *BurrowConfig {
var cfg BurrowConfig
// Set some non-standard defaults
cfg.Httpnotifier.MethodOpen = "POST"
cfg.Httpnotifier.SendClose = true
cfg.Httpnotifier.MethodClose = "DELETE"
err := gcfg.ReadFileInto(&cfg, cfgFile)
if err != nil {
log.Fatalf("Failed to parse gcfg data: %s", err)
os.Exit(1)
}
return &cfg
}
// Validate that the config is complete
// For a couple values, this will set reasonable defaults for missing values
func ValidateConfig(app *ApplicationContext) error {
if (app == nil) || (app.Config == nil) {
return errors.New("Application context or configuration struct are nil")
}
errs := make([]string, 0)
// General
if app.Config.General.LogDir == "" {
app.Config.General.LogDir, _ = os.Getwd()
}
if _, err := os.Stat(app.Config.General.LogDir); os.IsNotExist(err) {
errs = append(errs, "Log directory does not exist")
}
if app.Config.General.LogConfig != "" {
if _, err := os.Stat(app.Config.General.LogConfig); os.IsNotExist(err) {
errs = append(errs, "Log configuration file does not exist")
}
}
if app.Config.General.PIDFile == "" {
app.Config.General.PIDFile = "burrow.pid"
} else {
if !validateFilename(app.Config.General.PIDFile) {
errs = append(errs, "PID filename is invalid")
}
}
if app.Config.General.ClientID == "" {
app.Config.General.ClientID = "burrow-client"
} else {
if !validateTopic(app.Config.General.ClientID) {
errs = append(errs, "Kafka client ID is not valid")
}
}
// Zookeeper
if app.Config.Zookeeper.Port == 0 {
app.Config.Zookeeper.Port = 2181
}
if len(app.Config.Zookeeper.Hosts) == 0 {
errs = append(errs, "No Zookeeper hostnames specified")
} else {
hostlistError := checkHostlist(app.Config.Zookeeper.Hosts, app.Config.Zookeeper.Port, "Zookeeper")
if hostlistError != "" {
errs = append(errs, hostlistError)
}
}
if app.Config.Zookeeper.Timeout == 0 {
app.Config.Zookeeper.Timeout = 6
}
if app.Config.Zookeeper.LockPath == "" {
app.Config.Zookeeper.LockPath = "/burrow/notifier"
} else {
if !validateZookeeperPath(app.Config.Zookeeper.LockPath) {
errs = append(errs, "Zookeeper path is not valid")
}
}
// Kafka Client Profiles
// Set up a default profile, if needed
if app.Config.Clientprofile == nil {
app.Config.Clientprofile = make(map[string]*ClientProfile)
}
if _, ok := app.Config.Clientprofile["default"]; !ok {
app.Config.Clientprofile["default"] = &ClientProfile{
ClientID: app.Config.General.ClientID,
TLS: false,
}
}
for name, cfg := range app.Config.Clientprofile {
if cfg.ClientID == "" {
cfg.ClientID = "burrow-client"
} else {
if !validateTopic(cfg.ClientID) {
errs = append(errs, fmt.Sprintf("Kafka client ID is not valid for profile %s", name))
}
}
}
// Kafka Clusters
if len(app.Config.Kafka) == 0 {
errs = append(errs, "No Kafka clusters are configured")
}
for cluster, cfg := range app.Config.Kafka {
if cfg.BrokerPort == 0 {
cfg.BrokerPort = 9092
}
if len(cfg.Brokers) == 0 {
errs = append(errs, fmt.Sprintf("No Kafka brokers specified for cluster %s", cluster))
} else {
hostlistError := checkHostlist(cfg.Brokers, cfg.BrokerPort, "Kafka broker")
if hostlistError != "" {
errs = append(errs, hostlistError)
}
}
if cfg.ZookeeperPort == 0 {
cfg.ZookeeperPort = 2181
}
if len(cfg.Zookeepers) == 0 {
errs = append(errs, fmt.Sprintf("No Zookeeper hosts specified for cluster %s", cluster))
} else {
hostlistError := checkHostlist(cfg.Zookeepers, cfg.ZookeeperPort, "Zookeeper")
if hostlistError != "" {
errs = append(errs, hostlistError)
}
}
switch cfg.ZookeeperPath {
case "":
errs = append(errs, fmt.Sprintf("Zookeeper path is not specified for cluster %s", cluster))
case "/":
// If we're using the root path, instead of chroot, set it blank here so we don't get double slashes
cfg.ZookeeperPath = ""
default:
if !validateZookeeperPath(cfg.ZookeeperPath) {
errs = append(errs, fmt.Sprintf("Zookeeper path is not valid for cluster %s", cluster))
}
}
if cfg.OffsetsTopic == "" {
cfg.OffsetsTopic = "__consumer_offsets"
} else {
if !validateTopic(cfg.OffsetsTopic) {
errs = append(errs, fmt.Sprintf("Kafka offsets topic is not valid for cluster %s", cluster))
}
}
if cfg.Clientprofile == "" {
cfg.Clientprofile = "default"
} else {
if _, ok := app.Config.Clientprofile[cfg.Clientprofile]; !ok {
errs = append(errs, fmt.Sprintf("Kafka client profile is not defined for cluster %s", cluster))
}
}
}
// Storm Clusters
if len(app.Config.Storm) > 0 {
for cluster, cfg := range app.Config.Storm {
if cfg.ZookeeperPort == 0 {
cfg.ZookeeperPort = 2181
}
if len(cfg.Zookeepers) == 0 {
errs = append(errs, fmt.Sprintf("No Zookeeper hosts specified for cluster %s", cluster))
} else {
hostlistError := checkHostlist(cfg.Zookeepers, cfg.ZookeeperPort, "Zookeeper")
if hostlistError != "" {
errs = append(errs, hostlistError)
}
}
if len(cfg.ZookeeperPath) == 0 {
errs = append(errs, fmt.Sprintf("No Zookeeper paths specified for cluster %s", cluster))
} else {
for _, zkpath := range cfg.ZookeeperPath {
if zkpath == "" {
errs = append(errs, fmt.Sprintf("Zookeeper path is not specified for cluster %s", cluster))
} else if !validateZookeeperPath(zkpath) {
errs = append(errs, fmt.Sprintf("Zookeeper path is not valid for cluster %s", cluster))
}
}
}
}
}
// Tickers
if app.Config.Tickers.BrokerOffsets == 0 {
app.Config.Tickers.BrokerOffsets = 60
}
// Intervals
if app.Config.Lagcheck.Intervals == 0 {
app.Config.Lagcheck.Intervals = 10
}
if app.Config.Lagcheck.ExpireGroup == 0 {
app.Config.Lagcheck.ExpireGroup = 604800
}
if app.Config.Lagcheck.ZKCheck == 0 {
app.Config.Lagcheck.ZKCheck = 60
}
if app.Config.Lagcheck.StormCheck == 0 {
app.Config.Lagcheck.StormCheck = 60
}
if app.Config.Lagcheck.MinDistance == 0 {
app.Config.Lagcheck.MinDistance = 1
}
if app.Config.Lagcheck.ZKGroupRefresh == 0 {
app.Config.Lagcheck.ZKGroupRefresh = 300
}
if app.Config.Lagcheck.StormGroupRefresh == 0 {
app.Config.Lagcheck.StormGroupRefresh = 300
}
// HTTP Server
if app.Config.Httpserver.Enable {
if len(app.Config.Httpserver.Listen) == 0 {
if app.Config.Httpserver.Port == 0 {
errs = append(errs, "HTTP server port is not specified")
}
listenPort := fmt.Sprintf(":%v", app.Config.Httpserver.Port)
app.Config.Httpserver.Listen = append(app.Config.Httpserver.Listen, listenPort)
} else {
if app.Config.Httpserver.Port != 0 {
errs = append(errs, "Either HTTP server port or listen can be specified, but not both")
}
}
}
// Notify
if app.Config.Notify.Interval == 0 {
app.Config.Notify.Interval = 10
}
// SMTP server config
if app.Config.Smtp.Server != "" {
if !validateHostname(app.Config.Smtp.Server) {
errs = append(errs, "SMTP server is invalid")
}
if app.Config.Smtp.Port == 0 {
app.Config.Smtp.Port = 25
}
if app.Config.Smtp.From == "" {
errs = append(errs, "Email from address is not defined")
} else {
if !validateEmail(app.Config.Smtp.From) {
errs = append(errs, "Email from address is invalid")
}
}
if app.Config.Smtp.Template == "" {
app.Config.Smtp.Template = "config/default-email.tmpl"
}
if _, err := os.Stat(app.Config.Smtp.Template); os.IsNotExist(err) {
errs = append(errs, "Email template file does not exist")
}
if app.Config.Smtp.AuthType != "" {
if (app.Config.Smtp.AuthType != "plain") && (app.Config.Smtp.AuthType != "crammd5") {
errs = append(errs, "Email auth-type must be plain, crammd5, or blank")
}
}
// Username and password are not validated - they're optional
// Email configs
for email, cfg := range app.Config.Emailnotifier {
if !validateEmail(email) {
errs = append(errs, "Email address is invalid")
}
if len(cfg.Groups) == 0 {
errs = append(errs, "Email notification configured with no groups")
} else {
for _, group := range cfg.Groups {
groupParts := strings.Split(group, ",")
if len(groupParts) != 2 {
errs = append(errs, "Email notification groups must be specified as 'cluster,groupname'")
break
}
if _, ok := app.Config.Kafka[groupParts[0]]; !ok {
errs = append(errs, "One or more email notification groups has a bad cluster name")
break
}
if !validateTopic(groupParts[1]) {
errs = append(errs, "One or more email notification groups has an invalid group name")
break
}
}
}
if cfg.Interval == 0 {
errs = append(errs, "Email notification interval is not specified")
}
}
} else {
if len(app.Config.Emailnotifier) > 0 {
errs = append(errs, "Email notifications are configured, but SMTP server is not configured")
}
}
// HTTP Notifier config
if app.Config.Httpnotifier.UrlOpen != "" {
if !validateUrl(app.Config.Httpnotifier.UrlOpen) {
errs = append(errs, "HTTP notifier URL is invalid")
}
if app.Config.Httpnotifier.TemplateOpen == "" {
app.Config.Httpnotifier.TemplateOpen = "config/default-http-post.tmpl"
}
if _, err := os.Stat(app.Config.Httpnotifier.TemplateOpen); os.IsNotExist(err) {
errs = append(errs, "HTTP notifier template file does not exist")
}
if app.Config.Httpnotifier.TemplateClose == "" {
app.Config.Httpnotifier.TemplateClose = "config/default-http-delete.tmpl"
}
if app.Config.Httpnotifier.UrlClose == "" {
app.Config.Httpnotifier.UrlClose = app.Config.Httpnotifier.UrlOpen
} else {
if !validateUrl(app.Config.Httpnotifier.UrlClose) {
errs = append(errs, "HTTP notifier close URL is invalid")
}
}
if _, err := os.Stat(app.Config.Httpnotifier.TemplateClose); os.IsNotExist(err) {
errs = append(errs, "HTTP notifier close template file does not exist")
}
if app.Config.Httpnotifier.PostThreshold == 0 {
app.Config.Httpnotifier.PostThreshold = 2
}
if (app.Config.Httpnotifier.PostThreshold < 1) || (app.Config.Httpnotifier.PostThreshold > 3) {
errs = append(errs, "HTTP notifier post-threshold must be between 1 and 3")
}
if app.Config.Httpnotifier.Interval == 0 {
app.Config.Httpnotifier.Interval = 60
}
for _, extra := range app.Config.Httpnotifier.Extras {
// Each extra should be formatted as "string=string"
if matches, _ := regexp.MatchString(`^[a-zA-Z0-9_\-]+=.*$`, extra); !matches {
errs = append(errs, "One or more HTTP notifier extra fields are invalid")
break
}
}
}
// Slack Notifier config
if app.Config.Slacknotifier.Url != "" {
if !validateUrl(app.Config.Slacknotifier.Url) {
errs = append(errs, "Slack notifier URL is invalid")
}
if app.Config.Slacknotifier.Channel == "" {
app.Config.Slacknotifier.Channel = "#general"
}
if app.Config.Slacknotifier.Username == "" {
app.Config.Slacknotifier.Username = "Burrower"
}
if app.Config.Slacknotifier.IconUrl == "" {
app.Config.Slacknotifier.IconUrl = "https://slack.com/img/icons/app-57.png"
}
if app.Config.Slacknotifier.IconEmoji == "" {
app.Config.Slacknotifier.IconEmoji = ":ghost:"
}
}
if len(errs) > 0 {
return errors.New(strings.Join(errs, ". ") + ".")
} else {
return nil
}
}
func validateIP(ipaddr string) bool {
addr := net.ParseIP(ipaddr)
return addr != nil
}
func validateHostname(hostname string) bool {
matches, _ := regexp.MatchString(`^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])(\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]))*$`, hostname)
if !matches {
// Try as an IP address
return validateIP(hostname)
}
return matches
}
func validateZookeeperPath(path string) bool {
parts := strings.Split(path, "/")
if (len(parts) < 2) || (parts[0] != "") {
return false
}
if (len(parts) == 2) && (parts[1] == "") {
// Root node is OK
return true
}
for i, node := range parts {
if i == 0 {
continue
}
matches, _ := regexp.MatchString(`^[a-zA-Z0-9_\-][a-zA-Z0-9_\-\.]*$`, node)
if !matches {
return false
}
}
return true
}
func validateTopic(topic string) bool {
matches, _ := regexp.MatchString(`^[a-zA-Z0-9_\.-]+$`, topic)
return matches
}
// We don't match valid filenames, we match decent filenames
func validateFilename(filename string) bool {
matches, _ := regexp.MatchString(`^[a-zA-Z0-9_\.-]+$`, filename)
return matches
}
// Very simplistic email address validator - just looks for an @ and a .
func validateEmail(email string) bool {
matches, _ := regexp.MatchString(`^.+@.+\..+$`, email)
return matches
}
// Just use the golang Url library for this
func validateUrl(rawUrl string) bool {
_, err := url.Parse(rawUrl)
return err == nil
}
// Validate a list of ZK or Kafka hosts with optional ports
func checkHostlist(hosts []string, defaultPort int, appName string) string {
for i, host := range hosts {
hostparts := strings.Split(host, ":")
hostport := defaultPort
hostname := hostparts[0]
if len(hostparts) == 2 {
// Must be a hostname or IPv4 address with a port
var err error
hostport, err = strconv.Atoi(hostparts[1])
if (err != nil) || (hostport == 0) {
return fmt.Sprintf("One or more %s hostnames have invalid port components", appName)
}
}
if len(hostparts) > 2 {
// Must be an IPv6 address
// Try without popping off the last segment as a port number first
if validateIP(host) {
hostname = host
} else {
// The full host didn't validate as an IP, so let's pull off the last piece as a port number and try again
hostname = strings.Join(hostparts[:len(hostparts)-1], ":")
hostport, err := strconv.Atoi(hostparts[len(hostparts)-1])
if (err != nil) || (hostport == 0) {
return fmt.Sprintf("One or more %s hostnames have invalid port components", appName)
}
}
}
if !validateHostname(hostname) {
return fmt.Sprintf("One or more %s hostnames are invalid", appName)
}
hosts[i] = fmt.Sprintf("[%s]:%v", hostname, hostport)
}
return ""
}