forked from aquasecurity/tfsec
/
load.go
160 lines (144 loc) · 3.96 KB
/
load.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package rego
import (
"fmt"
"io"
"io/fs"
"io/ioutil"
"path/filepath"
"strings"
"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/bundle"
)
func isRegoFile(name string) bool {
return strings.HasSuffix(name, bundle.RegoExt) && !strings.HasSuffix(name, "_test"+bundle.RegoExt)
}
func (s *Scanner) loadPoliciesFromDirs(target fs.FS, paths []string) (map[string]*ast.Module, error) {
modules := make(map[string]*ast.Module)
for _, path := range paths {
if err := fs.WalkDir(target, filepath.ToSlash(path), func(path string, info fs.DirEntry, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if !isRegoFile(info.Name()) {
return nil
}
data, err := fs.ReadFile(target, filepath.ToSlash(path))
if err != nil {
return err
}
module, err := ast.ParseModuleWithOpts(path, string(data), ast.ParserOptions{})
if err != nil {
return err
}
modules[path] = module
return nil
}); err != nil {
return nil, err
}
}
return modules, nil
}
func (s *Scanner) loadPoliciesFromReaders(readers []io.Reader) (map[string]*ast.Module, error) {
modules := make(map[string]*ast.Module)
for i, r := range readers {
moduleName := fmt.Sprintf("reader_%d", i)
data, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
module, err := ast.ParseModuleWithOpts(moduleName, string(data), ast.ParserOptions{})
if err != nil {
return nil, err
}
modules[moduleName] = module
}
return modules, nil
}
func (s *Scanner) LoadEmbeddedLibraries() error {
if s.policies == nil {
s.policies = make(map[string]*ast.Module)
}
loadedLibs, err := loadEmbeddedLibraries()
if err != nil {
return fmt.Errorf("failed to load embedded rego libraries: %w", err)
}
for name, policy := range loadedLibs {
s.policies[name] = policy
}
s.debug.Log("Loaded %d embedded libraries (without embedded policies).", len(loadedLibs))
return nil
}
func (s *Scanner) LoadPolicies(loadEmbedded bool, srcFS fs.FS, paths []string, readers []io.Reader) error {
if s.policies == nil {
s.policies = make(map[string]*ast.Module)
}
if s.policyFS != nil {
s.debug.Log("Overriding filesystem for policies!")
srcFS = s.policyFS
}
if loadEmbedded {
loadedLibs, err := loadEmbeddedLibraries()
if err != nil {
return fmt.Errorf("failed to load embedded rego libraries: %w", err)
}
for name, policy := range loadedLibs {
s.policies[name] = policy
}
s.debug.Log("Loaded %d embedded libraries.", len(loadedLibs))
loaded, err := loadEmbeddedPolicies()
if err != nil {
return fmt.Errorf("failed to load embedded rego policies: %w", err)
}
for name, policy := range loaded {
s.policies[name] = policy
}
s.debug.Log("Loaded %d embedded policies.", len(loaded))
}
var err error
if len(paths) > 0 {
loaded, err := s.loadPoliciesFromDirs(srcFS, paths)
if err != nil {
return fmt.Errorf("failed to load rego policies from %s: %w", paths, err)
}
for name, policy := range loaded {
s.policies[name] = policy
}
s.debug.Log("Loaded %d policies from disk.", len(loaded))
}
if len(readers) > 0 {
loaded, err := s.loadPoliciesFromReaders(readers)
if err != nil {
return fmt.Errorf("failed to load rego policies from reader(s): %w", err)
}
for name, policy := range loaded {
s.policies[name] = policy
}
s.debug.Log("Loaded %d policies from reader(s).", len(loaded))
}
// gather namespaces
uniq := make(map[string]struct{})
for _, module := range s.policies {
namespace := getModuleNamespace(module)
uniq[namespace] = struct{}{}
}
var namespaces []string
for namespace := range uniq {
namespaces = append(namespaces, namespace)
}
store, err := initStore(s.dataDirs, namespaces)
if err != nil {
return fmt.Errorf("unable to load data: %w", err)
}
s.store = store
compiler := ast.NewCompiler()
compiler.Compile(s.policies)
if compiler.Failed() {
return compiler.Errors
}
s.compiler = compiler
s.retriever = NewMetadataRetriever(compiler)
return nil
}