File size: 21,240 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package config

import (
	"fmt"
	"net/http"
	"net/url"
	"strings"

	"github.com/pkg/errors"
	"github.com/target/goalert/validation"
	"github.com/target/goalert/validation/validate"
)

// SchemaVersion indicates the current config struct version.
const SchemaVersion = 1

// Config contains GoAlert application settings.
type Config struct {
	data        []byte
	fallbackURL string
	explicitURL string

	intEmailDomain string

	General struct {
		ApplicationName              string `public:"true" info:"The name used in messaging and page titles. Defaults to \"GoAlert\"."`
		PublicURL                    string `public:"true" info:"Publicly routable URL for UI links and API calls." deprecated:"Use --public-url flag instead, which takes precedence."`
		GoogleAnalyticsID            string `public:"true" info:"If set, will post user metrics to the corresponding data stream in Google Analytics 4."`
		NotificationDisclaimer       string `public:"true" info:"Disclaimer text for receiving pre-recorded notifications (appears on profile page)."`
		DisableMessageBundles        bool   `public:"true" info:"Disable bundling status updates and alert notifications."`
		ShortURL                     string `public:"true" info:"If set, messages will contain a shorter URL using this as a prefix (e.g. http://example.com). It should point to GoAlert and can be the same as the PublicURL."`
		DisableSMSLinks              bool   `public:"true" info:"If set, SMS messages will not contain a URL pointing to GoAlert."`
		DisableLabelCreation         bool   `public:"true" info:"Disables the ability to create new labels for services."`
		DisableCalendarSubscriptions bool   `public:"true" info:"If set, disables all active calendar subscriptions as well as the ability to create new calendar subscriptions."`
	}

	Services struct {
		RequiredLabels []string `public:"true" info:"List of label names to require new services to define."`
	}

	Maintenance struct {
		AlertCleanupDays     int  `public:"true" info:"Closed alerts will be deleted after this many days (0 means disable cleanup)."`
		AlertAutoCloseDays   int  `public:"true" info:"Unacknowledged alerts will automatically be closed after this many days of inactivity. (0 means disable auto-close)."`
		AutoCloseAckedAlerts bool `public:"true" info:"If set, alerts that are acknowledged will also be automatically closed after the configured number of days of inactivity."`
		APIKeyExpireDays     int  `public:"true" info:"Unused calendar API keys will be disabled after this many days (0 means disable cleanup)."`
		ScheduleCleanupDays  int  `public:"true" info:"Schedule on-call history will be deleted after this many days (0 means disable cleanup)."`
	}

	Auth struct {
		RefererURLs  []string `info:"Allowed referer URLs for auth and redirects." deprecated:"Use --public-url flag instead, which takes precedence."`
		DisableBasic bool     `public:"true" info:"Disallow username/password login."`
	}

	GitHub struct {
		Enable bool `public:"true" info:"Enable GitHub authentication."`

		NewUsers bool `info:"Allow new user creation via GitHub authentication."`

		ClientID     string
		ClientSecret string `password:"true"`

		AllowedUsers []string `info:"Allow any of the listed GitHub usernames to authenticate. Use '*' to allow any user."`
		AllowedOrgs  []string `info:"Allow any member of any listed GitHub org (or team, using the format 'org/team') to authenticate."`

		EnterpriseURL string `info:"GitHub URL (without /api) when used with GitHub Enterprise."`
	}

	OIDC struct {
		Enable bool `public:"true" info:"Enable OpenID Connect authentication."`

		NewUsers     bool   `info:"Allow new user creation via OIDC authentication."`
		OverrideName string `info:"Set the name/label on the login page to something other than OIDC."`

		IssuerURL    string
		ClientID     string
		ClientSecret string `password:"true"`

		Scopes                    string `info:"Requested scopes for authentication. If left blank, openid, profile, and email will be used."`
		UserInfoEmailPath         string `info:"JMESPath expression to find email address in UserInfo. If set, the email claim will be ignored in favor of this. (suggestion: email)."`
		UserInfoEmailVerifiedPath string `info:"JMESPath expression to find email verification state in UserInfo. If set, the email_verified claim will be ignored in favor of this. (suggestion: email_verified)."`
		UserInfoNamePath          string `info:"JMESPath expression to find full name in UserInfo. If set, the name claim will be ignored in favor of this. (suggestion: name || cn || join(' ', [firstname, lastname]))"`
	}

	Mailgun struct {
		Enable bool `public:"true"`

		APIKey      string `password:"true"`
		EmailDomain string `info:"The TO address for all incoming alerts."`
	}

	Slack struct {
		Enable bool `public:"true"`

		ClientID     string
		ClientSecret string `password:"true"`

		// The `xoxb-` prefix is documented by Slack.
		// https://api.slack.com/docs/token-types#bot
		AccessToken string `password:"true" info:"Slack app bot user OAuth access token (should start with xoxb-)."`

		SigningSecret       string `password:"true" info:"Signing secret to verify requests from slack."`
		InteractiveMessages bool   `info:"Enable interactive messages (e.g. buttons)."`
	}

	Twilio struct {
		Enable bool `public:"true" info:"Enables sending and processing of Voice and SMS messages through the Twilio notification provider."`

		VoiceName     string `info:"The Twilio voice to use for Text To Speech for phone calls. See https://www.twilio.com/docs/voice/twiml/say/text-speech#polly-standard-and-neural-voices"`
		VoiceLanguage string `info:"The Twilio voice language to use for Text To Speech for phone calls. See https://www.twilio.com/docs/voice/twiml/say/text-speech#polly-standard-and-neural-voices"`

		AccountSID         string
		AuthToken          string `password:"true" info:"The primary Auth Token for Twilio. Must be primary unless Alternate Auth Token is set. This token is used for outgoing requests."`
		AlternateAuthToken string `password:"true" info:"An alternate Auth Token for validating incoming requests. During a key change, set this to the Primary, and Auth Token to the Secondary, then promote and clear this field."`

		FromNumber string `public:"true" info:"The Twilio number to use for outgoing notifications."`

		MessagingServiceSID string `public:"true" info:"If set, replaces the use of From Number for SMS notifications."`

		RCSSenderID string `info:"The sender ID for RCS messages. Required if RCS is enabled for the MessagingServiceSID."`

		DisableTwoWaySMS      bool     `info:"Disables SMS reply codes for alert messages."`
		SMSCarrierLookup      bool     `info:"Perform carrier lookup of SMS contact methods (required for SMSFromNumberOverride). Extra charges may apply."`
		SMSFromNumberOverride []string `info:"List of 'carrier=number' pairs, SMS messages to numbers of the provided carrier string (exact match) will use the alternate From Number."`
	}

	SMTP struct {
		Enable bool `public:"true" info:"Enables email as a contact method."`

		From string `public:"true" info:"The email address messages should be sent from."`

		Address    string `info:"The server address to use for sending email. Port is optional and defaults to 465, or 25 if Disable TLS is set. Common ports are: 25 or 587 for STARTTLS (or unencrypted) and 465 for TLS."`
		DisableTLS bool   `info:"Disables TLS on the connection (STARTTLS will still be used if supported)."`
		SkipVerify bool   `info:"Disables certificate validation for TLS/STARTTLS (insecure)."`

		Username string `info:"Username for authentication."`
		Password string `password:"true" info:"Password for authentication."`
	}

	Webhook struct {
		Enable      bool     `public:"true" info:"Enables webhook as a contact method."`
		AllowedURLs []string `public:"true" info:"If set, allows webhooks for these domains only."`
	}

	Feedback struct {
		Enable      bool   `public:"true" info:"Enables Feedback link in nav bar."`
		OverrideURL string `public:"true" info:"Use a custom URL for Feedback link in nav bar."`
	}
}

// EmailIngressEnabled returns true if a provider is configured for generating alerts from email, otherwise false
func (cfg Config) EmailIngressEnabled() bool {
	if (cfg.Mailgun.Enable && cfg.Mailgun.EmailDomain != "") || cfg.intEmailDomain != "" {
		return true
	}
	return false
}

// EmailIngressDomain returns the domain configured to receive email for alert generation
func (cfg Config) EmailIngressDomain() string {
	if cfg.intEmailDomain != "" {
		// cli flag always takes precedence
		return cfg.intEmailDomain
	}
	if cfg.Mailgun.EmailDomain != "" {
		return cfg.Mailgun.EmailDomain
	}
	return ""
}

// TwilioSMSFromNumber will determine the appropriate FROM number to use for SMS messages to the given number
func (cfg Config) TwilioSMSFromNumber(carrier string) string {
	if carrier != "" {
		for _, s := range cfg.Twilio.SMSFromNumberOverride {
			parts := strings.SplitN(s, "=", 2)
			if len(parts) != 2 {
				continue
			}
			if parts[0] != carrier {
				continue
			}
			return parts[1]
		}
	}

	if cfg.Twilio.MessagingServiceSID != "" {
		return cfg.Twilio.MessagingServiceSID
	}

	return cfg.Twilio.FromNumber
}

// RequestURL returns the full URL for the given request based on the current public url.
func RequestURL(req *http.Request) string {
	cfg := FromContext(req.Context())
	if !cfg.ShouldUsePublicURL() {
		// fallback to old method
		u, err := url.ParseRequestURI(req.RequestURI)
		if err != nil {
			panic(errors.Wrap(err, "parse RequestURI"))
		}
		u.Host = req.Host
		u.Scheme = req.URL.Scheme
		return u.String()
	}

	base, err := url.Parse(cfg.PublicURL())
	if err != nil {
		panic(errors.Wrap(err, "parse PublicURL"))
	}

	base.Path = strings.TrimSuffix(base.Path, "/") + req.URL.Path
	base.RawQuery = req.URL.RawQuery

	return base.String()
}

func (cfg Config) rawCallbackURL(path string, mergeParams ...url.Values) *url.URL {
	base, err := url.Parse(cfg.PublicURL())
	if err != nil {
		panic(errors.Wrap(err, "parse PublicURL"))
	}

	next, err := url.Parse(path)
	if err != nil {
		panic(errors.Wrap(err, "parse path"))
	}

	base.Path = strings.TrimSuffix(base.Path, "/") + "/" + strings.TrimPrefix(next.Path, "/")

	params := base.Query()
	nx := next.Query()
	// set/override any params provided with path
	for name, val := range nx {
		params[name] = val
	}

	// set/override with any additionally provided params
	for _, merge := range mergeParams {
		for name, val := range merge {
			params[name] = val
		}
	}

	base.RawQuery = params.Encode()
	return base
}

// CallbackURL will return a public-routable URL to the given path.
// It will use PublicURL() to fill in missing pieces.
//
// It will panic if provided an invalid URL.
func (cfg Config) CallbackURL(path string, mergeParams ...url.Values) string {
	base := cfg.rawCallbackURL(path, mergeParams...)

	newPath := ShortPath(base.Path)
	if newPath != "" && cfg.General.ShortURL != "" {
		short, err := url.Parse(cfg.General.ShortURL)
		if err != nil {
			panic(errors.Wrap(err, "parse ShortURL"))
		}
		base.Path = newPath
		base.Host = short.Host
		base.Scheme = short.Scheme
	}

	return base.String()
}

// MatchURL will compare two url strings and will return true if they match.
func MatchURL(baseURL, testURL string) (bool, error) {
	compareQueryValues := func(baseVal, testVal url.Values) bool {
		for name := range baseVal {
			if baseVal.Get(name) == testVal.Get(name) {
				continue
			}
			return false
		}
		return true
	}

	addImplicitPort := func(u *url.URL) {
		if strings.Contains(u.Host, ":") {
			return
		}
		switch strings.ToLower(u.Scheme) {
		case "http":
			u.Host += ":80"
		case "https":
			u.Host += ":443"
		}
	}
	base, err := url.Parse(baseURL)
	if err != nil {
		return false, err
	}

	test, err := url.Parse(testURL)
	if err != nil {
		return false, err
	}

	addImplicitPort(base)
	addImplicitPort(test)

	// host/port check
	if !strings.EqualFold(base.Host, test.Host) {
		return false, nil
	}

	// scheme check
	if !strings.EqualFold(base.Scheme, test.Scheme) {
		return false, nil
	}

	// path check
	if len(base.Path) > 1 && !strings.HasPrefix(test.Path, base.Path) {
		return false, nil
	}

	// query check
	if !compareQueryValues(base.Query(), test.Query()) {
		return false, nil
	}

	return true, nil
}

// ValidWebhookURL returns true if the URL is an allowed webhook source.
func (cfg Config) ValidWebhookURL(testURL string) bool {
	if len(cfg.Webhook.AllowedURLs) == 0 {
		return true
	}
	for _, baseU := range cfg.Webhook.AllowedURLs {
		matched, err := MatchURL(baseU, testURL)
		if err != nil {
			return false
		}
		if matched {
			return true
		}
	}
	return false
}

// ShouldUsePublicURL returns true if redirects, validation, etc.. should use the
// configured PublicURL instead of host/referer.
func (cfg Config) ShouldUsePublicURL() bool { return cfg.explicitURL != "" }

// ValidReferer returns true if the URL is an allowed referer source.
func (cfg Config) ValidReferer(reqURL, ref string) bool {
	// --public-url flag takes precedence
	if cfg.explicitURL != "" {
		valid, _ := MatchURL(cfg.explicitURL, ref)
		return valid
	}

	pubURL := cfg.PublicURL()
	if pubURL != "" && strings.HasPrefix(ref, pubURL) {
		return true
	}

	if len(cfg.Auth.RefererURLs) == 0 {
		u, err := url.Parse(reqURL)
		if err != nil {
			return false
		}
		// just ensure ref is same host/scheme as req
		u.Path = ""
		u.RawQuery = ""
		matched, err := MatchURL(u.String(), ref)
		if err != nil {
			return false
		}
		return matched
	}

	for _, u := range cfg.Auth.RefererURLs {
		matched, err := MatchURL(u, ref)
		if err != nil {
			return false
		}
		if matched {
			return true
		}
	}

	return false
}

// ApplicationName will return the General.ApplicationName
func (cfg Config) ApplicationName() string {
	if cfg.General.ApplicationName == "" {
		return "GoAlert"
	}
	return cfg.General.ApplicationName
}

// PublicURL will return the General.PublicURL or a fallback address (i.e. the app listening port).
func (cfg Config) PublicURL() string {
	switch {
	case cfg.explicitURL != "":
		return strings.TrimSuffix(cfg.explicitURL, "/")
	case cfg.General.PublicURL != "":
		return strings.TrimSuffix(cfg.General.PublicURL, "/")
	}

	return strings.TrimSuffix(cfg.fallbackURL, "/")
}

func validateEnable(prefix string, isEnabled bool, vals ...string) error {
	if !isEnabled {
		return nil
	}

	var err error
	for i := 0; i < len(vals); i += 2 {
		if vals[i+1] != "" {
			continue
		}
		err = validate.Many(
			err,
			validation.NewFieldError(prefix+".Enable", fmt.Sprintf("requires %s.%s to be set ", prefix, vals[i])),
			validation.NewFieldError(fmt.Sprintf("%s.%s", prefix, vals[i]), "required to enable "+prefix),
		)
	}

	return err
}

// Validate will check that the Config values are valid.
func (cfg Config) Validate() error {
	var err error
	if cfg.General.PublicURL != "" {
		err = validate.Many(
			err,
			validate.AbsoluteURL("General.PublicURL", cfg.General.PublicURL),
		)
	}

	if cfg.General.ApplicationName != "" {
		err = validate.Many(err, validate.ASCII("General.ApplicationName", cfg.General.ApplicationName, 0, 32))
	}

	validateKey := func(fname, val string) error { return validate.ASCII(fname, val, 0, 128) }
	validatePath := func(fname, val string) error {
		if val == "" {
			return nil
		}
		return validate.JMESPath(fname, val)
	}
	validateScopes := func(fname, val string) error {
		if val == "" {
			return nil
		}
		return validate.OAuthScope(fname, val, "openid")
	}
	validateLabels := func(fname string, vals []string) (err error) {
		for i, v := range vals {
			err = validate.Many(err, validate.LabelKey(fmt.Sprintf("%s[%d]", fname, i), v))
		}
		return err
	}

	err = validate.Many(
		err,
		validate.Text("General.NotificationDisclaimer", cfg.General.NotificationDisclaimer, 0, 500),
		validateKey("Mailgun.APIKey", cfg.Mailgun.APIKey),
		validateKey("Slack.ClientID", cfg.Slack.ClientID),
		validateKey("Slack.ClientSecret", cfg.Slack.ClientSecret),
		validateKey("Twilio.AccountSID", cfg.Twilio.AccountSID),
		validateKey("Twilio.AuthToken", cfg.Twilio.AuthToken),
		validateLabels("Services.RequiredLabels", cfg.Services.RequiredLabels),
		validateKey("Twilio.AlternateAuthToken", cfg.Twilio.AlternateAuthToken),
		validate.ASCII("Twilio.VoiceName", cfg.Twilio.VoiceName, 0, 50),
		validate.ASCII("Twilio.VoiceLanguage", cfg.Twilio.VoiceLanguage, 0, 10),
		validateKey("GitHub.ClientID", cfg.GitHub.ClientID),
		validateKey("GitHub.ClientSecret", cfg.GitHub.ClientSecret),
		validateKey("Slack.AccessToken", cfg.Slack.AccessToken),
		validate.Range("Maintenance.AlertCleanupDays", cfg.Maintenance.AlertCleanupDays, 0, 9000),
		validate.Range("Maintenance.AlertAutoCloseDays", cfg.Maintenance.AlertAutoCloseDays, 0, 9000),
		validate.Range("Maintenance.APIKeyExpireDays", cfg.Maintenance.APIKeyExpireDays, 0, 9000),
		validate.Range("Maintenance.ScheduleCleanupDays", cfg.Maintenance.ScheduleCleanupDays, 0, 9000),
		validateScopes("OIDC.Scopes", cfg.OIDC.Scopes),
		validatePath("OIDC.UserInfoEmailPath", cfg.OIDC.UserInfoEmailPath),
		validatePath("OIDC.UserInfoEmailVerifiedPath", cfg.OIDC.UserInfoEmailVerifiedPath),
		validatePath("OIDC.UserInfoNamePath", cfg.OIDC.UserInfoNamePath),
		validateKey("Slack.SigningSecret", cfg.Slack.SigningSecret),
	)

	if cfg.General.GoogleAnalyticsID != "" {
		err = validate.Many(err, validate.MeasurementID("General.GoogleAnalyticsID", cfg.General.GoogleAnalyticsID))
	}

	if cfg.Twilio.VoiceName != "" && cfg.Twilio.VoiceLanguage == "" {
		err = validate.Many(err, validation.NewFieldError("Twilio.VoiceLanguage", "required when Twilio.VoiceName is set"))
	}

	if cfg.OIDC.IssuerURL != "" {
		err = validate.Many(err, validate.AbsoluteURL("OIDC.IssuerURL", cfg.OIDC.IssuerURL))
	}
	if cfg.OIDC.Scopes != "" {
		err = validate.Many(err, validateScopes("OIDC.Scopes", cfg.OIDC.Scopes))
	}
	if cfg.GitHub.EnterpriseURL != "" {
		err = validate.Many(err, validate.AbsoluteURL("GitHub.EnterpriseURL", cfg.GitHub.EnterpriseURL))
	}
	if cfg.Twilio.FromNumber != "" {
		err = validate.Many(err, validate.Phone("Twilio.FromNumber", cfg.Twilio.FromNumber))
	}
	if cfg.Twilio.MessagingServiceSID != "" {
		err = validate.Many(err, validate.TwilioSID("Twilio.MessagingServiceSID", "MG", cfg.Twilio.MessagingServiceSID))
	}
	if cfg.Twilio.RCSSenderID != "" {
		err = validate.Many(err, validate.ASCII("Twilio.RCSSenderSID", cfg.Twilio.RCSSenderID, 1, 255))
		if cfg.Twilio.MessagingServiceSID == "" {
			err = validate.Many(err, validation.NewFieldError("Twilio.MessagingServiceSID", "required when Twilio.RCSSenderID is set"))
		}
	}

	if cfg.Mailgun.EmailDomain != "" {
		err = validate.Many(err, validate.Email("Mailgun.EmailDomain", "example@"+cfg.Mailgun.EmailDomain))
	}
	if cfg.SMTP.From != "" {
		err = validate.Many(err, validate.Email("SMTP.From", cfg.SMTP.From))
	}
	if cfg.Slack.InteractiveMessages && cfg.Slack.SigningSecret == "" {
		err = validate.Many(err, validation.NewFieldError("Slack.SigningSecret", "required to enable Slack interactive messages"))
	}

	err = validate.Many(
		err,

		validateEnable("Mailgun", cfg.Mailgun.Enable,
			"APIKey", cfg.Mailgun.APIKey,
			"EmailDomain", cfg.Mailgun.EmailDomain,
		),

		validateEnable("Slack", cfg.Slack.Enable,
			"ClientID", cfg.Slack.ClientID,
			"ClientSecret", cfg.Slack.ClientSecret,
		),

		validateEnable("Twilio", cfg.Twilio.Enable,
			"AccountSID", cfg.Twilio.AccountSID,
			"AuthToken", cfg.Twilio.AuthToken,
			"FromNumber", cfg.Twilio.FromNumber,
		),

		validateEnable("GitHub", cfg.GitHub.Enable,
			"ClientID", cfg.GitHub.ClientID,
			"ClientSecret", cfg.GitHub.ClientSecret,
		),

		validateEnable("OIDC", cfg.OIDC.Enable,
			"IssuerURL", cfg.OIDC.IssuerURL,
			"ClientID", cfg.OIDC.ClientID,
			"ClientSecret", cfg.OIDC.ClientSecret,
		),
		validateEnable("SMTP", cfg.SMTP.Enable,
			"From", cfg.SMTP.From,
			"Address", cfg.SMTP.Address,
		),
	)

	if cfg.Feedback.OverrideURL != "" {
		err = validate.Many(
			err,
			validate.AbsoluteURL("Feedback.OverrideURL", cfg.Feedback.OverrideURL),
		)
	}

	if cfg.General.ShortURL != "" {
		err = validate.Many(
			err,
			validate.AbsoluteURL("General.ShortURL", cfg.General.ShortURL),
		)
	}

	for i, urlStr := range cfg.Auth.RefererURLs {
		field := fmt.Sprintf("Auth.RefererURLs[%d]", i)
		err = validate.Many(
			err,
			validate.AbsoluteURL(field, urlStr),
		)
	}

	for i, urlStr := range cfg.Webhook.AllowedURLs {
		field := fmt.Sprintf("Webhook.AllowedURLs[%d]", i)
		err = validate.Many(err, validate.AbsoluteURL(field, urlStr))
	}

	m := make(map[string]bool)
	for i, str := range cfg.Twilio.SMSFromNumberOverride {
		parts := strings.SplitN(str, "=", 2)
		fname := fmt.Sprintf("Twilio.SMSFromNumberOverride[%d]", i)
		if len(parts) != 2 {
			err = validate.Many(err, validation.NewFieldError(
				fname,
				"must be in the format 'carrier=number'",
			))
			continue
		}
		err = validate.Many(err,
			validate.ASCII(fname+".Carrier", parts[0], 1, 255),
			validate.Phone(fname+".Phone", parts[1]),
		)
		if m[parts[0]] {
			err = validate.Many(err, validation.NewFieldError(fname, fmt.Sprintf("carrier override '%s' already set", parts[0])))
		}
		m[parts[0]] = true
	}

	return err
}