-
Notifications
You must be signed in to change notification settings - Fork 351
/
openpolicyagent.go
767 lines (611 loc) · 21.2 KB
/
openpolicyagent.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
package openpolicyagent
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net/http"
"os"
"strings"
"sync"
"text/template"
"time"
ext_authz_v3_core "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
"github.com/google/uuid"
"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/config"
"github.com/open-policy-agent/opa/dependencies"
"github.com/open-policy-agent/opa/logging"
"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/runtime"
"github.com/open-policy-agent/opa/storage"
"github.com/open-policy-agent/opa/storage/inmem"
iCache "github.com/open-policy-agent/opa/topdown/cache"
opatracing "github.com/open-policy-agent/opa/tracing"
"github.com/opentracing/opentracing-go"
"golang.org/x/sync/semaphore"
"google.golang.org/protobuf/encoding/protojson"
"github.com/zalando/skipper/filters"
"github.com/zalando/skipper/filters/flowid"
"github.com/zalando/skipper/filters/openpolicyagent/internal/envoy"
"github.com/zalando/skipper/routing"
"github.com/zalando/skipper/tracing"
)
const (
DefaultCleanIdlePeriod = 10 * time.Second
defaultReuseDuration = 30 * time.Second
defaultShutdownGracePeriod = 30 * time.Second
DefaultOpaStartupTimeout = 30 * time.Second
DefaultMaxRequestBodySize = 1 << 20 // 1 MB
DefaultMaxMemoryBodyParsing = 100 * DefaultMaxRequestBodySize
defaultBodyBufferSize = 8192 * 1024
)
type OpenPolicyAgentRegistry struct {
// Ideally share one Bundle storage across many OPA "instances" using this registry.
// This allows to save memory on bundles that are shared
// between different policies (i.e. global team memberships)
// This not possible due to some limitations in OPA
// See https://github.com/open-policy-agent/opa/issues/5707
mu sync.Mutex
instances map[string]*OpenPolicyAgentInstance
lastused map[*OpenPolicyAgentInstance]time.Time
once sync.Once
closed bool
quit chan struct{}
reuseDuration time.Duration
cleanInterval time.Duration
maxMemoryBodyParsingSem *semaphore.Weighted
maxRequestBodyBytes int64
bodyReadBufferSize int64
}
type OpenPolicyAgentFilter interface {
OpenPolicyAgent() *OpenPolicyAgentInstance
}
func WithReuseDuration(duration time.Duration) func(*OpenPolicyAgentRegistry) error {
return func(cfg *OpenPolicyAgentRegistry) error {
cfg.reuseDuration = duration
return nil
}
}
func WithMaxRequestBodyBytes(n int64) func(*OpenPolicyAgentRegistry) error {
return func(cfg *OpenPolicyAgentRegistry) error {
cfg.maxRequestBodyBytes = n
return nil
}
}
func WithMaxMemoryBodyParsing(n int64) func(*OpenPolicyAgentRegistry) error {
return func(cfg *OpenPolicyAgentRegistry) error {
cfg.maxMemoryBodyParsingSem = semaphore.NewWeighted(n)
return nil
}
}
func WithReadBodyBufferSize(n int64) func(*OpenPolicyAgentRegistry) error {
return func(cfg *OpenPolicyAgentRegistry) error {
cfg.bodyReadBufferSize = n
return nil
}
}
func WithCleanInterval(interval time.Duration) func(*OpenPolicyAgentRegistry) error {
return func(cfg *OpenPolicyAgentRegistry) error {
cfg.cleanInterval = interval
return nil
}
}
func NewOpenPolicyAgentRegistry(opts ...func(*OpenPolicyAgentRegistry) error) *OpenPolicyAgentRegistry {
registry := &OpenPolicyAgentRegistry{
reuseDuration: defaultReuseDuration,
cleanInterval: DefaultCleanIdlePeriod,
instances: make(map[string]*OpenPolicyAgentInstance),
lastused: make(map[*OpenPolicyAgentInstance]time.Time),
quit: make(chan struct{}),
maxRequestBodyBytes: DefaultMaxMemoryBodyParsing,
bodyReadBufferSize: defaultBodyBufferSize,
}
for _, opt := range opts {
opt(registry)
}
if registry.maxMemoryBodyParsingSem == nil {
registry.maxMemoryBodyParsingSem = semaphore.NewWeighted(DefaultMaxMemoryBodyParsing)
}
go registry.startCleanerDaemon()
return registry
}
type OpenPolicyAgentInstanceConfig struct {
envoyMetadata *ext_authz_v3_core.Metadata
configTemplate []byte
startupTimeout time.Duration
}
func WithConfigTemplate(configTemplate []byte) func(*OpenPolicyAgentInstanceConfig) error {
return func(cfg *OpenPolicyAgentInstanceConfig) error {
cfg.configTemplate = configTemplate
return nil
}
}
func WithConfigTemplateFile(configTemplateFile string) func(*OpenPolicyAgentInstanceConfig) error {
return func(cfg *OpenPolicyAgentInstanceConfig) error {
var err error
cfg.configTemplate, err = os.ReadFile(configTemplateFile)
return err
}
}
func WithEnvoyMetadata(metadata *ext_authz_v3_core.Metadata) func(*OpenPolicyAgentInstanceConfig) error {
return func(cfg *OpenPolicyAgentInstanceConfig) error {
cfg.envoyMetadata = metadata
return nil
}
}
func WithEnvoyMetadataBytes(content []byte) func(*OpenPolicyAgentInstanceConfig) error {
return func(cfg *OpenPolicyAgentInstanceConfig) error {
cfg.envoyMetadata = &ext_authz_v3_core.Metadata{}
return protojson.Unmarshal(content, cfg.envoyMetadata)
}
}
func WithEnvoyMetadataFile(file string) func(*OpenPolicyAgentInstanceConfig) error {
return func(cfg *OpenPolicyAgentInstanceConfig) error {
content, err := os.ReadFile(file)
if err != nil {
return err
}
err = WithEnvoyMetadataBytes(content)(cfg)
if err != nil {
return fmt.Errorf("cannot parse '%q': %w", file, err)
}
return nil
}
}
func WithStartupTimeout(timeout time.Duration) func(*OpenPolicyAgentInstanceConfig) error {
return func(cfg *OpenPolicyAgentInstanceConfig) error {
cfg.startupTimeout = timeout
return nil
}
}
func (cfg *OpenPolicyAgentInstanceConfig) GetEnvoyMetadata() *ext_authz_v3_core.Metadata {
return cfg.envoyMetadata
}
func NewOpenPolicyAgentConfig(opts ...func(*OpenPolicyAgentInstanceConfig) error) (*OpenPolicyAgentInstanceConfig, error) {
cfg := OpenPolicyAgentInstanceConfig{
startupTimeout: DefaultOpaStartupTimeout,
}
for _, opt := range opts {
if err := opt(&cfg); err != nil {
return nil, err
}
}
if cfg.configTemplate == nil {
var err error
cfg.configTemplate, err = os.ReadFile("opaconfig.yaml")
if err != nil {
return nil, err
}
}
return &cfg, nil
}
func (registry *OpenPolicyAgentRegistry) Close() {
registry.once.Do(func() {
registry.mu.Lock()
defer registry.mu.Unlock()
ctx, cancel := context.WithTimeout(context.Background(), defaultShutdownGracePeriod)
defer cancel()
for _, instance := range registry.instances {
instance.Close(ctx)
}
registry.closed = true
close(registry.quit)
})
}
func (registry *OpenPolicyAgentRegistry) cleanUnusedInstances(t time.Time) {
registry.mu.Lock()
defer registry.mu.Unlock()
if registry.closed {
return
}
ctx, cancel := context.WithTimeout(context.Background(), defaultShutdownGracePeriod)
defer cancel()
for key, inst := range registry.instances {
lastused, ok := registry.lastused[inst]
if ok && t.Sub(lastused) > registry.reuseDuration {
inst.Close(ctx)
delete(registry.instances, key)
delete(registry.lastused, inst)
}
}
}
func (registry *OpenPolicyAgentRegistry) startCleanerDaemon() {
ticker := time.NewTicker(registry.cleanInterval)
defer ticker.Stop()
for {
select {
case <-registry.quit:
return
case t := <-ticker.C:
registry.cleanUnusedInstances(t)
}
}
}
// Do implements routing.PostProcessor and cleans unused OPA instances
func (registry *OpenPolicyAgentRegistry) Do(routes []*routing.Route) []*routing.Route {
inUse := make(map[*OpenPolicyAgentInstance]struct{})
for _, ri := range routes {
for _, fi := range ri.Filters {
if ff, ok := fi.Filter.(OpenPolicyAgentFilter); ok {
inUse[ff.OpenPolicyAgent()] = struct{}{}
}
}
}
registry.markUnused(inUse)
return routes
}
func (registry *OpenPolicyAgentRegistry) NewOpenPolicyAgentInstance(bundleName string, config OpenPolicyAgentInstanceConfig, filterName string) (*OpenPolicyAgentInstance, error) {
registry.mu.Lock()
defer registry.mu.Unlock()
if registry.closed {
return nil, fmt.Errorf("open policy agent registry is already closed")
}
if instance, ok := registry.instances[bundleName]; ok {
delete(registry.lastused, instance)
return instance, nil
}
instance, err := registry.newOpenPolicyAgentInstance(bundleName, config, filterName)
if err != nil {
return nil, err
}
registry.instances[bundleName] = instance
return instance, nil
}
func (registry *OpenPolicyAgentRegistry) markUnused(inUse map[*OpenPolicyAgentInstance]struct{}) {
registry.mu.Lock()
defer registry.mu.Unlock()
for _, instance := range registry.instances {
if _, ok := inUse[instance]; !ok {
registry.lastused[instance] = time.Now()
}
}
}
func (registry *OpenPolicyAgentRegistry) newOpenPolicyAgentInstance(bundleName string, config OpenPolicyAgentInstanceConfig, filterName string) (*OpenPolicyAgentInstance, error) {
runtime.RegisterPlugin(envoy.PluginName, envoy.Factory{})
configBytes, err := interpolateConfigTemplate(config.configTemplate, bundleName)
if err != nil {
return nil, err
}
engine, err := registry.new(inmem.New(), configBytes, config, filterName, bundleName,
registry.maxRequestBodyBytes, registry.bodyReadBufferSize)
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), config.startupTimeout)
defer cancel()
if err = engine.Start(ctx, config.startupTimeout); err != nil {
return nil, err
}
return engine, nil
}
type OpenPolicyAgentInstance struct {
manager *plugins.Manager
instanceConfig OpenPolicyAgentInstanceConfig
opaConfig *config.Config
bundleName string
preparedQuery *rego.PreparedEvalQuery
preparedQueryDoOnce *sync.Once
interQueryBuiltinCache iCache.InterQueryCache
once sync.Once
stopped bool
registry *OpenPolicyAgentRegistry
maxBodyBytes int64
bodyReadBufferSize int64
idGenerator flowid.Generator
}
func envVariablesMap() map[string]string {
rawEnvVariables := os.Environ()
envVariables := make(map[string]string)
for _, item := range rawEnvVariables {
tokens := strings.SplitN(item, "=", 2)
envVariables[tokens[0]] = tokens[1]
}
return envVariables
}
// Config sets the configuration file to use on the OPA instance.
func interpolateConfigTemplate(configTemplate []byte, bundleName string) ([]byte, error) {
var buf bytes.Buffer
tpl := template.Must(template.New("opa-config").Parse(string(configTemplate)))
binding := make(map[string]interface{})
binding["bundlename"] = bundleName
binding["Env"] = envVariablesMap()
err := tpl.ExecuteTemplate(&buf, "opa-config", binding)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// new returns a new OPA object.
func (registry *OpenPolicyAgentRegistry) new(store storage.Store, configBytes []byte, instanceConfig OpenPolicyAgentInstanceConfig, filterName string, bundleName string, maxBodyBytes int64, bodyReadBufferSize int64) (*OpenPolicyAgentInstance, error) {
id := uuid.New().String()
uniqueIDGenerator, err := flowid.NewStandardGenerator(32)
if err != nil {
return nil, err
}
opaConfig, err := config.ParseConfig(configBytes, id)
if err != nil {
return nil, err
}
runtime.RegisterPlugin(envoy.PluginName, envoy.Factory{})
var logger logging.Logger = &QuietLogger{target: logging.Get()}
logger = logger.WithFields(map[string]interface{}{"skipper-filter": filterName})
manager, err := plugins.New(configBytes, id, store, configLabelsInfo(*opaConfig), plugins.Logger(logger))
if err != nil {
return nil, err
}
discovery, err := discovery.New(manager, discovery.Factories(map[string]plugins.Factory{envoy.PluginName: envoy.Factory{}}))
if err != nil {
return nil, err
}
manager.Register("discovery", discovery)
opa := &OpenPolicyAgentInstance{
registry: registry,
instanceConfig: instanceConfig,
manager: manager,
opaConfig: opaConfig,
bundleName: bundleName,
maxBodyBytes: maxBodyBytes,
bodyReadBufferSize: bodyReadBufferSize,
preparedQueryDoOnce: new(sync.Once),
interQueryBuiltinCache: iCache.NewInterQueryCache(manager.InterQueryBuiltinCacheConfig()),
idGenerator: uniqueIDGenerator,
}
manager.RegisterCompilerTrigger(opa.compilerUpdated)
return opa, nil
}
// Start asynchronously starts the policy engine's plugins that download
// policies, report status, etc.
func (opa *OpenPolicyAgentInstance) Start(ctx context.Context, timeout time.Duration) error {
err := opa.manager.Start(ctx)
if err != nil {
return err
}
// check readiness of all plugins
pluginsReady := func() bool {
for _, status := range opa.manager.PluginStatus() {
if status != nil && status.State != plugins.StateOK {
return false
}
}
return true
}
err = waitFunc(ctx, pluginsReady, 100*time.Millisecond)
if err != nil {
for pluginName, status := range opa.manager.PluginStatus() {
if status != nil && status.State != plugins.StateOK {
opa.Logger().WithFields(map[string]interface{}{
"plugin_name": pluginName,
"plugin_state": status.State,
"error_message": status.Message,
}).Error("Open policy agent plugin did not start in time")
}
}
opa.Close(ctx)
return fmt.Errorf("one or more open policy agent plugins failed to start in %v with error: %w", timeout, err)
}
return nil
}
func (opa *OpenPolicyAgentInstance) Close(ctx context.Context) {
opa.once.Do(func() {
opa.manager.Stop(ctx)
opa.stopped = true
})
}
func waitFunc(ctx context.Context, fun func() bool, interval time.Duration) error {
if fun() {
return nil
}
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return fmt.Errorf("timed out while starting: %w", ctx.Err())
case <-ticker.C:
if fun() {
return nil
}
}
}
}
func configLabelsInfo(opaConfig config.Config) func(*plugins.Manager) {
info := ast.NewObject()
labels := ast.NewObject()
labelsWrapper := ast.NewObject()
for key, value := range opaConfig.Labels {
labels.Insert(ast.StringTerm(key), ast.StringTerm(value))
}
labelsWrapper.Insert(ast.StringTerm("labels"), ast.NewTerm(labels))
info.Insert(ast.StringTerm("config"), ast.NewTerm(labelsWrapper))
return plugins.Info(ast.NewTerm(info))
}
func (opa *OpenPolicyAgentInstance) InstanceConfig() *OpenPolicyAgentInstanceConfig {
return &opa.instanceConfig
}
func (opa *OpenPolicyAgentInstance) compilerUpdated(txn storage.Transaction) {
opa.preparedQueryDoOnce = new(sync.Once)
}
func (opa *OpenPolicyAgentInstance) EnvoyPluginConfig() envoy.PluginConfig {
if plugin, ok := opa.manager.Plugin(envoy.PluginName).(*envoy.Plugin); ok {
return plugin.GetConfig()
}
defaultConfig := envoy.PluginConfig{
Path: "envoy/authz/allow",
DryRun: false,
}
defaultConfig.ParseQuery()
return defaultConfig
}
func (opa *OpenPolicyAgentInstance) startSpanFromContextWithTracer(tr opentracing.Tracer, parent opentracing.Span, ctx context.Context) (opentracing.Span, context.Context) {
var span opentracing.Span
if parent != nil {
span = tr.StartSpan("open-policy-agent", opentracing.ChildOf(parent.Context()))
} else {
span = tracing.CreateSpan("open-policy-agent", ctx, tr)
}
span.SetTag("opa.bundle_name", opa.bundleName)
for label, value := range opa.manager.Labels() {
span.SetTag("opa.label."+label, value)
}
return span, opentracing.ContextWithSpan(ctx, span)
}
func (opa *OpenPolicyAgentInstance) StartSpanFromFilterContext(fc filters.FilterContext) (opentracing.Span, context.Context) {
return opa.startSpanFromContextWithTracer(fc.Tracer(), fc.ParentSpan(), fc.Request().Context())
}
func (opa *OpenPolicyAgentInstance) StartSpanFromContext(ctx context.Context) (opentracing.Span, context.Context) {
span := opentracing.SpanFromContext(ctx)
if span != nil {
return opa.startSpanFromContextWithTracer(span.Tracer(), span, ctx)
}
return opa.startSpanFromContextWithTracer(opentracing.GlobalTracer(), nil, ctx)
}
func (opa *OpenPolicyAgentInstance) MetricsKey(key string) string {
return key + "." + opa.bundleName
}
var (
ErrClosed = errors.New("reader closed")
ErrTotalBodyBytesExceeded = errors.New("buffer for in-flight request body authorization in Open Policy Agent exceeded")
)
type bufferedBodyReader struct {
input io.ReadCloser
maxBufferSize int64
bodyBuffer bytes.Buffer
readBuffer []byte
once sync.Once
err error
closed bool
}
func newBufferedBodyReader(input io.ReadCloser, maxBufferSize int64, readBufferSize int64) *bufferedBodyReader {
return &bufferedBodyReader{
input: input,
maxBufferSize: maxBufferSize,
readBuffer: make([]byte, readBufferSize),
}
}
func (m *bufferedBodyReader) fillBuffer(expectedSize int64) ([]byte, error) {
var err error
for err == nil && int64(m.bodyBuffer.Len()) < m.maxBufferSize && int64(m.bodyBuffer.Len()) < expectedSize {
var n int
n, err = m.input.Read(m.readBuffer)
m.bodyBuffer.Write(m.readBuffer[:n])
}
if err == io.EOF {
err = nil
}
return m.bodyBuffer.Bytes(), err
}
func (m *bufferedBodyReader) Read(p []byte) (int, error) {
if m.closed {
return 0, ErrClosed
}
if m.err != nil {
return 0, m.err
}
// First read the buffered body
if m.bodyBuffer.Len() != 0 {
return m.bodyBuffer.Read(p)
}
// Continue reading from the underlying body reader
return m.input.Read(p)
}
// Close closes the undelrying reader if it implements io.Closer.
func (m *bufferedBodyReader) Close() error {
var err error
m.once.Do(func() {
m.closed = true
if c, ok := m.input.(io.Closer); ok {
err = c.Close()
}
})
return err
}
func bodyUpperBound(contentLength, maxBodyBytes int64) int64 {
if contentLength <= 0 {
return maxBodyBytes
}
if contentLength < maxBodyBytes {
return contentLength
}
return maxBodyBytes
}
func (opa *OpenPolicyAgentInstance) ExtractHttpBodyOptionally(req *http.Request) (io.ReadCloser, []byte, func(), error) {
body := req.Body
if body != nil && !opa.EnvoyPluginConfig().SkipRequestBodyParse &&
req.ContentLength <= int64(opa.maxBodyBytes) {
bases, err := dependencies.Base(opa.Compiler(), opa.EnvoyPluginConfig().ParsedQuery)
if err != nil {
return req.Body, nil, func() {}, nil
}
for _, base := range bases {
if base.HasPrefix(ast.MustParseRef("input.parsed_body")) {
wrapper := newBufferedBodyReader(req.Body, opa.maxBodyBytes, opa.bodyReadBufferSize)
requestedBodyBytes := bodyUpperBound(req.ContentLength, opa.maxBodyBytes)
if !opa.registry.maxMemoryBodyParsingSem.TryAcquire(requestedBodyBytes) {
return req.Body, nil, func() {}, ErrTotalBodyBytesExceeded
}
rawBody, err := wrapper.fillBuffer(req.ContentLength)
return wrapper, rawBody, func() { opa.registry.maxMemoryBodyParsingSem.Release(requestedBodyBytes) }, err
}
}
}
return req.Body, nil, func() {}, nil
}
// ParsedQuery is an implementation of the envoyauth.EvalContext interface
func (opa *OpenPolicyAgentInstance) ParsedQuery() ast.Body {
return opa.EnvoyPluginConfig().ParsedQuery
}
// Store is an implementation of the envoyauth.EvalContext interface
func (opa *OpenPolicyAgentInstance) Store() storage.Store { return opa.manager.Store }
// Compiler is an implementation of the envoyauth.EvalContext interface
func (opa *OpenPolicyAgentInstance) Compiler() *ast.Compiler { return opa.manager.GetCompiler() }
// Runtime is an implementation of the envoyauth.EvalContext interface
func (opa *OpenPolicyAgentInstance) Runtime() *ast.Term { return opa.manager.Info }
// Logger is an implementation of the envoyauth.EvalContext interface
func (opa *OpenPolicyAgentInstance) Logger() logging.Logger { return opa.manager.Logger() }
// PreparedQueryDoOnce is an implementation of the envoyauth.EvalContext interface
func (opa *OpenPolicyAgentInstance) PreparedQueryDoOnce() *sync.Once { return opa.preparedQueryDoOnce }
// InterQueryBuiltinCache is an implementation of the envoyauth.EvalContext interface
func (opa *OpenPolicyAgentInstance) InterQueryBuiltinCache() iCache.InterQueryCache {
return opa.interQueryBuiltinCache
}
// PreparedQuery is an implementation of the envoyauth.EvalContext interface
func (opa *OpenPolicyAgentInstance) PreparedQuery() *rego.PreparedEvalQuery { return opa.preparedQuery }
// SetPreparedQuery is an implementation of the envoyauth.EvalContext interface
func (opa *OpenPolicyAgentInstance) SetPreparedQuery(q *rego.PreparedEvalQuery) {
opa.preparedQuery = q
}
// Config is an implementation of the envoyauth.EvalContext interface
func (opa *OpenPolicyAgentInstance) Config() *config.Config { return opa.opaConfig }
// DistributedTracing is an implementation of the envoyauth.EvalContext interface
func (opa *OpenPolicyAgentInstance) DistributedTracing() opatracing.Options {
return opatracing.NewOptions(opa)
}
// logging.Logger that does not pollute info with debug logs
type QuietLogger struct {
target logging.Logger
}
func (l *QuietLogger) WithFields(fields map[string]interface{}) logging.Logger {
return &QuietLogger{target: l.target.WithFields(fields)}
}
func (l *QuietLogger) SetLevel(level logging.Level) {
l.target.SetLevel(level)
}
func (l *QuietLogger) GetLevel() logging.Level {
return l.target.GetLevel()
}
func (l *QuietLogger) Debug(fmt string, a ...interface{}) {
l.target.Debug(fmt, a)
}
func (l *QuietLogger) Info(fmt string, a ...interface{}) {
l.target.Debug(fmt, a)
}
func (l *QuietLogger) Error(fmt string, a ...interface{}) {
l.target.Error(fmt, a)
}
func (l *QuietLogger) Warn(fmt string, a ...interface{}) {
l.target.Warn(fmt, a)
}