-
Notifications
You must be signed in to change notification settings - Fork 166
/
config.go
100 lines (86 loc) · 3.2 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
package config
import (
"net/url"
"strings"
"github.com/spf13/viper"
)
const (
Verbose = "VERBOSE"
ListeningPort = "LISTENING_PORT"
GrypeAddress = "GRYPE_ADDR"
KlarTrace = "KLAR_TRACE"
KlarResultServicePath = "KLAR_RESULT_SERVICE_PATH"
DockleImageName = "DOCKLE_IMAGE_NAME"
DockleTimeoutSec = "DOCKLE_TIMEOUT_SEC"
DockleResultServicePath = "DOCKLE_RESULT_SERVICE_PATH"
ResultListenPort = "RESULT_LISTEN_PORT"
ScannerHttpsProxy = "SCANNER_HTTPS_PROXY"
ScannerHttpProxy = "SCANNER_HTTP_PROXY"
CredsSecretNamespace = "CREDS_SECRET_NAMESPACE"
)
type Config struct {
Verbose bool
WebappPort string
GrypeAddress string
KlarTrace bool
KlarResultServicePath string
DockleImageName string
DockleTimeoutSec string
DockleResultServicePath string
ResultListenPort string
ResultServiceAddress string
ScannerHttpsProxy string
ScannerHttpProxy string
CredsSecretNamespace string
}
func setConfigDefaults() {
viper.SetDefault(Verbose, "false")
viper.SetDefault(ListeningPort, "8080")
viper.SetDefault(KlarResultServicePath, "http://kubei.kubei:8081/result/")
viper.SetDefault(DockleTimeoutSec, "300")
viper.SetDefault(DockleResultServicePath, "http://kubei.kubei:8081/dockerfileScanResult/")
viper.SetDefault(ResultListenPort, "8081")
viper.SetDefault(KlarTrace, "false") // Run Klar in more verbose mode
viper.SetDefault(GrypeAddress, "grype-server.kubei:9991")
viper.SetDefault(ScannerHttpsProxy, "")
viper.SetDefault(ScannerHttpProxy, "")
viper.SetDefault(CredsSecretNamespace, "kubei")
viper.AutomaticEnv()
}
// Extracts service hostname from the full url.
// example: `http://kubei.kubei:8081/result/` should return `kubei.kubei`
func getServiceAddress(serviceFullPath string) string {
u, err := url.Parse(serviceFullPath)
if err != nil {
panic(err)
}
return u.Hostname()
}
// Add default http scheme in case scheme is missing.
func getServiceFullPath(serviceFullPath string) string {
if !strings.Contains(serviceFullPath, "://") {
serviceFullPath = "http://" + serviceFullPath
}
return serviceFullPath
}
func LoadConfig() *Config {
setConfigDefaults()
klarResultServicePath := getServiceFullPath(viper.GetString(KlarResultServicePath))
dockleResultServicePath := getServiceFullPath(viper.GetString(DockleResultServicePath))
config := &Config{
Verbose: viper.GetBool(Verbose),
WebappPort: viper.GetString(ListeningPort),
GrypeAddress: viper.GetString(GrypeAddress),
KlarTrace: viper.GetBool(KlarTrace),
KlarResultServicePath: klarResultServicePath,
DockleImageName: viper.GetString(DockleImageName),
DockleTimeoutSec: viper.GetString(DockleTimeoutSec),
DockleResultServicePath: dockleResultServicePath,
ResultListenPort: viper.GetString(ResultListenPort),
ResultServiceAddress: getServiceAddress(klarResultServicePath),
ScannerHttpsProxy: viper.GetString(ScannerHttpsProxy),
ScannerHttpProxy: viper.GetString(ScannerHttpProxy),
CredsSecretNamespace: viper.GetString(CredsSecretNamespace),
}
return config
}