-
Notifications
You must be signed in to change notification settings - Fork 197
/
matcher.go
192 lines (171 loc) · 5.81 KB
/
matcher.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
package matcher
import (
"fmt"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gstruct"
"github.com/aquasecurity/starboard/pkg/apis/aquasecurity/v1alpha1"
"github.com/aquasecurity/starboard/pkg/kube"
"github.com/aquasecurity/starboard/pkg/starboard"
"github.com/onsi/gomega/types"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
)
var (
trivyScanner = v1alpha1.Scanner{
Name: "Trivy",
Vendor: "Aqua Security",
Version: "0.25.2",
}
builtInScanner = v1alpha1.Scanner{
Name: "Starboard",
Vendor: "Aqua Security",
Version: "dev",
}
)
// IsVulnerabilityReportForContainerOwnedBy succeeds if a v1alpha1.VulnerabilityReport has a valid structure,
// corresponds to the given container and is owned by the specified client.Object.
//
// Note: This matcher is not suitable for unit tests because it does not perform a strict validation
// of the actual v1alpha1.VulnerabilityReport.
func IsVulnerabilityReportForContainerOwnedBy(containerName string, owner client.Object) types.GomegaMatcher {
return &vulnerabilityReportMatcher{
scheme: starboard.NewScheme(),
containerName: containerName,
owner: owner,
}
}
type vulnerabilityReportMatcher struct {
scheme *runtime.Scheme
owner client.Object
containerName string
failureMessage string
negatedFailureMessage string
}
func (m *vulnerabilityReportMatcher) Match(actual interface{}) (bool, error) {
_, ok := actual.(v1alpha1.VulnerabilityReport)
if !ok {
return false, fmt.Errorf("%T expects a %T", vulnerabilityReportMatcher{}, v1alpha1.VulnerabilityReport{})
}
gvk, err := apiutil.GVKForObject(m.owner, m.scheme)
if err != nil {
return false, err
}
keys, err := m.objectToLabelsAsMatchKeys(m.owner)
if err != nil {
return false, err
}
keys[starboard.LabelContainerName] = Equal(m.containerName)
matcher := MatchFields(IgnoreExtras, Fields{
"ObjectMeta": MatchFields(IgnoreExtras, Fields{
"Labels": MatchKeys(IgnoreExtras, keys),
"OwnerReferences": ConsistOf(metav1.OwnerReference{
APIVersion: gvk.GroupVersion().Identifier(),
Kind: gvk.Kind,
Name: m.owner.GetName(),
UID: m.owner.GetUID(),
Controller: pointer.BoolPtr(true),
BlockOwnerDeletion: pointer.BoolPtr(false),
}),
}),
"Report": MatchFields(IgnoreExtras, Fields{
"Scanner": Equal(trivyScanner),
"Vulnerabilities": Not(BeNil()),
}),
})
success, err := matcher.Match(actual)
if err != nil {
return false, err
}
m.failureMessage = matcher.FailureMessage(actual)
m.negatedFailureMessage = matcher.NegatedFailureMessage(actual)
return success, nil
}
func (m *vulnerabilityReportMatcher) objectToLabelsAsMatchKeys(obj client.Object) (map[interface{}]types.GomegaMatcher, error) {
kind := obj.GetObjectKind().GroupVersionKind().Kind
if kind == "" {
gvk, err := apiutil.GVKForObject(m.owner, m.scheme)
if err != nil {
return nil, err
}
kind = gvk.Kind
}
labels := kube.ObjectRefToLabels(kube.ObjectRef{
Kind: kube.Kind(kind),
Name: obj.GetName(),
Namespace: obj.GetNamespace(),
})
keys := make(map[interface{}]types.GomegaMatcher)
for k, v := range labels {
keys[k] = Equal(v)
}
return keys, nil
}
func (m *vulnerabilityReportMatcher) FailureMessage(_ interface{}) string {
// TODO Add more descriptive message rather than rely on composed matchers' defaults
return m.failureMessage
}
func (m *vulnerabilityReportMatcher) NegatedFailureMessage(_ interface{}) string {
return m.negatedFailureMessage
}
// IsConfigAuditReportOwnedBy succeeds if a v1alpha1.ConfigAuditReport has a valid structure,
// and is owned by the specified client.Object.
//
// Note: This matcher is not suitable for unit tests because it does not perform a strict validation
// of the actual v1alpha1.ConfigAuditReport.
func IsConfigAuditReportOwnedBy(owner client.Object) types.GomegaMatcher {
return &configAuditReportMatcher{
owner: owner,
}
}
type configAuditReportMatcher struct {
owner client.Object
failureMessage string
negatedFailureMessage string
}
func (m *configAuditReportMatcher) Match(actual interface{}) (bool, error) {
_, ok := actual.(v1alpha1.ConfigAuditReport)
if !ok {
return false, fmt.Errorf("%T expects a %T", configAuditReportMatcher{}, v1alpha1.ConfigAuditReport{})
}
gvk, err := apiutil.GVKForObject(m.owner, starboard.NewScheme())
if err != nil {
return false, err
}
matcher := MatchFields(IgnoreExtras, Fields{
"ObjectMeta": MatchFields(IgnoreExtras, Fields{
"Labels": MatchKeys(IgnoreExtras, Keys{
starboard.LabelResourceKind: Equal(gvk.Kind),
starboard.LabelResourceName: Equal(m.owner.GetName()),
starboard.LabelResourceNamespace: Equal(m.owner.GetNamespace()),
}),
"OwnerReferences": ConsistOf(metav1.OwnerReference{
APIVersion: gvk.GroupVersion().Identifier(),
Kind: gvk.Kind,
Name: m.owner.GetName(),
UID: m.owner.GetUID(),
Controller: pointer.BoolPtr(true),
BlockOwnerDeletion: pointer.BoolPtr(false),
}),
}),
"Report": MatchFields(IgnoreExtras, Fields{
"Scanner": Equal(builtInScanner),
}),
})
success, err := matcher.Match(actual)
if err != nil {
return false, err
}
m.failureMessage = matcher.FailureMessage(actual)
m.negatedFailureMessage = matcher.NegatedFailureMessage(actual)
return success, nil
}
func (m *configAuditReportMatcher) FailureMessage(_ interface{}) string {
// TODO Add more descriptive message rather than rely on composed matchers' defaults
return m.failureMessage
}
func (m *configAuditReportMatcher) NegatedFailureMessage(_ interface{}) string {
return m.negatedFailureMessage
}