-
Notifications
You must be signed in to change notification settings - Fork 138
/
context.go
393 lines (354 loc) · 12.9 KB
/
context.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
// Copyright 2018 Globo.com authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package context
import (
"fmt"
"strings"
"sync"
"time"
"github.com/patrickmn/go-cache"
"github.com/globocom/huskyCI/api/db"
postgres "github.com/globocom/huskyCI/api/db/postgres"
"github.com/globocom/huskyCI/api/types"
)
// APIConfiguration holds all API configuration.
var (
APIConfiguration *APIConfig
onceConfig sync.Once
DefaultConf *DefaultConfig
)
func init() {
DefaultConf = &DefaultConfig{
Caller: &ExternalCalls{},
}
}
// DBConfig represents DB configuration.
type DBConfig struct {
Address string
DatabaseName string
Username string
Password string
Port int
Timeout time.Duration
PoolLimit int
MaxOpenConns int
MaxIdleConns int
ConnMaxLifetime time.Duration
}
// DockerHostsConfig represents Docker Hosts configuration.
type DockerHostsConfig struct {
Address string
DockerAPIPort int
PathCertificate string
Host string
TLSVerify int
}
// GraylogConfig represents Graylog configuration.
type GraylogConfig struct {
Address string
Protocol string
AppName string
Tag string
DevelopmentEnv bool
}
// APIConfig represents API configuration.
type APIConfig struct {
Port int
Version string
ReleaseDate string
AllowOriginValue string
UseTLS bool
GitPrivateSSHKey string
GraylogConfig *GraylogConfig
DBConfig *DBConfig
DockerHostsConfig *DockerHostsConfig
EnrySecurityTest *types.SecurityTest
GitAuthorsSecurityTest *types.SecurityTest
GosecSecurityTest *types.SecurityTest
BanditSecurityTest *types.SecurityTest
BrakemanSecurityTest *types.SecurityTest
NpmAuditSecurityTest *types.SecurityTest
YarnAuditSecurityTest *types.SecurityTest
SpotBugsSecurityTest *types.SecurityTest
GitleaksSecurityTest *types.SecurityTest
SafetySecurityTest *types.SecurityTest
TFSecSecurityTest *types.SecurityTest
DBInstance db.Requests
Cache *cache.Cache
}
// DefaultConfig is the struct that stores the caller for testing.
type DefaultConfig struct {
Caller CallerInterface
}
// GetAPIConfig returns the instance of an APIConfig.
func (dF DefaultConfig) GetAPIConfig() (*APIConfig, error) {
// load Viper using api/config.yml
if err := dF.Caller.SetConfigFile("config", "."); err != nil {
fmt.Println("Error reading Viper config: ", err)
return nil, err
}
dF.SetOnceConfig()
return APIConfiguration, nil
}
// SetOnceConfig sets APIConfiguration once
func (dF DefaultConfig) SetOnceConfig() {
onceConfig.Do(func() {
APIConfiguration = &APIConfig{
Port: dF.GetAPIPort(),
Version: dF.GetAPIVersion(),
ReleaseDate: dF.GetAPIReleaseDate(),
AllowOriginValue: dF.GetAllowOriginValue(),
UseTLS: dF.GetAPIUseTLS(),
GitPrivateSSHKey: dF.getGitPrivateSSHKey(),
GraylogConfig: dF.getGraylogConfig(),
DBConfig: dF.getDBConfig(),
DockerHostsConfig: dF.getDockerHostsConfig(),
EnrySecurityTest: dF.getSecurityTestConfig("enry"),
GitAuthorsSecurityTest: dF.getSecurityTestConfig("gitauthors"),
GosecSecurityTest: dF.getSecurityTestConfig("gosec"),
BanditSecurityTest: dF.getSecurityTestConfig("bandit"),
BrakemanSecurityTest: dF.getSecurityTestConfig("brakeman"),
NpmAuditSecurityTest: dF.getSecurityTestConfig("npmaudit"),
YarnAuditSecurityTest: dF.getSecurityTestConfig("yarnaudit"),
SpotBugsSecurityTest: dF.getSecurityTestConfig("spotbugs"),
GitleaksSecurityTest: dF.getSecurityTestConfig("gitleaks"),
SafetySecurityTest: dF.getSecurityTestConfig("safety"),
TFSecSecurityTest: dF.getSecurityTestConfig("tfsec"),
DBInstance: dF.GetDB(),
Cache: dF.GetCache(),
}
})
}
// GetAPIPort will return the port number
// where HuskyCI will be listening to.
// If HUSKYCI_API_PORT is not set, it will
// return the default 8888 port.
func (dF DefaultConfig) GetAPIPort() int {
apiPort, err := dF.Caller.ConvertStrToInt(dF.Caller.GetEnvironmentVariable("HUSKYCI_API_PORT"))
if err != nil {
apiPort = 8888
}
return apiPort
}
// GetAPIVersion returns current API version
func (dF DefaultConfig) GetAPIVersion() string {
return "0.14.0"
}
// GetAPIReleaseDate returns current API release date
func (dF DefaultConfig) GetAPIReleaseDate() string {
return "2020-06-24"
}
// GetAllowOriginValue returns the allow origin value
func (dF DefaultConfig) GetAllowOriginValue() string {
urlCORS := dF.Caller.GetEnvironmentVariable("HUSKYCI_API_ALLOW_ORIGIN_CORS")
if urlCORS == "" {
return "http://127.0.0.1:8888"
}
return urlCORS
}
// GetAPIUseTLS returns a boolean. If true, Husky API
// will be initialized with TLS. Otherwise, it won't.
// This depends on HUSKYCI_API_ENABLE_HTTPS variable.
func (dF DefaultConfig) GetAPIUseTLS() bool {
option := dF.Caller.GetEnvironmentVariable("HUSKYCI_API_ENABLE_HTTPS")
if strings.EqualFold(option, "true") || option == "1" {
return true
}
return false
}
func (dF DefaultConfig) getGitPrivateSSHKey() string {
return dF.Caller.GetEnvironmentVariable("HUSKYCI_API_GIT_PRIVATE_SSH_KEY")
}
func (dF DefaultConfig) getGraylogConfig() *GraylogConfig {
return &GraylogConfig{
Address: dF.Caller.GetEnvironmentVariable("HUSKYCI_LOGGING_GRAYLOG_ADDR"),
Protocol: dF.Caller.GetEnvironmentVariable("HUSKYCI_LOGGING_GRAYLOG_PROTO"),
AppName: dF.Caller.GetEnvironmentVariable("HUSKYCI_LOGGING_GRAYLOG_APP_NAME"),
Tag: dF.Caller.GetEnvironmentVariable("HUSKYCI_LOGGING_GRAYLOG_TAG"),
DevelopmentEnv: dF.GetGraylogIsDev(),
}
}
// GetGraylogIsDev returns a true boolean if
// it is running in a development environment.
// This tells GlbGelf to generate logs only to
// stdout. Otherwise, it will return false. It
// depends on HUSKYCI_LOGGING_GRAYLOG_DEV env.
func (dF DefaultConfig) GetGraylogIsDev() bool {
option := dF.Caller.GetEnvironmentVariable("HUSKYCI_LOGGING_GRAYLOG_DEV")
if strings.EqualFold(option, "false") || option == "0" {
return false
}
return true
}
func (dF DefaultConfig) getDBConfig() *DBConfig {
return &DBConfig{
Address: dF.Caller.GetEnvironmentVariable("HUSKYCI_DATABASE_DB_ADDR"),
DatabaseName: dF.Caller.GetEnvironmentVariable("HUSKYCI_DATABASE_DB_NAME"),
Username: dF.Caller.GetEnvironmentVariable("HUSKYCI_DATABASE_DB_USERNAME"),
Password: dF.Caller.GetEnvironmentVariable("HUSKYCI_DATABASE_DB_PASSWORD"),
Port: dF.GetDBPort(),
Timeout: dF.GetDBTimeout(),
PoolLimit: dF.GetDBPoolLimit(),
MaxOpenConns: dF.GetMaxOpenConns(),
MaxIdleConns: dF.GetMaxIdleConns(),
ConnMaxLifetime: dF.GetConnMaxLifetime(),
}
}
// GetMaxOpenConns returns the maximum number
// of DB opened connections. It depends on an env
// called HUSKYCI_DATABASE_DB_MAX_OPEN_CONNS.
func (dF DefaultConfig) GetMaxOpenConns() int {
maxOpenConns, err := dF.Caller.ConvertStrToInt(
dF.Caller.GetEnvironmentVariable("HUSKYCI_DATABASE_DB_MAX_OPEN_CONNS"))
if err != nil {
return 1
}
return maxOpenConns
}
// GetMaxIdleConns returns the maximum number
// of DB idle connections. It depends on an env
// called HUSKYCI_DATABASE_DB_MAX_IDLE_CONNS.
func (dF DefaultConfig) GetMaxIdleConns() int {
maxIdleConns, err := dF.Caller.ConvertStrToInt(
dF.Caller.GetEnvironmentVariable("HUSKYCI_DATABASE_DB_MAX_IDLE_CONNS"))
if err != nil {
return 1
}
return maxIdleConns
}
// GetConnMaxLifetime returns the maximum duration
// of a DB connection. It depends on an env
// called HUSKYCI_DATABASE_DB_CONN_MAXLIFETIME.
func (dF DefaultConfig) GetConnMaxLifetime() time.Duration {
connMaxLifetime, err := dF.Caller.ConvertStrToInt(
dF.Caller.GetEnvironmentVariable("HUSKYCI_DATABASE_DB_CONN_MAXLIFETIME"))
if err != nil {
return time.Hour
}
return time.Hour * time.Duration(connMaxLifetime)
}
// GetDBPort returns the port where DB
// will be listening to. It depends on an env
// called HUSKYCI_DATABASE_DB_PORT.
func (dF DefaultConfig) GetDBPort() int {
dbPort, err := dF.Caller.ConvertStrToInt(dF.Caller.GetEnvironmentVariable("HUSKYCI_DATABASE_DB_PORT"))
if err != nil {
return 27017
}
return dbPort
}
// GetDBTimeout returns a time.Duration for
// duration of a connection with DB. This
// depends on HUSKYCI_DATABASE_DB_TIMEOUT.
func (dF DefaultConfig) GetDBTimeout() time.Duration {
dbTimeout, err := dF.Caller.ConvertStrToInt(dF.Caller.GetEnvironmentVariable("HUSKYCI_DATABASE_DB_TIMEOUT"))
if err != nil {
return dF.Caller.GetTimeDurationInSeconds(60)
}
return dF.Caller.GetTimeDurationInSeconds(dbTimeout)
}
// GetDBPoolLimit returns an integer with
// the limit of pool of connections opened with
// DB. This depends on an enviroment var
// called HUSKYCI_DATABASE_DB_POOL_LIMIT.
func (dF DefaultConfig) GetDBPoolLimit() int {
dbPoolLimit, err := dF.Caller.ConvertStrToInt(dF.Caller.GetEnvironmentVariable("HUSKYCI_DATABASE_DB_POOL_LIMIT"))
if err != nil || dbPoolLimit <= 0 {
return 1000
}
return dbPoolLimit
}
func (dF DefaultConfig) getDockerHostsConfig() *DockerHostsConfig {
dockerAPIPort := dF.GetDockerAPIPort()
dockerHostsAddressesEnv := dF.Caller.GetEnvironmentVariable("HUSKYCI_DOCKERAPI_ADDR")
dockerHostsAddresses := strings.Split(dockerHostsAddressesEnv, " ")
dockerHostsPathCertificates := dF.Caller.GetEnvironmentVariable("HUSKYCI_DOCKERAPI_CERT_PATH")
return &DockerHostsConfig{
Address: dockerHostsAddresses[0],
DockerAPIPort: dockerAPIPort,
PathCertificate: dockerHostsPathCertificates,
Host: fmt.Sprintf("%s:%d", dockerHostsAddresses[0], dockerAPIPort),
TLSVerify: dF.GetDockerAPITLSVerify(),
}
}
// GetDockerAPIPort will return the port number
// where Docker API will be listening to. This
// depends on HUSKYCI_DOCKERAPI_PORT.
func (dF DefaultConfig) GetDockerAPIPort() int {
dockerAPIport, err := dF.Caller.ConvertStrToInt(dF.Caller.GetEnvironmentVariable("HUSKYCI_DOCKERAPI_PORT"))
if err != nil {
return 2376
}
return dockerAPIport
}
// GetDockerAPITLSVerify returns an int that is
// interpreted as a boolean. If HUSKYCI_DOCKERAPI_TLS_VERIFY
// is false, it will return 0 and TLS won't be configured
// in the Docker API. Otherwise, it will return 1 and Docker
// API will use TLS protocol.
func (dF DefaultConfig) GetDockerAPITLSVerify() int {
option := dF.Caller.GetEnvironmentVariable("HUSKYCI_DOCKERAPI_TLS_VERIFY")
if strings.EqualFold(option, "false") || option == "0" {
return 0
}
return 1
}
func (dF DefaultConfig) getSecurityTestConfig(securityTestName string) *types.SecurityTest {
return &types.SecurityTest{
Name: dF.Caller.GetStringFromConfigFile(fmt.Sprintf("%s.name", securityTestName)),
Image: dF.Caller.GetStringFromConfigFile(fmt.Sprintf("%s.image", securityTestName)),
ImageTag: dF.Caller.GetStringFromConfigFile(fmt.Sprintf("%s.imageTag", securityTestName)),
Cmd: dF.Caller.GetStringFromConfigFile(fmt.Sprintf("%s.cmd", securityTestName)),
Type: dF.Caller.GetStringFromConfigFile(fmt.Sprintf("%s.type", securityTestName)),
Language: dF.Caller.GetStringFromConfigFile(fmt.Sprintf("%s.language", securityTestName)),
Default: dF.Caller.GetBoolFromConfigFile(fmt.Sprintf("%s.default", securityTestName)),
TimeOutInSeconds: dF.Caller.GetIntFromConfigFile(fmt.Sprintf("%s.timeOutInSeconds", securityTestName)),
}
}
// GetDB returns a Requests implementation based on the
// on the type configured on HUSKYCI_DATABASE_TYPE env var.
// The default returns a MongoRequests that implements mongo
// queries.
func (dF DefaultConfig) GetDB() db.Requests {
dB := dF.Caller.GetEnvironmentVariable("HUSKYCI_DATABASE_TYPE")
if strings.EqualFold(dB, "postgres") {
postgresOperations := postgres.PostgresHandler{}
sqlConfig := postgres.SQLConfig{
Postgres: &postgresOperations,
}
jsonHandler := db.JSONCaller{}
sqlJSONRetriever := db.SQLJSONRetrieve{
Psql: &sqlConfig,
JSONHandler: &jsonHandler,
}
postgres := db.PostgresRequests{
DataRetriever: &sqlJSONRetriever,
JSONHandler: &jsonHandler,
}
return &postgres
}
return &db.MongoRequests{}
}
// GetCache returns a new cache based on the HUSKYCI_CACHE_DEFAULT_EXPIRATION
// and HUSKYCI_CACHE_CLEANUP_INTERVAL environment variables.
func (dF DefaultConfig) GetCache() *cache.Cache {
var (
defaultExpiration time.Duration
cleanupInterval time.Duration
err error
)
defaultExpiration, err = time.ParseDuration(
dF.Caller.GetEnvironmentVariable("HUSKYCI_CACHE_DEFAULT_EXPIRATION"),
)
if err != nil {
defaultExpiration = 5 * time.Minute
}
cleanupInterval, err = time.ParseDuration(
dF.Caller.GetEnvironmentVariable("HUSKYCI_CACHE_CLEANUP_INTERVAL"),
)
if err != nil {
cleanupInterval = 10 * time.Minute
}
return cache.New(defaultExpiration, cleanupInterval)
}