-
Notifications
You must be signed in to change notification settings - Fork 1
/
local.go
190 lines (158 loc) · 4.5 KB
/
local.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package opac
import (
"context"
"encoding/json"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"github.com/m-mizutani/goerr"
"github.com/m-mizutani/zlog"
"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/rego"
"github.com/open-policy-agent/opa/topdown/print"
)
// Local loads and compile local policy data and evaluate input data with them.
type Local struct {
flies []string
dirs []string
policies map[string]string
compiler *ast.Compiler
query string
logger *zlog.Logger
print io.Writer
}
// LocalOption is Option of functional option pattern for Local
type LocalOption func(x *Local)
// WithFile specifies .rego policy file path
func WithFile(filePath string) LocalOption {
return func(x *Local) {
x.flies = append(x.flies, filepath.Clean(filePath))
}
}
// WithDir specifies directory path of .rego policy. Import policy files recursively.
func WithDir(dirPath string) LocalOption {
return func(x *Local) {
x.dirs = append(x.dirs, filepath.Clean(dirPath))
}
}
// WithPolicyData specifies raw policy data with name. If the `name` conflicts with file path loaded by WithFile or WithDir, the policy overwrites data loaded by WithFile or WithDir.
func WithPolicyData(name, policy string) LocalOption {
return func(x *Local) {
x.policies[name] = policy
}
}
// WithPackage specifies using package name. e.g. "example.my_policy"
func WithPackage(pkg string) LocalOption {
return func(x *Local) {
x.query = "data." + pkg
}
}
// WithLoggingLocal enables logger for debug
func WithLoggingLocal() LocalOption {
return func(x *Local) {
x.logger = zlog.New(zlog.WithLogLevel("debug"))
}
}
// WithRegoPrint enables OPA print function and output to `w`
func WithRegoPrint(w io.Writer) LocalOption {
return func(x *Local) {
x.print = w
}
}
// NewLocal creates a new Local client. It requires one or more WithFile, WithDir or WithPolicyData.
func NewLocal(options ...LocalOption) (*Local, error) {
client := &Local{
query: "data",
logger: zlog.New(),
policies: make(map[string]string),
}
for _, opt := range options {
opt(client)
}
policies := make(map[string]string)
var targetFiles []string
for _, dirPath := range client.dirs {
err := filepath.WalkDir(dirPath, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return ErrReadRegoDir.Wrap(err).With("path", path)
}
if d.IsDir() {
return nil
}
if filepath.Ext(path) != ".rego" {
return nil
}
targetFiles = append(targetFiles, path)
return nil
})
if err != nil {
return nil, goerr.Wrap(err)
}
}
targetFiles = append(targetFiles, client.flies...)
for _, filePath := range targetFiles {
raw, err := os.ReadFile(filepath.Clean(filePath))
if err != nil {
return nil, ErrReadRegoFile.Wrap(err).With("path", filePath)
}
policies[filePath] = string(raw)
}
for k, v := range client.policies {
policies[k] = v
}
if len(policies) == 0 {
return nil, goerr.Wrap(ErrNoPolicyData)
}
compiler, err := ast.CompileModulesWithOpt(policies, ast.CompileOpts{
EnablePrintStatements: true,
})
if err != nil {
return nil, goerr.Wrap(err)
}
client.compiler = compiler
client.logger.
With("query", client.query).
With("loaded files", targetFiles).
Debug("created local client")
return client, nil
}
type printLogger struct {
w io.Writer
}
func (x *printLogger) Print(ctx print.Context, msg string) error {
if x.w != nil {
fmt.Fprintf(x.w, "%s:%d %s", ctx.Location.File, ctx.Location.Row, msg)
}
return nil
}
// Query evaluates policy with `input` data. The result will be written to `out`. `out` must be pointer of instance.
func (x *Local) Query(ctx context.Context, input interface{}, output interface{}) error {
x.logger.With("in", input).Debug("start Local.Query")
q := rego.New(
rego.Query(x.query),
rego.PrintHook(&printLogger{
w: x.print,
}),
rego.Compiler(x.compiler),
rego.Input(input),
)
rs, err := q.Eval(ctx)
if err != nil {
return goerr.Wrap(err, "fail to eval local policy").With("input", input)
}
if len(rs) == 0 || len(rs[0].Expressions) == 0 {
return goerr.Wrap(ErrNoEvalResult)
}
x.logger.With("rs", rs).Debug("got a result of rego.Eval")
raw, err := json.Marshal(rs[0].Expressions[0].Value)
if err != nil {
return goerr.Wrap(err, "fail to marshal a result of rego.Eval").With("rs", rs)
}
if err := json.Unmarshal(raw, output); err != nil {
return goerr.Wrap(err, "fail to unmarshal a result of rego.Eval to out").With("rs", rs)
}
x.logger.With("result set", rs).Debug("done Local.Query")
return nil
}