Skip to content

Commit 797b36f

Browse files
authoredNov 20, 2024··
fix(k8s): check all results for vulnerabilities (#7946)
1 parent 516e7cb commit 797b36f

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed
 

‎pkg/k8s/report/report.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,12 @@ func shouldAddToReport(scanners types.Scanners) bool {
280280
}
281281

282282
func vulnerabilitiesOrSecretResource(resource Resource) bool {
283-
return len(resource.Results) > 0 && (len(resource.Results[0].Vulnerabilities) > 0 || len(resource.Results[0].Secrets) > 0)
283+
for _, result := range resource.Results {
284+
if len(result.Vulnerabilities) > 0 || len(resource.Results[0].Secrets) > 0 {
285+
return true
286+
}
287+
}
288+
return false
284289
}
285290

286291
func misconfigsResource(resource Resource) bool {

‎pkg/k8s/report/report_test.go

+68
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,58 @@ var (
118118
},
119119
},
120120
}
121+
deployOrionWithThirdVulns = Resource{
122+
Namespace: "default",
123+
Kind: "Deploy",
124+
Name: "orion",
125+
Metadata: []types.Metadata{
126+
{
127+
ImageID: "123",
128+
RepoTags: []string{
129+
"alpine:3.14",
130+
},
131+
RepoDigests: []string{
132+
"alpine:3.14@sha256:8fe1727132b2506c17ba0e1f6a6ed8a016bb1f5735e43b2738cd3fd1979b6260",
133+
},
134+
},
135+
},
136+
Results: types.Results{
137+
{},
138+
{},
139+
{
140+
Vulnerabilities: []types.DetectedVulnerability{
141+
{
142+
VulnerabilityID: "CVE-2022-1111",
143+
Vulnerability: dbTypes.Vulnerability{Severity: "LOW"},
144+
},
145+
{
146+
VulnerabilityID: "CVE-2022-2222",
147+
Vulnerability: dbTypes.Vulnerability{Severity: "MEDIUM"},
148+
},
149+
{
150+
VulnerabilityID: "CVE-2022-3333",
151+
Vulnerability: dbTypes.Vulnerability{Severity: "HIGH"},
152+
},
153+
{
154+
VulnerabilityID: "CVE-2022-4444",
155+
Vulnerability: dbTypes.Vulnerability{Severity: "CRITICAL"},
156+
},
157+
{
158+
VulnerabilityID: "CVE-2022-5555",
159+
Vulnerability: dbTypes.Vulnerability{Severity: "UNKNOWN"},
160+
},
161+
{
162+
VulnerabilityID: "CVE-2022-6666",
163+
Vulnerability: dbTypes.Vulnerability{Severity: "CRITICAL"},
164+
},
165+
{
166+
VulnerabilityID: "CVE-2022-7777",
167+
Vulnerability: dbTypes.Vulnerability{Severity: "MEDIUM"},
168+
},
169+
},
170+
},
171+
},
172+
}
121173

122174
orionDeployWithAnotherMisconfig = Resource{
123175
Namespace: "default",
@@ -492,6 +544,17 @@ func TestReport_consolidate(t *testing.T) {
492544
"default/cronjob/hello": cronjobHelloWithVulns,
493545
},
494546
},
547+
{
548+
name: "report with vulnerabilities in the third result",
549+
report: Report{
550+
Resources: []Resource{
551+
deployOrionWithThirdVulns,
552+
},
553+
},
554+
expectedFindings: map[string]Resource{
555+
"default/deploy/orion": deployOrionWithThirdVulns,
556+
},
557+
},
495558
{
496559
name: "report with misconfigs in image and pod",
497560
report: Report{
@@ -521,6 +584,11 @@ func TestReport_consolidate(t *testing.T) {
521584
for _, tt := range tests {
522585
t.Run(tt.name, func(t *testing.T) {
523586
consolidateReport := tt.report.consolidate()
587+
588+
if len(consolidateReport.Findings) != len(tt.expectedFindings) {
589+
t.Errorf("expected %d findings, got %d", len(tt.expectedFindings), len(consolidateReport.Findings))
590+
}
591+
524592
for _, f := range consolidateReport.Findings {
525593
key := f.fullname()
526594

0 commit comments

Comments
 (0)
Please sign in to comment.