Skip to content

Commit

Permalink
fix(k8s): skip resources without misconfigs (#7797)
Browse files Browse the repository at this point in the history
  • Loading branch information
afdesk authored Oct 31, 2024
1 parent f2bb9c6 commit 7882776
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pkg/k8s/report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,13 @@ func (r Report) consolidate() ConsolidatedReport {
for _, m := range r.Resources {
if vulnerabilitiesOrSecretResource(m) {
vulnerabilities = append(vulnerabilities, m)
} else {
}
if misconfigsResource(m) {
res, ok := index[m.fullname()]
index[m.fullname()] = m
if ok {
index[m.fullname()].Results[0].Misconfigurations = append(index[m.fullname()].Results[0].Misconfigurations, res.Results[0].Misconfigurations...)
}
}
}

Expand Down Expand Up @@ -278,6 +283,10 @@ func vulnerabilitiesOrSecretResource(resource Resource) bool {
return len(resource.Results) > 0 && (len(resource.Results[0].Vulnerabilities) > 0 || len(resource.Results[0].Secrets) > 0)
}

func misconfigsResource(resource Resource) bool {
return len(resource.Results) > 0 && len(resource.Results[0].Misconfigurations) > 0
}

func nodeKind(resource Resource) Resource {
if nodeInfoResource(resource) {
resource.Kind = "Node"
Expand Down
33 changes: 33 additions & 0 deletions pkg/k8s/report/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,23 @@ var (
},
}

orionDeployWithAnotherMisconfig = Resource{
Namespace: "default",
Kind: "Deploy",
Name: "orion",
Results: types.Results{
{
Misconfigurations: []types.DetectedMisconfiguration{
{
ID: "ID201",
Status: types.MisconfStatusFailure,
Severity: "HIGH",
},
},
},
},
}

image1WithVulns = Resource{
Namespace: "default",
Kind: "Pod",
Expand Down Expand Up @@ -424,6 +441,10 @@ var (
)

func TestReport_consolidate(t *testing.T) {
concatenatedResource := orionDeployWithAnotherMisconfig
concatenatedResource.Results[0].Misconfigurations = append(concatenatedResource.Results[0].Misconfigurations,
deployOrionWithMisconfigs.Results[0].Misconfigurations...)

tests := []struct {
name string
report Report
Expand Down Expand Up @@ -471,6 +492,18 @@ func TestReport_consolidate(t *testing.T) {
"default/cronjob/hello": cronjobHelloWithVulns,
},
},
{
name: "report with misconfigs in image and pod",
report: Report{
Resources: []Resource{
deployOrionWithMisconfigs,
orionDeployWithAnotherMisconfig,
},
},
expectedFindings: map[string]Resource{
"default/deploy/orion": concatenatedResource,
},
},
{
name: "report with multi image pod containing vulnerabilities",
report: Report{
Expand Down

0 comments on commit 7882776

Please sign in to comment.