Skip to content

Commit 60fcc0b

Browse files
committed
Copy scb annotations from scheduledScans to scans
1 parent f29e18c commit 60fcc0b

File tree

4 files changed

+81
-85
lines changed

4 files changed

+81
-85
lines changed

operator/controllers/execution/scheduledscan_controller.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"fmt"
2222
"reflect"
23+
"regexp"
2324
"sort"
2425
"time"
2526

@@ -123,8 +124,9 @@ func (r *ScheduledScanReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
123124
// It's time!
124125
var scan = &executionv1.Scan{
125126
ObjectMeta: metav1.ObjectMeta{
126-
Namespace: scheduledScan.Namespace,
127-
Labels: scheduledScan.ObjectMeta.GetLabels(),
127+
Namespace: scheduledScan.Namespace,
128+
Labels: scheduledScan.ObjectMeta.GetLabels(),
129+
Annotations: getAnnotationsForScan(scheduledScan),
128130
},
129131
Spec: *scheduledScan.Spec.ScanSpec.DeepCopy(),
130132
}
@@ -153,6 +155,24 @@ func (r *ScheduledScanReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
153155
return ctrl.Result{RequeueAfter: nextSchedule.Sub(time.Now())}, nil
154156
}
155157

158+
// Copy over securecodebox.io annotations from the scheduledScan to the created scan
159+
func getAnnotationsForScan(scheduledScan executionv1.ScheduledScan) map[string]string {
160+
annotations := map[string]string{}
161+
162+
if scheduledScan.Annotations == nil {
163+
return annotations
164+
}
165+
166+
re := regexp.MustCompile(`.*securecodebox\.io/.*`)
167+
for key, value := range scheduledScan.Annotations {
168+
if matches := re.MatchString(key); matches {
169+
annotations[key] = value
170+
}
171+
}
172+
173+
return annotations
174+
}
175+
156176
// Returns a sorted list of scans with a matching state
157177
func getScansWithState(scans []executionv1.Scan, state string) []executionv1.Scan {
158178
// Get a sorted list of scans.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package controllers
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
executionv1 "github.com/secureCodeBox/secureCodeBox/operator/apis/execution/v1"
8+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9+
)
10+
11+
type testData struct {
12+
in map[string]string
13+
expectedMapKeyLength int
14+
}
15+
16+
// Tests that getAnnotationsForScan drops all annotations not prefixed with "*.securecodebox.io/*"
17+
func TestGetAnnotationsForScan(t *testing.T) {
18+
tests := []testData{
19+
{
20+
in: map[string]string{
21+
"foobar": "bar",
22+
},
23+
expectedMapKeyLength: 0,
24+
},
25+
{
26+
in: map[string]string{
27+
"foobar.securecodebox.io/bar": "bar",
28+
},
29+
expectedMapKeyLength: 1,
30+
},
31+
{
32+
in: map[string]string{
33+
"barfoo.securecodebox.io/bar": "bar",
34+
"foo": "bar",
35+
},
36+
expectedMapKeyLength: 1,
37+
},
38+
{
39+
in: map[string]string{
40+
"barfoo.securecodebox.io/bar": "bar",
41+
"barfoo.securecodebox.io/foo": "bar",
42+
},
43+
expectedMapKeyLength: 2,
44+
},
45+
}
46+
47+
for _, test := range tests {
48+
scheduledScan := executionv1.ScheduledScan{
49+
ObjectMeta: metav1.ObjectMeta{
50+
Name: "foobar",
51+
Annotations: test.in,
52+
},
53+
}
54+
actual := getAnnotationsForScan(scheduledScan)
55+
if len(actual) != test.expectedMapKeyLength {
56+
t.Error(fmt.Errorf("getAnnotationsForScan should only copy over annotations following the pattern '*.securecodebox.io', but map: %v returned a map with %d keys (%d expected)", test.in, len(actual), test.expectedMapKeyLength))
57+
}
58+
}
59+
}

operator/controllers/execution/suite_test.go

Lines changed: 0 additions & 81 deletions
This file was deleted.

operator/go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ go 1.15
55
require (
66
github.com/go-logr/logr v0.1.0
77
github.com/minio/minio-go/v7 v7.0.6
8-
github.com/onsi/ginkgo v1.11.0
9-
github.com/onsi/gomega v1.8.1
108
k8s.io/api v0.17.2
119
k8s.io/apimachinery v0.17.2
1210
k8s.io/client-go v0.17.2

0 commit comments

Comments
 (0)