forked from mandiant/gocrack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
73 lines (58 loc) · 1.99 KB
/
config.go
File metadata and controls
73 lines (58 loc) · 1.99 KB
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
package web
import (
"errors"
"time"
"github.com/fireeye/gocrack/shared"
)
type corsSettings struct {
AllowedOrigins []string `yaml:"allowed_origins"`
MaxPreflightAge *shared.HumanDuration `yaml:"max_preflight_age,omitempty"`
}
type listener struct {
Address string `yaml:"address"`
Certificate string `yaml:"ssl_certificate"`
PrivateKey string `yaml:"ssl_private_key"`
CACertificate *string `yaml:"ssl_ca_certificate,omitempty"`
UseSSL bool `yaml:"ssl_enabled"`
}
type uiSettings struct {
StaticPath string `yaml:"static_path"`
CSRFEnabled *bool `yaml:"csrf_enabled,omitempty"`
CSRFKey string `yaml:"csrf_key"`
}
// Config describes the various options available to the API server
type Config struct {
Listener listener `yaml:"listener"`
CORS corsSettings `yaml:"cors"`
UserInterface uiSettings `yaml:"ui"`
}
var (
errMissingCORSOrigins = errors.New("web_server.cors.allowed_origins must contain one or more domains")
errPreflightAgeNegative = errors.New("web_server.cors.max_preflight_age must be a posititve duration")
errMissingCSRFKey = errors.New("web_server.ui.csrf_key must be a secure key")
defaultPreflightAge = &shared.HumanDuration{Duration: 24 * time.Hour}
defaultListenerAddress = ":4013"
)
// Validate the API server configuration
func (s *Config) Validate() error {
if s.Listener.Address == "" {
s.Listener.Address = defaultListenerAddress
}
if len(s.CORS.AllowedOrigins) == 0 {
return errMissingCORSOrigins
}
if s.UserInterface.CSRFEnabled == nil {
s.UserInterface.CSRFEnabled = shared.GetBoolPtr(true)
}
// Check and see if the key is set if CSRF is Enabled
if *s.UserInterface.CSRFEnabled && s.UserInterface.CSRFKey == "" {
return errMissingCSRFKey
}
if s.CORS.MaxPreflightAge == nil || s.CORS.MaxPreflightAge.Duration.Nanoseconds() < 0 {
s.CORS.MaxPreflightAge = defaultPreflightAge
}
if s.UserInterface.StaticPath == "" {
s.UserInterface.StaticPath = "./static"
}
return nil
}