forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
save.go
295 lines (264 loc) · 6.32 KB
/
save.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
package topdown
import (
"github.com/open-policy-agent/opa/ast"
)
// saveSet contains a set of terms that are treated as unknown. The set is
// implemented as a tree so that we can partially evaluate data/input that is
// only partially known. E.g., policies expressed over input.x and input.y may
// be partially evaluated assuming input.x is known and input.y is unknown.
//
// Any time a variable is unified with an element in the set (an unknown), the
// variable is added to the set so that subsequent references to the variable
// are also treated as unknown.
type saveSet struct {
s []*saveSetElem
}
func newSaveSet(terms []*ast.Term) *saveSet {
return &saveSet{[]*saveSetElem{newSaveSetElem(terms)}}
}
func (n *saveSet) Empty() bool {
if n != nil {
for i := range n.s {
if len(n.s[i].children) != 0 {
return false
}
}
}
return true
}
func (n *saveSet) Contains(x *ast.Term) bool {
if n != nil {
for i := len(n.s) - 1; i >= 0; i-- {
if n.s[i].Contains(x) {
return true
}
}
}
return false
}
func (n *saveSet) ContainsRecursive(x *ast.Term) bool {
stop := false
ast.WalkTerms(x, func(t *ast.Term) bool {
if stop {
return true
}
if n.Contains(t) {
stop = true
}
return stop
})
return stop
}
func (n *saveSet) ContainsRecursiveAny(xs []*ast.Term) bool {
for i := range xs {
if n.ContainsRecursive(xs[i]) {
return true
}
}
return false
}
func (n *saveSet) Push(x *saveSetElem) {
n.s = append(n.s, x)
}
func (n *saveSet) Pop() {
n.s = n.s[:len(n.s)-1]
}
func (n *saveSet) Vars() ast.VarSet {
if n == nil {
return nil
}
result := ast.NewVarSet()
for i := 0; i < len(n.s); i++ {
for k := range n.s[i].children {
if v, ok := k.(ast.Var); ok {
result.Add(v)
}
}
}
return result
}
type saveSetElem struct {
children map[ast.Value]*saveSetElem
}
func newSaveSetElem(terms []*ast.Term) *saveSetElem {
elem := &saveSetElem{
children: map[ast.Value]*saveSetElem{},
}
for i := range terms {
elem.Insert(terms[i])
}
return elem
}
func (n *saveSetElem) Empty() bool {
return n == nil || len(n.children) == 0
}
func (n *saveSetElem) Contains(x *ast.Term) bool {
switch x := x.Value.(type) {
case ast.Ref:
curr := n
for i := 0; i < len(x); i++ {
if curr = curr.child(x[i].Value); curr == nil {
return false
} else if curr.Empty() {
return true
}
}
return true
case ast.Var, ast.String:
return n.child(x) != nil
default:
return false
}
}
func (n *saveSetElem) Insert(x *ast.Term) *saveSetElem {
if n == nil {
return nil
}
switch v := x.Value.(type) {
case ast.Ref:
curr := n
for i := 0; i < len(v); i++ {
curr = curr.Insert(v[i])
}
return curr
case ast.Var, ast.String:
child := n.children[v]
if child == nil {
child = newSaveSetElem(nil)
n.children[v] = child
}
return child
}
return nil
}
func (n *saveSetElem) child(v ast.Value) *saveSetElem {
if n == nil {
return nil
}
return n.children[v]
}
// saveStack contains a stack of queries that represent the result of partial
// evaluation. When partial evaluation completes, the top of the stack
// represents a complete, partially evaluated query that can be saved and
// evaluated later.
//
// The result is stored in a stack so that partial evaluation of a query can be
// paused and then resumed in cases where different queries make up the result
// of partial evaluation, such as when a rule with a default clause is
// partially evaluated. In this case, the partially evaluated rule will be
// output in the support module.
type saveStack struct {
Stack []saveStackQuery
}
func newSaveStack() *saveStack {
return &saveStack{
Stack: []saveStackQuery{
{},
},
}
}
func (s *saveStack) PushQuery(query saveStackQuery) {
s.Stack = append(s.Stack, query)
}
func (s *saveStack) PopQuery() saveStackQuery {
last := s.Stack[len(s.Stack)-1]
s.Stack = s.Stack[:len(s.Stack)-1]
return last
}
func (s *saveStack) Push(expr *ast.Expr, b1 *bindings, b2 *bindings) {
idx := len(s.Stack) - 1
s.Stack[idx] = append(s.Stack[idx], saveStackElem{expr, b1, b2})
}
func (s *saveStack) Pop() {
idx := len(s.Stack) - 1
query := s.Stack[idx]
s.Stack[idx] = query[:len(query)-1]
}
type saveStackQuery []saveStackElem
func (s saveStackQuery) Plug(b *bindings) ast.Body {
if len(s) == 0 {
return ast.NewBody(ast.NewExpr(ast.BooleanTerm(true)))
}
result := make(ast.Body, len(s))
for i := range s {
expr := s[i].Plug(b)
result.Set(expr, i)
}
return result
}
type saveStackElem struct {
Expr *ast.Expr
B1 *bindings
B2 *bindings
}
func (e saveStackElem) Plug(caller *bindings) *ast.Expr {
expr := e.Expr.Copy()
switch terms := expr.Terms.(type) {
case []*ast.Term:
if expr.IsEquality() {
terms[1] = e.B1.PlugNamespaced(terms[1], caller)
terms[2] = e.B2.PlugNamespaced(terms[2], caller)
} else {
for i := 1; i < len(terms); i++ {
terms[i] = e.B1.PlugNamespaced(terms[i], caller)
}
}
case *ast.Term:
expr.Terms = e.B1.PlugNamespaced(terms, caller)
}
for i := range expr.With {
expr.With[i].Value = e.B1.PlugNamespaced(expr.With[i].Value, caller)
}
return expr
}
// saveSupport contains additional partially evaluated policies that are part
// of the output of partial evaluation.
//
// The support structure is accumulated as partial evaluation runs and then
// considered complete once partial evaluation finishes (but not before). This
// differs from partially evaluated queries which are considered complete as
// soon as each one finishes.
type saveSupport struct {
modules map[string]*ast.Module
}
func newSaveSupport() *saveSupport {
return &saveSupport{
modules: map[string]*ast.Module{},
}
}
func (s *saveSupport) List() []*ast.Module {
result := []*ast.Module{}
for _, module := range s.modules {
result = append(result, module)
}
return result
}
func (s *saveSupport) Exists(path ast.Ref) bool {
k := path[:len(path)-1].String()
module, ok := s.modules[k]
if !ok {
return false
}
name := ast.Var(path[len(path)-1].Value.(ast.String))
for _, rule := range module.Rules {
if rule.Head.Name.Equal(name) {
return true
}
}
return false
}
func (s *saveSupport) Insert(path ast.Ref, rule *ast.Rule) {
pkg := path[:len(path)-1]
k := pkg.String()
module, ok := s.modules[k]
if !ok {
module = &ast.Module{
Package: &ast.Package{
Path: pkg,
},
}
s.modules[k] = module
}
rule.Module = module
module.Rules = append(module.Rules, rule)
}