-
Notifications
You must be signed in to change notification settings - Fork 68
/
config_test.go
268 lines (254 loc) · 7.74 KB
/
config_test.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
package etc
import (
"os"
"testing"
"time"
"github.com/aquasecurity/harbor-scanner-trivy/pkg/harbor"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type Envs map[string]string
func TestGetLogLevel(t *testing.T) {
testCases := []struct {
Name string
Envs Envs
ExpectedLogLevel logrus.Level
}{
{
Name: "Should return default log level when env is not set",
ExpectedLogLevel: logrus.InfoLevel,
},
{
Name: "Should return default log level when env has invalid value",
Envs: Envs{
"SCANNER_LOG_LEVEL": "unknown_level",
},
ExpectedLogLevel: logrus.InfoLevel,
},
{
Name: "Should return log level set as env",
Envs: Envs{
"SCANNER_LOG_LEVEL": "trace",
},
ExpectedLogLevel: logrus.TraceLevel,
},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
setenvs(t, tc.Envs)
assert.Equal(t, tc.ExpectedLogLevel, GetLogLevel())
})
}
}
func TestGetConfig(t *testing.T) {
testCases := []struct {
name string
envs Envs
expectedError error
expectedConfig Config
}{
{
name: "Should enable Trivy debug mode when log level is set to debug",
envs: Envs{
"SCANNER_LOG_LEVEL": "debug",
},
expectedConfig: Config{
API: API{
Addr: ":8080",
ReadTimeout: parseDuration(t, "15s"),
WriteTimeout: parseDuration(t, "15s"),
IdleTimeout: parseDuration(t, "60s"),
},
Trivy: Trivy{
DebugMode: true,
CacheDir: "/home/scanner/.cache/trivy",
ReportsDir: "/home/scanner/.cache/reports",
VulnType: "os,library",
SecurityChecks: "vuln",
Severity: "UNKNOWN,LOW,MEDIUM,HIGH,CRITICAL",
Insecure: false,
GitHubToken: "",
Timeout: parseDuration(t, "5m0s"),
},
RedisPool: RedisPool{
URL: "redis://localhost:6379",
MaxActive: 5,
MaxIdle: 5,
IdleTimeout: parseDuration(t, "5m"),
ConnectionTimeout: parseDuration(t, "1s"),
ReadTimeout: parseDuration(t, "1s"),
WriteTimeout: parseDuration(t, "1s"),
},
RedisStore: RedisStore{
Namespace: "harbor.scanner.trivy:data-store",
ScanJobTTL: parseDuration(t, "1h"),
},
JobQueue: JobQueue{
Namespace: "harbor.scanner.trivy:job-queue",
WorkerConcurrency: 1,
},
},
},
{
name: "Should return default config",
expectedConfig: Config{
API: API{
Addr: ":8080",
ReadTimeout: parseDuration(t, "15s"),
WriteTimeout: parseDuration(t, "15s"),
IdleTimeout: parseDuration(t, "60s"),
},
Trivy: Trivy{
DebugMode: false,
CacheDir: "/home/scanner/.cache/trivy",
ReportsDir: "/home/scanner/.cache/reports",
VulnType: "os,library",
SecurityChecks: "vuln",
Severity: "UNKNOWN,LOW,MEDIUM,HIGH,CRITICAL",
Insecure: false,
GitHubToken: "",
Timeout: parseDuration(t, "5m0s"),
},
RedisPool: RedisPool{
URL: "redis://localhost:6379",
MaxActive: 5,
MaxIdle: 5,
IdleTimeout: parseDuration(t, "5m"),
ConnectionTimeout: parseDuration(t, "1s"),
ReadTimeout: parseDuration(t, "1s"),
WriteTimeout: parseDuration(t, "1s"),
},
RedisStore: RedisStore{
Namespace: "harbor.scanner.trivy:data-store",
ScanJobTTL: parseDuration(t, "1h"),
},
JobQueue: JobQueue{
Namespace: "harbor.scanner.trivy:job-queue",
WorkerConcurrency: 1,
},
},
},
{
name: "Should overwrite default config with environment variables",
envs: Envs{
"SCANNER_API_SERVER_ADDR": ":4200",
"SCANNER_API_SERVER_TLS_CERTIFICATE": "/certs/tls.crt",
"SCANNER_API_SERVER_TLS_KEY": "/certs/tls.key",
"SCANNER_API_SERVER_CLIENT_CAS": "/certs/tls1.crt,/certs/tls2.crt",
"SCANNER_API_SERVER_TLS_MIN_VERSION": "1.0",
"SCANNER_API_SERVER_TLS_MAX_VERSION": "1.2",
"SCANNER_API_SERVER_READ_TIMEOUT": "1h",
"SCANNER_API_SERVER_WRITE_TIMEOUT": "2m",
"SCANNER_API_SERVER_IDLE_TIMEOUT": "3m10s",
"SCANNER_TRIVY_CACHE_DIR": "/home/scanner/trivy-cache",
"SCANNER_TRIVY_REPORTS_DIR": "/home/scanner/trivy-reports",
"SCANNER_TRIVY_DEBUG_MODE": "true",
"SCANNER_TRIVY_VULN_TYPE": "os,library",
"SCANNER_TRIVY_SECURITY_CHECKS": "vuln",
"SCANNER_TRIVY_SEVERITY": "CRITICAL",
"SCANNER_TRIVY_IGNORE_UNFIXED": "true",
"SCANNER_TRIVY_INSECURE": "true",
"SCANNER_TRIVY_SKIP_UPDATE": "true",
"SCANNER_TRIVY_OFFLINE_SCAN": "true",
"SCANNER_TRIVY_GITHUB_TOKEN": "<GITHUB_TOKEN>",
"SCANNER_TRIVY_TIMEOUT": "15m30s",
"SCANNER_STORE_REDIS_NAMESPACE": "store.ns",
"SCANNER_STORE_REDIS_SCAN_JOB_TTL": "2h45m15s",
"SCANNER_JOB_QUEUE_REDIS_NAMESPACE": "job-queue.ns",
"SCANNER_JOB_QUEUE_WORKER_CONCURRENCY": "3",
"SCANNER_REDIS_URL": "redis://harbor-harbor-redis:6379",
"SCANNER_REDIS_POOL_MAX_ACTIVE": "3",
"SCANNER_REDIS_POOL_MAX_IDLE": "7",
"SCANNER_REDIS_POOL_IDLE_TIMEOUT": "3m",
},
expectedConfig: Config{
API: API{
Addr: ":4200",
TLSCertificate: "/certs/tls.crt",
TLSKey: "/certs/tls.key",
ClientCAs: []string{"/certs/tls1.crt", "/certs/tls2.crt"},
ReadTimeout: parseDuration(t, "1h"),
WriteTimeout: parseDuration(t, "2m"),
IdleTimeout: parseDuration(t, "3m10s"),
},
Trivy: Trivy{
CacheDir: "/home/scanner/trivy-cache",
ReportsDir: "/home/scanner/trivy-reports",
DebugMode: true,
VulnType: "os,library",
SecurityChecks: "vuln",
Severity: "CRITICAL",
IgnoreUnfixed: true,
SkipUpdate: true,
OfflineScan: true,
Insecure: true,
GitHubToken: "<GITHUB_TOKEN>",
Timeout: parseDuration(t, "15m30s"),
},
RedisPool: RedisPool{
URL: "redis://harbor-harbor-redis:6379",
MaxActive: 3,
MaxIdle: 7,
IdleTimeout: parseDuration(t, "3m"),
ConnectionTimeout: parseDuration(t, "1s"),
ReadTimeout: parseDuration(t, "1s"),
WriteTimeout: parseDuration(t, "1s"),
},
RedisStore: RedisStore{
Namespace: "store.ns",
ScanJobTTL: parseDuration(t, "2h45m15s"),
},
JobQueue: JobQueue{
Namespace: "job-queue.ns",
WorkerConcurrency: 3,
},
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
setenvs(t, tc.envs)
config, err := GetConfig()
assert.Equal(t, tc.expectedError, err)
assert.Equal(t, tc.expectedConfig, config)
})
}
}
func TestGetScannerMetadata(t *testing.T) {
testCases := []struct {
name string
envs Envs
expectedScanner harbor.Scanner
}{
{
name: "Should return version set via env",
envs: Envs{"TRIVY_VERSION": "0.1.6"},
expectedScanner: harbor.Scanner{Name: "Trivy", Vendor: "Aqua Security", Version: "0.1.6"},
},
{
name: "Should return unknown version when it is not set via env",
expectedScanner: harbor.Scanner{Name: "Trivy", Vendor: "Aqua Security", Version: "Unknown"},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
setenvs(t, tc.envs)
assert.Equal(t, tc.expectedScanner, GetScannerMetadata())
})
}
}
func setenvs(t *testing.T, envs Envs) {
t.Helper()
os.Clearenv()
for k, v := range envs {
err := os.Setenv(k, v)
require.NoError(t, err)
}
}
func parseDuration(t *testing.T, s string) time.Duration {
t.Helper()
duration, err := time.ParseDuration(s)
require.NoError(t, err)
return duration
}