|
| 1 | +package v1 |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + |
| 9 | + v1 "github.com/authzed/authzed-go/proto/authzed/api/v1" |
| 10 | + |
| 11 | + "github.com/authzed/spicedb/internal/datastore/dsfortesting" |
| 12 | + "github.com/authzed/spicedb/internal/datastore/memdb" |
| 13 | + "github.com/authzed/spicedb/internal/dispatch" |
| 14 | + "github.com/authzed/spicedb/internal/testfixtures" |
| 15 | + caveattypes "github.com/authzed/spicedb/pkg/caveats/types" |
| 16 | + "github.com/authzed/spicedb/pkg/datalayer" |
| 17 | + "github.com/authzed/spicedb/pkg/middleware/consistency" |
| 18 | + dispatchv1 "github.com/authzed/spicedb/pkg/proto/dispatch/v1" |
| 19 | +) |
| 20 | + |
| 21 | +// debugInfoDispatcher is a dispatcher that returns a successful check result whose |
| 22 | +// metadata carries debug information with a nil Check.Request. This mirrors the |
| 23 | +// shape produced by combineResponseMetadata for combined trace nodes, which can |
| 24 | +// reach a non-tracing CheckBulkPermissions caller when an identical tracing-enabled |
| 25 | +// request shares its dispatch via singleflight. |
| 26 | +type debugInfoDispatcher struct { |
| 27 | + dispatch.Dispatcher |
| 28 | +} |
| 29 | + |
| 30 | +func (d debugInfoDispatcher) DispatchCheck(_ context.Context, req *dispatchv1.DispatchCheckRequest) (*dispatchv1.DispatchCheckResponse, error) { |
| 31 | + return &dispatchv1.DispatchCheckResponse{ |
| 32 | + ResultsByResourceId: memberResults(req.ResourceIds), |
| 33 | + Metadata: &dispatchv1.ResponseMeta{ |
| 34 | + DispatchCount: 1, |
| 35 | + DebugInfo: &dispatchv1.DebugInformation{ |
| 36 | + Check: &dispatchv1.CheckDebugTrace{ |
| 37 | + SubProblems: []*dispatchv1.CheckDebugTrace{{IsCachedResult: true}}, |
| 38 | + }, |
| 39 | + }, |
| 40 | + }, |
| 41 | + }, nil |
| 42 | +} |
| 43 | + |
| 44 | +// matchingDebugInfoDispatcher returns a successful check result whose debug |
| 45 | +// information carries a Check.Request matching the dispatched resource IDs. This |
| 46 | +// exercises the normal tracing path where CheckBulkPermissions synthesizes a |
| 47 | +// debug trace for each resource. |
| 48 | +type matchingDebugInfoDispatcher struct { |
| 49 | + dispatch.Dispatcher |
| 50 | +} |
| 51 | + |
| 52 | +func (d matchingDebugInfoDispatcher) DispatchCheck(_ context.Context, req *dispatchv1.DispatchCheckRequest) (*dispatchv1.DispatchCheckResponse, error) { |
| 53 | + return &dispatchv1.DispatchCheckResponse{ |
| 54 | + ResultsByResourceId: memberResults(req.ResourceIds), |
| 55 | + Metadata: &dispatchv1.ResponseMeta{ |
| 56 | + DispatchCount: 1, |
| 57 | + DebugInfo: &dispatchv1.DebugInformation{ |
| 58 | + Check: &dispatchv1.CheckDebugTrace{ |
| 59 | + Request: &dispatchv1.DispatchCheckRequest{ |
| 60 | + ResourceRelation: req.ResourceRelation, |
| 61 | + ResourceIds: req.ResourceIds, |
| 62 | + Subject: req.Subject, |
| 63 | + ResultsSetting: req.ResultsSetting, |
| 64 | + Debug: req.Debug, |
| 65 | + }, |
| 66 | + Results: memberResults(req.ResourceIds), |
| 67 | + }, |
| 68 | + }, |
| 69 | + }, |
| 70 | + }, nil |
| 71 | +} |
| 72 | + |
| 73 | +func memberResults(resourceIDs []string) map[string]*dispatchv1.ResourceCheckResult { |
| 74 | + results := make(map[string]*dispatchv1.ResourceCheckResult, len(resourceIDs)) |
| 75 | + for _, resourceID := range resourceIDs { |
| 76 | + results[resourceID] = &dispatchv1.ResourceCheckResult{ |
| 77 | + Membership: dispatchv1.ResourceCheckResult_MEMBER, |
| 78 | + } |
| 79 | + } |
| 80 | + return results |
| 81 | +} |
| 82 | + |
| 83 | +func runBulkCheck(t *testing.T, d dispatch.Dispatcher, withTracing bool) *v1.CheckBulkPermissionsResponse { |
| 84 | + t.Helper() |
| 85 | + require := require.New(t) |
| 86 | + |
| 87 | + uninitialized, err := dsfortesting.NewMemDBDatastoreForTesting(t, 0, 0, memdb.DisableGC) |
| 88 | + require.NoError(err) |
| 89 | + |
| 90 | + ds, _ := testfixtures.StandardDatastoreWithData(t, uninitialized) |
| 91 | + dl := datalayer.NewDataLayer(ds) |
| 92 | + |
| 93 | + ctx := datalayer.ContextWithDataLayer(consistency.ContextWithHandle(t.Context()), dl) |
| 94 | + |
| 95 | + req := &v1.CheckBulkPermissionsRequest{ |
| 96 | + Items: []*v1.CheckBulkPermissionsRequestItem{ |
| 97 | + { |
| 98 | + Resource: &v1.ObjectReference{ |
| 99 | + ObjectType: "document", |
| 100 | + ObjectId: "masterplan", |
| 101 | + }, |
| 102 | + Permission: "view", |
| 103 | + Subject: &v1.SubjectReference{ |
| 104 | + Object: &v1.ObjectReference{ |
| 105 | + ObjectType: "user", |
| 106 | + ObjectId: "eng_lead", |
| 107 | + }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + WithTracing: withTracing, |
| 112 | + } |
| 113 | + |
| 114 | + require.NoError(consistency.AddRevisionToContext(ctx, req, dl, "", consistency.TreatMismatchingTokensAsError)) |
| 115 | + |
| 116 | + bc := &bulkChecker{ |
| 117 | + maxAPIDepth: 50, |
| 118 | + maxCaveatContextSize: 4096, |
| 119 | + maxConcurrency: 1, |
| 120 | + caveatTypeSet: caveattypes.Default.TypeSet, |
| 121 | + dispatch: d, |
| 122 | + dispatchChunkSize: 100, |
| 123 | + } |
| 124 | + |
| 125 | + resp, err := bc.checkBulkPermissions(ctx, req) |
| 126 | + require.NoError(err) |
| 127 | + return resp |
| 128 | +} |
| 129 | + |
| 130 | +// TestCheckBulkPermissionsWithNilDebugRequest ensures that a debug information entry |
| 131 | +// with a nil Check.Request does not panic CheckBulkPermissions when tracing was not |
| 132 | +// requested. See https://github.com/authzed/spicedb/issues/3159. |
| 133 | +func TestCheckBulkPermissionsWithNilDebugRequest(t *testing.T) { |
| 134 | + require := require.New(t) |
| 135 | + |
| 136 | + resp := runBulkCheck(t, debugInfoDispatcher{}, false) |
| 137 | + require.Len(resp.Pairs, 1) |
| 138 | + |
| 139 | + item := resp.Pairs[0].GetItem() |
| 140 | + require.NotNil(item) |
| 141 | + require.Equal(v1.CheckPermissionResponse_PERMISSIONSHIP_HAS_PERMISSION, item.Permissionship) |
| 142 | + require.Nil(item.DebugTrace) |
| 143 | +} |
| 144 | + |
| 145 | +// TestCheckBulkPermissionsWithMatchingDebugRequest ensures that a debug information |
| 146 | +// entry whose Check.Request matches the resource is surfaced as a debug trace on the |
| 147 | +// result, exercising the positive branch of the resource-ID matching loop. |
| 148 | +func TestCheckBulkPermissionsWithMatchingDebugRequest(t *testing.T) { |
| 149 | + require := require.New(t) |
| 150 | + |
| 151 | + resp := runBulkCheck(t, matchingDebugInfoDispatcher{}, true) |
| 152 | + require.Len(resp.Pairs, 1) |
| 153 | + |
| 154 | + item := resp.Pairs[0].GetItem() |
| 155 | + require.NotNil(item) |
| 156 | + require.Equal(v1.CheckPermissionResponse_PERMISSIONSHIP_HAS_PERMISSION, item.Permissionship) |
| 157 | + require.NotNil(item.DebugTrace) |
| 158 | + require.NotNil(item.DebugTrace.Check) |
| 159 | + require.Equal("masterplan", item.DebugTrace.Check.Resource.GetObjectId()) |
| 160 | + require.Equal("view", item.DebugTrace.Check.Permission) |
| 161 | +} |
0 commit comments