@@ -13,6 +13,7 @@ import (
13
13
"github.com/open-policy-agent/opa/rego"
14
14
"github.com/open-policy-agent/opa/storage"
15
15
"github.com/open-policy-agent/opa/util"
16
+ "github.com/samber/lo"
16
17
17
18
"github.com/aquasecurity/trivy/pkg/iac/framework"
18
19
"github.com/aquasecurity/trivy/pkg/iac/providers"
@@ -145,6 +146,9 @@ type Input struct {
145
146
Path string `json:"path"`
146
147
FS fs.FS `json:"-"`
147
148
Contents any `json:"contents"`
149
+
150
+ // parsed is the parsed input value for the rego query
151
+ parsed ast.Value
148
152
}
149
153
150
154
func GetInputsContents (inputs []Input ) []any {
@@ -159,6 +163,21 @@ func (s *Scanner) ScanInput(ctx context.Context, sourceType types.Source, inputs
159
163
160
164
s .logger .Debug ("Scanning inputs" , "count" , len (inputs ))
161
165
166
+ if len (inputs ) == 0 {
167
+ return nil , nil
168
+ }
169
+
170
+ inputs = lo .FilterMap (inputs , func (input Input , _ int ) (Input , bool ) {
171
+ s .trace ("INPUT" , input )
172
+ parsed , err := parseRawInput (input .Contents )
173
+ if err != nil {
174
+ s .logger .Error ("Failed to parse input" , log .FilePath (input .Path ), log .Err (err ))
175
+ return input , false
176
+ }
177
+ input .parsed = parsed
178
+ return input , true
179
+ })
180
+
162
181
var results scan.Results
163
182
164
183
for _ , module := range s .policies {
@@ -194,10 +213,6 @@ func (s *Scanner) ScanInput(ctx context.Context, sourceType types.Source, inputs
194
213
continue
195
214
}
196
215
197
- if len (inputs ) == 0 {
198
- continue
199
- }
200
-
201
216
usedRules := set .New [string ]()
202
217
203
218
// all rules
@@ -302,14 +317,8 @@ func (s *Scanner) applyRule(ctx context.Context, namespace, rule string, inputs
302
317
var results scan.Results
303
318
qualified := fmt .Sprintf ("data.%s.%s" , namespace , rule )
304
319
for _ , input := range inputs {
305
- s .trace ("INPUT" , input )
306
- parsedInput , err := parseRawInput (input .Contents )
307
- if err != nil {
308
- s .logger .Error ("Error occurred while parsing input" , log .Err (err ))
309
- continue
310
- }
311
320
312
- resultSet , traces , err := s .runQuery (ctx , qualified , parsedInput , false )
321
+ resultSet , traces , err := s .runQuery (ctx , qualified , input . parsed , false )
313
322
if err != nil {
314
323
return nil , err
315
324
}
0 commit comments