@@ -9,20 +9,24 @@ import (
99 "fmt"
1010 "os"
1111 "os/exec"
12- "path/filepath"
1312 "slices"
1413 "strconv"
1514 "strings"
15+ "time"
1616
1717 . "github.com/onsi/ginkgo/v2"
1818 . "github.com/onsi/gomega"
1919 "github.com/samber/lo"
20+ "gopkg.in/yaml.v2"
21+ "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
22+ "k8s.io/apimachinery/pkg/runtime/schema"
2023 "k8s.io/utils/ptr"
2124 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
2225 capie2e "sigs.k8s.io/cluster-api/test/e2e"
2326 capiframework "sigs.k8s.io/cluster-api/test/framework"
2427 "sigs.k8s.io/cluster-api/test/framework/clusterctl"
2528 "sigs.k8s.io/cluster-api/util"
29+ ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
2630
2731 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
2832 apivariables "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/variables"
@@ -313,29 +317,113 @@ var _ = Describe("Quick start", func() {
313317 if os .Getenv ("RUN_CIS_BENCHMARK" ) == "true" {
314318 By ("Running CIS benchmark against workload cluster" )
315319
316- trivyCmd := exec .Command ( //nolint:gosec // Only used for testing so safe here.
317- "trivy" ,
318- "k8s" ,
319- "--compliance=k8s-cis-1.23" ,
320- "--disable-node-collector" ,
321- "--report=summary" ,
322- fmt .Sprintf (
323- "--output=%s" ,
324- filepath .Join (
325- os .Getenv ("GIT_REPO_ROOT" ),
326- "cis-benchmark-report.txt" ,
327- ),
328- ),
320+ trivyInstallCmd := exec .Command ( //nolint:gosec // Only used for testing so safe here.
321+ "helm" ,
322+ "upgrade" ,
323+ "--install" ,
324+ "trivy-operator" ,
325+ "oci://ghcr.io/aquasecurity/helm-charts/trivy-operator" ,
326+ "--namespace=trivy-system" ,
327+ "--create-namespace" ,
328+ "--wait" ,
329+ "--wait-for-jobs" ,
330+ "--values=testdata/trivy-operator-values.yaml" ,
329331 fmt .Sprintf (
330332 "--kubeconfig=%s" ,
331333 workloadProxy .GetKubeconfigPath (),
332334 ),
333335 )
336+ trivyInstallCmd .Stdout = GinkgoWriter
337+ trivyInstallCmd .Stderr = GinkgoWriter
338+ Expect (
339+ trivyInstallCmd .Run (),
340+ ).To (Succeed (), "trivy operator installation failed" )
341+
342+ complianceReport := unstructured.Unstructured {}
343+ complianceReport .SetGroupVersionKind (
344+ schema.GroupVersionKind {
345+ Group : "aquasecurity.github.io" ,
346+ Version : "v1alpha1" ,
347+ Kind : "ClusterComplianceReport" ,
348+ },
349+ )
350+ complianceReport .SetName ("k8s-cis-1.23" )
351+
352+ var (
353+ passed , failed int64
354+ err error
355+ )
356+ Eventually (func () (bool , error ) {
357+ Expect (
358+ workloadClient .Get (
359+ ctx ,
360+ ctrlclient .ObjectKeyFromObject (& complianceReport ),
361+ & complianceReport ,
362+ ),
363+ ).To (Succeed (), "failed to get compliance report" )
364+
365+ passed , _ , err = unstructured .NestedInt64 (
366+ complianceReport .Object ,
367+ "status" ,
368+ "summary" ,
369+ "passCount" ,
370+ )
371+ if err != nil {
372+ return false , fmt .Errorf (
373+ "failed to get compliance report status: %w" ,
374+ err ,
375+ )
376+ }
377+ failed , _ , err = unstructured .NestedInt64 (
378+ complianceReport .Object ,
379+ "status" ,
380+ "summary" ,
381+ "failCount" ,
382+ )
383+ if err != nil {
384+ return false , fmt .Errorf (
385+ "failed to get compliance report status: %w" ,
386+ err ,
387+ )
388+ }
389+
390+ return passed + failed > 0 , nil
391+ }).WithTimeout (5 * time .Minute ).
392+ WithPolling (time .Second ).
393+ Should (BeTrue (), "failed to wait for compliance report" )
394+
395+ complianceStatus , _ , err := unstructured .NestedMap (
396+ complianceReport .Object ,
397+ "status" ,
398+ )
399+ Expect (err ).ToNot (HaveOccurred (), "failed to get compliance status" )
400+
401+ complianceReportFile , err := os .Create ("cis-benchmark-report.md" )
402+ Expect (
403+ err ,
404+ ).ToNot (HaveOccurred (), "failed to create compliance report file" )
405+ defer complianceReportFile .Close ()
406+
407+ _ , err = complianceReportFile .WriteString (
408+ "# CIS Benchmark Report\n \n ```yaml\n " ,
409+ )
410+ Expect (
411+ err ,
412+ ).NotTo (HaveOccurred (), "failed to write compliance report header" )
334413
335- trivyCmd .Stdout = GinkgoWriter
336- trivyCmd .Stderr = GinkgoWriter
414+ complianceReportEncoder := yaml .NewEncoder (complianceReportFile )
415+ Expect (
416+ complianceReportEncoder .Encode (complianceStatus ),
417+ ).To (Succeed (), "failed to write compliance report" )
418+ Expect (
419+ complianceReportEncoder .Close (),
420+ ).To (Succeed (), "failed to close compliance report encoder" )
421+ _ , err = complianceReportFile .WriteString ("\n ```\n " )
422+ Expect (
423+ err ,
424+ ).NotTo (HaveOccurred (), "failed to write compliance report footer" )
337425
338- Expect (trivyCmd . Run ()) .To (Succeed (), "CIS benchmark failed" )
426+ Expect (failed ) .To (BeZero (), "CIS benchmark failed with errors " )
339427 }
340428 },
341429 }
0 commit comments