Skip to content

Commit aa822c2

Browse files
authored
refactor(misconf): improve error handling in the Rego scanner (aquasecurity#6527)
1 parent 30cc88f commit aa822c2

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

pkg/iac/rego/scanner.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,10 @@ func (s *Scanner) ScanInput(ctx context.Context, inputs ...Input) (scan.Results,
241241

242242
staticMeta, err := s.retriever.RetrieveMetadata(ctx, module, GetInputsContents(inputs)...)
243243
if err != nil {
244-
return nil, err
244+
s.debug.Log(
245+
"Error occurred while retrieving metadata from check %q: %s",
246+
module.Package.Location.File, err)
247+
continue
245248
}
246249

247250
if isPolicyWithSubtype(s.sourceType) {
@@ -267,7 +270,10 @@ func (s *Scanner) ScanInput(ctx context.Context, inputs ...Input) (scan.Results,
267270
if isEnforcedRule(ruleName) {
268271
ruleResults, err := s.applyRule(ctx, namespace, ruleName, inputs, staticMeta.InputOptions.Combined)
269272
if err != nil {
270-
return nil, err
273+
s.debug.Log(
274+
"Error occurred while applying rule %q from check %q: %s",
275+
ruleName, module.Package.Location.File, err)
276+
continue
271277
}
272278
results = append(results, s.embellishResultsWithRuleMetadata(ruleResults, *staticMeta)...)
273279
}

pkg/iac/rego/scanner_test.go

+35
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"path/filepath"
99
"strings"
1010
"testing"
11+
"testing/fstest"
1112

1213
"github.com/aquasecurity/trivy/pkg/iac/severity"
1314
"github.com/aquasecurity/trivy/pkg/iac/types"
@@ -976,3 +977,37 @@ deny {
976977
assert.Equal(t, 0, len(results.GetPassed()))
977978
assert.Equal(t, 0, len(results.GetIgnored()))
978979
}
980+
981+
func Test_NoErrorsWhenUsingBadRegoCheck(t *testing.T) {
982+
983+
// this check cause eval_conflict_error
984+
// https://www.openpolicyagent.org/docs/latest/policy-language/#functions
985+
fsys := fstest.MapFS{
986+
"checks/bad.rego": {
987+
Data: []byte(`package defsec.test
988+
989+
p(x) = y {
990+
y := x[_]
991+
}
992+
993+
deny {
994+
p([1, 2, 3])
995+
}
996+
`),
997+
},
998+
}
999+
1000+
var buf bytes.Buffer
1001+
scanner := NewScanner(
1002+
types.SourceYAML,
1003+
options.ScannerWithDebug(&buf),
1004+
)
1005+
require.NoError(
1006+
t,
1007+
scanner.LoadPolicies(false, false, fsys, []string{"checks"}, nil),
1008+
)
1009+
_, err := scanner.ScanInput(context.TODO(), Input{})
1010+
assert.NoError(t, err)
1011+
assert.Contains(t, buf.String(),
1012+
`Error occurred while applying rule "deny" from check "checks/bad.rego"`)
1013+
}

0 commit comments

Comments
 (0)