forked from evergreen-ci/evergreen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
819 lines (717 loc) · 28.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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
package evergreen
import (
"context"
"fmt"
"net/http"
"os"
"path/filepath"
"reflect"
"strings"
"time"
"github.com/evergreen-ci/evergreen/util"
"github.com/evergreen-ci/utility"
"github.com/mongodb/amboy/logger"
"github.com/mongodb/anser/bsonutil"
"github.com/mongodb/grip"
"github.com/mongodb/grip/level"
"github.com/mongodb/grip/message"
"github.com/mongodb/grip/send"
"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readconcern"
"go.mongodb.org/mongo-driver/mongo/writeconcern"
"gopkg.in/yaml.v3"
)
var (
// BuildRevision should be specified with -ldflags at build time
BuildRevision = ""
// ClientVersion is the commandline version string used to control auto-updating.
ClientVersion = "2023-05-02"
// Agent version to control agent rollover.
AgentVersion = "2023-05-09"
)
// ConfigSection defines a sub-document in the evergreen config
// any config sections must also be added to registry.go
type ConfigSection interface {
// SectionId returns the ID of the section to be used in the database document and struct tag
SectionId() string
// Get populates the section from the DB
Get(Environment) error
// Set upserts the section document into the DB
Set() error
// ValidateAndDefault validates input and sets defaults
ValidateAndDefault() error
}
// Settings contains all configuration settings for running Evergreen. Settings
// with the "id" struct tag should implement the ConfigSection interface.
type Settings struct {
Id string `bson:"_id" json:"id" yaml:"id"`
Alerts AlertsConfig `yaml:"alerts" bson:"alerts" json:"alerts" id:"alerts"`
Amboy AmboyConfig `yaml:"amboy" bson:"amboy" json:"amboy" id:"amboy"`
Api APIConfig `yaml:"api" bson:"api" json:"api" id:"api"`
ApiUrl string `yaml:"api_url" bson:"api_url" json:"api_url"`
AuthConfig AuthConfig `yaml:"auth" bson:"auth" json:"auth" id:"auth"`
AWSInstanceRole string `yaml:"aws_instance_role" bson:"aws_instance_role" json:"aws_instance_role"`
Banner string `bson:"banner" json:"banner" yaml:"banner"`
BannerTheme BannerTheme `bson:"banner_theme" json:"banner_theme" yaml:"banner_theme"`
Cedar CedarConfig `bson:"cedar" json:"cedar" yaml:"cedar" id:"cedar"`
ClientBinariesDir string `yaml:"client_binaries_dir" bson:"client_binaries_dir" json:"client_binaries_dir"`
CommitQueue CommitQueueConfig `yaml:"commit_queue" bson:"commit_queue" json:"commit_queue" id:"commit_queue"`
ConfigDir string `yaml:"configdir" bson:"configdir" json:"configdir"`
ContainerPools ContainerPoolsConfig `yaml:"container_pools" bson:"container_pools" json:"container_pools" id:"container_pools"`
Credentials map[string]string `yaml:"credentials" bson:"credentials" json:"credentials"`
CredentialsNew util.KeyValuePairSlice `yaml:"credentials_new" bson:"credentials_new" json:"credentials_new"`
Database DBSettings `yaml:"database" json:"database" bson:"database"`
DataPipes DataPipesConfig `yaml:"data_pipes" json:"data_pipes" bson:"data_pipes" id:"data_pipes"`
DomainName string `yaml:"domain_name" bson:"domain_name" json:"domain_name"`
Expansions map[string]string `yaml:"expansions" bson:"expansions" json:"expansions"`
ExpansionsNew util.KeyValuePairSlice `yaml:"expansions_new" bson:"expansions_new" json:"expansions_new"`
GithubPRCreatorOrg string `yaml:"github_pr_creator_org" bson:"github_pr_creator_org" json:"github_pr_creator_org"`
GithubOrgs []string `yaml:"github_orgs" bson:"github_orgs" json:"github_orgs"`
DisabledGQLQueries []string `yaml:"disabled_gql_queries" bson:"disabled_gql_queries" json:"disabled_gql_queries"`
HostInit HostInitConfig `yaml:"hostinit" bson:"hostinit" json:"hostinit" id:"hostinit"`
HostJasper HostJasperConfig `yaml:"host_jasper" bson:"host_jasper" json:"host_jasper" id:"host_jasper"`
Jira JiraConfig `yaml:"jira" bson:"jira" json:"jira" id:"jira"`
JIRANotifications JIRANotificationsConfig `yaml:"jira_notifications" json:"jira_notifications" bson:"jira_notifications" id:"jira_notifications"`
Keys map[string]string `yaml:"keys" bson:"keys" json:"keys"`
KeysNew util.KeyValuePairSlice `yaml:"keys_new" bson:"keys_new" json:"keys_new"`
LDAPRoleMap LDAPRoleMap `yaml:"ldap_role_map" bson:"ldap_role_map" json:"ldap_role_map"`
LoggerConfig LoggerConfig `yaml:"logger_config" bson:"logger_config" json:"logger_config" id:"logger_config"`
LogPath string `yaml:"log_path" bson:"log_path" json:"log_path"`
NewRelic NewRelicConfig `yaml:"newrelic" bson:"newrelic" json:"newrelic" id:"newrelic"`
Notify NotifyConfig `yaml:"notify" bson:"notify" json:"notify" id:"notify"`
Plugins PluginConfig `yaml:"plugins" bson:"plugins" json:"plugins"`
PluginsNew util.KeyValuePairSlice `yaml:"plugins_new" bson:"plugins_new" json:"plugins_new"`
PodLifecycle PodLifecycleConfig `yaml:"pod_lifecycle" bson:"pod_lifecycle" json:"pod_lifecycle" id:"pod_lifecycle"`
PprofPort string `yaml:"pprof_port" bson:"pprof_port" json:"pprof_port"`
ProjectCreation ProjectCreationConfig `yaml:"project_creation" bson:"project_creation" json:"project_creation" id:"project_creation"`
Providers CloudProviders `yaml:"providers" bson:"providers" json:"providers" id:"providers"`
RepoTracker RepoTrackerConfig `yaml:"repotracker" bson:"repotracker" json:"repotracker" id:"repotracker"`
Scheduler SchedulerConfig `yaml:"scheduler" bson:"scheduler" json:"scheduler" id:"scheduler"`
ServiceFlags ServiceFlags `bson:"service_flags" json:"service_flags" id:"service_flags" yaml:"service_flags"`
SSHKeyDirectory string `yaml:"ssh_key_directory" bson:"ssh_key_directory" json:"ssh_key_directory"`
SSHKeyPairs []SSHKeyPair `yaml:"ssh_key_pairs" bson:"ssh_key_pairs" json:"ssh_key_pairs"`
Slack SlackConfig `yaml:"slack" bson:"slack" json:"slack" id:"slack"`
Splunk SplunkConfig `yaml:"splunk" bson:"splunk" json:"splunk" id:"splunk"`
Triggers TriggerConfig `yaml:"triggers" bson:"triggers" json:"triggers" id:"triggers"`
Ui UIConfig `yaml:"ui" bson:"ui" json:"ui" id:"ui"`
Spawnhost SpawnHostConfig `yaml:"spawnhost" bson:"spawnhost" json:"spawnhost" id:"spawnhost"`
ShutdownWaitSeconds int `yaml:"shutdown_wait_seconds" bson:"shutdown_wait_seconds" json:"shutdown_wait_seconds"`
Tracer TracerConfig `yaml:"tracer" bson:"tracer" json:"tracer" id:"tracer"`
}
func (c *Settings) SectionId() string { return ConfigDocID }
func (c *Settings) Get(env Environment) error {
ctx, cancel := env.Context()
defer cancel()
coll := env.DB().Collection(ConfigCollection)
res := coll.FindOne(ctx, byId(c.SectionId()))
if err := res.Err(); err != nil {
if err == mongo.ErrNoDocuments {
*c = Settings{}
return nil
}
return errors.Wrapf(err, "getting config section '%s'", c.SectionId())
}
if err := res.Decode(c); err != nil {
return errors.Wrapf(err, "decoding config section '%s'", c.SectionId())
}
return nil
}
// Set saves the global fields in the configuration (i.e. those that are not
// ConfigSections).
func (c *Settings) Set() error {
env := GetEnvironment()
ctx, cancel := env.Context()
defer cancel()
coll := env.DB().Collection(ConfigCollection)
_, err := coll.UpdateOne(ctx, byId(c.SectionId()), bson.M{
"$set": bson.M{
apiUrlKey: c.ApiUrl,
awsInstanceRoleKey: c.AWSInstanceRole,
bannerKey: c.Banner,
bannerThemeKey: c.BannerTheme,
clientBinariesDirKey: c.ClientBinariesDir,
commitQueueKey: c.CommitQueue,
configDirKey: c.ConfigDir,
credentialsKey: c.Credentials,
credentialsNewKey: c.CredentialsNew,
domainNameKey: c.DomainName,
expansionsKey: c.Expansions,
expansionsNewKey: c.ExpansionsNew,
githubPRCreatorOrgKey: c.GithubPRCreatorOrg,
githubOrgsKey: c.GithubOrgs,
disabledGQLQueriesKey: c.DisabledGQLQueries,
keysKey: c.Keys,
keysNewKey: c.KeysNew,
ldapRoleMapKey: c.LDAPRoleMap,
logPathKey: c.LogPath,
pprofPortKey: c.PprofPort,
pluginsKey: c.Plugins,
pluginsNewKey: c.PluginsNew,
splunkKey: c.Splunk,
sshKeyDirectoryKey: c.SSHKeyDirectory,
sshKeyPairsKey: c.SSHKeyPairs,
spawnhostKey: c.Spawnhost,
shutdownWaitKey: c.ShutdownWaitSeconds,
},
}, options.Update().SetUpsert(true))
return errors.Wrapf(err, "updating section '%s'", c.SectionId())
}
func (c *Settings) ValidateAndDefault() error {
var err error
catcher := grip.NewSimpleCatcher()
if c.ApiUrl == "" {
catcher.Add(errors.New("API hostname must not be empty"))
}
if c.ConfigDir == "" {
catcher.Add(errors.New("config directory must not be empty"))
}
if len(c.CredentialsNew) > 0 {
if c.Credentials, err = c.CredentialsNew.Map(); err != nil {
catcher.Add(errors.Wrap(err, "parsing credentials"))
}
}
if len(c.ExpansionsNew) > 0 {
if c.Expansions, err = c.ExpansionsNew.Map(); err != nil {
catcher.Add(errors.Wrap(err, "parsing expansions"))
}
}
if len(c.KeysNew) > 0 {
if c.Keys, err = c.KeysNew.Map(); err != nil {
catcher.Add(errors.Wrap(err, "parsing keys"))
}
}
if len(c.PluginsNew) > 0 {
tempPlugins, err := c.PluginsNew.NestedMap()
if err != nil {
catcher.Add(errors.Wrap(err, "parsing plugins"))
}
c.Plugins = map[string]map[string]interface{}{}
for k1, v1 := range tempPlugins {
c.Plugins[k1] = map[string]interface{}{}
for k2, v2 := range v1 {
c.Plugins[k1][k2] = v2
}
}
}
keys := map[string]bool{}
for _, mapping := range c.LDAPRoleMap {
if keys[mapping.LDAPGroup] {
catcher.Errorf("duplicate LDAP group value %s found in LDAP-role mappings", mapping.LDAPGroup)
}
keys[mapping.LDAPGroup] = true
}
if len(c.SSHKeyPairs) != 0 && c.SSHKeyDirectory == "" {
catcher.New("cannot use SSH key pairs without setting a directory for them")
}
for i := 0; i < len(c.SSHKeyPairs); i++ {
catcher.NewWhen(c.SSHKeyPairs[i].Name == "", "must specify a name for SSH key pairs")
catcher.ErrorfWhen(c.SSHKeyPairs[i].Public == "", "must specify a public key for SSH key pair '%s'", c.SSHKeyPairs[i].Name)
catcher.ErrorfWhen(c.SSHKeyPairs[i].Private == "", "must specify a private key for SSH key pair '%s'", c.SSHKeyPairs[i].Name)
// Avoid overwriting the filepath stored in Keys, which is a special
// case for the path to the legacy SSH identity file.
for _, key := range c.Keys {
catcher.ErrorfWhen(c.SSHKeyPairs[i].PrivatePath(c) == key, "cannot overwrite the legacy SSH key '%s'", key)
}
// ValidateAndDefault can be called before the environment has been
// initialized.
if env := GetEnvironment(); env != nil {
// Ensure we are not modify any existing keys.
if settings := env.Settings(); settings != nil {
for _, key := range env.Settings().SSHKeyPairs {
if key.Name == c.SSHKeyPairs[i].Name {
catcher.ErrorfWhen(c.SSHKeyPairs[i].Public != key.Public, "cannot modify public key for existing SSH key pair '%s'", key.Name)
catcher.ErrorfWhen(c.SSHKeyPairs[i].Private != key.Private, "cannot modify private key for existing SSH key pair '%s'", key.Name)
}
}
}
}
if c.SSHKeyPairs[i].EC2Regions == nil {
c.SSHKeyPairs[i].EC2Regions = []string{}
}
}
// ValidateAndDefault can be called before the environment has been
// initialized.
if env := GetEnvironment(); env != nil {
if settings := env.Settings(); settings != nil {
// Ensure we are not deleting any existing keys.
for _, key := range GetEnvironment().Settings().SSHKeyPairs {
var found bool
for _, newKey := range c.SSHKeyPairs {
if newKey.Name == key.Name {
found = true
break
}
}
catcher.ErrorfWhen(!found, "cannot find existing SSH key '%s'", key.Name)
}
}
}
if catcher.HasErrors() {
return catcher.Resolve()
}
if c.ClientBinariesDir == "" {
c.ClientBinariesDir = ClientDirectory
}
if c.LogPath == "" {
c.LogPath = LocalLoggingOverride
}
if c.ShutdownWaitSeconds < 0 {
c.ShutdownWaitSeconds = DefaultShutdownWaitSeconds
}
return nil
}
// NewSettings builds an in-memory representation of the given settings file.
func NewSettings(filename string) (*Settings, error) {
configData, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
settings := &Settings{}
err = yaml.Unmarshal(configData, settings)
if err != nil {
return nil, err
}
return settings, nil
}
// GetConfig retrieves the Evergreen config document. If no document is
// present in the DB, it will return the defaults.
func GetConfig() (*Settings, error) { return BootstrapConfig(GetEnvironment()) }
// Bootstrap config gets a config from the database defined in the environment.
func BootstrapConfig(env Environment) (*Settings, error) {
config := &Settings{}
// retrieve the root config document
if err := config.Get(env); err != nil {
return nil, err
}
// retrieve the other config sub-documents and form the whole struct
catcher := grip.NewSimpleCatcher()
sections := ConfigRegistry.GetSections()
valConfig := reflect.ValueOf(*config)
//iterate over each field in the config struct
for i := 0; i < valConfig.NumField(); i++ {
// retrieve the 'id' struct tag
sectionId := valConfig.Type().Field(i).Tag.Get("id")
if sectionId == "" { // no 'id' tag means this is a simple field that we can skip
continue
}
// get the property name and find its corresponding section in the registry
propName := valConfig.Type().Field(i).Name
section, ok := sections[sectionId]
if !ok {
catcher.Add(fmt.Errorf("config section '%s' not found in registry", sectionId))
continue
}
// retrieve the section's document from the db
if err := section.Get(env); err != nil {
catcher.Add(errors.Wrapf(err, "populating section '%s'", sectionId))
continue
}
// set the value of the section struct to the value of the corresponding field in the config
sectionVal := reflect.ValueOf(section).Elem()
propVal := reflect.ValueOf(config).Elem().FieldByName(propName)
if !propVal.CanSet() {
catcher.Errorf("unable to set field '%s' in section '%s'", propName, sectionId)
continue
}
propVal.Set(sectionVal)
}
if catcher.HasErrors() {
return nil, errors.WithStack(catcher.Resolve())
}
return config, nil
}
// UpdateConfig updates all evergreen settings documents in DB
func UpdateConfig(config *Settings) error {
// update the root config document
if err := config.Set(); err != nil {
return err
}
// update the other config sub-documents
catcher := grip.NewSimpleCatcher()
valConfig := reflect.ValueOf(*config)
//iterate over each field in the config struct
for i := 0; i < valConfig.NumField(); i++ {
// retrieve the 'id' struct tag
sectionId := valConfig.Type().Field(i).Tag.Get("id")
if sectionId == "" { // no 'id' tag means this is a simple field that we can skip
continue
}
// get the property name and find its value within the settings struct
propName := valConfig.Type().Field(i).Name
propVal := valConfig.FieldByName(propName)
// create a reflective copy of the struct
valPointer := reflect.Indirect(reflect.New(propVal.Type()))
valPointer.Set(propVal)
// convert the pointer to that struct to an empty interface
propInterface := valPointer.Addr().Interface()
// type assert to the ConfigSection interface
section, ok := propInterface.(ConfigSection)
if !ok {
catcher.Errorf("unable to convert config section '%s'", propName)
continue
}
catcher.Add(section.Set())
}
return errors.WithStack(catcher.Resolve())
}
// Validate checks the settings and returns nil if the config is valid,
// or an error with a message explaining why otherwise.
func (settings *Settings) Validate() error {
catcher := grip.NewSimpleCatcher()
// validate the root-level settings struct
catcher.Add(settings.ValidateAndDefault())
// validate each sub-document
valConfig := reflect.ValueOf(*settings)
// iterate over each field in the config struct
for i := 0; i < valConfig.NumField(); i++ {
// retrieve the 'id' struct tag
sectionId := valConfig.Type().Field(i).Tag.Get("id")
if sectionId == "" { // no 'id' tag means this is a simple field that we can skip
continue
}
// get the property name and find its value within the settings struct
propName := valConfig.Type().Field(i).Name
propVal := valConfig.FieldByName(propName)
// the goal is to convert this struct which we know implements ConfigSection
// from a reflection data structure back to the interface
// the below creates a copy and takes the address of it as a workaround because
// you can't take the address of it via reflection for some reason
// (and all interface methods on the struct have pointer receivers)
// create a reflective copy of the struct
valPointer := reflect.Indirect(reflect.New(propVal.Type()))
valPointer.Set(propVal)
// convert the pointer to that struct to an empty interface
propInterface := valPointer.Addr().Interface()
// type assert to the ConfigSection interface
section, ok := propInterface.(ConfigSection)
if !ok {
catcher.Errorf("unable to convert config section '%s'", propName)
continue
}
err := section.ValidateAndDefault()
if err != nil {
catcher.Add(err)
continue
}
// set the value of the section struct in case there was any defaulting done
sectionVal := reflect.ValueOf(section).Elem()
propAddr := reflect.ValueOf(settings).Elem().FieldByName(propName)
if !propAddr.CanSet() {
catcher.Errorf("unable to set field '%s' 'in' %s", propName, sectionId)
continue
}
propAddr.Set(sectionVal)
}
return errors.WithStack(catcher.Resolve())
}
func (s *Settings) GetSender(ctx context.Context, env Environment) (send.Sender, error) {
var (
sender send.Sender
fallback send.Sender
err error
senders []send.Sender
)
levelInfo := s.LoggerConfig.Info()
fallback, err = send.NewErrorLogger("evergreen.err",
send.LevelInfo{Default: level.Info, Threshold: level.Debug})
if err != nil {
return nil, errors.Wrap(err, "configuring error fallback logger")
}
// setup the base/default logger (generally direct to systemd
// or standard output)
switch s.LogPath {
case LocalLoggingOverride:
// log directly to systemd if possible, and log to
// standard output otherwise.
sender = getSystemLogger()
case StandardOutputLoggingOverride, "":
sender = send.MakeNative()
default:
sender, err = send.MakeFileLogger(s.LogPath)
if err != nil {
return nil, errors.Wrap(err, "configuring file logger")
}
}
if err = sender.SetLevel(levelInfo); err != nil {
return nil, errors.Wrap(err, "setting level")
}
if err = sender.SetErrorHandler(send.ErrorHandlerFromSender(fallback)); err != nil {
return nil, errors.Wrap(err, "setting error handler")
}
senders = append(senders, sender)
// set up external log aggregation services:
//
if s.Splunk.SplunkConnectionInfo.Populated() {
retryConf := utility.NewDefaultHTTPRetryConf()
retryConf.MaxDelay = time.Second
retryConf.BaseDelay = 10 * time.Millisecond
retryConf.MaxRetries = 10
client := utility.GetHTTPRetryableClient(retryConf)
splunkSender, err := s.makeSplunkSender(ctx, client, levelInfo, fallback)
if err != nil {
utility.PutHTTPClient(client)
return nil, errors.Wrap(err, "configuring splunk logger")
}
env.RegisterCloser("splunk-http-client", false, func(_ context.Context) error {
utility.PutHTTPClient(client)
return nil
})
senders = append(senders, splunkSender)
}
// the slack logging service is only for logging very high level alerts.
if s.Slack.Token != "" {
sender, err = send.NewSlackLogger(s.Slack.Options, s.Slack.Token,
send.LevelInfo{Default: level.Critical, Threshold: level.FromString(s.Slack.Level)})
if err == nil {
var slackFallback send.Sender
switch len(senders) {
case 0:
slackFallback = fallback
case 1:
slackFallback = senders[0]
default:
slackFallback = send.NewConfiguredMultiSender(senders...)
}
if err = sender.SetErrorHandler(send.ErrorHandlerFromSender(slackFallback)); err != nil {
return nil, errors.Wrap(err, "setting error handler")
}
senders = append(senders, logger.MakeQueueSender(ctx, env.LocalQueue(), sender))
}
grip.Warning(errors.Wrap(err, "setting up Slack alert logger"))
}
return send.NewConfiguredMultiSender(senders...), nil
}
func (s *Settings) makeSplunkSender(ctx context.Context, client *http.Client, levelInfo send.LevelInfo, fallback send.Sender) (send.Sender, error) {
sender, err := send.NewSplunkLoggerWithClient("", s.Splunk.SplunkConnectionInfo, grip.GetSender().Level(), client)
if err != nil {
return nil, errors.Wrap(err, "making splunk logger")
}
if err = sender.SetLevel(levelInfo); err != nil {
return nil, errors.Wrap(err, "setting Splunk level")
}
if err = sender.SetErrorHandler(send.ErrorHandlerFromSender(fallback)); err != nil {
return nil, errors.Wrap(err, "setting Splunk error handler")
}
opts := send.BufferedSenderOptions{
FlushInterval: time.Duration(s.LoggerConfig.Buffer.DurationSeconds) * time.Second,
BufferSize: s.LoggerConfig.Buffer.Count,
}
if s.LoggerConfig.Buffer.UseAsync {
if sender, err = send.NewBufferedAsyncSender(ctx,
sender,
send.BufferedAsyncSenderOptions{
BufferedSenderOptions: opts,
IncomingBufferFactor: s.LoggerConfig.Buffer.IncomingBufferFactor,
}); err != nil {
return nil, errors.Wrap(err, "making Splunk async buffered sender")
}
} else {
if sender, err = send.NewBufferedSender(ctx, sender, opts); err != nil {
return nil, errors.Wrap(err, "making Splunk buffered sender")
}
}
return sender, nil
}
func (s *Settings) GetGithubOauthStrings() ([]string, error) {
var tokens []string
var tokenName string
token, ok := s.Credentials["github"]
if ok && token != "" {
// we want to make sure tokens[0] is always the default token
tokens = append(tokens, token)
} else {
return nil, errors.New("no 'github' token in settings")
}
for i := 1; i < 10; i++ {
tokenName = fmt.Sprintf("github_alt%d", i)
token, ok := s.Credentials[tokenName]
if ok && token != "" {
tokens = append(tokens, token)
} else {
break
}
}
return tokens, nil
}
func (s *Settings) GetGithubOauthToken() (string, error) {
if s == nil {
return "", errors.New("not defined")
}
oauthStrings, err := s.GetGithubOauthStrings()
if err != nil {
return "", err
}
timeSeed := time.Now().Nanosecond()
randomStartIdx := timeSeed % len(oauthStrings)
var oauthString string
for i := range oauthStrings {
oauthString = oauthStrings[randomStartIdx+i]
splitToken, err := splitToken(oauthString)
if err != nil {
grip.Error(message.Fields{
"error": err,
"message": fmt.Sprintf("problem with github_alt%d", i)})
} else {
return splitToken, nil
}
}
return "", errors.New("all github tokens are malformatted. Proper format is github:token <token> or github_alt#:token <token>")
}
func splitToken(oauthString string) (string, error) {
splitToken := strings.Split(oauthString, " ")
if len(splitToken) != 2 || splitToken[0] != "token" {
return "", errors.New("token format was invalid, expected 'token [token]'")
}
return splitToken[1], nil
}
func GetServiceFlags() (*ServiceFlags, error) {
flags := &ServiceFlags{}
if err := flags.Get(GetEnvironment()); err != nil {
return nil, errors.Wrapf(err, "getting section '%s'", flags.SectionId())
}
return flags, nil
}
// PluginConfig holds plugin-specific settings, which are handled.
// manually by their respective plugins
type PluginConfig map[string]map[string]interface{}
// SSHKeyPair represents a public and private SSH key pair.
type SSHKeyPair struct {
Name string `bson:"name" json:"name" yaml:"name"`
Public string `bson:"public" json:"public" yaml:"public"`
Private string `bson:"private" json:"private" yaml:"private"`
// EC2Regions contains all EC2 regions that have stored this SSH key.
EC2Regions []string `bson:"ec2_regions" json:"ec2_regions" yaml:"ec2_regions"`
}
// AddEC2Region adds the given EC2 region to the set of regions containing the
// SSH key.
func (p *SSHKeyPair) AddEC2Region(region string) error {
env := GetEnvironment()
ctx, cancel := env.Context()
defer cancel()
coll := env.DB().Collection(ConfigCollection)
query := bson.M{
idKey: ConfigDocID,
sshKeyPairsKey: bson.M{
"$elemMatch": bson.M{
sshKeyPairNameKey: p.Name,
},
},
}
var update bson.M
if len(p.EC2Regions) == 0 {
// In case this is the first element, we have to push to create the
// array first.
update = bson.M{
"$push": bson.M{bsonutil.GetDottedKeyName(sshKeyPairsKey, "$", sshKeyPairEC2RegionsKey): region},
}
} else {
update = bson.M{
"$addToSet": bson.M{bsonutil.GetDottedKeyName(sshKeyPairsKey, "$", sshKeyPairEC2RegionsKey): region},
}
}
if _, err := coll.UpdateOne(ctx, query, update); err != nil {
return errors.WithStack(err)
}
if !utility.StringSliceContains(p.EC2Regions, region) {
p.EC2Regions = append(p.EC2Regions, region)
}
return nil
}
func (p *SSHKeyPair) PrivatePath(settings *Settings) string {
return filepath.Join(settings.SSHKeyDirectory, p.Name)
}
type WriteConcern struct {
W int `yaml:"w"`
WMode string `yaml:"wmode"`
WTimeout int `yaml:"wtimeout"`
J bool `yaml:"j"`
}
func (wc WriteConcern) Resolve() *writeconcern.WriteConcern {
opts := []writeconcern.Option{}
if wc.J {
opts = append(opts, writeconcern.J(true))
}
if wc.WMode == "majority" {
opts = append(opts, writeconcern.WMajority())
} else if wc.W > 0 {
opts = append(opts, writeconcern.W(wc.W))
}
if wc.WTimeout > 0 {
opts = append(opts, writeconcern.WTimeout(time.Duration(wc.WTimeout)*time.Millisecond))
}
return writeconcern.New().WithOptions(opts...)
}
type ReadConcern struct {
Level string `yaml:"level"`
}
func (rc ReadConcern) Resolve() *readconcern.ReadConcern {
if rc.Level == "majority" {
return readconcern.Majority()
} else if rc.Level == "local" {
return readconcern.Local()
} else if rc.Level == "" {
return readconcern.Majority()
} else {
grip.Error(message.Fields{
"error": "ReadConcern Level is not majority or local, setting to majority",
"rcLevel": rc.Level})
return readconcern.Majority()
}
}
type DBSettings struct {
Url string `yaml:"url"`
DB string `yaml:"db"`
WriteConcernSettings WriteConcern `yaml:"write_concern"`
ReadConcernSettings ReadConcern `yaml:"read_concern"`
AuthFile string `yaml:"auth_file"`
}
func (dbs *DBSettings) HasAuth() bool {
return dbs.AuthFile != ""
}
type dbCreds struct {
DBUser string `yaml:"mdb_database_username"`
DBPwd string `yaml:"mdb_database_password"`
}
func (dbs *DBSettings) GetAuth() (string, string, error) {
return GetAuthFromYAML(dbs.AuthFile)
}
func GetAuthFromYAML(authFile string) (string, string, error) {
creds := &dbCreds{}
file, err := os.Open(authFile)
if err != nil {
return "", "", err
}
defer file.Close()
decoder := yaml.NewDecoder(file)
if err := decoder.Decode(&creds); err != nil {
return "", "", err
}
return creds.DBUser, creds.DBPwd, nil
}
// supported banner themes in Evergreen
type BannerTheme string
const (
Announcement BannerTheme = "announcement"
Information = "information"
Warning = "warning"
Important = "important"
)
func IsValidBannerTheme(input string) (bool, BannerTheme) {
switch input {
case "":
return true, ""
case "announcement":
return true, Announcement
case "information":
return true, Information
case "warning":
return true, Warning
case "important":
return true, Important
default:
return false, ""
}
}