|
| 1 | +package trivy_operator |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + corev1 "k8s.io/api/core/v1" |
| 9 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 10 | + "k8s.io/apimachinery/pkg/labels" |
| 11 | + "k8s.io/apimachinery/pkg/types" |
| 12 | + crclient "sigs.k8s.io/controller-runtime/pkg/client" |
| 13 | + |
| 14 | + v1alpha1 "github.com/aquasecurity/trivy-operator/pkg/apis/aquasecurity/v1alpha1" |
| 15 | + "github.com/aquasecurity/trivy-operator/pkg/operator/etc" |
| 16 | + "github.com/aquasecurity/trivy-operator/pkg/trivyoperator" |
| 17 | + "github.com/aquasecurity/trivy-operator/tests/itest/helper" |
| 18 | + |
| 19 | + . "github.com/onsi/ginkgo/v2" |
| 20 | + . "github.com/onsi/gomega" |
| 21 | +) |
| 22 | + |
| 23 | +var _ = Describe("Trivy ignoreFile integration", func() { |
| 24 | + var ctx context.Context |
| 25 | + |
| 26 | + BeforeEach(func() { |
| 27 | + ctx = context.Background() |
| 28 | + }) |
| 29 | + |
| 30 | + It("hides CVE when set in .trivyignore", func() { |
| 31 | + const IgnoredCVE = "CVE-2018-14618" |
| 32 | + |
| 33 | + // Patch trivy-operator-trivy-config ConfigMap to include CVEToIgnore in trivy.ignoreFile |
| 34 | + operatorConfig, err := etc.GetOperatorConfig() |
| 35 | + Expect(err).ToNot(HaveOccurred()) |
| 36 | + operatorNamespace, err := operatorConfig.GetOperatorNamespace() |
| 37 | + Expect(err).ToNot(HaveOccurred()) |
| 38 | + |
| 39 | + cm := &corev1.ConfigMap{} |
| 40 | + Expect(kubeClient.Get(ctx, clientObjectKey(operatorNamespace, trivyoperator.TrivyConfigMapName), cm)).To(Succeed()) |
| 41 | + |
| 42 | + cm.Data["trivy.ignoreFile"] = fmt.Sprintf("%s\n", IgnoredCVE) |
| 43 | + |
| 44 | + By("Updating trivy.ignoreFile in ConfigMap") |
| 45 | + Expect(kubeClient.Update(ctx, cm)).To(Succeed()) |
| 46 | + |
| 47 | + // Brief wait to reduce races where a scan could start before CM is observed by the controller |
| 48 | + time.Sleep(5 * time.Second) |
| 49 | + |
| 50 | + // Create an unmanaged Pod using kube-bench image and assert the ignored CVE is not reported |
| 51 | + pod := helper.NewPod(). |
| 52 | + WithRandomName("trivyignore-kubebench"). |
| 53 | + WithNamespace(inputs.PrimaryNamespace). |
| 54 | + WithContainer("kube-bench", "mirror.gcr.io/knqyf263/vuln-image:1.2.3", []string{"/bin/sh", "-c", "--"}, []string{"while true; do sleep 30; done;"}). |
| 55 | + Build() |
| 56 | + |
| 57 | + By("Creating kube-bench Pod") |
| 58 | + Expect(inputs.Create(ctx, pod)).To(Succeed()) |
| 59 | + DeferCleanup(func() { |
| 60 | + _ = inputs.Delete(ctx, pod) |
| 61 | + }) |
| 62 | + |
| 63 | + By("Waiting for VulnerabilityReport") |
| 64 | + Eventually(inputs.HasVulnerabilityReportOwnedBy(ctx, pod), inputs.AssertTimeout, inputs.PollingInterval).Should(BeTrue()) |
| 65 | + |
| 66 | + vrList := &v1alpha1.VulnerabilityReportList{} |
| 67 | + Expect(kubeClient.List(ctx, vrList, clientListOptionsForOwner(pod.ObjectMeta, "Pod"))).To(Succeed()) |
| 68 | + Expect(vrList.Items).ToNot(BeEmpty(), "expected at least one VulnerabilityReport for the pod") |
| 69 | + |
| 70 | + // The report must not include the ignored CVE ID |
| 71 | + for _, vr := range vrList.Items { |
| 72 | + for _, v := range vr.Report.Vulnerabilities { |
| 73 | + Expect(v.VulnerabilityID).ToNot(Equal(IgnoredCVE), fmt.Sprintf("unexpectedly found ignored CVE %s", IgnoredCVE)) |
| 74 | + } |
| 75 | + } |
| 76 | + }) |
| 77 | +}) |
| 78 | + |
| 79 | +// clientListOptionsForOwner builds ListOptions that match a VulnerabilityReport owned by the given object. |
| 80 | +func clientListOptionsForOwner(obj metav1.ObjectMeta, kind string) crclient.ListOption { |
| 81 | + sel := labels.Set{ |
| 82 | + trivyoperator.LabelResourceNamespace: obj.Namespace, |
| 83 | + trivyoperator.LabelResourceKind: kind, |
| 84 | + trivyoperator.LabelResourceName: obj.Name, |
| 85 | + } |
| 86 | + return crclient.MatchingLabels(sel) |
| 87 | +} |
| 88 | + |
| 89 | +// clientObjectKey returns a NamespacedName tuple for use with client.Get. |
| 90 | +func clientObjectKey(namespace, name string) types.NamespacedName { |
| 91 | + return types.NamespacedName{Namespace: namespace, Name: name} |
| 92 | +} |
0 commit comments