-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnopa.go
283 lines (244 loc) · 6.85 KB
/
nopa.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Copyright 2025 Sencillo
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package nopa
import (
"context"
"encoding/json"
"fmt"
"log/slog"
"sync"
"time"
"github.com/nats-io/nats.go/jetstream"
"github.com/open-policy-agent/opa/v1/ast"
"github.com/open-policy-agent/opa/v1/bundle"
"github.com/open-policy-agent/opa/v1/metrics"
"github.com/open-policy-agent/opa/v1/rego"
"github.com/open-policy-agent/opa/v1/storage"
"github.com/open-policy-agent/opa/v1/storage/inmem"
"github.com/open-policy-agent/opa/v1/topdown/cache"
)
var (
ErrNotFound error = fmt.Errorf("package not found")
)
// BundleModifyFunc will take a bundle and allow for modifications
// like adding custom modules
type BundleModifyFunc func(b bundle.Bundle) (bundle.Bundle, error)
type Agent struct {
BundleName string
ObjectStore jetstream.ObjectStore
OPAStore storage.Store
mutex sync.RWMutex
Logger *slog.Logger
Env map[string]string
astFunc func(*rego.Rego)
Compiler *ast.Compiler
Modifiers []BundleModifyFunc
Cache cache.InterQueryCache
}
type AgentOpts struct {
BundleName string
ObjectStore jetstream.ObjectStore
Logger *slog.Logger
Env map[string]string
Modifiers []BundleModifyFunc
}
func NewAgent(opts AgentOpts) *Agent {
config, _ := cache.ParseCachingConfig(nil)
interQueryCache := cache.NewInterQueryCache(config)
a := &Agent{
BundleName: opts.BundleName,
ObjectStore: opts.ObjectStore,
Logger: opts.Logger,
Env: opts.Env,
OPAStore: inmem.New(),
Compiler: ast.NewCompiler(),
Modifiers: opts.Modifiers,
Cache: cache.InterQueryCache(interQueryCache),
}
a.SetRuntime()
return a
}
func (a *Agent) SetRuntime() {
obj := ast.NewObject()
env := ast.NewObject()
for k, v := range a.Env {
env.Insert(ast.StringTerm(k), ast.StringTerm(v))
}
obj.Insert(ast.StringTerm("env"), ast.NewTerm(env))
a.astFunc = rego.Runtime(obj.Get(ast.StringTerm("env")))
}
// SetBundle updates the in-memory store with the bundle retrieved from the NATS object store
func (a *Agent) SetBundle(name string) error {
ctxT, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
a.Logger.Info("locking requests to update bundle")
a.mutex.Lock()
a.Logger.Info("locked successfully")
defer func() {
a.Logger.Info("unlocking requests")
a.mutex.Unlock()
a.Logger.Info("unlocked successfully")
}()
// get bundle from NATS object bucket
f, err := a.ObjectStore.Get(ctxT, name)
if err != nil {
return fmt.Errorf("error getting object %v", err)
}
a.Logger.Info("retrieved bundle from object store")
// build new reader from tarball retrieved over NATS
tarball := bundle.NewCustomReader(bundle.NewTarballLoaderWithBaseURL(f, ""))
b, err := tarball.Read()
if err != nil {
return fmt.Errorf("error reading bundle: %v", err)
}
a.Logger.Info("generated tarball from bundle successfully")
for _, v := range a.Modifiers {
a.Logger.Debug("modifying bundle")
b, err = v(b)
if err != nil {
return fmt.Errorf("error in bundle modifier: %w", err)
}
}
if err := a.Activate(ctxT, b); err != nil {
return err
}
a.Logger.Info("activated bundle successfully")
return nil
}
func (a *Agent) WatchBundleUpdates(ctx context.Context, errChan chan<- error) {
watcher, err := a.ObjectStore.Watch(ctx, jetstream.IgnoreDeletes())
if err != nil {
a.Logger.Error(err.Error())
}
for v := range watcher.Updates() {
if v == nil {
continue
}
if v.Name != a.BundleName {
continue
}
if err := a.SetBundle(v.Name); err != nil {
err = fmt.Errorf("error setting bundle: %w", err)
a.Logger.Error(err.Error())
errChan <- err
}
}
}
func (a *Agent) MustWatchBundleUpdates(ctx context.Context) {
watcher, err := a.ObjectStore.Watch(ctx, jetstream.IgnoreDeletes())
if err != nil {
a.Logger.Error(err.Error())
}
for v := range watcher.Updates() {
if v == nil {
continue
}
if v.Name != a.BundleName {
continue
}
if err := a.SetBundle(v.Name); err != nil {
panic(fmt.Sprintf("error setting bundle: %v", err))
}
}
}
// Eval evaluates the input against the policy package
func (a *Agent) Eval(ctx context.Context, input []byte, pkg string) ([]byte, error) {
if input == nil {
return nil, fmt.Errorf("input required")
}
if pkg == "" {
return nil, fmt.Errorf("package name required")
}
a.Logger.Info(fmt.Sprintf("evaluating package: %s", pkg))
a.Logger.Debug(fmt.Sprintf("parsing input: %v", string(input)))
data, _, err := readInputGetV1(input)
if err != nil {
a.Logger.Error(err.Error())
return nil, err
}
a.mutex.RLock()
defer a.mutex.RUnlock()
c := storage.NewContext()
txn, err := a.OPAStore.NewTransaction(ctx, storage.TransactionParams{Context: c})
if err != nil {
a.Logger.Error(err.Error())
return nil, err
}
defer a.OPAStore.Abort(ctx, txn)
r := rego.New(
rego.Compiler(a.Compiler),
rego.Query(pkg),
rego.Transaction(txn),
rego.Store(a.OPAStore),
rego.ParsedInput(data),
rego.InterQueryBuiltinCache(a.Cache),
a.astFunc,
)
prepared, err := r.PrepareForEval(ctx)
if err != nil {
a.Logger.Error(err.Error())
return nil, err
}
results, err := prepared.Eval(ctx,
rego.EvalParsedInput(data),
rego.EvalTransaction(txn),
rego.EvalInterQueryBuiltinCache(a.Cache),
)
if err != nil {
a.Logger.Error(err.Error())
return nil, err
}
if len(results) < 1 {
return nil, ErrNotFound
}
value, err := json.Marshal(results[0].Expressions[0].Value)
if err != nil {
return nil, err
}
a.Logger.Debug(fmt.Sprintf("response: %s", string(value)))
return value, nil
}
func (a *Agent) Activate(ctx context.Context, b bundle.Bundle) error {
bundles := map[string]*bundle.Bundle{
"nopa": &b,
}
c := storage.NewContext()
txn, err := a.OPAStore.NewTransaction(ctx, storage.TransactionParams{Context: c, Write: true})
if err != nil {
return err
}
opts := bundle.ActivateOpts{
Ctx: ctx,
Store: a.OPAStore,
Bundles: bundles,
Txn: txn,
TxnCtx: c,
Compiler: a.Compiler,
Metrics: metrics.New(),
}
if err := bundle.Activate(&opts); err != nil {
a.Logger.Error(err.Error())
a.OPAStore.Abort(ctx, txn)
return err
}
return a.OPAStore.Commit(ctx, txn)
}
func readInputGetV1(data []byte) (ast.Value, *any, error) {
var input any
if err := json.Unmarshal(data, &input); err != nil {
return nil, nil, fmt.Errorf("invalid input: %w", err)
}
v, err := ast.InterfaceToValue(input)
return v, &input, err
}