/
options.go
132 lines (102 loc) · 3.33 KB
/
options.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
// Copyright 2021 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 sdk
import (
"fmt"
"io"
"github.com/sirupsen/logrus"
"github.com/open-policy-agent/opa/hooks"
"github.com/open-policy-agent/opa/logging"
"github.com/open-policy-agent/opa/plugins"
"github.com/open-policy-agent/opa/storage"
"github.com/open-policy-agent/opa/storage/inmem"
)
// Options contains parameters to setup and configure OPA.
type Options struct {
// Config provides the OPA configuration for this instance. The config can
// be supplied as a YAML or JSON byte stream. See
// https://www.openpolicyagent.org/docs/latest/configuration/ for detailed
// description of the supported configuration.
Config io.Reader
// Logger sets the logging implementation to use for standard logs emitted
// by OPA. By default, standard logging is disabled.
Logger logging.Logger
// ConsoleLogger sets the logging implementation to use for emitting Status
// and Decision Logs to the console. By default, console logging is enabled.
ConsoleLogger logging.Logger
// Ready sets a channel to notify when the OPA instance is ready. If this
// field is not set, the New() function will block until ready. The channel
// is closed to signal readiness.
Ready chan struct{}
// Plugins provides a set of plugins.Factory instances that will be
// registered with the OPA SDK instance.
Plugins map[string]plugins.Factory
// ID provides an option to set a static ID for the OPA system, avoiding
// the need to generate a random one at initialization. Setting a static ID
// is recommended, as it makes it easier to track the system over time.
ID string
// Store sets the store to be used by the SDK instance. If nil, it'll use OPA's
// inmem store.
Store storage.Store
// Hooks allows hooking into the internals of SDK operations (TODO(sr): find better words)
Hooks hooks.Hooks
config []byte
block bool
}
func (o *Options) init() error {
if o.Ready == nil {
o.Ready = make(chan struct{})
o.block = true
}
if o.Logger == nil {
o.Logger = logging.NewNoOpLogger()
}
if o.ConsoleLogger == nil {
l := logging.New()
l.SetFormatter(&logrus.JSONFormatter{})
o.ConsoleLogger = l
}
if o.Config == nil {
o.config = []byte("{}")
} else {
bs, err := io.ReadAll(o.Config)
if err != nil {
return err
}
o.config = bs
}
if o.Store == nil {
o.Store = inmem.New()
}
if err := o.Hooks.Validate(); err != nil {
return fmt.Errorf("hooks: %w", err)
}
return nil
}
// ConfigOptions contains parameters to (re-)configure OPA.
type ConfigOptions struct {
// Config provides the OPA configuration for this instance. The config can
// be supplied as a YAML or JSON byte stream. See
// https://www.openpolicyagent.org/docs/latest/configuration/ for detailed
// description of the supported configuration.
Config io.Reader
// Ready sets a channel to notify when the OPA instance is ready. If this
// field is not set, the Configure() function will block until ready. The
// channel is closed to signal readiness.
Ready chan struct{}
config []byte
block bool
}
func (o *ConfigOptions) init() error {
if o.Ready == nil {
o.Ready = make(chan struct{})
o.block = true
}
bs, err := io.ReadAll(o.Config)
if err != nil {
return err
}
o.config = bs
return nil
}