-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
store.go
48 lines (42 loc) · 1.27 KB
/
store.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package rego
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/open-policy-agent/opa/loader"
"github.com/open-policy-agent/opa/storage"
)
// initialize a store populated with OPA data files found in dataPaths
func initStore(dataFS fs.FS, dataPaths, namespaces []string) (storage.Store, error) {
// FilteredPaths will recursively find all file paths that contain a valid document
// extension from the given list of data paths.
allDocumentPaths, _ := loader.FilteredPathsFS(dataFS, dataPaths, func(abspath string, info os.FileInfo, depth int) bool {
if info.IsDir() {
return false // filter in, include
}
ext := strings.ToLower(filepath.Ext(info.Name()))
for _, filter := range []string{
".yaml",
".yml",
".json",
} {
if filter == ext {
return false // filter in, include
}
}
return true // filter out, exclude
})
documents, err := loader.NewFileLoader().WithFS(dataFS).All(allDocumentPaths)
if err != nil {
return nil, fmt.Errorf("load documents: %w", err)
}
// pass all namespaces so that rego rule can refer to namespaces as data.namespaces
documents.Documents["namespaces"] = namespaces
store, err := documents.Store()
if err != nil {
return nil, fmt.Errorf("get documents store: %w", err)
}
return store, nil
}