-
Notifications
You must be signed in to change notification settings - Fork 204
/
settings.go
207 lines (156 loc) · 4.85 KB
/
settings.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
package hoverfly
import (
"github.com/SpectoLabs/hoverfly/core/cors"
"os"
"strconv"
"sync"
"strings"
"github.com/SpectoLabs/hoverfly/core/middleware"
log "github.com/sirupsen/logrus"
)
// Configuration - initial structure of configuration
type Configuration struct {
AdminPort string
ProxyPort string
ListenOnHost string
Mode string
Destination string
Middleware middleware.Middleware
DatabasePath string
Webserver bool
TLSVerification bool
UpstreamProxy string
PACFile []byte
Verbose bool
DisableCache bool
CacheSize int
SecretKey []byte
JWTExpirationDelta int
AuthEnabled bool
ProxyAuthorizationHeader string
HttpsOnly bool
PlainHttpTunneling bool
CORS cors.Configs
NoImportCheck bool
ClientAuthenticationDestination string
ClientAuthenticationClientCert string
ClientAuthenticationClientKey string
ClientAuthenticationCACert string
ProxyControlWG sync.WaitGroup
mu sync.Mutex
}
// SetMode - provides safe way to set new mode
func (c *Configuration) SetMode(mode string) {
c.mu.Lock()
c.Mode = mode
c.mu.Unlock()
}
func (c *Configuration) SetUpstreamProxy(upstreamProxy string) {
if !strings.HasPrefix(upstreamProxy, "http://") && !strings.HasPrefix(upstreamProxy, "https://") {
upstreamProxy = "http://" + upstreamProxy
}
c.UpstreamProxy = upstreamProxy
}
// GetMode - provides safe way to get current mode
func (c *Configuration) GetMode() string {
c.mu.Lock()
mode := c.Mode
c.mu.Unlock()
return mode
}
// DefaultPort - default proxy port
const DefaultPort = "8500"
// DefaultAdminPort - default admin interface port
const DefaultAdminPort = "8888"
const DefaultListenOnHost = "127.0.0.1"
// DefaultDatabasePath - default database name that will be created
// or used by Hoverfly
const DefaultDatabasePath = "requests.db"
// DefaultJWTExpirationDelta - default token expiration if environment variable is no provided
const DefaultJWTExpirationDelta = 1 * 24 * 60 * 60
// Environment variables
const (
// TODO Should use naming convention for environment variables
HoverflyAuthEnabledEV = "HoverflyAuthEnabled"
HoverflySecretEV = "HoverflySecret"
HoverflyTokenExpirationEV = "HoverflyTokenExpiration"
HoverflyAdminPortEV = "AdminPort"
HoverflyProxyPortEV = "ProxyPort"
HoverflyDBEV = "HoverflyDB"
HoverflyMiddlewareEV = "HoverflyMiddleware"
HoverflyTLSVerification = "HoverflyTlsVerification"
HoverflyAdminUsernameEV = "HoverflyAdmin"
HoverflyAdminPasswordEV = "HoverflyAdminPass"
HoverflyImportRecordsEV = "HoverflyImport"
HoverflyUpstreamProxyPortEV = "UpstreamProxy"
HoverflySkipImportCheckEV = "SKIP_IMPORT_CHECK"
)
// InitSettings gets and returns initial configuration from env
// variables or sets defaults
func InitSettings() *Configuration {
var appConfig Configuration
// getting default admin interface port
if os.Getenv(HoverflyAdminPortEV) != "" {
appConfig.AdminPort = os.Getenv(HoverflyAdminPortEV)
} else {
appConfig.AdminPort = DefaultAdminPort
}
// getting proxy port
if os.Getenv(HoverflyProxyPortEV) != "" {
appConfig.ProxyPort = os.Getenv(HoverflyProxyPortEV)
} else {
appConfig.ProxyPort = DefaultPort
}
appConfig.ListenOnHost = DefaultListenOnHost
// getting external proxy
if os.Getenv(HoverflyUpstreamProxyPortEV) != "" {
appConfig.UpstreamProxy = os.Getenv(HoverflyUpstreamProxyPortEV)
} else {
appConfig.UpstreamProxy = ""
}
databasePath := os.Getenv(HoverflyDBEV)
if databasePath == "" {
appConfig.DatabasePath = DefaultDatabasePath
}
appConfig.Webserver = false
if os.Getenv(HoverflySecretEV) != "" {
appConfig.SecretKey = []byte(os.Getenv(HoverflySecretEV))
} else {
appConfig.SecretKey = getRandomName(10)
}
if os.Getenv(HoverflyTokenExpirationEV) != "" {
exp, err := strconv.Atoi(os.Getenv(HoverflyTokenExpirationEV))
if err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
"HoverflyTokenExpiration": os.Getenv(HoverflyTokenExpirationEV),
}).Fatal("failed to get token exipration delta, using default value")
exp = DefaultJWTExpirationDelta
}
appConfig.JWTExpirationDelta = exp
} else {
appConfig.JWTExpirationDelta = DefaultJWTExpirationDelta
}
if os.Getenv(HoverflyAuthEnabledEV) == "true" {
appConfig.AuthEnabled = true
} else {
appConfig.AuthEnabled = false
}
// middleware configuration
newMiddleware, _ := middleware.ConvertToNewMiddleware(os.Getenv(HoverflyMiddlewareEV))
appConfig.Middleware = *newMiddleware
if os.Getenv(HoverflyTLSVerification) == "false" {
appConfig.TLSVerification = false
} else {
appConfig.TLSVerification = true
}
if os.Getenv(HoverflySkipImportCheckEV) == "true" {
appConfig.NoImportCheck = true
} else {
appConfig.NoImportCheck = false
}
appConfig.Mode = "simulate"
appConfig.ProxyAuthorizationHeader = "Proxy-Authorization"
appConfig.CacheSize = 1000
return &appConfig
}