-
Notifications
You must be signed in to change notification settings - Fork 246
/
reachabilitygraph.go
346 lines (289 loc) · 12.2 KB
/
reachabilitygraph.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
package typesystem
import (
"context"
"fmt"
"sort"
"strconv"
"sync"
"github.com/cespare/xxhash/v2"
"golang.org/x/exp/maps"
core "github.com/authzed/spicedb/pkg/proto/core/v1"
"github.com/authzed/spicedb/pkg/tuple"
)
// ReachabilityGraph is a helper struct that provides an easy way to determine all entrypoints
// for a subject of a particular type into a schema, for the purpose of walking from the subject
// to a specific resource relation.
type ReachabilityGraph struct {
ts *TypeSystem
cachedGraphs sync.Map
hasOptimizedEntrypointCache sync.Map
}
// ReachabilityEntrypoint is an entrypoint into the reachability graph for a subject of particular
// type.
type ReachabilityEntrypoint struct {
re *core.ReachabilityEntrypoint
parentRelation *core.RelationReference
}
// Hash returns a hash representing the data in the entrypoint, for comparison to other entrypoints.
// This is ONLY stable within a single version of SpiceDB and should NEVER be stored for later
// comparison outside of the process.
func (re ReachabilityEntrypoint) Hash() (uint64, error) {
size := re.re.SizeVT()
if re.parentRelation != nil {
size += re.parentRelation.SizeVT()
}
hashData := make([]byte, 0, size)
data, err := re.re.MarshalVT()
if err != nil {
return 0, err
}
hashData = append(hashData, data...)
if re.parentRelation != nil {
data, err := re.parentRelation.MarshalVT()
if err != nil {
return 0, err
}
hashData = append(hashData, data...)
}
return xxhash.Sum64(hashData), nil
}
// EntrypointKind is the kind of the entrypoint.
func (re ReachabilityEntrypoint) EntrypointKind() core.ReachabilityEntrypoint_ReachabilityEntrypointKind {
return re.re.Kind
}
// TuplesetRelation returns the tupleset relation of the TTU, if a TUPLESET_TO_USERSET_ENTRYPOINT.
func (re ReachabilityEntrypoint) TuplesetRelation() (string, error) {
if re.EntrypointKind() != core.ReachabilityEntrypoint_TUPLESET_TO_USERSET_ENTRYPOINT {
return "", fmt.Errorf("cannot call TupleToUserset for kind %v", re.EntrypointKind())
}
return re.re.TuplesetRelation, nil
}
// DirectRelation is the relation that this entrypoint represents, if a RELATION_ENTRYPOINT.
func (re ReachabilityEntrypoint) DirectRelation() (*core.RelationReference, error) {
if re.EntrypointKind() != core.ReachabilityEntrypoint_RELATION_ENTRYPOINT {
return nil, fmt.Errorf("cannot call DirectRelation for kind %v", re.EntrypointKind())
}
return re.re.TargetRelation, nil
}
// ContainingRelationOrPermission is the relation or permission containing this entrypoint.
func (re ReachabilityEntrypoint) ContainingRelationOrPermission() *core.RelationReference {
return re.parentRelation
}
// IsDirectResult returns whether the entrypoint, when evaluated, becomes a direct result of
// the parent relation/permission. A direct result only exists if the entrypoint is not contained
// under an intersection or exclusion, which makes the entrypoint's object merely conditionally
// reachable.
func (re ReachabilityEntrypoint) IsDirectResult() bool {
return re.re.ResultStatus == core.ReachabilityEntrypoint_DIRECT_OPERATION_RESULT
}
func (re ReachabilityEntrypoint) String() string {
return re.MustDebugString()
}
func (re ReachabilityEntrypoint) MustDebugString() string {
switch re.EntrypointKind() {
case core.ReachabilityEntrypoint_RELATION_ENTRYPOINT:
return fmt.Sprintf("relation-entrypoint: %s#%s", re.re.TargetRelation.Namespace, re.re.TargetRelation.Relation)
case core.ReachabilityEntrypoint_TUPLESET_TO_USERSET_ENTRYPOINT:
return fmt.Sprintf("ttu-entrypoint: %s#%s | %s | %s#%s", re.parentRelation.Namespace, re.parentRelation.Relation, re.re.TuplesetRelation, re.re.TargetRelation.Namespace, re.re.TargetRelation.Relation)
case core.ReachabilityEntrypoint_COMPUTED_USERSET_ENTRYPOINT:
return fmt.Sprintf("computed-entrypoint: %s#%s", re.re.TargetRelation.Namespace, re.re.TargetRelation.Relation)
default:
panic("unknown relation entrypoint kind")
}
}
// ReachabilityGraphFor returns a reachability graph for the given namespace.
func ReachabilityGraphFor(ts *ValidatedNamespaceTypeSystem) *ReachabilityGraph {
return &ReachabilityGraph{ts.TypeSystem, sync.Map{}, sync.Map{}}
}
// RelationsEncountered returns all relations encountered while walking, starting
// at the given subject type and walking to the given resource type.
func (rg *ReachabilityGraph) RelationsEncountered(
ctx context.Context,
subjectType *core.RelationReference,
resourceType *core.RelationReference,
) ([]*core.RelationReference, error) {
_, relationNames, err := rg.entrypointsForSubjectToResource(ctx, subjectType, resourceType, reachabilityFull, entrypointLookupFindAll)
if err != nil {
return nil, err
}
relationRefs := make([]*core.RelationReference, 0, len(relationNames))
for _, relationName := range relationNames {
namespace, relation := tuple.MustSplitRelRef(relationName)
relationRefs = append(relationRefs, &core.RelationReference{
Namespace: namespace,
Relation: relation,
})
}
return relationRefs, nil
}
// AllEntrypointsForSubjectToResource returns the entrypoints into the reachability graph, starting
// at the given subject type and walking to the given resource type.
func (rg *ReachabilityGraph) AllEntrypointsForSubjectToResource(
ctx context.Context,
subjectType *core.RelationReference,
resourceType *core.RelationReference,
) ([]ReachabilityEntrypoint, error) {
entrypoints, _, err := rg.entrypointsForSubjectToResource(ctx, subjectType, resourceType, reachabilityFull, entrypointLookupFindAll)
return entrypoints, err
}
// OptimizedEntrypointsForSubjectToResource returns the *optimized* set of entrypoints into the
// reachability graph, starting at the given subject type and walking to the given resource type.
//
// The optimized set will skip branches on intersections and exclusions in an attempt to minimize
// the number of entrypoints.
func (rg *ReachabilityGraph) OptimizedEntrypointsForSubjectToResource(
ctx context.Context,
subjectType *core.RelationReference,
resourceType *core.RelationReference,
) ([]ReachabilityEntrypoint, error) {
entrypoints, _, err := rg.entrypointsForSubjectToResource(ctx, subjectType, resourceType, reachabilityOptimized, entrypointLookupFindAll)
return entrypoints, err
}
// HasOptimizedEntrypointsForSubjectToResource returns whether there exists any *optimized*
// entrypoints into the reachability graph, starting at the given subject type and walking
// to the given resource type.
//
// The optimized set will skip branches on intersections and exclusions in an attempt to minimize
// the number of entrypoints.
func (rg *ReachabilityGraph) HasOptimizedEntrypointsForSubjectToResource(
ctx context.Context,
subjectType *core.RelationReference,
resourceType *core.RelationReference,
) (bool, error) {
cacheKey := tuple.StringRR(subjectType) + "=>" + tuple.StringRR(resourceType)
if result, ok := rg.hasOptimizedEntrypointCache.Load(cacheKey); ok {
return result.(bool), nil
}
// TODO(jzelinskie): measure to see if it's worth singleflighting this
found, _, err := rg.entrypointsForSubjectToResource(ctx, subjectType, resourceType, reachabilityOptimized, entrypointLookupFindOne)
if err != nil {
return false, err
}
result := len(found) > 0
rg.hasOptimizedEntrypointCache.Store(cacheKey, result)
return result, nil
}
type entrypointLookupOption int
const (
entrypointLookupFindAll entrypointLookupOption = iota
entrypointLookupFindOne
)
func (rg *ReachabilityGraph) entrypointsForSubjectToResource(
ctx context.Context,
subjectType *core.RelationReference,
resourceType *core.RelationReference,
reachabilityOption reachabilityOption,
entrypointLookupOption entrypointLookupOption,
) ([]ReachabilityEntrypoint, []string, error) {
if resourceType.Namespace != rg.ts.nsDef.Name {
return nil, nil, fmt.Errorf("gave mismatching namespace name for resource type to reachability graph")
}
collected := &[]ReachabilityEntrypoint{}
encounteredRelations := map[string]struct{}{}
err := rg.collectEntrypoints(ctx, subjectType, resourceType, collected, encounteredRelations, reachabilityOption, entrypointLookupOption)
if err != nil {
return nil, maps.Keys(encounteredRelations), err
}
collectedEntrypoints := *collected
// Deduplicate any entrypoints found. An example that can cause a duplicate is a relation which references
// the same subject type multiple times due to caveats:
//
// relation somerel: user | user with somecaveat
//
// This will produce two entrypoints (one per user reference), but as entrypoints themselves are not caveated,
// one is spurious.
entrypointMap := make(map[uint64]ReachabilityEntrypoint, len(collectedEntrypoints))
uniqueEntrypoints := make([]ReachabilityEntrypoint, 0, len(collectedEntrypoints))
for _, entrypoint := range collectedEntrypoints {
hash, err := entrypoint.Hash()
if err != nil {
return nil, maps.Keys(encounteredRelations), err
}
if _, ok := entrypointMap[hash]; !ok {
entrypointMap[hash] = entrypoint
uniqueEntrypoints = append(uniqueEntrypoints, entrypoint)
}
}
return uniqueEntrypoints, maps.Keys(encounteredRelations), nil
}
func (rg *ReachabilityGraph) getOrBuildGraph(ctx context.Context, resourceType *core.RelationReference, reachabilityOption reachabilityOption) (*core.ReachabilityGraph, error) {
// Check the cache.
// TODO(jschorr): Move this to a global cache.
cacheKey := tuple.StringRR(resourceType) + "-" + strconv.Itoa(int(reachabilityOption))
if cached, ok := rg.cachedGraphs.Load(cacheKey); ok {
return cached.(*core.ReachabilityGraph), nil
}
// Load the type system for the target resource relation.
namespace, err := rg.ts.resolver.LookupNamespace(ctx, resourceType.Namespace)
if err != nil {
return nil, err
}
rts, err := NewNamespaceTypeSystem(namespace, rg.ts.resolver)
if err != nil {
return nil, err
}
rrg, err := computeReachability(ctx, rts, resourceType.Relation, reachabilityOption)
if err != nil {
return nil, err
}
rg.cachedGraphs.Store(cacheKey, rrg)
return rrg, err
}
func (rg *ReachabilityGraph) collectEntrypoints(
ctx context.Context,
subjectType *core.RelationReference,
resourceType *core.RelationReference,
collected *[]ReachabilityEntrypoint,
encounteredRelations map[string]struct{},
reachabilityOption reachabilityOption,
entrypointLookupOption entrypointLookupOption,
) error {
// Ensure that we only process each relation once.
key := tuple.JoinRelRef(resourceType.Namespace, resourceType.Relation)
if _, ok := encounteredRelations[key]; ok {
return nil
}
encounteredRelations[key] = struct{}{}
rrg, err := rg.getOrBuildGraph(ctx, resourceType, reachabilityOption)
if err != nil {
return err
}
// Add subject type entrypoints.
subjectTypeEntrypoints, ok := rrg.EntrypointsBySubjectType[subjectType.Namespace]
if ok {
addEntrypoints(subjectTypeEntrypoints, resourceType, collected)
}
if entrypointLookupOption == entrypointLookupFindOne && len(*collected) > 0 {
return nil
}
// Add subject relation entrypoints.
subjectRelationEntrypoints, ok := rrg.EntrypointsBySubjectRelation[tuple.JoinRelRef(subjectType.Namespace, subjectType.Relation)]
if ok {
addEntrypoints(subjectRelationEntrypoints, resourceType, collected)
}
if entrypointLookupOption == entrypointLookupFindOne && len(*collected) > 0 {
return nil
}
// Sort the keys to ensure a stable graph is produced.
keys := maps.Keys(rrg.EntrypointsBySubjectRelation)
sort.Strings(keys)
// Recursively collect over any reachability graphs for subjects with non-ellipsis relations.
for _, entrypointSetKey := range keys {
entrypointSet := rrg.EntrypointsBySubjectRelation[entrypointSetKey]
if entrypointSet.SubjectRelation != nil && entrypointSet.SubjectRelation.Relation != tuple.Ellipsis {
err := rg.collectEntrypoints(ctx, subjectType, entrypointSet.SubjectRelation, collected, encounteredRelations, reachabilityOption, entrypointLookupOption)
if err != nil {
return err
}
if entrypointLookupOption == entrypointLookupFindOne && len(*collected) > 0 {
return nil
}
}
}
return nil
}
func addEntrypoints(entrypoints *core.ReachabilityEntrypoints, parentRelation *core.RelationReference, collected *[]ReachabilityEntrypoint) {
for _, entrypoint := range entrypoints.Entrypoints {
*collected = append(*collected, ReachabilityEntrypoint{entrypoint, parentRelation})
}
}