-
Notifications
You must be signed in to change notification settings - Fork 126
/
schemaBasedEntityFilter.go
299 lines (271 loc) · 12.2 KB
/
schemaBasedEntityFilter.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
package engines
import (
"context"
"errors"
"golang.org/x/sync/errgroup"
"github.com/Permify/permify/internal/schema"
"github.com/Permify/permify/internal/storage"
storageContext "github.com/Permify/permify/internal/storage/context"
"github.com/Permify/permify/pkg/database"
base "github.com/Permify/permify/pkg/pb/base/v1"
"github.com/Permify/permify/pkg/tuple"
)
// SchemaBasedEntityFilter is a struct that performs permission checks on a set of entities
type SchemaBasedEntityFilter struct {
// dataReader is responsible for reading relationship information
dataReader storage.DataReader
schema *base.SchemaDefinition
}
// NewSchemaBasedEntityFilter creates a new EntityFilter engine
func NewSchemaBasedEntityFilter(dataReader storage.DataReader, sch *base.SchemaDefinition) *SchemaBasedEntityFilter {
return &SchemaBasedEntityFilter{
dataReader: dataReader,
schema: sch,
}
}
// EntityFilter is a method of the EntityFilterEngine struct. It executes a permission request for linked entities.
func (engine *SchemaBasedEntityFilter) EntityFilter(
ctx context.Context, // A context used for tracing and cancellation.
request *base.PermissionEntityFilterRequest, // A permission request for linked entities.
visits *ERMap, // A map that keeps track of visited entities to avoid infinite loops.
publisher *BulkEntityPublisher, // A custom publisher that publishes results in bulk.
) (err error) { // Returns an error if one occurs during execution.
// Check if direct result
if request.GetEntityReference().GetType() == request.GetSubject().GetType() && request.GetEntityReference().GetRelation() == request.GetSubject().GetRelation() {
found := &base.Entity{
Type: request.GetSubject().GetType(),
Id: request.GetSubject().GetId(),
}
// If the entity reference is the same as the subject, publish the result directly and return.
publisher.Publish(found, &base.PermissionCheckRequestMetadata{
SnapToken: request.GetMetadata().GetSnapToken(),
SchemaVersion: request.GetMetadata().GetSchemaVersion(),
Depth: request.GetMetadata().GetDepth(),
}, request.GetContext(), base.CheckResult_CHECK_RESULT_UNSPECIFIED)
}
// Retrieve linked entrances
cn := schema.NewLinkedGraph(engine.schema) // Create a new linked graph from the schema definition.
var entrances []*schema.LinkedEntrance
entrances, err = cn.RelationshipLinkedEntrances(
&base.RelationReference{
Type: request.GetEntityReference().GetType(),
Relation: request.GetEntityReference().GetRelation(),
},
&base.RelationReference{
Type: request.GetSubject().GetType(),
Relation: request.GetSubject().GetRelation(),
},
) // Retrieve the linked entrances between the entity reference and subject.
// Create a new context for executing goroutines and a cancel function.
cctx, cancel := context.WithCancel(ctx)
defer cancel()
// Create a new errgroup and a new context that inherits the original context.
g, cont := errgroup.WithContext(cctx)
// Loop over each linked entrance.
for _, entrance := range entrances {
// Switch on the kind of linked entrance.
switch entrance.LinkedEntranceKind() {
case schema.RelationLinkedEntrance: // If the linked entrance is a relation entrance.
err = engine.relationEntrance(cont, request, entrance, visits, g, publisher) // Call the relation entrance method.
if err != nil {
return err
}
case schema.ComputedUserSetLinkedEntrance: // If the linked entrance is a computed user set entrance.
err = engine.l(ctx, request, &base.EntityAndRelation{ // Call the run method with a new entity and relation.
Entity: &base.Entity{
Type: entrance.TargetEntrance.GetType(),
Id: request.GetSubject().GetId(),
},
Relation: entrance.TargetEntrance.GetRelation(),
}, visits, g, publisher)
if err != nil {
return err
}
case schema.TupleToUserSetLinkedEntrance: // If the linked entrance is a tuple to user set entrance.
err = engine.tupleToUserSetEntrance(cont, request, entrance, visits, g, publisher) // Call the tuple to user set entrance method.
if err != nil {
return err
}
default:
return errors.New("unknown linked entrance type") // Return an error if the linked entrance is of an unknown type.
}
}
return g.Wait() // Wait for all goroutines in the errgroup to complete and return any errors that occur.
}
// relationEntrance is a method of the EntityFilterEngine struct. It handles relation entrances.
func (engine *SchemaBasedEntityFilter) relationEntrance(
ctx context.Context, // A context used for tracing and cancellation.
request *base.PermissionEntityFilterRequest, // A permission request for linked entities.
entrance *schema.LinkedEntrance, // A linked entrance.
visits *ERMap, // A map that keeps track of visited entities to avoid infinite loops.
g *errgroup.Group, // An errgroup used for executing goroutines.
publisher *BulkEntityPublisher, // A custom publisher that publishes results in bulk.
) error { // Returns an error if one occurs during execution.
// Define a TupleFilter. This specifies which tuples we're interested in.
// We want tuples that match the entity type and ID from the request, and have a specific relation.
filter := &base.TupleFilter{
Entity: &base.EntityFilter{
Type: entrance.TargetEntrance.GetType(),
Ids: []string{},
},
Relation: entrance.TargetEntrance.GetRelation(),
Subject: &base.SubjectFilter{
Type: request.GetSubject().GetType(),
Ids: []string{request.GetSubject().GetId()},
Relation: request.GetSubject().GetRelation(),
},
}
// Use the filter to query for relationships in the given context.
// NewContextualRelationships() creates a ContextualRelationships instance from tuples in the request.
// QueryRelationships() then uses the filter to find and return matching relationships.
cti, err := storageContext.NewContextualTuples(request.GetContext().GetTuples()...).QueryRelationships(filter)
if err != nil {
return err
}
// Query the relationships for the entity in the request.
// TupleFilter helps in filtering out the relationships for a specific entity and a permission.
rit, err := engine.dataReader.QueryRelationships(ctx, request.GetTenantId(), filter, request.GetMetadata().GetSnapToken())
if err != nil {
return err
}
// Create a new UniqueTupleIterator from the two TupleIterators.
// NewUniqueTupleIterator() ensures that the iterator only returns unique tuples.
it := database.NewUniqueTupleIterator(rit, cti)
for it.HasNext() { // Loop over each relationship.
// Get the next tuple's subject.
current, ok := it.GetNext()
if !ok {
break
}
g.Go(func() error {
return engine.l(ctx, request, &base.EntityAndRelation{ // Call the run method with a new entity and relation.
Entity: &base.Entity{
Type: current.GetEntity().GetType(),
Id: current.GetEntity().GetId(),
},
Relation: current.GetRelation(),
}, visits, g, publisher)
})
}
return nil
}
// tupleToUserSetEntrance is a method of the EntityFilterEngine struct. It handles tuple to user set entrances.
func (engine *SchemaBasedEntityFilter) tupleToUserSetEntrance(
// A context used for tracing and cancellation.
ctx context.Context,
// A permission request for linked entities.
request *base.PermissionEntityFilterRequest,
// A linked entrance.
entrance *schema.LinkedEntrance,
// A map that keeps track of visited entities to avoid infinite loops.
visits *ERMap,
// An errgroup used for executing goroutines.
g *errgroup.Group,
// A custom publisher that publishes results in bulk.
publisher *BulkEntityPublisher,
) error { // Returns an error if one occurs during execution.
for _, relation := range []string{"", tuple.ELLIPSIS} {
// Define a TupleFilter. This specifies which tuples we're interested in.
// We want tuples that match the entity type and ID from the request, and have a specific relation.
filter := &base.TupleFilter{
Entity: &base.EntityFilter{
Type: entrance.TargetEntrance.GetType(),
Ids: []string{},
},
Relation: entrance.TupleSetRelation, // Query for relationships that match the tuple set relation.
Subject: &base.SubjectFilter{
Type: request.GetSubject().GetType(),
Ids: []string{request.GetSubject().GetId()},
Relation: relation,
},
}
// Use the filter to query for relationships in the given context.
// NewContextualRelationships() creates a ContextualRelationships instance from tuples in the request.
// QueryRelationships() then uses the filter to find and return matching relationships.
cti, err := storageContext.NewContextualTuples(request.GetContext().GetTuples()...).QueryRelationships(filter)
if err != nil {
return err
}
// Use the filter to query for relationships in the database.
// relationshipReader.QueryRelationships() uses the filter to find and return matching relationships.
rit, err := engine.dataReader.QueryRelationships(ctx, request.GetTenantId(), filter, request.GetMetadata().GetSnapToken())
if err != nil {
return err
}
// Create a new UniqueTupleIterator from the two TupleIterators.
// NewUniqueTupleIterator() ensures that the iterator only returns unique tuples.
it := database.NewUniqueTupleIterator(rit, cti)
for it.HasNext() { // Loop over each relationship.
// Get the next tuple's subject.
current, ok := it.GetNext()
if !ok {
break
}
g.Go(func() error {
return engine.l(ctx, request, &base.EntityAndRelation{ // Call the run method with a new entity and relation.
Entity: &base.Entity{
Type: entrance.TargetEntrance.GetType(),
Id: current.GetEntity().GetId(),
},
Relation: entrance.TargetEntrance.GetRelation(),
}, visits, g, publisher)
})
}
}
return nil
}
// run is a method of the EntityFilterEngine struct. It executes the linked entity engine for a given request.
func (engine *SchemaBasedEntityFilter) l(
ctx context.Context, // A context used for tracing and cancellation.
request *base.PermissionEntityFilterRequest, // A permission request for linked entities.
found *base.EntityAndRelation, // An entity and relation that was previously found.
visits *ERMap, // A map that keeps track of visited entities to avoid infinite loops.
g *errgroup.Group, // An errgroup used for executing goroutines.
publisher *BulkEntityPublisher, // A custom publisher that publishes results in bulk.
) error { // Returns an error if one occurs during execution.
if !visits.Add(found.GetEntity(), found.GetRelation()) { // If the entity and relation has already been visited.
return nil
}
var err error
// Retrieve linked entrances
cn := schema.NewLinkedGraph(engine.schema)
var entrances []*schema.LinkedEntrance
entrances, err = cn.RelationshipLinkedEntrances(
&base.RelationReference{
Type: request.GetEntityReference().GetType(),
Relation: request.GetEntityReference().GetRelation(),
},
&base.RelationReference{
Type: request.GetSubject().GetType(),
Relation: request.GetSubject().GetRelation(),
},
) // Retrieve the linked entrances for the request.
if err != nil {
return err
}
if entrances == nil { // If there are no linked entrances for the request.
if found.GetEntity().GetType() == request.GetEntityReference().GetType() && found.GetRelation() == request.GetEntityReference().GetRelation() { // Check if the found entity matches the requested entity reference.
publisher.Publish(found.GetEntity(), &base.PermissionCheckRequestMetadata{ // Publish the found entity with the permission check metadata.
SnapToken: request.GetMetadata().GetSnapToken(),
SchemaVersion: request.GetMetadata().GetSchemaVersion(),
Depth: request.GetMetadata().GetDepth(),
}, request.GetContext(), base.CheckResult_CHECK_RESULT_UNSPECIFIED)
return nil
}
return nil // Otherwise, return without publishing any results.
}
g.Go(func() error {
return engine.EntityFilter(ctx, &base.PermissionEntityFilterRequest{ // Call the Run method recursively with a new permission request.
TenantId: request.GetTenantId(),
EntityReference: request.GetEntityReference(),
Subject: &base.Subject{
Type: found.GetEntity().GetType(),
Id: found.GetEntity().GetId(),
Relation: found.GetRelation(),
},
Metadata: request.GetMetadata(),
Context: request.GetContext(),
}, visits, publisher)
})
return nil
}