Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not return nil on unknown subject set on expand #1139

Merged
merged 2 commits into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 51 additions & 50 deletions internal/expand/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,67 +41,68 @@ func (e *Engine) BuildTree(ctx context.Context, subject relationtuple.Subject, r
restDepth = globalMaxDepth
}

if subSet, isSubjectSet := subject.(*relationtuple.SubjectSet); isSubjectSet {
ctx, wasAlreadyVisited := graph.CheckAndAddVisited(ctx, subject)
if wasAlreadyVisited {
subSet, isSubjectSet := subject.(*relationtuple.SubjectSet)
if !isSubjectSet {
// is SubjectID
return &relationtuple.Tree{
Type: ketoapi.TreeNodeLeaf,
Subject: subject,
}, nil
}

ctx, wasAlreadyVisited := graph.CheckAndAddVisited(ctx, subject)
if wasAlreadyVisited {
return nil, nil
}

subTree := &relationtuple.Tree{
Type: ketoapi.TreeNodeUnion,
Subject: subject,
}

var (
rels []*relationtuple.RelationTuple
nextPage string
)
// do ... while nextPage != ""
for ok := true; ok; ok = nextPage != "" {
var err error
rels, nextPage, err = e.d.RelationTupleManager().GetRelationTuples(
ctx,
&relationtuple.RelationQuery{
Relation: &subSet.Relation,
Object: &subSet.Object,
Namespace: &subSet.Namespace,
},
x.WithToken(nextPage),
)
if err != nil {
return nil, err
} else if len(rels) == 0 {
return nil, nil
}

subTree := &relationtuple.Tree{
Type: ketoapi.TreeNodeUnion,
Subject: subject,
if restDepth <= 1 {
subTree.Type = ketoapi.TreeNodeLeaf
return subTree, nil
}

var (
rels []*relationtuple.RelationTuple
nextPage string
)
// do ... while nextPage != ""
for ok := true; ok; ok = nextPage != "" {
var err error
rels, nextPage, err = e.d.RelationTupleManager().GetRelationTuples(
ctx,
&relationtuple.RelationQuery{
Relation: &subSet.Relation,
Object: &subSet.Object,
Namespace: &subSet.Namespace,
},
x.WithToken(nextPage),
)
children := make([]*relationtuple.Tree, len(rels))
for ri, r := range rels {
child, err := e.BuildTree(ctx, r.Subject, restDepth-1)
if err != nil {
return nil, err
} else if len(rels) == 0 {
return nil, nil
}

if restDepth <= 1 {
subTree.Type = ketoapi.TreeNodeLeaf
return subTree, nil
}

children := make([]*relationtuple.Tree, len(rels))
for ri, r := range rels {
child, err := e.BuildTree(ctx, r.Subject, restDepth-1)
if err != nil {
return nil, err
}
if child == nil {
child = &relationtuple.Tree{
Type: ketoapi.TreeNodeLeaf,
Subject: r.Subject,
}
if child == nil {
child = &relationtuple.Tree{
Type: ketoapi.TreeNodeLeaf,
Subject: r.Subject,
}
children[ri] = child
}
subTree.Children = append(subTree.Children, children...)
children[ri] = child
}

return subTree, nil
subTree.Children = append(subTree.Children, children...)
}

// is SubjectID
return &relationtuple.Tree{
Type: ketoapi.TreeNodeLeaf,
Subject: subject,
}, nil
return subTree, nil
}
11 changes: 11 additions & 0 deletions internal/expand/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,4 +374,15 @@ func TestEngine(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, expectedTree, tree)
})

t.Run("case=returns result on unknown subject", func(t *testing.T) {
_, e := newTestEngine(t, []*namespace.Namespace{})
tree, err := e.BuildTree(context.Background(), &relationtuple.SubjectSet{
Namespace: "unknown",
Object: uuid.Must(uuid.NewV4()),
Relation: "rel",
}, 100)
require.NoError(t, err)
assert.Nil(t, tree)
})
}
5 changes: 5 additions & 0 deletions internal/expand/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ func (h *handler) getExpand(w http.ResponseWriter, r *http.Request, _ httprouter
h.d.Writer().WriteError(w, r, err)
return
}
if res == nil {
h.d.Writer().Write(w, r, herodot.ErrNotFound.WithError("no relation tuple found"))
return
}

tree, err := h.d.Mapper().ToTree(r.Context(), res)
if err != nil {
h.d.Writer().WriteError(w, r, err)
Expand Down