/
opa.go
232 lines (197 loc) · 6.13 KB
/
opa.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright 2018 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
package opa
import (
"context"
"crypto/rand"
"fmt"
"io"
"os"
"strings"
"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/bundle"
"github.com/open-policy-agent/opa/loader"
"github.com/open-policy-agent/opa/metrics"
"github.com/open-policy-agent/opa/plugins"
"github.com/open-policy-agent/opa/plugins/discovery"
"github.com/open-policy-agent/opa/rego"
"github.com/open-policy-agent/opa/storage"
"github.com/open-policy-agent/opa/storage/inmem"
"github.com/pkg/errors"
"github.com/unbasical/kelon/pkg/constants/logging"
)
// OPA represents an instance of the policy engine.
type OPA struct {
configBytes []byte
manager *plugins.Manager
}
type loadResult struct {
loader.Result
Bundles map[string]*bundle.Bundle
}
// ConfigOPA sets the configuration file to use on the OPA instance.
func ConfigOPA(fileName string) func(opa *OPA) error {
return func(opa *OPA) error {
bs, err := os.ReadFile(fileName)
if err != nil {
return err
}
opa.configBytes = bs
return nil
}
}
// Returns a new OPA instance.
func NewOPA(ctx context.Context, regosPath string, opts ...func(*OPA) error) (*OPA, error) {
opa := &OPA{}
// Configure OPA
for _, opt := range opts {
if err := opt(opa); err != nil {
return nil, err
}
}
// Init store
store := inmem.New()
id, err := uuid4()
if err != nil {
return nil, errors.Wrap(err, "NewOPA: Unable to create uuid4")
}
opa.manager, err = plugins.New(opa.configBytes, id, store)
if err != nil {
return nil, errors.Wrap(err, "NewOPA: Error while creating manager plugin")
}
disc, err := discovery.New(opa.manager)
if err != nil {
return nil, errors.Wrap(err, "NewOPA: Error while creating discovery plugin")
}
opa.manager.Register("discovery", disc)
// Load regos
if err := opa.LoadRegosFromPath(ctx, regosPath); err != nil {
return nil, errors.Wrap(err, "NewOPA: Unable to load regos")
}
return opa, nil
}
func (opa *OPA) LoadRegosFromPath(ctx context.Context, regosPath string) error {
// Return with no error on empty path
if regosPath == "" {
return nil
}
store := opa.manager.Store
logging.LogForComponent("OPA").Debugf("Loading regos from dir: %s", regosPath)
filter := func(abspath string, info os.FileInfo, depth int) bool {
return !strings.HasSuffix(abspath, ".rego")
}
loaded, err := loadPaths([]string{regosPath}, filter, true)
if err != nil {
return errors.Wrap(err, "NewOPA: Error while loading rego dir")
}
for bundleName, loadedBundle := range loaded.Bundles {
logging.LogForComponent("OPA").Infof("Loading Bundle: %s", bundleName)
for _, module := range loadedBundle.Modules {
logging.LogForComponent("OPA").Infof("Loaded Package: [%s] -> module [%s]", module.Parsed.Package.String(), module.Path)
}
}
txn, err := store.NewTransaction(ctx, storage.WriteParams)
if err != nil {
return errors.Wrap(err, "NewOPA: Error while opening transaction")
}
if len(loaded.Documents) > 0 {
if err := store.Write(ctx, txn, storage.AddOp, storage.MustParsePath(regosPath), loaded.Documents); err != nil {
return errors.Wrap(err, "NewOPA: Error while writing document")
}
}
if err := compileAndStoreInputs(ctx, store, txn, loaded, 1); err != nil {
store.Abort(ctx, txn)
return errors.Wrap(err, "NewOPA: Error while storing inputs")
}
if err := store.Commit(ctx, txn); err != nil {
return errors.Wrap(err, "NewOPA: Error while commit")
}
return nil
}
// Start asynchronously starts the policy engine's plugins that download
// policies, report status, etc.
func (opa *OPA) Start(ctx context.Context) error {
return opa.manager.Start(ctx)
}
// Bool returns a boolean policy decision.
func (opa *OPA) PartialEvaluate(ctx context.Context, input interface{}, query string, opts ...func(*rego.Rego)) (*rego.PartialQueries, error) {
m := metrics.New()
var partialResult *rego.PartialQueries
err := storage.Txn(ctx, opa.manager.Store, storage.TransactionParams{}, func(txn storage.Transaction) error {
var err error
r := rego.New(append(opts,
rego.Metrics(m),
rego.Query(query),
rego.Input(input),
rego.Compiler(opa.manager.GetCompiler()),
rego.Store(opa.manager.Store),
rego.Transaction(txn))...)
rs, err := r.Partial(ctx)
if err != nil {
return err
}
partialResult = rs
return nil
})
return partialResult, err
}
func uuid4() (string, error) {
bs := make([]byte, 16)
n, err := io.ReadFull(rand.Reader, bs)
if n != len(bs) || err != nil {
return "", err
}
bs[8] = bs[8]&^0xc0 | 0x80
bs[6] = bs[6]&^0xf0 | 0x40
return fmt.Sprintf("%x-%x-%x-%x-%x", bs[0:4], bs[4:6], bs[6:8], bs[8:10], bs[10:]), nil
}
func compileAndStoreInputs(ctx context.Context, store storage.Store, txn storage.Transaction, loaded *loadResult, errorLimit int) error {
policies := make(map[string]*ast.Module, len(loaded.Modules))
for id, parsed := range loaded.Modules {
policies[id] = parsed.Parsed
}
c := ast.NewCompiler().SetErrorLimit(errorLimit).WithPathConflictsCheck(storage.NonEmpty(ctx, store, txn))
opts := &bundle.ActivateOpts{
Ctx: ctx,
Store: store,
Txn: txn,
Compiler: c,
Metrics: metrics.New(),
Bundles: loaded.Bundles,
ExtraModules: policies,
}
err := bundle.Activate(opts)
if err != nil {
return err
}
// Policies in bundles will have already been added to the store, but
// modules loaded outside of bundles will need to be added manually.
for id, parsed := range loaded.Modules {
if err := store.UpsertPolicy(ctx, txn, id, parsed.Raw); err != nil {
return err
}
}
return nil
}
func loadPaths(paths []string, filter loader.Filter, asBundle bool) (*loadResult, error) {
result := &loadResult{}
var err error
if asBundle {
result.Bundles = make(map[string]*bundle.Bundle, len(paths))
for _, path := range paths {
result.Bundles[path], err = loader.AsBundle(path)
if err != nil {
return nil, err
}
}
} else {
loaded, err := loader.Filtered(paths, filter)
if err != nil {
return nil, err
}
result.Modules = loaded.Modules
result.Documents = loaded.Documents
}
return result, nil
}