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

feat(misconf): support for ignoring by inline comments for Helm #8138

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
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
Loading