-
Notifications
You must be signed in to change notification settings - Fork 246
/
graph.go
428 lines (362 loc) · 14.4 KB
/
graph.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
package graph
import (
"context"
"errors"
"fmt"
"github.com/rs/zerolog"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/authzed/spicedb/internal/dispatch"
"github.com/authzed/spicedb/internal/graph"
log "github.com/authzed/spicedb/internal/logging"
datastoremw "github.com/authzed/spicedb/internal/middleware/datastore"
"github.com/authzed/spicedb/pkg/datastore"
core "github.com/authzed/spicedb/pkg/proto/core/v1"
v1 "github.com/authzed/spicedb/pkg/proto/dispatch/v1"
"github.com/authzed/spicedb/pkg/tuple"
)
const errDispatch = "error dispatching request: %w"
var tracer = otel.Tracer("spicedb/internal/dispatch/local")
// ConcurrencyLimits defines per-dispatch-type concurrency limits.
//
//go:generate go run github.com/ecordell/optgen -output zz_generated.options.go . ConcurrencyLimits
type ConcurrencyLimits struct {
Check uint16 `debugmap:"visible"`
ReachableResources uint16 `debugmap:"visible"`
LookupResources uint16 `debugmap:"visible"`
LookupSubjects uint16 `debugmap:"visible"`
}
const defaultConcurrencyLimit = 50
// WithOverallDefaultLimit sets the overall default limit for any unspecified limits
// and returns a new struct.
func (cl ConcurrencyLimits) WithOverallDefaultLimit(overallDefaultLimit uint16) ConcurrencyLimits {
return limitsOrDefaults(cl, overallDefaultLimit)
}
func (cl ConcurrencyLimits) MarshalZerologObject(e *zerolog.Event) {
e.Uint16("concurrency-limit-check-permission", cl.Check)
e.Uint16("concurrency-limit-lookup-resources", cl.LookupResources)
e.Uint16("concurrency-limit-lookup-subjects", cl.LookupSubjects)
e.Uint16("concurrency-limit-reachable-resources", cl.ReachableResources)
}
func limitsOrDefaults(limits ConcurrencyLimits, overallDefaultLimit uint16) ConcurrencyLimits {
limits.Check = limitOrDefault(limits.Check, overallDefaultLimit)
limits.LookupResources = limitOrDefault(limits.LookupResources, overallDefaultLimit)
limits.LookupSubjects = limitOrDefault(limits.LookupSubjects, overallDefaultLimit)
limits.ReachableResources = limitOrDefault(limits.ReachableResources, overallDefaultLimit)
return limits
}
func limitOrDefault(limit uint16, defaultLimit uint16) uint16 {
if limit <= 0 {
return defaultLimit
}
return limit
}
// SharedConcurrencyLimits returns a ConcurrencyLimits struct with the limit
// set to that provided for each operation.
func SharedConcurrencyLimits(concurrencyLimit uint16) ConcurrencyLimits {
return ConcurrencyLimits{
Check: concurrencyLimit,
ReachableResources: concurrencyLimit,
LookupResources: concurrencyLimit,
LookupSubjects: concurrencyLimit,
}
}
// NewLocalOnlyDispatcher creates a dispatcher that consults with the graph to formulate a response.
func NewLocalOnlyDispatcher(concurrencyLimit uint16) dispatch.Dispatcher {
return NewLocalOnlyDispatcherWithLimits(SharedConcurrencyLimits(concurrencyLimit))
}
// NewLocalOnlyDispatcherWithLimits creates a dispatcher thatg consults with the graph to formulate a response
// and has the defined concurrency limits per dispatch type.
func NewLocalOnlyDispatcherWithLimits(concurrencyLimits ConcurrencyLimits) dispatch.Dispatcher {
d := &localDispatcher{}
concurrencyLimits = limitsOrDefaults(concurrencyLimits, defaultConcurrencyLimit)
d.checker = graph.NewConcurrentChecker(d, concurrencyLimits.Check)
d.expander = graph.NewConcurrentExpander(d)
d.reachableResourcesHandler = graph.NewCursoredReachableResources(d, concurrencyLimits.ReachableResources)
d.lookupResourcesHandler = graph.NewCursoredLookupResources(d, d, concurrencyLimits.LookupResources)
d.lookupSubjectsHandler = graph.NewConcurrentLookupSubjects(d, concurrencyLimits.LookupSubjects)
return d
}
// NewDispatcher creates a dispatcher that consults with the graph and redispatches subproblems to
// the provided redispatcher.
func NewDispatcher(redispatcher dispatch.Dispatcher, concurrencyLimits ConcurrencyLimits) dispatch.Dispatcher {
concurrencyLimits = limitsOrDefaults(concurrencyLimits, defaultConcurrencyLimit)
checker := graph.NewConcurrentChecker(redispatcher, concurrencyLimits.Check)
expander := graph.NewConcurrentExpander(redispatcher)
reachableResourcesHandler := graph.NewCursoredReachableResources(redispatcher, concurrencyLimits.ReachableResources)
lookupResourcesHandler := graph.NewCursoredLookupResources(redispatcher, redispatcher, concurrencyLimits.LookupResources)
lookupSubjectsHandler := graph.NewConcurrentLookupSubjects(redispatcher, concurrencyLimits.LookupSubjects)
return &localDispatcher{
checker: checker,
expander: expander,
reachableResourcesHandler: reachableResourcesHandler,
lookupResourcesHandler: lookupResourcesHandler,
lookupSubjectsHandler: lookupSubjectsHandler,
}
}
type localDispatcher struct {
checker *graph.ConcurrentChecker
expander *graph.ConcurrentExpander
reachableResourcesHandler *graph.CursoredReachableResources
lookupResourcesHandler *graph.CursoredLookupResources
lookupSubjectsHandler *graph.ConcurrentLookupSubjects
}
func (ld *localDispatcher) loadNamespace(ctx context.Context, nsName string, revision datastore.Revision) (*core.NamespaceDefinition, error) {
ds := datastoremw.MustFromContext(ctx).SnapshotReader(revision)
// Load namespace and relation from the datastore
ns, _, err := ds.ReadNamespaceByName(ctx, nsName)
if err != nil {
return nil, rewriteNamespaceError(err)
}
return ns, err
}
func (ld *localDispatcher) parseRevision(ctx context.Context, s string) (datastore.Revision, error) {
ds := datastoremw.MustFromContext(ctx)
return ds.RevisionFromString(s)
}
func (ld *localDispatcher) lookupRelation(_ context.Context, ns *core.NamespaceDefinition, relationName string) (*core.Relation, error) {
var relation *core.Relation
for _, candidate := range ns.Relation {
if candidate.Name == relationName {
relation = candidate
break
}
}
if relation == nil {
return nil, NewRelationNotFoundErr(ns.Name, relationName)
}
return relation, nil
}
// DispatchCheck implements dispatch.Check interface
func (ld *localDispatcher) DispatchCheck(ctx context.Context, req *v1.DispatchCheckRequest) (*v1.DispatchCheckResponse, error) {
resourceType := tuple.StringRR(req.ResourceRelation)
spanName := "DispatchCheck → " + resourceType + "@" + req.Subject.Namespace + "#" + req.Subject.Relation
ctx, span := tracer.Start(ctx, spanName, trace.WithAttributes(
attribute.String("resource-type", resourceType),
attribute.StringSlice("resource-ids", req.ResourceIds),
attribute.String("subject", tuple.StringONR(req.Subject)),
))
defer span.End()
if err := dispatch.CheckDepth(ctx, req); err != nil {
if req.Debug != v1.DispatchCheckRequest_ENABLE_BASIC_DEBUGGING {
return &v1.DispatchCheckResponse{
Metadata: &v1.ResponseMeta{
DispatchCount: 0,
},
}, rewriteError(ctx, err)
}
// NOTE: we return debug information here to ensure tooling can see the cycle.
return &v1.DispatchCheckResponse{
Metadata: &v1.ResponseMeta{
DispatchCount: 0,
DebugInfo: &v1.DebugInformation{
Check: &v1.CheckDebugTrace{
Request: req,
},
},
},
}, rewriteError(ctx, err)
}
revision, err := ld.parseRevision(ctx, req.Metadata.AtRevision)
if err != nil {
return &v1.DispatchCheckResponse{Metadata: emptyMetadata}, rewriteError(ctx, err)
}
ns, err := ld.loadNamespace(ctx, req.ResourceRelation.Namespace, revision)
if err != nil {
return &v1.DispatchCheckResponse{Metadata: emptyMetadata}, rewriteError(ctx, err)
}
relation, err := ld.lookupRelation(ctx, ns, req.ResourceRelation.Relation)
if err != nil {
return &v1.DispatchCheckResponse{Metadata: emptyMetadata}, rewriteError(ctx, err)
}
// If the relation is aliasing another one and the subject does not have the same type as
// resource, load the aliased relation and dispatch to it. We cannot use the alias if the
// resource and subject types are the same because a check on the *exact same* resource and
// subject must pass, and we don't know how many intermediate steps may hit that case.
if relation.AliasingRelation != "" && req.ResourceRelation.Namespace != req.Subject.Namespace {
relation, err := ld.lookupRelation(ctx, ns, relation.AliasingRelation)
if err != nil {
return &v1.DispatchCheckResponse{Metadata: emptyMetadata}, rewriteError(ctx, err)
}
// Rewrite the request over the aliased relation.
validatedReq := graph.ValidatedCheckRequest{
DispatchCheckRequest: &v1.DispatchCheckRequest{
ResourceRelation: &core.RelationReference{
Namespace: req.ResourceRelation.Namespace,
Relation: relation.Name,
},
ResourceIds: req.ResourceIds,
Subject: req.Subject,
Metadata: req.Metadata,
Debug: req.Debug,
},
Revision: revision,
}
resp, err := ld.checker.Check(ctx, validatedReq, relation)
return resp, rewriteError(ctx, err)
}
resp, err := ld.checker.Check(ctx, graph.ValidatedCheckRequest{
DispatchCheckRequest: req,
Revision: revision,
}, relation)
return resp, rewriteError(ctx, err)
}
// DispatchExpand implements dispatch.Expand interface
func (ld *localDispatcher) DispatchExpand(ctx context.Context, req *v1.DispatchExpandRequest) (*v1.DispatchExpandResponse, error) {
ctx, span := tracer.Start(ctx, "DispatchExpand", trace.WithAttributes(
attribute.String("start", tuple.StringONR(req.ResourceAndRelation)),
))
defer span.End()
if err := dispatch.CheckDepth(ctx, req); err != nil {
return &v1.DispatchExpandResponse{Metadata: emptyMetadata}, err
}
revision, err := ld.parseRevision(ctx, req.Metadata.AtRevision)
if err != nil {
return &v1.DispatchExpandResponse{Metadata: emptyMetadata}, err
}
ns, err := ld.loadNamespace(ctx, req.ResourceAndRelation.Namespace, revision)
if err != nil {
return &v1.DispatchExpandResponse{Metadata: emptyMetadata}, err
}
relation, err := ld.lookupRelation(ctx, ns, req.ResourceAndRelation.Relation)
if err != nil {
return &v1.DispatchExpandResponse{Metadata: emptyMetadata}, err
}
return ld.expander.Expand(ctx, graph.ValidatedExpandRequest{
DispatchExpandRequest: req,
Revision: revision,
}, relation)
}
// DispatchReachableResources implements dispatch.ReachableResources interface
func (ld *localDispatcher) DispatchReachableResources(
req *v1.DispatchReachableResourcesRequest,
stream dispatch.ReachableResourcesStream,
) error {
resourceType := tuple.StringRR(req.ResourceRelation)
subjectRelation := tuple.StringRR(req.SubjectRelation)
spanName := "DispatchReachableResources → " + resourceType + "@" + subjectRelation
ctx, span := tracer.Start(stream.Context(), spanName, trace.WithAttributes(
attribute.String("resource-type", resourceType),
attribute.String("subject-type", subjectRelation),
attribute.StringSlice("subject-ids", req.SubjectIds),
))
defer span.End()
if err := dispatch.CheckDepth(ctx, req); err != nil {
return err
}
revision, err := ld.parseRevision(ctx, req.Metadata.AtRevision)
if err != nil {
return err
}
return ld.reachableResourcesHandler.ReachableResources(
graph.ValidatedReachableResourcesRequest{
DispatchReachableResourcesRequest: req,
Revision: revision,
},
dispatch.StreamWithContext(ctx, stream),
)
}
// DispatchLookupResources implements dispatch.LookupResources interface
func (ld *localDispatcher) DispatchLookupResources(
req *v1.DispatchLookupResourcesRequest,
stream dispatch.LookupResourcesStream,
) error {
ctx, span := tracer.Start(stream.Context(), "DispatchLookupResources", trace.WithAttributes(
attribute.String("resource-type", tuple.StringRR(req.ObjectRelation)),
attribute.String("subject", tuple.StringONR(req.Subject)),
))
defer span.End()
if err := dispatch.CheckDepth(ctx, req); err != nil {
return err
}
revision, err := ld.parseRevision(ctx, req.Metadata.AtRevision)
if err != nil {
return err
}
return ld.lookupResourcesHandler.LookupResources(
graph.ValidatedLookupResourcesRequest{
DispatchLookupResourcesRequest: req,
Revision: revision,
},
dispatch.StreamWithContext(ctx, stream),
)
}
// DispatchLookupSubjects implements dispatch.LookupSubjects interface
func (ld *localDispatcher) DispatchLookupSubjects(
req *v1.DispatchLookupSubjectsRequest,
stream dispatch.LookupSubjectsStream,
) error {
resourceType := tuple.StringRR(req.ResourceRelation)
subjectRelation := tuple.StringRR(req.SubjectRelation)
spanName := "DispatchLookupSubjects → " + resourceType + "@" + subjectRelation
ctx, span := tracer.Start(stream.Context(), spanName, trace.WithAttributes(
attribute.String("resource-type", resourceType),
attribute.String("subject-type", subjectRelation),
attribute.StringSlice("resource-ids", req.ResourceIds),
))
defer span.End()
if err := dispatch.CheckDepth(ctx, req); err != nil {
return err
}
revision, err := ld.parseRevision(ctx, req.Metadata.AtRevision)
if err != nil {
return err
}
return ld.lookupSubjectsHandler.LookupSubjects(
graph.ValidatedLookupSubjectsRequest{
DispatchLookupSubjectsRequest: req,
Revision: revision,
},
dispatch.StreamWithContext(ctx, stream),
)
}
func (ld *localDispatcher) Close() error {
return nil
}
func (ld *localDispatcher) ReadyState() dispatch.ReadyState {
return dispatch.ReadyState{IsReady: true}
}
func rewriteNamespaceError(original error) error {
nsNotFound := datastore.ErrNamespaceNotFound{}
switch {
case errors.As(original, &nsNotFound):
return NewNamespaceNotFoundErr(nsNotFound.NotFoundNamespaceName())
case errors.As(original, &ErrNamespaceNotFound{}):
fallthrough
case errors.As(original, &ErrRelationNotFound{}):
return original
default:
return fmt.Errorf(errDispatch, original)
}
}
// rewriteError transforms graph errors into a gRPC Status
func rewriteError(ctx context.Context, err error) error {
if err == nil {
return nil
}
// Check if the error can be directly used.
if st, ok := status.FromError(err); ok {
return st.Err()
}
switch {
case errors.Is(err, context.DeadlineExceeded):
return status.Errorf(codes.DeadlineExceeded, "%s", err)
case errors.Is(err, context.Canceled):
err := context.Cause(ctx)
if err != nil {
if _, ok := status.FromError(err); ok {
return err
}
}
return status.Errorf(codes.Canceled, "%s", err)
default:
log.Ctx(ctx).Err(err).Msg("received unexpected graph error")
return err
}
}
var emptyMetadata = &v1.ResponseMeta{
DispatchCount: 0,
}