Skip to content

Commit c631031

Browse files
committed
fix: use FS with OS-specific path separator for loading docuemnts
Signed-off-by: nikpivkin <[email protected]>
1 parent aa47845 commit c631031

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

pkg/iac/rego/store.go

+21-1
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,41 @@ package rego
22

33
import (
44
"fmt"
5+
"io"
56
"io/fs"
67
"os"
78
"path/filepath"
89
"slices"
910
"strings"
1011

12+
"github.com/liamg/memoryfs"
1113
"github.com/open-policy-agent/opa/v1/loader"
1214
"github.com/open-policy-agent/opa/v1/storage"
1315
)
1416

1517
// initialize a store populated with OPA data files found in dataPaths
1618
func initStore(dataFS fs.FS, dataPaths, namespaces []string) (storage.Store, error) {
19+
fsys := memoryfs.New()
20+
// The virtual file system uses a slash ('/') as a path separator,
21+
// but OPA uses the filepath package, which is OS-dependent.
22+
// So we need to create a copy of the filesystem where the paths use the an OS-specific separator.
23+
if err := fs.WalkDir(dataFS, ".", func(path string, d fs.DirEntry, err error) error {
24+
if err != nil {
25+
return err
26+
}
27+
if d.IsDir() {
28+
return fsys.MkdirAll(path, d.Type().Perm())
29+
}
30+
return fsys.WriteLazyFile(filepath.FromSlash(path), func() (io.Reader, error) {
31+
return fsys.Open(path)
32+
}, d.Type().Perm())
33+
}); err != nil {
34+
return nil, fmt.Errorf("walk dir: %w", err)
35+
}
36+
1737
// FilteredPaths will recursively find all file paths that contain a valid document
1838
// extension from the given list of data paths.
19-
allDocumentPaths, _ := loader.FilteredPathsFS(dataFS, dataPaths,
39+
allDocumentPaths, _ := loader.FilteredPathsFS(fsys, dataPaths,
2040
func(abspath string, info os.FileInfo, depth int) bool {
2141
return !info.IsDir() && !isDataFile(info)
2242
},

0 commit comments

Comments
 (0)