-
Notifications
You must be signed in to change notification settings - Fork 352
/
Copy pathrewrites.go
346 lines (306 loc) · 9.8 KB
/
rewrites.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package check
import (
"context"
"github.com/pkg/errors"
"github.com/ory/keto/internal/check/checkgroup"
"github.com/ory/keto/internal/namespace/ast"
"github.com/ory/keto/internal/relationtuple"
"github.com/ory/keto/internal/x"
"github.com/ory/keto/ketoapi"
)
func checkNotImplemented(_ context.Context, resultCh chan<- checkgroup.Result) {
resultCh <- checkgroup.Result{Err: errors.WithStack(errors.New("not implemented"))}
}
func toTreeNodeType(op ast.Operator) ketoapi.TreeNodeType {
switch op {
case ast.OperatorOr:
return ketoapi.TreeNodeUnion
case ast.OperatorAnd:
return ketoapi.TreeNodeIntersection
default:
return ketoapi.TreeNodeUnion
}
}
func (e *Engine) checkSubjectSetRewrite(
ctx context.Context,
tuple *relationTuple,
rewrite *ast.SubjectSetRewrite,
restDepth int,
) checkgroup.CheckFunc {
if restDepth <= 0 {
e.d.Logger().Debug("reached max-depth, therefore this query will not be further expanded")
return checkgroup.UnknownMemberFunc
}
e.d.Logger().
WithField("request", tuple.String()).
Trace("check subject-set rewrite")
var (
op binaryOperator
checks []checkgroup.CheckFunc
handled = make(map[int]struct{})
)
switch rewrite.Operation {
case ast.OperatorOr:
op = or
case ast.OperatorAnd:
op = and
default:
return checkNotImplemented
}
// Shortcut for ORs of ComputedSubjectSets
if rewrite.Operation == ast.OperatorOr {
var computedSubjectSets []string
for i, child := range rewrite.Children {
switch c := child.(type) {
case *ast.ComputedSubjectSet:
handled[i] = struct{}{}
computedSubjectSets = append(computedSubjectSets, c.Relation)
}
}
if len(computedSubjectSets) > 0 {
checks = append(checks, func(ctx context.Context, resultCh chan<- checkgroup.Result) {
res, err := e.d.Traverser().TraverseSubjectSetRewrite(ctx, tuple, computedSubjectSets)
if err != nil {
resultCh <- checkgroup.Result{Err: errors.WithStack(err)}
return
}
g := checkgroup.New(ctx)
defer func() { resultCh <- g.Result() }()
for _, result := range res {
if result.Found {
g.SetIsMember()
return
}
}
// If not, we must go another hop:
for _, result := range res {
g.Add(e.checkIsAllowed(ctx, result.To, restDepth-1, true))
}
})
}
}
for i, child := range rewrite.Children {
if _, found := handled[i]; found {
continue
}
switch c := child.(type) {
case *ast.TupleToSubjectSet:
checks = append(checks, checkgroup.WithEdge(checkgroup.Edge{
Tuple: *tuple,
Type: ketoapi.TreeNodeTupleToSubjectSet,
}, e.checkTupleToSubjectSet(tuple, c, restDepth)))
case *ast.ComputedSubjectSet:
checks = append(checks, checkgroup.WithEdge(checkgroup.Edge{
Tuple: *tuple,
Type: ketoapi.TreeNodeComputedSubjectSet,
}, e.checkComputedSubjectSet(ctx, tuple, c, restDepth)))
case *ast.SubjectSetRewrite:
checks = append(checks, checkgroup.WithEdge(checkgroup.Edge{
Tuple: *tuple,
Type: toTreeNodeType(c.Operation),
}, e.checkSubjectSetRewrite(ctx, tuple, c, restDepth-1)))
case *ast.InvertResult:
checks = append(checks, checkgroup.WithEdge(checkgroup.Edge{
Tuple: *tuple,
Type: ketoapi.TreeNodeNot,
}, e.checkInverted(ctx, tuple, c, restDepth)))
case *ast.SubjectEqualsObject:
checks = append(checks, checkgroup.WithEdge(checkgroup.Edge{
Tuple: *tuple,
Type: ketoapi.TreeNodeLeaf,
}, e.checkSubjectEqualsObject(ctx, tuple, restDepth)))
default:
return checkNotImplemented
}
}
return func(ctx context.Context, resultCh chan<- checkgroup.Result) {
resultCh <- op(ctx, checks)
}
}
func (e *Engine) checkInverted(
ctx context.Context,
tuple *relationTuple,
inverted *ast.InvertResult,
restDepth int,
) checkgroup.CheckFunc {
if restDepth < 0 {
e.d.Logger().Debug("reached max-depth, therefore this query will not be further expanded")
return checkgroup.UnknownMemberFunc
}
e.d.Logger().
WithField("request", tuple.String()).
Trace("invert check")
var check checkgroup.CheckFunc
switch c := inverted.Child.(type) {
case *ast.TupleToSubjectSet:
check = checkgroup.WithEdge(checkgroup.Edge{
Tuple: *tuple,
Type: ketoapi.TreeNodeTupleToSubjectSet,
}, e.checkTupleToSubjectSet(tuple, c, restDepth))
case *ast.ComputedSubjectSet:
check = checkgroup.WithEdge(checkgroup.Edge{
Tuple: *tuple,
Type: ketoapi.TreeNodeComputedSubjectSet,
}, e.checkComputedSubjectSet(ctx, tuple, c, restDepth))
case *ast.SubjectSetRewrite:
check = checkgroup.WithEdge(checkgroup.Edge{
Tuple: *tuple,
Type: toTreeNodeType(c.Operation),
}, e.checkSubjectSetRewrite(ctx, tuple, c, restDepth))
case *ast.InvertResult:
check = checkgroup.WithEdge(checkgroup.Edge{
Tuple: *tuple,
Type: ketoapi.TreeNodeNot,
}, e.checkInverted(ctx, tuple, c, restDepth))
case *ast.SubjectEqualsObject:
check = checkgroup.WithEdge(checkgroup.Edge{
Tuple: *tuple,
Type: ketoapi.TreeNodeLeaf,
}, e.checkSubjectEqualsObject(ctx, tuple, restDepth))
default:
return checkNotImplemented
}
return func(ctx context.Context, resultCh chan<- checkgroup.Result) {
innerCh := make(chan checkgroup.Result)
go check(ctx, innerCh)
select {
case result := <-innerCh:
// invert result here
switch result.Membership {
case checkgroup.IsMember:
result.Membership = checkgroup.NotMember
case checkgroup.NotMember:
result.Membership = checkgroup.IsMember
}
resultCh <- result
case <-ctx.Done():
resultCh <- checkgroup.Result{Err: errors.WithStack(ctx.Err())}
}
}
}
// checkSubjectEqualsObject verifies that the subject and object are the same.
//
// Checks that the subject and object refer to the same entity. The check
// is performed by creating a subject from the object based on what the tuple subject type is.
// If the tuple subject is a SubjectSet, the subject's Namespace is used with the object. If the
// tuple subject is a SubjectID, the object's ID is used as a SubjectID.
// The object-subject and tuple subject are compared using Subject.Equals. This was added to support
// `this == ctx.subject` for identity permission cases. See https://github.com/ory/keto/issues/1204
func (e *Engine) checkSubjectEqualsObject(
_ context.Context,
r *relationTuple,
restDepth int,
) checkgroup.CheckFunc {
if restDepth < 0 {
e.d.Logger().Debug("reached max-depth, therefore this query will not be further expanded")
return checkgroup.UnknownMemberFunc
}
e.d.Logger().
WithField("request", r.String()).
Trace("check subject equals object")
var objAsSubj relationtuple.Subject
switch r.Subject.(type) {
case *relationtuple.SubjectSet:
objAsSubj = &relationtuple.SubjectSet{
Namespace: r.Namespace,
Object: r.Object,
}
case *relationtuple.SubjectID:
objAsSubj = &relationtuple.SubjectID{
ID: r.Object,
}
default:
return checkgroup.UnknownMemberFunc
}
if r.Subject.Equals(objAsSubj) {
return checkgroup.IsMemberFunc
}
return checkgroup.NotMemberFunc
}
// checkComputedSubjectSet rewrites the relation tuple to use the subject-set relation
// instead of the relation from the tuple.
//
// A relation tuple n:obj#original_rel@user is rewritten to
// n:obj#subject-set@user, where the 'subject-set' relation is taken from the
// subjectSet.Relation.
func (e *Engine) checkComputedSubjectSet(
ctx context.Context,
r *relationTuple,
subjectSet *ast.ComputedSubjectSet,
restDepth int,
) checkgroup.CheckFunc {
if restDepth < 0 {
e.d.Logger().Debug("reached max-depth, therefore this query will not be further expanded")
return checkgroup.UnknownMemberFunc
}
e.d.Logger().
WithField("request", r.String()).
WithField("computed subjectSet relation", subjectSet.Relation).
Trace("check computed subjectSet")
return e.checkIsAllowed(ctx, &relationTuple{
Namespace: r.Namespace,
Object: r.Object,
Relation: subjectSet.Relation,
Subject: r.Subject,
}, restDepth, false)
}
// checkTupleToSubjectSet rewrites the relation tuple to use the subject-set relation.
//
// Given a relation tuple like docs:readme#editor@user, and a tuple-to-subject-set
// rewrite with the relation "parent" and the computed subject-set relation
// "owner", the following checks will be performed:
//
// - query for all tuples like docs:readme#parent@??? to get a list of subjects
// that have the parent relation on docs:readme
//
// * For each matching subject, then check if subject#owner@user.
func (e *Engine) checkTupleToSubjectSet(
tuple *relationTuple,
subjectSet *ast.TupleToSubjectSet,
restDepth int,
) checkgroup.CheckFunc {
if restDepth < 0 {
e.d.Logger().Debug("reached max-depth, therefore this query will not be further expanded")
return checkgroup.UnknownMemberFunc
}
e.d.Logger().
WithField("request", tuple.String()).
WithField("tuple to subject-set relation", subjectSet.Relation).
WithField("tuple to subject-set computed", subjectSet.ComputedSubjectSetRelation).
Trace("check tuple to subjectSet")
return func(ctx context.Context, resultCh chan<- checkgroup.Result) {
var (
prevPage, nextPage string
tuples []*relationTuple
err error
)
g := checkgroup.New(ctx)
for nextPage = "x"; nextPage != "" && !g.Done(); prevPage = nextPage {
tuples, nextPage, err = e.d.RelationTupleManager().GetRelationTuples(
ctx,
&query{
Namespace: &tuple.Namespace,
Object: &tuple.Object,
Relation: &subjectSet.Relation,
},
x.WithToken(prevPage))
if err != nil {
g.Add(checkgroup.ErrorFunc(err))
return
}
for _, t := range tuples {
if subSet, ok := t.Subject.(*relationtuple.SubjectSet); ok {
g.Add(e.checkIsAllowed(ctx, &relationTuple{
Namespace: subSet.Namespace,
Object: subSet.Object,
Relation: subjectSet.ComputedSubjectSetRelation,
Subject: tuple.Subject,
}, restDepth-1, false))
}
}
}
resultCh <- g.Result()
}
}