Skip to content

Commit

Permalink
feat(misconf): support for ignoring by inline comments for Helm
Browse files Browse the repository at this point in the history
Signed-off-by: nikpivkin <[email protected]>
  • Loading branch information
nikpivkin committed Dec 19, 2024
1 parent c4a4a5f commit db64570
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 21 deletions.
3 changes: 3 additions & 0 deletions pkg/iac/scanners/helm/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/liamg/memoryfs"

"github.com/aquasecurity/trivy/pkg/iac/detection"
"github.com/aquasecurity/trivy/pkg/iac/ignore"
"github.com/aquasecurity/trivy/pkg/iac/rego"
"github.com/aquasecurity/trivy/pkg/iac/scan"
"github.com/aquasecurity/trivy/pkg/iac/scanners"
Expand Down Expand Up @@ -125,6 +126,7 @@ func (s *Scanner) getScanResults(path string, ctx context.Context, target fs.FS)
file := file
s.logger.Debug("Processing rendered chart file", log.FilePath(file.TemplateFilePath))

ignoreRules := ignore.Parse(file.ManifestContent, file.TemplateFilePath, "")
manifests, err := kparser.Parse(ctx, strings.NewReader(file.ManifestContent), file.TemplateFilePath)
if err != nil {
return nil, fmt.Errorf("unmarshal yaml: %w", err)
Expand All @@ -150,6 +152,7 @@ func (s *Scanner) getScanResults(path string, ctx context.Context, target fs.FS)
return nil, err
}
fileResults.SetSourceAndFilesystem(helmParser.ChartSource, renderedFS, detection.IsArchive(helmParser.ChartSource))
fileResults.Ignore(ignoreRules, nil)
}

results = append(results, fileResults...)
Expand Down
36 changes: 15 additions & 21 deletions pkg/iac/scanners/helm/test/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"
"testing"

"github.com/samber/lo"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -136,28 +137,28 @@ func Test_helm_scanner_with_dir(t *testing.T) {
require.NotNil(t, results)

failed := results.GetFailed()
assert.Len(t, failed, 14)
assert.Len(t, failed, 13)

visited := make(map[string]bool)
var errorCodes []string
for _, result := range failed {
id := result.Flatten().RuleID
if _, exists := visited[id]; !exists {
visited[id] = true
errorCodes = append(errorCodes, id)
}
visited[result.Rule().AVDID] = true
}
errorCodes := lo.Keys(visited)

sort.Strings(errorCodes)

assert.Equal(t, []string{
assert.ElementsMatch(t, []string{
"AVD-KSV-0001", "AVD-KSV-0003",
"AVD-KSV-0011", "AVD-KSV-0012", "AVD-KSV-0014",
"AVD-KSV-0015", "AVD-KSV-0016", "AVD-KSV-0018",
"AVD-KSV-0015", "AVD-KSV-0016",
"AVD-KSV-0020", "AVD-KSV-0021", "AVD-KSV-0030",
"AVD-KSV-0104", "AVD-KSV-0106",
"AVD-KSV-0117",
}, errorCodes)

ignored := results.GetIgnored()
assert.Len(t, ignored, 1)

assert.Equal(t, "AVD-KSV-0018", ignored[0].Rule().AVDID)
assert.Equal(t, "templates/deployment.yaml", ignored[0].Metadata().Range().GetFilename())
}
}

Expand Down Expand Up @@ -231,19 +232,12 @@ deny[res] {
assert.Len(t, failed, 15)

visited := make(map[string]bool)
var errorCodes []string
for _, result := range failed {
id := result.Flatten().RuleID
if _, exists := visited[id]; !exists {
visited[id] = true
errorCodes = append(errorCodes, id)
}
visited[result.Rule().AVDID] = true
}
assert.Len(t, errorCodes, 14)

sort.Strings(errorCodes)
errorCodes := lo.Keys(visited)

assert.Equal(t, []string{
assert.ElementsMatch(t, []string{
"AVD-KSV-0001", "AVD-KSV-0003",
"AVD-KSV-0011", "AVD-KSV-0012", "AVD-KSV-0014",
"AVD-KSV-0015", "AVD-KSV-0016", "AVD-KSV-0018",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ spec:
securityContext:
{}
containers:
# trivy:ignore:KSV018
- name: testchart
securityContext:
runAsUser: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ spec:
securityContext:
{}
containers:
# trivy:ignore:KSV018
- name: testchart
securityContext:
{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ spec:
securityContext:
{{- toYaml .Values.podSecurityContext | nindent 8 }}
containers:
# trivy:ignore:KSV018
- name: {{ .Chart.Name }}
securityContext:
{{- toYaml .Values.securityContext | nindent 12 }}
Expand Down

0 comments on commit db64570

Please sign in to comment.