This repository has been archived by the owner on Dec 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapp_blackduck.go
344 lines (313 loc) · 11.1 KB
/
app_blackduck.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
package testutils
import (
"encoding/json"
"fmt"
"log"
"reflect"
"regexp"
"strings"
"github.com/blackducksoftware/synopsysctl/pkg/blackduck"
"github.com/blackducksoftware/synopsysctl/pkg/util"
corev1 "k8s.io/api/core/v1"
)
// GetLatestBlackDuckVersion ...
func GetLatestBlackDuckVersion() string {
if TestConfig.BlackDuck.Version != "" {
return TestConfig.BlackDuck.Version
}
return "2020.4.0"
}
// GetBlackDuckTLSCertPath ...
func GetBlackDuckTLSCertPath() string {
if TestConfig.BlackDuck.TLSCertPath != "" {
return TestConfig.BlackDuck.TLSCertPath
}
return GetMockTLSCertificate()
}
// GetBlackDuckTLSKeyPath ...
func GetBlackDuckTLSKeyPath() string {
if TestConfig.BlackDuck.TLSKeyPath != "" {
return TestConfig.BlackDuck.TLSKeyPath
}
return GetMockTLSKey()
}
// NewBlackDuckTester ...
func NewBlackDuckTester() *BlackDuckTester {
t := BlackDuckTester{
AppName: util.BlackDuckName,
}
// Get default values for an Black Duck test
t.Name = GenName(t.AppName)
t.Namespace = CreateUniqueNamespace(t.AppName)
t.Version = GetLatestBlackDuckVersion()
t.Labels = ""
// Config for Black Duck
t.FlagTree = blackduck.FlagTree{}
t.FlagTree.PersistentStorage = "TRUE"
t.FlagTree.ExposeService = "NODEPORT"
return &t
}
// BlackDuckTester ...
type BlackDuckTester struct {
AppName string
Name string
Namespace string
Version string
Labels string
FlagTree blackduck.FlagTree
}
// WaitUntilReady ...
func (t BlackDuckTester) WaitUntilReady() {
err := util.WaitForPodsToAppear(KubeClient, t.Namespace, t.Labels)
if err != nil {
log.Fatalf("%+v", err)
}
util.WaitForPodsToBeRunningOrComplete(KubeClient, t.Namespace, t.Labels)
if err != nil {
log.Fatalf("%+v", err)
}
util.WaitForPodsToStopTerminating(KubeClient, t.Namespace)
if err != nil {
log.Fatalf("%+v", err)
}
}
// Verify ...
func (t BlackDuckTester) Verify() error {
checks := []func() error{
t.checkPersistentStorageExists,
t.checkExposedService,
t.checkSecurityContexts,
t.checkBinaryAnalysis,
}
var err error
for _, check := range checks {
err = check()
if err != nil {
return err
}
}
return nil
}
func (t BlackDuckTester) getPodIDHelper(podName string) string {
// podName format: <name>-blackduck-<podname>
// podID format: blackduck-<podname>
rePattern := fmt.Sprintf("%s-(blackduck-[a-z]+[-a-z]*)-[0-9]+", t.Name)
r, _ := regexp.Compile(rePattern)
matches := r.FindStringSubmatch(podName)
if len(matches) >= 2 {
return matches[1]
}
return ""
}
func (t BlackDuckTester) podWithNameExists(podName, namespace string) bool {
targetPoID := podName
found := false
podList, err := util.ListPodsWithLabels(KubeClient, namespace, "")
if err != nil {
return false
}
for _, po := range podList.Items {
poID := t.getPodIDHelper(po.Name)
if poID == targetPoID {
found = true
break
}
}
return found
}
func (t BlackDuckTester) checkPersistentStorageExists() error {
pvcList, err := util.ListPVCs(KubeClient, t.Namespace, "")
if err != nil {
return err
}
if strings.ToUpper(t.FlagTree.PersistentStorage) == "TRUE" {
// Verify PVCs exist
if len(pvcList.Items) == 0 {
return fmt.Errorf("persistentStorage is enabled but found %+v PVCs", len(pvcList.Items))
}
} else {
// Verify PVCs do not exist
if len(pvcList.Items) != 0 {
return fmt.Errorf("persistentStorage is disabled but found %+v PVCs", len(pvcList.Items))
}
}
return nil
}
func (t BlackDuckTester) checkBinaryAnalysis() error {
binaryScannerPoID := "blackduck-binaryscanner"
rabbitmqPoID := "blackduck-rabbitmq"
foundBinaryScanner := t.podWithNameExists(binaryScannerPoID, t.Namespace)
foundRabbitMQ := t.podWithNameExists(rabbitmqPoID, t.Namespace)
if foundBinaryScanner != t.FlagTree.EnableBinaryAnalysis {
return fmt.Errorf("enableBinaryAnalysis is %+v but foundBinaryScanner='%+v'", t.FlagTree.EnableBinaryAnalysis, foundBinaryScanner)
}
if t.FlagTree.EnableBinaryAnalysis && !foundRabbitMQ {
return fmt.Errorf("enableBinaryAnalysis is %+v but didn't find RabbitMQ", t.FlagTree.EnableBinaryAnalysis)
}
return nil
}
func (t BlackDuckTester) checkExposedService() error {
serviceList, err := util.ListServices(KubeClient, t.Namespace, "")
if err != nil {
return err
}
if t.FlagTree.ExposeService != util.NONE {
if t.FlagTree.ExposeService != util.OPENSHIFT {
// Get Exposed Service
var exposedService *corev1.Service
for _, svc := range serviceList.Items {
if svc.Spec.Type != corev1.ServiceTypeClusterIP {
exposedService = &svc
break
}
}
if exposedService == nil {
return fmt.Errorf("expected LoadBalancer or NodePort but failed to find the exposed service")
}
switch t.FlagTree.ExposeService {
case util.LOADBALANCER:
if exposedService.Spec.Type != corev1.ServiceTypeLoadBalancer {
return fmt.Errorf("expected LoadBalancer but got %+v", exposedService.Spec.Type)
}
case util.NODEPORT:
if exposedService.Spec.Type != corev1.ServiceTypeNodePort {
return fmt.Errorf("expected NodePort but got %+v", exposedService.Spec.Type)
}
}
} else {
routeClient := util.GetRouteClient(Restconfig, KubeClient, t.Namespace)
routes, err := util.ListRoutes(routeClient, t.Namespace, t.Labels)
if err != nil {
return fmt.Errorf("failed to get routes")
}
if len(routes.Items) != 1 {
return fmt.Errorf("expected 1 route but found %+v routes", len(routes.Items))
}
}
}
return nil
}
func podIDToPodSecurityContextIDHelper(podID string) string {
podIDToPodSecurityContextID := map[string]string{
"blackduck-postgres": "blackduck-postgres",
"blackduck-authentication": "blackduck-authentication",
"blackduck-cfssl": "blackduck-cfssl",
"blackduck-documentation": "blackduck-documentation",
"blackduck-jobrunner": "blackduck-jobrunner",
"blackduck-rabbitmq": "blackduck-rabbitmq",
"blackduck-registration": "blackduck-registration",
"blackduck-scan": "blackduck-scan",
"blackduck-uploadcache": "blackduck-uploadcache",
"blackduck-webapp-logstash": "blackduck-webapp",
"blackduck-webserver": "blackduck-nginx",
"blackduck-binaryscanner": "appcheck-worker",
}
if val, ok := podIDToPodSecurityContextID[podID]; ok {
return val
}
return ""
}
func podIDToSecurityContextIDsHelper(podID string) []string {
podIDToSecurityContextIDs := map[string][]string{
"blackduck-init": {"blackduck-init"},
"blackduck-webapp-logstash": {"blackduck-logstash"},
}
if val, ok := podIDToSecurityContextIDs[podID]; ok {
return val
}
return []string{}
}
func (t BlackDuckTester) checkSecurityContexts() error {
userSecurityContexts := map[string]corev1.PodSecurityContext{}
if t.FlagTree.SecurityContextFilePath != "" {
data, err := util.ReadFileData(t.FlagTree.SecurityContextFilePath)
if err != nil {
return fmt.Errorf("failed to read security context file: %+v", err)
}
err = json.Unmarshal([]byte(data), &userSecurityContexts)
if err != nil {
return fmt.Errorf("failed to unmarshal security contexts: %+v", err)
}
}
podList, err := util.ListPodsWithLabels(KubeClient, t.Namespace, t.Labels)
if err != nil {
return fmt.Errorf("failed to list pods: %+v", err)
}
// Get Security Contexts of each Pod
podSecurityContextMap := map[string]*corev1.PodSecurityContext{}
securityContextMap := map[string][]*corev1.SecurityContext{}
for _, po := range podList.Items {
poID := t.getPodIDHelper(po.Name)
if poID == "" {
continue
}
defaultPodSecurityContext := &corev1.PodSecurityContext{
FSGroup: util.IntToInt64(0),
}
emptyPodSecurityContext := &corev1.PodSecurityContext{}
if po.Spec.SecurityContext != nil {
if po.Spec.SecurityContext != nil && !reflect.DeepEqual(po.Spec.SecurityContext, defaultPodSecurityContext) && !reflect.DeepEqual(po.Spec.SecurityContext, emptyPodSecurityContext) {
podSecurityContextMap[poID] = po.Spec.SecurityContext
}
}
for _, container := range po.Spec.Containers {
if container.SecurityContext != nil && container.Name != "synopsys-init" {
if _, ok := securityContextMap[poID]; ok {
securityContextMap[poID] = append(securityContextMap[poID], container.SecurityContext)
} else {
securityContextMap[poID] = []*corev1.SecurityContext{container.SecurityContext}
}
}
}
}
// Check if security contexts were provided then they were found
if len(userSecurityContexts) == 0 && (len(podSecurityContextMap) > 0 || len(securityContextMap) > 0) {
return fmt.Errorf("no security contexts were set but found %d PodSecurityContexts and %d SecurityContexts", len(podSecurityContextMap), len(securityContextMap))
}
if len(userSecurityContexts) > 0 && (len(podSecurityContextMap) == 0 && len(securityContextMap) == 0) {
return fmt.Errorf("security contexts were set but found %d PodSecurityContexts and %d SecurityContexts", len(podSecurityContextMap), len(securityContextMap))
}
// Check Pod Security Context values that were found
for poID, podSecurityContext := range podSecurityContextMap {
pscID := podIDToPodSecurityContextIDHelper(poID)
if userSecurityContext, ok := userSecurityContexts[pscID]; ok {
if podSecurityContext.FSGroup != nil && *userSecurityContext.FSGroup != *podSecurityContext.FSGroup {
return fmt.Errorf("PodSecurityContext FSGroup mismatch for %s: got %+v; expected %+v", pscID, podSecurityContext.FSGroup, userSecurityContext.FSGroup)
}
if podSecurityContext.RunAsGroup != nil && *userSecurityContext.RunAsGroup != *podSecurityContext.RunAsGroup {
return fmt.Errorf("PodSecurityContext RunAsGroup mismatch for %s: got %+v; expected %+v", pscID, podSecurityContext.RunAsGroup, userSecurityContext.RunAsGroup)
}
if podSecurityContext.RunAsUser != nil && *userSecurityContext.RunAsUser != *podSecurityContext.RunAsUser {
return fmt.Errorf("PodSecurityContext RunAsUser mismatch for %s: got %+v; expected %+v", pscID, podSecurityContext.RunAsUser, userSecurityContext.RunAsUser)
}
}
}
// Check Security Context values that were found
for poID, foundSecurityContexts := range securityContextMap {
for _, foundSecurityContext := range foundSecurityContexts { // for each security context that was found
// Get names of Security Contexts for the Pod
scIDs := podIDToSecurityContextIDsHelper(poID)
// Check if the found Security Context matches a Security Context provided by the user
foundMatch := false
for _, scID := range scIDs {
// Check if the user provided the Security Context
if userSecurityContext, ok := userSecurityContexts[scID]; ok {
if foundSecurityContext.RunAsGroup != nil && *userSecurityContext.RunAsGroup != *foundSecurityContext.RunAsGroup {
continue
// return fmt.Errorf("SecurityContext RunAsGroup mismatch for %s: got %+v; expected %+v", poID, securityContext.RunAsGroup, userSecurityContext.RunAsGroup)
}
if foundSecurityContext.RunAsUser != nil && *userSecurityContext.RunAsUser != *foundSecurityContext.RunAsUser {
continue
// return fmt.Errorf("SecurityContext RunAsUser mismatch for %s: got %+v; expected %+v", poID, securityContext.RunAsUser, userSecurityContext.RunAsUser)
}
foundMatch = true
break
}
}
if !foundMatch {
return fmt.Errorf("no Security Contexts in containers %+v matched for Pod %s", scIDs, poID)
}
}
}
return nil
}