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