forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
100 lines (76 loc) · 2.38 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
// 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 (
"io"
"io/ioutil"
"github.com/sirupsen/logrus"
"github.com/open-policy-agent/opa/logging"
)
// 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{}
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.NewStandardLogger()
l.SetFormatter(&logrus.JSONFormatter{})
o.ConsoleLogger = l
}
bs, err := ioutil.ReadAll(o.Config)
if err != nil {
return err
}
o.config = bs
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 := ioutil.ReadAll(o.Config)
if err != nil {
return err
}
o.config = bs
return nil
}