forked from dbaseqp/Quotient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
661 lines (593 loc) · 16.1 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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
package main
import (
"errors"
"fmt"
"log"
"os"
"regexp"
"sort"
"strings"
"time"
"quotient/checks"
"golang.org/x/exp/slices"
"github.com/BurntSushi/toml"
)
var (
configErrors = []string{}
supportedEvents = []string{"rvb", "koth"} // golang doesn't have constant arrays :/
)
type Config struct {
// General engine settings
Event string
EventType string
DBConnectURL string
BindAddress string
Gateway string
Interface string
Subnet string
Timezone string
JWTPrivateKey string
JWTPublicKey string
// LDAP settings
LdapConnectUrl string
LdapBindDn string
LdapBindPassword string
LdapBaseDn string
LdapAdminGroupDn string
LdapTeamGroupDn string
// Optional settings
EasyPCR bool
Verbose bool
Port int
Https bool
Cert string `toml:"cert,omitempty" json:"cert,omitempty"`
Key string `toml:"key,omitempty" json:"key,omitempty"`
StartPaused bool
// Restrict information
DisableInfoPage bool
DisableHeadToHead bool
DisableExternalPorts bool
// Round settings
Delay int
Jitter int
// Defaults for checks
Points int
Timeout int
SlaThreshold int
SlaPenalty int
Admin []Admin
Box []Box
}
// mostly used for Form and NOT FOR DATABASE... maybe change later
type Admin struct {
ID uint
Name string
Pw string
}
type Box struct {
Name string
IP string
FQDN string `toml:"FQDN,omitempty" json:"fqdn,omitempty"`
// Internal use but not in config file
Runners []checks.Runner `toml:"-"`
Custom []checks.Custom `toml:"Custom,omitempty" json:"custom,omitempty"`
Dns []checks.Dns `toml:"Dns,omitempty" json:"dns,omitempty"`
Ftp []checks.Ftp `toml:"Ftp,omitempty" json:"ftp,omitempty"`
Imap []checks.Imap `toml:"Imap,omitempty" json:"imap,omitempty"`
Ldap []checks.Ldap `toml:"Ldap,omitempty" json:"ldap,omitempty"`
Ping []checks.Ping `toml:"Ping,omitempty" json:"ping,omitempty"`
Pop3 []checks.Pop3 `toml:"Pop3,omitempty" json:"pop3,omitempty"`
Rdp []checks.Rdp `toml:"Rdp,omitempty" json:"rdp,omitempty"`
Smb []checks.Smb `toml:"Smb,omitempty" json:"smb,omitempty"`
Smtp []checks.Smtp `toml:"Smtp,omitempty" json:"smtp,omitempty"`
Sql []checks.Sql `toml:"Sql,omitempty" json:"sql,omitempty"`
Ssh []checks.Ssh `toml:"Ssh,omitempty" json:"ssh,omitempty"`
Tcp []checks.Tcp `toml:"Tcp,omitempty" json:"tcp,omitempty"`
Vnc []checks.Vnc `toml:"Vnc,omitempty" json:"vnc,omitempty"`
Web []checks.Web `toml:"Web,omitempty" json:"web,omitempty"`
WinRM []checks.WinRM `toml:"Winrm,omitempty" json:"winrm,omitempty"`
}
func readConfig(path string) Config {
conf := Config{}
fileContent, err := os.ReadFile(path)
if err != nil {
log.Fatalln("Configuration file ("+path+") not found:", err)
}
if md, err := toml.Decode(string(fileContent), &conf); err != nil {
log.Fatalln(err)
} else {
for _, undecoded := range md.Undecoded() {
errMsg := "[WARN] Undecoded configuration key \"" + undecoded.String() + "\" will not be used."
configErrors = append(configErrors, errMsg)
log.Println(errMsg)
}
}
return conf
}
// general error checking
func checkConfig(conf *Config) error {
// check top level configs
// required settings
var errResult error
if conf.Event == "" {
errResult = errors.Join(errResult, errors.New("event title blank or not specified"))
}
if !slices.Contains(supportedEvents, conf.EventType) {
errResult = errors.Join(errResult, errors.New("not a valid event type"))
}
if conf.DBConnectURL == "" {
errResult = errors.Join(errResult, errors.New("no db connect url specified"))
}
if conf.BindAddress == "" {
errResult = errors.Join(errResult, errors.New("no bind address specified"))
}
if conf.Gateway == "" {
errResult = errors.Join(errResult, errors.New("no gateway specified"))
}
if conf.Interface == "" {
errResult = errors.Join(errResult, errors.New("no interface specified"))
}
if conf.Subnet == "" {
errResult = errors.Join(errResult, errors.New("no rotation subnet specified"))
}
if conf.JWTPrivateKey == "" || conf.JWTPublicKey == "" {
errResult = errors.Join(errResult, errors.New("missing JWT private/public key pair"))
}
if len(conf.Admin) == 0 {
errResult = errors.Join(errResult, errors.New("missing at least 1 defined admin user"))
} else {
for _, admin := range conf.Admin {
if admin.Name == "" || admin.Pw == "" {
errResult = errors.Join(errResult, errors.New("admin "+admin.Name+" missing required property"))
}
}
}
if conf.Timezone == "" {
errResult = errors.Join(errResult, errors.New("no timezone specified"))
}
// optional settings
if conf.Delay == 0 {
conf.Delay = 60
}
if conf.Jitter == 0 {
conf.Jitter = 5
}
if conf.Https == true {
if conf.Cert == "" || conf.Key == "" {
errResult = errors.Join(errResult, errors.New("https requires a cert and key pair"))
}
}
if conf.Port == 0 {
if conf.Https {
conf.Port = 443
} else {
conf.Port = 80
}
}
if conf.Jitter >= conf.Delay {
errResult = errors.Join(errResult, errors.New("jitter must be smaller than delay"))
}
if conf.Timeout == 0 {
conf.Timeout = conf.Delay / 2
}
if conf.Timeout >= conf.Delay-conf.Jitter {
errResult = errors.Join(errResult, errors.New("timeout must be smaller than delay minus jitter"))
}
if conf.Points == 0 {
conf.Points = 1
}
if conf.SlaThreshold == 0 {
conf.SlaThreshold = 5
}
if conf.SlaPenalty == 0 {
conf.SlaPenalty = conf.SlaThreshold * conf.Points
}
if conf.StartPaused {
enginePauseWg.Add(1)
enginePause = true
}
// =======================================
// prepare for box config checking
// sort boxes
sort.SliceStable(conf.Box, func(i, j int) bool {
return conf.Box[i].IP < conf.Box[j].IP
})
// check for duplicate box names
dupeBoxMap := make(map[string]bool)
for _, b := range conf.Box {
if b.Name == "" {
errResult = errors.Join(errResult, errors.New("a box is missing a name"))
}
if _, ok := dupeBoxMap[b.Name]; ok {
errResult = errors.Join(errResult, errors.New("duplicate box name found: "+b.Name))
}
}
// ACTUALLY DO CHECKS FOR BOX AND SERVICE CONFIGURATION
err := parseEnvironment(conf.Box)
if err != nil {
errResult = errors.Join(errResult, err)
}
// look for duplicate check names
for _, box := range conf.Box {
for j := 0; j < len(box.Runners)-1; j++ {
if box.Runners[j].GetService().Name == box.Runners[j+1].GetService().Name {
errResult = errors.Join(errResult, errors.New("duplicate check name '"+box.Runners[j].GetService().Name+"' and '"+box.Runners[j+1].GetService().Name+"' for box "+box.Name))
}
}
}
// errResult is nil by default if no errors occured
return errResult
}
func parseEnvironment(boxes []Box) error {
var errResult error
for i, box := range boxes {
// Immediately fail if boxes aren't configured properly
if box.Name == "" {
return fmt.Errorf("no name found for box %d", i)
}
if box.IP == "" && box.FQDN == "" {
return errors.New("no ip/fqdn found for box " + box.Name)
}
// Ensure TeamID replacement chars are lowercase
box.IP = strings.ToLower(box.IP)
boxes[i].IP = box.IP
box.FQDN = strings.ToLower(box.FQDN)
boxes[i].FQDN = box.FQDN
for j, c := range box.Custom {
if c.Display == "" {
c.Display = "custom"
}
if c.Name == "" {
c.Name = box.Name + "-" + c.Display
}
if len(c.CredLists) < 1 && !strings.Contains(c.Command, "USERNAME") && !strings.Contains(c.Command, "PASSWORD") {
c.Anonymous = true
}
if err := configureService(&c.Service, box); err != nil {
errResult = errors.Join(errResult, err)
}
boxes[i].Custom[j] = c
boxes[i].Runners = append(boxes[i].Runners, c)
}
for j, c := range box.Dns {
c.Anonymous = true // call me when you need authed DNS
if c.Display == "" {
c.Display = "dns"
}
if c.Name == "" {
c.Name = box.Name + "-" + c.Display
}
if len(c.Record) < 1 {
errResult = errors.Join(errResult, errors.New("dns check "+c.Name+" has no records"))
}
if c.Port == 0 {
c.Port = 53
}
if err := configureService(&c.Service, box); err != nil {
errResult = errors.Join(errResult, err)
}
boxes[i].Dns[j] = c
boxes[i].Runners = append(boxes[i].Runners, c)
}
for j, c := range box.Ftp {
if c.Display == "" {
c.Display = "ftp"
}
if c.Name == "" {
c.Name = box.Name + "-" + c.Display
}
if c.Port == 0 {
c.Port = 21
}
for _, f := range c.File {
if f.Regex != "" && f.Hash != "" {
errResult = errors.Join(errResult, errors.New("can't have both regex and hash for ftp file check"))
}
}
if err := configureService(&c.Service, box); err != nil {
errResult = errors.Join(errResult, err)
}
boxes[i].Ftp[j] = c
boxes[i].Runners = append(boxes[i].Runners, c)
}
for j, c := range box.Imap {
if c.Display == "" {
c.Display = "imap"
}
if c.Name == "" {
c.Name = box.Name + "-" + c.Display
}
if c.Port == 0 {
c.Port = 143
}
if err := configureService(&c.Service, box); err != nil {
errResult = errors.Join(errResult, err)
}
boxes[i].Imap[j] = c
boxes[i].Runners = append(boxes[i].Runners, c)
}
for j, c := range box.Ldap {
if c.Display == "" {
c.Display = "ldap"
}
if c.Name == "" {
c.Name = box.Name + "-" + c.Display
}
if c.Port == 0 {
c.Port = 636
}
if c.Anonymous {
errResult = errors.Join(errResult, errors.New("anonymous ldap not supported"))
}
if err := configureService(&c.Service, box); err != nil {
errResult = errors.Join(errResult, err)
}
boxes[i].Ldap[j] = c
boxes[i].Runners = append(boxes[i].Runners, c)
}
for j, c := range box.Ping {
c.Anonymous = true
if c.Count == 0 {
c.Count = 1
}
if c.Display == "" {
c.Display = "ping"
}
if c.Name == "" {
c.Name = box.Name + "-" + c.Display
}
if err := configureService(&c.Service, box); err != nil {
errResult = errors.Join(errResult, err)
}
boxes[i].Ping[j] = c
boxes[i].Runners = append(boxes[i].Runners, c)
}
for j, c := range box.Pop3 {
if c.Display == "" {
c.Display = "pop3"
}
if c.Name == "" {
c.Name = box.Name + "-" + c.Display
}
if c.Port == 0 {
c.Port = 110
}
if err := configureService(&c.Service, box); err != nil {
errResult = errors.Join(errResult, err)
}
boxes[i].Pop3[j] = c
boxes[i].Runners = append(boxes[i].Runners, c)
}
for j, c := range box.Rdp {
if c.Display == "" {
c.Display = "rdp"
}
if c.Name == "" {
c.Name = box.Name + "-" + c.Display
}
if c.Port == 0 {
c.Port = 3389
}
if err := configureService(&c.Service, box); err != nil {
errResult = errors.Join(errResult, err)
}
boxes[i].Rdp[j] = c
boxes[i].Runners = append(boxes[i].Runners, c)
}
for j, c := range box.Smb {
if c.Display == "" {
c.Display = "smb"
}
if c.Name == "" {
c.Name = box.Name + "-" + c.Display
}
if c.Port == 0 {
c.Port = 445
}
if err := configureService(&c.Service, box); err != nil {
errResult = errors.Join(errResult, err)
}
boxes[i].Smb[j] = c
boxes[i].Runners = append(boxes[i].Runners, c)
}
for j, c := range box.Smtp {
if c.Display == "" {
c.Display = "smtp"
}
if c.Name == "" {
c.Name = box.Name + "-" + c.Display
}
if c.Port == 0 {
c.Port = 25
}
if err := configureService(&c.Service, box); err != nil {
errResult = errors.Join(errResult, err)
}
boxes[i].Smtp[j] = c
boxes[i].Runners = append(boxes[i].Runners, c)
}
for j, c := range box.Sql {
if c.Display == "" {
c.Display = "sql"
}
if c.Name == "" {
c.Name = box.Name + "-" + c.Display
}
if c.Kind == "" {
c.Kind = "mysql"
}
if c.Port == 0 {
c.Port = 3306
}
for _, q := range c.Query {
if q.UseRegex {
regexp.MustCompile(q.Output)
}
if q.UseRegex && q.Contains {
errResult = errors.Join(errResult, errors.New("cannot use both regex and contains"))
}
}
if err := configureService(&c.Service, box); err != nil {
errResult = errors.Join(errResult, err)
}
boxes[i].Sql[j] = c
boxes[i].Runners = append(boxes[i].Runners, c)
}
for j, c := range box.Ssh {
if c.Display == "" {
c.Display = "ssh"
}
if c.Name == "" {
c.Name = box.Name + "-" + c.Display
}
if c.Port == 0 {
c.Port = 22
}
if c.PrivKey != "" && c.BadAttempts != 0 {
errResult = errors.Join(errResult, errors.New("can not have bad attempts with pubkey for ssh"))
}
for _, r := range c.Command {
if r.UseRegex {
regexp.MustCompile(r.Output)
}
if r.UseRegex && r.Contains {
errResult = errors.Join(errResult, errors.New("cannot use both regex and contains"))
}
}
if c.Anonymous {
errResult = errors.Join(errResult, errors.New("anonymous ssh not supported"))
}
if err := configureService(&c.Service, box); err != nil {
errResult = errors.Join(errResult, err)
}
boxes[i].Ssh[j] = c
boxes[i].Runners = append(boxes[i].Runners, c)
}
for j, c := range box.Tcp {
c.Anonymous = true
if c.Display == "" {
c.Display = "tcp"
}
if c.Name == "" {
c.Name = box.Name + "-" + c.Display
}
if c.Port == 0 {
errResult = errors.Join(errResult, errors.New("tcp port required"))
}
if err := configureService(&c.Service, box); err != nil {
errResult = errors.Join(errResult, err)
}
boxes[i].Tcp[j] = c
boxes[i].Runners = append(boxes[i].Runners, c)
}
for j, c := range box.Vnc {
if c.Display == "" {
c.Display = "vnc"
}
if c.Name == "" {
c.Name = box.Name + "-" + c.Display
}
if c.Port == 0 {
c.Port = 5900
}
if err := configureService(&c.Service, box); err != nil {
errResult = errors.Join(errResult, err)
}
boxes[i].Vnc[j] = c
boxes[i].Runners = append(boxes[i].Runners, c)
}
for j, c := range box.Web {
if c.Display == "" {
c.Display = "web"
}
if c.Name == "" {
c.Name = box.Name + "-" + c.Display
}
if c.Port == 0 {
if c.Scheme == "https" {
c.Port = 443
} else {
c.Port = 80
}
}
if len(c.Url) == 0 {
errResult = errors.Join(errResult, errors.New("no urls specified for web check "+c.Name))
}
if len(c.CredLists) < 1 {
c.Anonymous = true
}
if c.Scheme == "" {
c.Scheme = "http"
}
for _, u := range c.Url {
if u.Diff != 0 && u.CompareFile == "" {
errResult = errors.Join(errResult, errors.New("need compare file for diff in web"))
}
}
if err := configureService(&c.Service, box); err != nil {
errResult = errors.Join(errResult, err)
}
boxes[i].Web[j] = c
boxes[i].Runners = append(boxes[i].Runners, c)
}
for j, c := range box.WinRM {
if c.Display == "" {
c.Display = "winrm"
}
if c.Name == "" {
c.Name = box.Name + "-" + c.Display
}
if c.Port == 0 {
if c.Encrypted {
c.Port = 443
} else {
c.Port = 80
}
}
if c.Anonymous {
errResult = errors.Join(errResult, errors.New("anonymous winrm not supported"))
}
for _, r := range c.Command {
if r.UseRegex {
regexp.MustCompile(r.Output)
}
if r.UseRegex && r.Contains {
errResult = errors.Join(errResult, errors.New("cannot use both regex and contains"))
}
}
if err := configureService(&c.Service, box); err != nil {
errResult = errors.Join(errResult, err)
}
boxes[i].WinRM[j] = c
boxes[i].Runners = append(boxes[i].Runners, c)
}
}
return nil
}
// configure general service attributes
func configureService(service *checks.Service, box Box) error {
service.BoxName = box.Name
service.BoxIP = box.IP
service.BoxFQDN = box.FQDN
if service.Points == 0 {
service.Points = eventConf.Points
}
if service.Timeout == 0 {
service.Timeout = eventConf.Timeout
}
if service.SlaPenalty == 0 {
service.SlaPenalty = eventConf.SlaPenalty
}
if service.SlaThreshold == 0 {
service.SlaThreshold = eventConf.SlaThreshold
}
if service.StopTime.IsZero() {
service.StopTime = time.Now().AddDate(3, 0, 0) // 3 years ahead should be far enough
}
for _, list := range service.CredLists {
if !strings.HasSuffix(list, ".credlist") {
return errors.New("check " + service.Name + " has invalid credlist names")
}
}
return nil
}