-
Notifications
You must be signed in to change notification settings - Fork 197
/
helper.go
363 lines (320 loc) · 9.52 KB
/
helper.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
package helper
import (
"context"
"fmt"
"time"
"github.com/aquasecurity/starboard/pkg/apis/aquasecurity/v1alpha1"
"github.com/aquasecurity/starboard/pkg/kube"
"github.com/aquasecurity/starboard/pkg/kubebench"
"github.com/aquasecurity/starboard/pkg/starboard"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/rand"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
)
type PodBuilder struct {
name string
namespace string
containers []corev1.Container
imagePullSecrets []corev1.LocalObjectReference
}
func NewPod() *PodBuilder {
return &PodBuilder{}
}
func (b *PodBuilder) WithName(name string) *PodBuilder {
b.name = name
return b
}
func (b *PodBuilder) WithRandomName(prefix string) *PodBuilder {
return b.WithName(prefix + "-" + rand.String(5))
}
func (b *PodBuilder) WithNamespace(namespace string) *PodBuilder {
b.namespace = namespace
return b
}
func (b *PodBuilder) WithContainer(name, image string) *PodBuilder {
b.containers = append(b.containers, corev1.Container{
Name: name,
Image: image,
})
return b
}
func (b *PodBuilder) WithImagePullSecret(name string) *PodBuilder {
b.imagePullSecrets = append(b.imagePullSecrets, corev1.LocalObjectReference{
Name: name,
})
return b
}
func (b *PodBuilder) Build() *corev1.Pod {
return &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: b.name,
Namespace: b.namespace,
},
Spec: corev1.PodSpec{
Containers: b.containers,
ImagePullSecrets: b.imagePullSecrets,
},
}
}
type DeploymentBuilder struct {
name string
namespace string
containers []corev1.Container
}
func NewDeployment() *DeploymentBuilder {
return &DeploymentBuilder{}
}
func (b *DeploymentBuilder) WithName(name string) *DeploymentBuilder {
b.name = name
return b
}
func (b *DeploymentBuilder) WithRandomName(prefix string) *DeploymentBuilder {
return b.WithName(prefix + "-" + rand.String(5))
}
func (b *DeploymentBuilder) WithNamespace(namespace string) *DeploymentBuilder {
b.namespace = namespace
return b
}
func (b *DeploymentBuilder) WithContainer(name, image string) *DeploymentBuilder {
b.containers = append(b.containers, corev1.Container{
Name: name,
Image: image,
})
return b
}
func (b *DeploymentBuilder) Build() *appsv1.Deployment {
return &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: b.name,
Namespace: b.namespace,
},
Spec: appsv1.DeploymentSpec{
Replicas: pointer.Int32Ptr(1),
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"app": b.name,
},
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: labels.Set{
"app": b.name,
},
},
Spec: corev1.PodSpec{
Containers: b.containers,
},
},
},
}
}
var (
trivyScanner = v1alpha1.Scanner{
Name: "Trivy",
Vendor: "Aqua Security",
Version: "0.16.0",
}
)
type VulnerabilityReportBuilder struct {
name string
namespace string
ownerKind kube.Kind
ownerName string
}
func NewVulnerabilityReport() *VulnerabilityReportBuilder {
return &VulnerabilityReportBuilder{}
}
func (b *VulnerabilityReportBuilder) WithName(name string) *VulnerabilityReportBuilder {
b.name = name
return b
}
func (b *VulnerabilityReportBuilder) WithNamespace(namespace string) *VulnerabilityReportBuilder {
b.namespace = namespace
return b
}
func (b *VulnerabilityReportBuilder) WithOwnerKind(kind kube.Kind) *VulnerabilityReportBuilder {
b.ownerKind = kind
return b
}
func (b *VulnerabilityReportBuilder) WithOwnerName(name string) *VulnerabilityReportBuilder {
b.ownerName = name
return b
}
func (b *VulnerabilityReportBuilder) Build() *v1alpha1.VulnerabilityReport {
return &v1alpha1.VulnerabilityReport{
ObjectMeta: metav1.ObjectMeta{
Name: b.name,
Namespace: b.namespace,
Labels: map[string]string{
starboard.LabelContainerName: "nginx", // TODO Make it configurable
starboard.LabelResourceKind: string(b.ownerKind),
starboard.LabelResourceName: b.ownerName,
starboard.LabelResourceNamespace: b.namespace,
},
},
Report: v1alpha1.VulnerabilityScanResult{
UpdateTimestamp: metav1.NewTime(time.Now()),
Scanner: trivyScanner,
Registry: v1alpha1.Registry{
Server: "index.docker.io",
},
Artifact: v1alpha1.Artifact{
Repository: "library/nginx",
Tag: "1.16",
},
Summary: v1alpha1.VulnerabilitySummary{
MediumCount: 1,
},
Vulnerabilities: []v1alpha1.Vulnerability{
{
VulnerabilityID: "CVE-2020-3810",
Resource: "apt",
InstalledVersion: "1.8.2",
FixedVersion: "1.8.2.1",
Severity: v1alpha1.SeverityMedium,
Title: "",
Description: "Missing input validation in the ar/tar implementations of APT before version 2.1.2 could result in denial of service when processing specially crafted deb files.",
Links: []string{
"https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2020-3810",
},
},
},
},
}
}
// Helper is a mix of asserts and helpers, but we can fix that later.
type Helper struct {
scheme *runtime.Scheme
kubeClient client.Client
kubeBenchReportReader kubebench.Reader
}
func NewHelper(scheme *runtime.Scheme, client client.Client) *Helper {
return &Helper{
scheme: scheme,
kubeClient: client,
kubeBenchReportReader: kubebench.NewReadWriter(client),
}
}
func (h *Helper) HasActiveReplicaSet(namespace, name string) func() (bool, error) {
return func() (bool, error) {
rs, err := h.GetActiveReplicaSetForDeployment(namespace, name)
if err != nil {
return false, err
}
return rs != nil, nil
}
}
func (h *Helper) HasVulnerabilityReportOwnedBy(obj client.Object) func() (bool, error) {
return func() (bool, error) {
gvk, err := apiutil.GVKForObject(obj, h.scheme)
if err != nil {
return false, err
}
var reportList v1alpha1.VulnerabilityReportList
err = h.kubeClient.List(context.Background(), &reportList, client.MatchingLabels{
starboard.LabelResourceKind: gvk.Kind,
starboard.LabelResourceName: obj.GetName(),
starboard.LabelResourceNamespace: obj.GetNamespace(),
})
if err != nil {
return false, err
}
return len(reportList.Items) == 1, nil
}
}
func (h *Helper) HasConfigAuditReportOwnedBy(obj client.Object) func() (bool, error) {
return func() (bool, error) {
gvk, err := apiutil.GVKForObject(obj, h.scheme)
if err != nil {
return false, err
}
var reportsList v1alpha1.ConfigAuditReportList
err = h.kubeClient.List(context.Background(), &reportsList, client.MatchingLabels{
starboard.LabelResourceKind: gvk.Kind,
starboard.LabelResourceName: obj.GetName(),
starboard.LabelResourceNamespace: obj.GetNamespace(),
})
if err != nil {
return false, err
}
return len(reportsList.Items) == 1 && reportsList.Items[0].DeletionTimestamp == nil, nil
}
}
func (h *Helper) DeleteConfigAuditReportOwnedBy(obj client.Object) error {
gvk, err := apiutil.GVKForObject(obj, h.scheme)
if err != nil {
return err
}
var reportsList v1alpha1.ConfigAuditReportList
err = h.kubeClient.List(context.Background(), &reportsList, client.MatchingLabels{
starboard.LabelResourceKind: gvk.Kind,
starboard.LabelResourceName: obj.GetName(),
starboard.LabelResourceNamespace: obj.GetNamespace(),
})
if err != nil {
return err
}
return h.kubeClient.Delete(context.Background(), &reportsList.Items[0])
}
func (h *Helper) GetActiveReplicaSetForDeployment(namespace, name string) (*appsv1.ReplicaSet, error) {
var deployment appsv1.Deployment
var replicaSetList appsv1.ReplicaSetList
err := h.kubeClient.Get(context.TODO(), types.NamespacedName{
Name: name, Namespace: namespace,
}, &deployment)
if err != nil {
return nil, err
}
deploymentSelector, err := metav1.LabelSelectorAsMap(deployment.Spec.Selector)
if err != nil {
return nil, fmt.Errorf("mapping label selector: %w", err)
}
selector := labels.Set(deploymentSelector)
err = h.kubeClient.List(context.TODO(), &replicaSetList, client.MatchingLabels(selector))
if err != nil {
return nil, err
}
for _, replicaSet := range replicaSetList.Items {
if deployment.Annotations["deployment.kubernetes.io/revision"] !=
replicaSet.Annotations["deployment.kubernetes.io/revision"] {
continue
}
return replicaSet.DeepCopy(), nil
}
return nil, nil
}
func (h *Helper) HasCISKubeBenchReportOwnedBy(node corev1.Node) func() (bool, error) {
return func() (bool, error) {
report, err := h.kubeBenchReportReader.FindByOwner(context.Background(), kube.Object{Kind: kube.KindNode, Name: node.Name})
if err != nil {
return false, err
}
return report != nil, nil
}
}
func (h *Helper) UpdateDeploymentImage(namespace, name string) error {
// TODO Check kubectl set image implementation
return wait.PollImmediate(5*time.Second, 2*time.Minute, func() (bool, error) {
var deployment appsv1.Deployment
err := h.kubeClient.Get(context.TODO(), types.NamespacedName{Name: name, Namespace: namespace}, &deployment)
if err != nil {
return false, err
}
dcDeploy := deployment.DeepCopy()
dcDeploy.Spec.Template.Spec.Containers[0].Image = "wordpress:5"
err = h.kubeClient.Update(context.TODO(), dcDeploy)
if err != nil && errors.IsConflict(err) {
return false, nil
}
return err == nil, err
})
}