Skip to content

Commit a74048b

Browse files
committed
fixup! test(e2e): Use trivy-operator for CIS benchmark
1 parent 1988893 commit a74048b

File tree

4 files changed

+116
-19
lines changed

4 files changed

+116
-19
lines changed

.github/workflows/e2e.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ jobs:
105105
RUN_CIS_BENCHMARK: ${{ inputs.run-cis-benchmark }}
106106

107107
- name: Add job summary for CIS benchmark
108-
if: inputs.run-cis-benchmark
108+
if: failure() && inputs.run-cis-benchmark
109109
run: |
110110
echo "## CIS Benchmark" >>"${GITHUB_STEP_SUMMARY}"
111-
cat test/e2e/cis-benchmark-report.txt >>"${GITHUB_STEP_SUMMARY}"
111+
cat test/e2e/cis-benchmark-report.md >>"${GITHUB_STEP_SUMMARY}"
112112
113113
- if: success() || failure() # always run even if the previous step fails
114114
name: Publish e2e test report

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,4 @@ test/e2e/config/caren-envsubst.yaml
5050
hack/tools/fetch-images/fetch-images
5151
caren-images.txt
5252
hack/examples/release/*-cluster-class.yaml
53+
test/e2e/cis-benchmark-report.txt

test/e2e/quick_start_test.go

Lines changed: 105 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Copyright 2025 Nutanix. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
compliance:
5+
specs: ["k8s-cis-1.23"]
6+
reportType: all
7+
failEntriesLimit: 1000
8+
cron: '* * * * *'

0 commit comments

Comments
 (0)