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
|
package computed
import (
"context"
cexpr "github.com/authzed/spicedb/internal/caveats"
"github.com/authzed/spicedb/internal/dispatch"
datastoremw "github.com/authzed/spicedb/internal/middleware/datastore"
caveattypes "github.com/authzed/spicedb/pkg/caveats/types"
"github.com/authzed/spicedb/pkg/datastore"
"github.com/authzed/spicedb/pkg/genutil/slicez"
v1 "github.com/authzed/spicedb/pkg/proto/dispatch/v1"
"github.com/authzed/spicedb/pkg/spiceerrors"
"github.com/authzed/spicedb/pkg/tuple"
)
// DebugOption defines the various debug level options for Checks.
type DebugOption int
const (
// NoDebugging indicates that debug information should be retained
// while performing the Check.
NoDebugging DebugOption = 0
// BasicDebuggingEnabled indicates that basic debug information, such
// as which steps were taken, should be retained while performing the
// Check and returned to the caller.
//
// NOTE: This has a minor performance impact.
BasicDebuggingEnabled DebugOption = 1
// TraceDebuggingEnabled indicates that the Check is being issued for
// tracing the exact calls made for debugging, which means that not only
// should debug information be recorded and returned, but that optimizations
// such as batching should be disabled.
//
// WARNING: This has a fairly significant performance impact and should only
// be used in tooling!
TraceDebuggingEnabled DebugOption = 2
)
// CheckParameters are the parameters for the ComputeCheck call. *All* are required.
type CheckParameters struct {
ResourceType tuple.RelationReference
Subject tuple.ObjectAndRelation
CaveatContext map[string]any
AtRevision datastore.Revision
MaximumDepth uint32
DebugOption DebugOption
CheckHints []*v1.CheckHint
}
// ComputeCheck computes a check result for the given resource and subject, computing any
// caveat expressions found.
func ComputeCheck(
ctx context.Context,
d dispatch.Check,
ts *caveattypes.TypeSet,
params CheckParameters,
resourceID string,
dispatchChunkSize uint16,
) (*v1.ResourceCheckResult, *v1.ResponseMeta, error) {
resultsMap, meta, di, err := computeCheck(ctx, d, ts, params, []string{resourceID}, dispatchChunkSize)
if err != nil {
return nil, meta, err
}
spiceerrors.DebugAssert(func() bool {
return (len(di) == 0 && meta.DebugInfo == nil) || (len(di) == 1 && meta.DebugInfo != nil)
}, "mismatch in debug information returned from computeCheck")
return resultsMap[resourceID], meta, err
}
// ComputeBulkCheck computes a check result for the given resources and subject, computing any
// caveat expressions found.
func ComputeBulkCheck(
ctx context.Context,
d dispatch.Check,
ts *caveattypes.TypeSet,
params CheckParameters,
resourceIDs []string,
dispatchChunkSize uint16,
) (map[string]*v1.ResourceCheckResult, *v1.ResponseMeta, []*v1.DebugInformation, error) {
return computeCheck(ctx, d, ts, params, resourceIDs, dispatchChunkSize)
}
func computeCheck(ctx context.Context,
d dispatch.Check,
ts *caveattypes.TypeSet,
params CheckParameters,
resourceIDs []string,
dispatchChunkSize uint16,
) (map[string]*v1.ResourceCheckResult, *v1.ResponseMeta, []*v1.DebugInformation, error) {
debugging := v1.DispatchCheckRequest_NO_DEBUG
if params.DebugOption == BasicDebuggingEnabled {
debugging = v1.DispatchCheckRequest_ENABLE_BASIC_DEBUGGING
} else if params.DebugOption == TraceDebuggingEnabled {
debugging = v1.DispatchCheckRequest_ENABLE_TRACE_DEBUGGING
}
setting := v1.DispatchCheckRequest_REQUIRE_ALL_RESULTS
if len(resourceIDs) == 1 {
setting = v1.DispatchCheckRequest_ALLOW_SINGLE_RESULT
}
// Ensure that the number of resources IDs given to each dispatch call is not in excess of the maximum.
results := make(map[string]*v1.ResourceCheckResult, len(resourceIDs))
metadata := &v1.ResponseMeta{}
bf, err := v1.NewTraversalBloomFilter(uint(params.MaximumDepth))
if err != nil {
return nil, nil, nil, spiceerrors.MustBugf("failed to create new traversal bloom filter")
}
caveatRunner := cexpr.NewCaveatRunner(ts)
// TODO(jschorr): Should we make this run in parallel via the preloadedTaskRunner?
debugInfo := make([]*v1.DebugInformation, 0)
_, err = slicez.ForEachChunkUntil(resourceIDs, dispatchChunkSize, func(resourceIDsToCheck []string) (bool, error) {
checkResult, err := d.DispatchCheck(ctx, &v1.DispatchCheckRequest{
ResourceRelation: params.ResourceType.ToCoreRR(),
ResourceIds: resourceIDsToCheck,
ResultsSetting: setting,
Subject: params.Subject.ToCoreONR(),
Metadata: &v1.ResolverMeta{
AtRevision: params.AtRevision.String(),
DepthRemaining: params.MaximumDepth,
TraversalBloom: bf,
},
Debug: debugging,
CheckHints: params.CheckHints,
})
if checkResult.Metadata.DebugInfo != nil {
debugInfo = append(debugInfo, checkResult.Metadata.DebugInfo)
}
if len(resourceIDs) == 1 {
metadata = checkResult.Metadata
} else {
metadata = &v1.ResponseMeta{
DispatchCount: metadata.DispatchCount + checkResult.Metadata.DispatchCount,
DepthRequired: max(metadata.DepthRequired, checkResult.Metadata.DepthRequired),
CachedDispatchCount: metadata.CachedDispatchCount + checkResult.Metadata.CachedDispatchCount,
DebugInfo: nil,
}
}
if err != nil {
return false, err
}
for _, resourceID := range resourceIDsToCheck {
computed, err := computeCaveatedCheckResult(ctx, caveatRunner, params, resourceID, checkResult)
if err != nil {
return false, err
}
results[resourceID] = computed
}
return true, nil
})
return results, metadata, debugInfo, err
}
func computeCaveatedCheckResult(ctx context.Context, runner *cexpr.CaveatRunner, params CheckParameters, resourceID string, checkResult *v1.DispatchCheckResponse) (*v1.ResourceCheckResult, error) {
result, ok := checkResult.ResultsByResourceId[resourceID]
if !ok {
return &v1.ResourceCheckResult{
Membership: v1.ResourceCheckResult_NOT_MEMBER,
}, nil
}
if result.Membership == v1.ResourceCheckResult_MEMBER {
return result, nil
}
ds := datastoremw.MustFromContext(ctx)
reader := ds.SnapshotReader(params.AtRevision)
caveatResult, err := runner.RunCaveatExpression(ctx, result.Expression, params.CaveatContext, reader, cexpr.RunCaveatExpressionNoDebugging)
if err != nil {
return nil, err
}
if caveatResult.IsPartial() {
missingFields, _ := caveatResult.MissingVarNames()
return &v1.ResourceCheckResult{
Membership: v1.ResourceCheckResult_CAVEATED_MEMBER,
Expression: result.Expression,
MissingExprFields: missingFields,
}, nil
}
if caveatResult.Value() {
return &v1.ResourceCheckResult{
Membership: v1.ResourceCheckResult_MEMBER,
}, nil
}
return &v1.ResourceCheckResult{
Membership: v1.ResourceCheckResult_NOT_MEMBER,
}, nil
}
|