-
Notifications
You must be signed in to change notification settings - Fork 2
/
query.go
161 lines (134 loc) · 4.26 KB
/
query.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
package runtime
import (
"context"
"github.com/google/uuid"
"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/metrics"
"github.com/open-policy-agent/opa/rego"
"github.com/open-policy-agent/opa/server/types"
"github.com/open-policy-agent/opa/storage"
"github.com/open-policy-agent/opa/topdown"
"github.com/open-policy-agent/opa/topdown/lineage"
"github.com/pkg/errors"
)
// map of unsafe builtins.
var unsafeBuiltinsMap = map[string]struct{}{ast.HTTPSend.Name: {}}
// Result contains the results of a Query execution.
type Result struct {
Result rego.ResultSet
Metrics map[string]interface{}
Explanation types.TraceV1
DecisionID string
}
// Query executes a REGO query against the Aserto OPA Runtime
// explain can be "notes", "full" or "off".
func (r *Runtime) Query(ctx context.Context, qStr string, input map[string]interface{}, pretty, includeMetrics, includeInstrumentation bool, explain types.ExplainModeV1) (*Result, error) {
m := metrics.New()
decisionID := uuid.New().String()
parsedQuery, err := r.ValidateQuery(qStr)
if err != nil {
return nil, errors.Wrap(err, "failed to validate query")
}
txn, err := r.storage.NewTransaction(ctx)
if err != nil {
return nil, errors.Wrap(err, "failed to create new OPA store transaction")
}
defer r.storage.Abort(ctx, txn)
results, err := r.execQuery(ctx, txn, decisionID, parsedQuery, input, m, explain, includeMetrics, includeInstrumentation, pretty)
if err != nil {
return nil, errors.Wrapf(err, "query execution failed, decision-id: [%s], query: [%s]", decisionID, qStr)
}
return results, nil
}
func (r *Runtime) ValidateQuery(query string) (ast.Body, error) {
var body ast.Body
body, err := ast.ParseBody(query)
if err != nil {
return nil, err
}
return body, nil
}
func (r *Runtime) execQuery(ctx context.Context, txn storage.Transaction, decisionID string, parsedQuery ast.Body, input map[string]interface{}, m metrics.Metrics, explainMode types.ExplainModeV1, includeMetrics, includeInstrumentation, pretty bool) (*Result, error) {
var buf *topdown.BufferTracer
if explainMode != types.ExplainOffV1 {
buf = topdown.NewBufferTracer()
}
opts := r.builtins
compiler := r.pluginsManager.GetCompiler()
opts = append(opts,
rego.Store(r.storage),
rego.Transaction(txn),
rego.Compiler(compiler),
rego.ParsedQuery(parsedQuery),
rego.Metrics(m),
rego.Instrument(includeInstrumentation),
rego.QueryTracer(buf),
rego.Trace(true),
rego.Runtime(r.pluginsManager.Info),
rego.UnsafeBuiltins(unsafeBuiltinsMap),
rego.InterQueryBuiltinCache(r.InterQueryCache),
rego.Input(input),
rego.Imports(r.imports),
)
for _, r := range r.pluginsManager.GetWasmResolvers() {
for _, entrypoint := range r.Entrypoints() {
opts = append(opts, rego.Resolver(entrypoint, r))
}
}
regoQuery := rego.New(opts...)
output, err := regoQuery.Eval(ctx)
if err != nil {
r.Logger.Warn().
Err(err).Str("decisionID", decisionID).
Str("query", parsedQuery.String()).
Interface("input", input).
Msg("error evaluating query")
return nil, errors.Wrap(err, "failed to evaluate rego query")
}
results := &Result{
Result: output,
DecisionID: decisionID,
}
if includeMetrics || includeInstrumentation {
results.Metrics = m.All()
}
if explainMode != types.ExplainOffV1 {
results.Explanation = r.getExplainResponse(explainMode, *buf, pretty)
}
r.Logger.Debug().
Err(err).Str("decisionID", decisionID).
Str("query", parsedQuery.String()).
Interface("input", input).
Msg("query evaluated")
return results, err
}
func (r *Runtime) getExplainResponse(explainMode types.ExplainModeV1, trace []*topdown.Event, pretty bool) (explanation types.TraceV1) {
switch explainMode {
case types.ExplainNotesV1:
var err error
explanation, err = types.NewTraceV1(lineage.Notes(trace), pretty)
if err != nil {
break
}
case types.ExplainFailsV1:
var err error
explanation, err = types.NewTraceV1(lineage.Fails(trace), pretty)
if err != nil {
break
}
case types.ExplainDebugV1:
var err error
explanation, err = types.NewTraceV1(lineage.Debug(trace), pretty)
if err != nil {
break
}
case types.ExplainFullV1:
var err error
explanation, err = types.NewTraceV1(trace, pretty)
if err != nil {
break
}
case types.ExplainOffV1:
}
return explanation
}