Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(k8s): check all results for vulnerabilities #7946

Merged
merged 2 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pkg/k8s/report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,12 @@ func shouldAddToReport(scanners types.Scanners) bool {
}

func vulnerabilitiesOrSecretResource(resource Resource) bool {
return len(resource.Results) > 0 && (len(resource.Results[0].Vulnerabilities) > 0 || len(resource.Results[0].Secrets) > 0)
for _, result := range resource.Results {
if len(result.Vulnerabilities) > 0 || len(resource.Results[0].Secrets) > 0 {
return true
}
}
return false
}

func misconfigsResource(resource Resource) bool {
Expand Down
68 changes: 68 additions & 0 deletions pkg/k8s/report/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,58 @@ var (
},
},
}
deployOrionWithThirdVulns = Resource{
Namespace: "default",
Kind: "Deploy",
Name: "orion",
Metadata: []types.Metadata{
{
ImageID: "123",
RepoTags: []string{
"alpine:3.14",
},
RepoDigests: []string{
"alpine:3.14@sha256:8fe1727132b2506c17ba0e1f6a6ed8a016bb1f5735e43b2738cd3fd1979b6260",
},
},
},
Results: types.Results{
{},
{},
{
Vulnerabilities: []types.DetectedVulnerability{
{
VulnerabilityID: "CVE-2022-1111",
Vulnerability: dbTypes.Vulnerability{Severity: "LOW"},
},
{
VulnerabilityID: "CVE-2022-2222",
Vulnerability: dbTypes.Vulnerability{Severity: "MEDIUM"},
},
{
VulnerabilityID: "CVE-2022-3333",
Vulnerability: dbTypes.Vulnerability{Severity: "HIGH"},
},
{
VulnerabilityID: "CVE-2022-4444",
Vulnerability: dbTypes.Vulnerability{Severity: "CRITICAL"},
},
{
VulnerabilityID: "CVE-2022-5555",
Vulnerability: dbTypes.Vulnerability{Severity: "UNKNOWN"},
},
{
VulnerabilityID: "CVE-2022-6666",
Vulnerability: dbTypes.Vulnerability{Severity: "CRITICAL"},
},
{
VulnerabilityID: "CVE-2022-7777",
Vulnerability: dbTypes.Vulnerability{Severity: "MEDIUM"},
},
},
},
},
}

orionDeployWithAnotherMisconfig = Resource{
Namespace: "default",
Expand Down Expand Up @@ -492,6 +544,17 @@ func TestReport_consolidate(t *testing.T) {
"default/cronjob/hello": cronjobHelloWithVulns,
},
},
{
name: "report with vulnerabilities in the third result",
report: Report{
Resources: []Resource{
deployOrionWithThirdVulns,
},
},
expectedFindings: map[string]Resource{
"default/deploy/orion": deployOrionWithThirdVulns,
},
},
{
name: "report with misconfigs in image and pod",
report: Report{
Expand Down Expand Up @@ -521,6 +584,11 @@ func TestReport_consolidate(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
consolidateReport := tt.report.consolidate()

if len(consolidateReport.Findings) != len(tt.expectedFindings) {
t.Errorf("expected %d findings, got %d", len(tt.expectedFindings), len(consolidateReport.Findings))
}

for _, f := range consolidateReport.Findings {
key := f.fullname()

Expand Down