@@ -3,26 +3,43 @@ package rego
3
3
import (
4
4
"fmt"
5
5
"io/fs"
6
- "os"
7
- "path/filepath"
6
+ "path"
8
7
"slices"
9
8
"strings"
10
9
11
10
"github.com/open-policy-agent/opa/v1/loader"
12
11
"github.com/open-policy-agent/opa/v1/storage"
12
+
13
+ "github.com/aquasecurity/trivy/pkg/log"
14
+ "github.com/aquasecurity/trivy/pkg/set"
13
15
)
14
16
15
17
// initialize a store populated with OPA data files found in dataPaths
16
18
func initStore (dataFS fs.FS , dataPaths , namespaces []string ) (storage.Store , error ) {
17
- // FilteredPaths will recursively find all file paths that contain a valid document
18
- // extension from the given list of data paths.
19
- allDocumentPaths , _ := loader .FilteredPathsFS (dataFS , dataPaths ,
20
- func (abspath string , info os.FileInfo , depth int ) bool {
21
- return ! isDataFile (info )
22
- },
23
- )
24
-
25
- documents , err := loader .NewFileLoader ().WithFS (dataFS ).All (allDocumentPaths )
19
+ dataFiles := set .New [string ]()
20
+
21
+ // The virtual file system uses a slash ('/') as a path separator,
22
+ // but OPA uses the filepath package, which is OS-dependent.
23
+ // Therefore, we need to collect all the paths ourselves and pass them to OPA.
24
+ for _ , root := range dataPaths {
25
+ if err := fs .WalkDir (dataFS , root , func (path string , d fs.DirEntry , err error ) error {
26
+ if err != nil {
27
+ return err
28
+ }
29
+ if d .IsDir () {
30
+ return nil
31
+ }
32
+
33
+ if isDataFile (path ) {
34
+ dataFiles .Append (path )
35
+ }
36
+ return nil
37
+ }); err != nil {
38
+ log .Error ("Failed to collect data file paths" , log .String ("root" , root ), log .Err (err ))
39
+ }
40
+ }
41
+
42
+ documents , err := loader .NewFileLoader ().WithFS (dataFS ).All (dataFiles .Items ())
26
43
if err != nil {
27
44
return nil , fmt .Errorf ("load documents: %w" , err )
28
45
}
@@ -37,10 +54,10 @@ func initStore(dataFS fs.FS, dataPaths, namespaces []string) (storage.Store, err
37
54
return store , nil
38
55
}
39
56
40
- func isDataFile (fi fs. FileInfo ) bool {
41
- return ! fi . IsDir () && slices .Contains ([]string {
57
+ func isDataFile (filePath string ) bool {
58
+ return slices .Contains ([]string {
42
59
".yaml" ,
43
60
".yml" ,
44
61
".json" ,
45
- }, strings .ToLower (filepath .Ext (fi . Name () )))
62
+ }, strings .ToLower (path .Ext (filePath )))
46
63
}
0 commit comments