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

introduce lookup resources page limit #395

Merged
merged 3 commits into from
Jul 15, 2024
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
4 changes: 0 additions & 4 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ linters:
enable:
- "bidichk"
- "bodyclose"
- "deadcode"
- "errcheck"
- "errname"
- "errorlint"
Expand All @@ -20,7 +19,6 @@ linters:
- "gosec"
- "gosimple"
- "govet"
- "ifshort"
- "importas"
- "ineffassign"
- "makezero"
Expand All @@ -30,12 +28,10 @@ linters:
- "revive"
- "rowserrcheck"
- "staticcheck"
- "structcheck"
- "stylecheck"
- "tenv"
- "typecheck"
- "unconvert"
- "unused"
- "varcheck"
- "wastedassign"
- "whitespace"
89 changes: 58 additions & 31 deletions internal/commands/permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func RegisterPermissionCmd(rootCmd *cobra.Command) *cobra.Command {
lookupResourcesCmd.Flags().Bool("json", false, "output as JSON")
lookupResourcesCmd.Flags().String("revision", "", "optional revision at which to check")
lookupResourcesCmd.Flags().String("caveat-context", "", "the caveat context to send along with the lookup, in JSON form")
lookupResourcesCmd.Flags().Uint32("page-limit", 0, "limit of relations returned per page")
registerConsistencyFlags(lookupResourcesCmd.Flags())

permissionCmd.AddCommand(lookupSubjectsCmd)
Expand Down Expand Up @@ -416,6 +417,8 @@ func expandCmdFunc(cmd *cobra.Command, args []string) error {
return nil
}

var newLookupResourcesPageCallbackForTests func(readByPage uint)

func lookupResourcesCmdFunc(cmd *cobra.Command, args []string) error {
objectNS := args[0]
relation := args[1]
Expand All @@ -424,6 +427,7 @@ func lookupResourcesCmdFunc(cmd *cobra.Command, args []string) error {
return err
}

pageLimit := cobrautil.MustGetUint32(cmd, "page-limit")
caveatContext, err := GetCaveatContext(cmd)
if err != nil {
return err
Expand All @@ -438,46 +442,69 @@ func lookupResourcesCmdFunc(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
request := &v1.LookupResourcesRequest{
ResourceObjectType: objectNS,
Permission: relation,
Subject: &v1.SubjectReference{
Object: &v1.ObjectReference{
ObjectType: subjectNS,
ObjectId: subjectID,
},
OptionalRelation: subjectRel,
},
Context: caveatContext,
Consistency: consistency,
}
log.Trace().Interface("request", request).Send()

respStream, err := client.LookupResources(cmd.Context(), request)
if err != nil {
return err
}

var cursor *v1.Cursor
var totalCount uint
for {
resp, err := respStream.Recv()
switch {
case errors.Is(err, io.EOF):
return nil
case err != nil:
request := &v1.LookupResourcesRequest{
ResourceObjectType: objectNS,
Permission: relation,
Subject: &v1.SubjectReference{
Object: &v1.ObjectReference{
ObjectType: subjectNS,
ObjectId: subjectID,
},
OptionalRelation: subjectRel,
},
Context: caveatContext,
Consistency: consistency,
OptionalLimit: pageLimit,
OptionalCursor: cursor,
}
log.Trace().Interface("request", request).Uint32("page-limit", pageLimit).Send()

respStream, err := client.LookupResources(cmd.Context(), request)
if err != nil {
return err
default:
if cobrautil.MustGetBool(cmd, "json") {
prettyProto, err := PrettyProto(resp)
if err != nil {
return err
}

var count uint

stream:
for {
resp, err := respStream.Recv()
switch {
case errors.Is(err, io.EOF):
break stream
case err != nil:
return err
default:
count++
totalCount++
if cobrautil.MustGetBool(cmd, "json") {
prettyProto, err := PrettyProto(resp)
if err != nil {
return err
}

console.Println(string(prettyProto))
}

console.Println(string(prettyProto))
console.Println(prettyLookupPermissionship(resp.ResourceObjectId, resp.Permissionship, resp.PartialCaveatInfo))
cursor = resp.AfterResultCursor
}
}

console.Println(prettyLookupPermissionship(resp.ResourceObjectId, resp.Permissionship, resp.PartialCaveatInfo))
if newLookupResourcesPageCallbackForTests != nil {
newLookupResourcesPageCallbackForTests(count)
}
if count == 0 || pageLimit == 0 || count < uint(pageLimit) {
log.Trace().Interface("request", request).Uint32("page-limit", pageLimit).Uint("count", totalCount).Send()
break
}
}

return nil
}

func lookupSubjectsCmdFunc(cmd *cobra.Command, args []string) error {
Expand Down
96 changes: 96 additions & 0 deletions internal/commands/permission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import (
"fmt"
"testing"

"github.com/authzed/spicedb/pkg/tuple"

"github.com/authzed/zed/internal/console"
zedtesting "github.com/authzed/zed/internal/testing"

"github.com/rs/zerolog"
"github.com/spf13/cobra"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -98,3 +103,94 @@ func TestCheckErrorWithInvalidDebugInformation(t *testing.T) {
require.NotNil(t, err)
require.ErrorContains(t, err, "unknown field: invalid")
}

func TestLookupResourcesCommand(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
srv := zedtesting.NewTestServer(ctx, t)
go func() {
require.NoError(t, srv.Run(ctx))
}()
conn, err := srv.GRPCDialContext(ctx)
require.NoError(t, err)

originalClient := client.NewClient
defer func() {
client.NewClient = originalClient
}()

client.NewClient = zedtesting.ClientFromConn(conn)

c, err := zedtesting.ClientFromConn(conn)(nil)
require.NoError(t, err)

_, err = c.WriteSchema(ctx, &v1.WriteSchemaRequest{Schema: testSchema})
require.NoError(t, err)

var updates []*v1.RelationshipUpdate
for i := 0; i < 10; i++ {
updates = append(updates, &v1.RelationshipUpdate{
Operation: v1.RelationshipUpdate_OPERATION_TOUCH,
Relationship: tuple.ParseRel(fmt.Sprintf("test/resource:%d#reader@test/user:1", i)),
})
}

_, err = c.WriteRelationships(ctx, &v1.WriteRelationshipsRequest{Updates: updates})
require.NoError(t, err)

// we override this to obtain the results being printed and validate them
previous := console.Println
vroldanbet marked this conversation as resolved.
Show resolved Hide resolved
defer func() {
console.Println = previous
}()
var count int
console.Println = func(values ...any) {
count += len(values)
}

// use test callback to make sure pagination is correct
var receivedPageSizes []uint
newLookupResourcesPageCallbackForTests = func(readPageSize uint) {
receivedPageSizes = append(receivedPageSizes, readPageSize)
}
defer func() {
newLookupResourcesPageCallbackForTests = nil
}()

// test no page size, server computes returns all resources in one go
cmd := testLookupResourcesCommand(t, 0)
err = lookupResourcesCmdFunc(cmd, []string{"test/resource", "read", "test/user:1"})
require.NoError(t, err)
require.Equal(t, 10, count)
require.EqualValues(t, []uint{10}, receivedPageSizes)

// use page size same as number of elements
count = 0
receivedPageSizes = nil
cmd = testLookupResourcesCommand(t, 10)
err = lookupResourcesCmdFunc(cmd, []string{"test/resource", "read", "test/user:1"})
require.NoError(t, err)
require.Equal(t, 10, count)
require.EqualValues(t, []uint{10, 0}, receivedPageSizes)

// use odd page size
count = 0
receivedPageSizes = nil
cmd = testLookupResourcesCommand(t, 3)
err = lookupResourcesCmdFunc(cmd, []string{"test/resource", "read", "test/user:1"})
vroldanbet marked this conversation as resolved.
Show resolved Hide resolved
require.NoError(t, err)
require.Equal(t, 10, count)
require.EqualValues(t, []uint{3, 3, 3, 1}, receivedPageSizes)
}

func testLookupResourcesCommand(t *testing.T, limit uint32) *cobra.Command {
return zedtesting.CreateTestCobraCommandWithFlagValue(t,
zedtesting.BoolFlag{FlagName: "consistency-full", FlagValue: true},
zedtesting.StringFlag{FlagName: "consistency-at-least"},
zedtesting.BoolFlag{FlagName: "consistency-min-latency", FlagValue: false},
zedtesting.StringFlag{FlagName: "consistency-at-exactly"},
zedtesting.StringFlag{FlagName: "revision"},
zedtesting.StringFlag{FlagName: "caveat-context"},
zedtesting.UintFlag32{FlagName: "page-limit", FlagValue: limit},
zedtesting.BoolFlag{FlagName: "json"})
}
2 changes: 2 additions & 0 deletions internal/commands/relationship_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
const testSchema = `definition test/resource {
relation reader: test/user
relation writer: test/user

permission read = reader + writer
}

definition test/user {}`
Expand Down
2 changes: 1 addition & 1 deletion internal/console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var Errorf = func(format string, a ...any) {
}

// Println prints a line with optional values to the console.
func Println(values ...any) {
var Println = func(values ...any) {
for _, value := range values {
Printf("%v\n", value)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/testing/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/authzed/zed/internal/client"
)

func ClientFromConn(conn *grpc.ClientConn) func(cmd *cobra.Command) (client.Client, error) {
func ClientFromConn(conn *grpc.ClientConn) func(_ *cobra.Command) (client.Client, error) {
return func(_ *cobra.Command) (client.Client, error) {
return &authzed.ClientWithExperimental{
Client: authzed.Client{
Expand Down
Loading