forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec.go
220 lines (175 loc) · 6.1 KB
/
exec.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
package cmd
import (
"bytes"
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"github.com/open-policy-agent/opa/cmd/internal/exec"
"github.com/open-policy-agent/opa/internal/config"
internal_logging "github.com/open-policy-agent/opa/internal/logging"
"github.com/open-policy-agent/opa/logging"
"github.com/open-policy-agent/opa/plugins"
"github.com/open-policy-agent/opa/plugins/bundle"
"github.com/open-policy-agent/opa/plugins/discovery"
"github.com/open-policy-agent/opa/plugins/logs"
"github.com/open-policy-agent/opa/plugins/status"
"github.com/open-policy-agent/opa/sdk"
"github.com/open-policy-agent/opa/util"
"github.com/spf13/cobra"
)
func init() {
var bundlePaths repeatedStringFlag
params := exec.NewParams(os.Stdout)
var cmd = &cobra.Command{
Use: `exec <path> [<path> [...]]`,
Short: "Execute against input files",
Long: `Execute against input files.
The 'exec' command executes OPA against one or more input files. If the paths
refer to directories, OPA will execute against files contained inside those
directories, recursively.
The 'exec' command accepts a --config-file/-c or series of --set options as
arguments. These options behave the same as way as 'opa run'. Since the 'exec'
command is intended to execute OPA in one-shot, the 'exec' command will
manually trigger plugins before and after policy execution:
Before: Discovery -> Bundle -> Status
After: Decision Logs
By default, the 'exec' command executes the "default decision" (specified in
the OPA configuration) against each input file. This can be overridden by
specifying the --decision argument and pointing at a specific policy decision,
e.g., opa exec --decision /foo/bar/baz ...`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
params.Paths = args
params.BundlePaths = bundlePaths.v
if err := runExec(params); err != nil {
logging.Get().WithFields(map[string]interface{}{"err": err}).Error("Unexpected error.")
os.Exit(1)
}
},
}
addBundleFlag(cmd.Flags(), &bundlePaths)
addOutputFormat(cmd.Flags(), params.OutputFormat)
addConfigFileFlag(cmd.Flags(), ¶ms.ConfigFile)
addConfigOverrides(cmd.Flags(), ¶ms.ConfigOverrides)
addConfigOverrideFiles(cmd.Flags(), ¶ms.ConfigOverrideFiles)
cmd.Flags().StringVarP(¶ms.Decision, "decision", "", "", "set decision to evaluate")
cmd.Flags().VarP(params.LogLevel, "log-level", "l", "set log level")
cmd.Flags().Var(params.LogFormat, "log-format", "set log format")
RootCommand.AddCommand(cmd)
}
func runExec(params *exec.Params) error {
stdLogger, consoleLogger, err := setupLogging(params.LogLevel.String(), params.LogFormat.String())
if err != nil {
return fmt.Errorf("config error: %w", err)
}
config, err := setupConfig(params.ConfigFile, params.ConfigOverrides, params.ConfigOverrideFiles, params.BundlePaths)
if err != nil {
return fmt.Errorf("config error: %w", err)
}
ctx := context.Background()
ready := make(chan struct{})
opa, err := sdk.New(ctx, sdk.Options{
Config: bytes.NewReader(config),
Logger: stdLogger,
ConsoleLogger: consoleLogger,
Ready: ready,
})
if err != nil {
return fmt.Errorf("runtime error: %w", err)
}
if err := triggerPlugins(ctx, opa, []string{discovery.Name, bundle.Name, status.Name}); err != nil {
return fmt.Errorf("runtime error: %w", err)
}
<-ready
if err := exec.Exec(ctx, opa, params); err != nil {
return fmt.Errorf("exec error: %w", err)
}
if err := triggerPlugins(ctx, opa, []string{logs.Name}); err != nil {
return fmt.Errorf("runtime error: %w", err)
}
return nil
}
func triggerPlugins(ctx context.Context, opa *sdk.OPA, names []string) error {
for _, name := range names {
if p, ok := opa.Plugin(name).(plugins.Triggerable); ok {
if err := p.Trigger(ctx); err != nil {
return err
}
}
}
return nil
}
func setupLogging(level, format string) (logging.Logger, logging.Logger, error) {
lvl, err := internal_logging.GetLevel(level)
if err != nil {
return nil, nil, err
}
logging.Get().SetFormatter(internal_logging.GetFormatter(format))
logging.Get().SetLevel(lvl)
stdLogger := logging.New()
stdLogger.SetLevel(lvl)
stdLogger.SetFormatter(internal_logging.GetFormatter(format))
consoleLogger := logging.New()
consoleLogger.SetFormatter(internal_logging.GetFormatter(format))
return stdLogger, consoleLogger, nil
}
func setupConfig(file string, overrides []string, overrideFiles []string, bundlePaths []string) ([]byte, error) {
bs, err := config.Load(file, overrides, overrideFiles)
if err != nil {
return nil, err
}
var root map[string]interface{}
if err := util.Unmarshal(bs, &root); err != nil {
return nil, err
}
if err := injectExplicitBundles(root, bundlePaths); err != nil {
return nil, err
}
// NOTE(tsandall): This could be generalized in the future if we need to
// deal with arbitrary plugins.
// NOTE(tsandall): Overriding the discovery trigger mode to manual means
// that all plugins will inherit the trigger mode by default. If the plugin
// trigger mode is explicitly set to something other than 'manual' this will
// result in a configuration error.
if cfg, ok := root["discovery"].(map[string]interface{}); ok {
cfg["trigger"] = "manual"
}
if cfg, ok := root["bundles"].(map[string]interface{}); ok {
for _, x := range cfg {
if bcfg, ok := x.(map[string]interface{}); ok {
bcfg["trigger"] = "manual"
}
}
}
if cfg, ok := root["decision_logs"].(map[string]interface{}); ok {
if rcfg, ok := cfg["reporting"].(map[string]interface{}); ok {
rcfg["trigger"] = "manual"
}
}
if cfg, ok := root["status"].(map[string]interface{}); ok {
cfg["trigger"] = "manual"
}
return json.Marshal(root)
}
func injectExplicitBundles(root map[string]interface{}, paths []string) error {
if len(paths) == 0 {
return nil
}
bundles, ok := root["bundles"].(map[string]interface{})
if !ok {
bundles = map[string]interface{}{}
root["bundles"] = bundles
}
for i := range paths {
abspath, err := filepath.Abs(paths[i])
if err != nil {
return err
}
bundles[fmt.Sprintf("~%d", i)] = map[string]interface{}{
"resource": fmt.Sprintf("file://%v", abspath),
}
}
return nil
}