Skip to content

Commit

Permalink
Added limits and continuation tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
wscalf committed May 21, 2024
1 parent 1f3a4b5 commit 555f289
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 32 deletions.
86 changes: 65 additions & 21 deletions api/rebac/v1/lookup.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions api/rebac/v1/lookup.proto
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ message LookupSubjectsRequest {
string subject_type = 1;
string relation = 2;
ObjectReference object = 3;
optional uint32 limit = 4;
optional string continuation_token = 5;
}

message LookupSubjectsResponse {
SubjectReference subject = 1;
bool has_more = 2;
string continuation_token = 3;
}
8 changes: 7 additions & 1 deletion internal/biz/relationships.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ import (
// relationship domain objects re-used from the api layer for now, but otherwise would be defined here
type TouchSemantics bool

type ContinuationToken string
type SubjectResult struct {
Subject *v1.SubjectReference
Continuation ContinuationToken
}

type ZanzibarRepository interface {
Check(ctx context.Context, request *v1.CheckRequest) (*v1.CheckResponse, error)
LookupSubjects(context.Context, string, string, *v1.ObjectReference) (chan *v1.SubjectReference, chan error, error)
LookupSubjects(ctx context.Context, subject_type, relation string, object *v1.ObjectReference, limit uint32, continuation ContinuationToken) (chan *SubjectResult, chan error, error)
CreateRelationships(context.Context, []*v1.Relationship, TouchSemantics) error
ReadRelationships(context.Context, *v1.RelationshipFilter) ([]*v1.Relationship, error)
DeleteRelationships(context.Context, *v1.RelationshipFilter) error
Expand Down
32 changes: 24 additions & 8 deletions internal/data/spicedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,14 @@ func NewSpiceDbRepository(c *conf.Data, logger log.Logger) (*SpiceDbRepository,
return &SpiceDbRepository{client}, cleanup, nil
}

func (s *SpiceDbRepository) LookupSubjects(ctx context.Context, subject_type string, relation string, object *apiV1.ObjectReference) (chan *apiV1.SubjectReference, chan error, error) {
func (s *SpiceDbRepository) LookupSubjects(ctx context.Context, subject_type, relation string, object *apiV1.ObjectReference, limit uint32, continuation biz.ContinuationToken) (chan *biz.SubjectResult, chan error, error) {
var cursor *v1.Cursor = nil
if continuation != "" {
cursor = &v1.Cursor{
Token: string(continuation),
}
}

client, err := s.client.LookupSubjects(ctx, &v1.LookupSubjectsRequest{
Resource: &v1.ObjectReference{
ObjectType: object.Type,
Expand All @@ -85,14 +92,15 @@ func (s *SpiceDbRepository) LookupSubjects(ctx context.Context, subject_type str
Permission: relation,
SubjectObjectType: subject_type,
OptionalSubjectRelation: "",
OptionalConcreteLimit: 0,
OptionalConcreteLimit: limit,
OptionalCursor: cursor,
})

if err != nil {
return nil, nil, err
}

subjects := make(chan *apiV1.SubjectReference)
subjects := make(chan *biz.SubjectResult)
errs := make(chan error, 1)

go func() {
Expand All @@ -107,13 +115,21 @@ func (s *SpiceDbRepository) LookupSubjects(ctx context.Context, subject_type str
return
}

continuation := biz.ContinuationToken("")
if msg.AfterResultCursor != nil {
continuation = biz.ContinuationToken(msg.AfterResultCursor.Token)
}

subj := msg.GetSubject()
subjects <- &apiV1.SubjectReference{
Object: &apiV1.ObjectReference{
Type: subject_type,
Id: subj.SubjectObjectId,
subjects <- &biz.SubjectResult{
Subject: &apiV1.SubjectReference{
Object: &apiV1.ObjectReference{
Type: subject_type,
Id: subj.SubjectObjectId,
},
Relation: "",
},
Relation: "",
Continuation: continuation,
}
}
}()
Expand Down
14 changes: 12 additions & 2 deletions internal/service/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,28 @@ func NewLookupSubjectsService(repo biz.ZanzibarRepository) *LookupService {

func (s *LookupService) Subjects(req *pb.LookupSubjectsRequest, conn pb.Lookup_SubjectsServer) error {
ctx := context.TODO() //Doesn't get context from grpc?
limit := uint32(1000)
if req.Limit != nil {
limit = *req.Limit
}

continuation := biz.ContinuationToken("")
if req.ContinuationToken != nil {
continuation = biz.ContinuationToken(*req.ContinuationToken)
}
subs, errs, err := s.repo.LookupSubjects(ctx, req.SubjectType, req.Relation, &pb.ObjectReference{
Type: req.Object.Type, //Need null check
Id: req.Object.Id,
})
}, limit, continuation)

if err != nil {
return err
}

for sub := range subs {
err = conn.Send(&pb.LookupSubjectsResponse{
Subject: sub,
Subject: sub.Subject,
ContinuationToken: string(sub.Continuation),
})
if err != nil {
return err
Expand Down
13 changes: 13 additions & 0 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,15 @@ paths:
in: query
schema:
type: string
- name: limit
in: query
schema:
type: integer
format: uint32
- name: continuationToken
in: query
schema:
type: string
responses:
"200":
description: OK
Expand Down Expand Up @@ -210,6 +219,10 @@ components:
properties:
subject:
$ref: '#/components/schemas/api.rebac.v1.SubjectReference'
hasMore:
type: boolean
continuationToken:
type: string
api.rebac.v1.ObjectReference:
type: object
properties:
Expand Down

0 comments on commit 555f289

Please sign in to comment.