Skip to content

Commit aa47845

Browse files
committed
fix(misconf): do not skip loading documents from subdirectories
Signed-off-by: nikpivkin <[email protected]>
1 parent 124e161 commit aa47845

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

pkg/iac/rego/store.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func initStore(dataFS fs.FS, dataPaths, namespaces []string) (storage.Store, err
1818
// extension from the given list of data paths.
1919
allDocumentPaths, _ := loader.FilteredPathsFS(dataFS, dataPaths,
2020
func(abspath string, info os.FileInfo, depth int) bool {
21-
return !isDataFile(info)
21+
return !info.IsDir() && !isDataFile(info)
2222
},
2323
)
2424

@@ -38,7 +38,7 @@ func initStore(dataFS fs.FS, dataPaths, namespaces []string) (storage.Store, err
3838
}
3939

4040
func isDataFile(fi fs.FileInfo) bool {
41-
return !fi.IsDir() && slices.Contains([]string{
41+
return slices.Contains([]string{
4242
".yaml",
4343
".yml",
4444
".json",

pkg/iac/rego/store_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package rego
2+
3+
import (
4+
"encoding/json"
5+
"testing"
6+
"testing/fstest"
7+
8+
"github.com/open-policy-agent/opa/v1/storage"
9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
11+
)
12+
13+
func TestInitStore(t *testing.T) {
14+
fsys := fstest.MapFS{
15+
"test1.yml": &fstest.MapFile{Data: []byte("foo: 1")},
16+
"test2.yaml": &fstest.MapFile{Data: []byte("bar: 2")},
17+
"test3.json": &fstest.MapFile{Data: []byte(`{"baz": 3}`)},
18+
"dir/test4.yaml": &fstest.MapFile{Data: []byte("qux: 4")},
19+
}
20+
store, err := initStore(fsys, []string{"."}, []string{"builtin.aws.test", "user.test"})
21+
require.NoError(t, err)
22+
23+
tx, err := store.NewTransaction(t.Context())
24+
require.NoError(t, err)
25+
doc, err := store.Read(t.Context(), tx, storage.MustParsePath("/"))
26+
require.NoError(t, err)
27+
28+
expected := map[string]any{
29+
"foo": json.Number("1"),
30+
"bar": json.Number("2"),
31+
"baz": json.Number("3"),
32+
"qux": json.Number("4"),
33+
"namespaces": []any{"builtin.aws.test", "user.test"},
34+
}
35+
assert.Equal(t, expected, doc)
36+
}

0 commit comments

Comments
 (0)