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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
package schema
import (
"context"
"fmt"
"github.com/authzed/spicedb/pkg/spiceerrors"
"github.com/authzed/spicedb/pkg/tuple"
core "github.com/authzed/spicedb/pkg/proto/core/v1"
)
type reachabilityOption int
const (
reachabilityFull reachabilityOption = iota
reachabilityFirst
)
func computeReachability(ctx context.Context, def *Definition, relationName string, option reachabilityOption) (*core.ReachabilityGraph, error) {
targetRelation, ok := def.relationMap[relationName]
if !ok {
return nil, fmt.Errorf("relation `%s` not found under type `%s` missing when computing reachability", relationName, def.nsDef.Name)
}
if !def.HasTypeInformation(relationName) && targetRelation.GetUsersetRewrite() == nil {
return nil, fmt.Errorf("relation `%s` missing type information when computing reachability for namespace `%s`", relationName, def.nsDef.Name)
}
graph := &core.ReachabilityGraph{
EntrypointsBySubjectType: map[string]*core.ReachabilityEntrypoints{},
EntrypointsBySubjectRelation: map[string]*core.ReachabilityEntrypoints{},
}
usersetRewrite := targetRelation.GetUsersetRewrite()
if usersetRewrite != nil {
return graph, computeRewriteReachability(ctx, graph, usersetRewrite, core.ReachabilityEntrypoint_DIRECT_OPERATION_RESULT, targetRelation, def, option)
}
// If there is no userRewrite, then we have a relation and its entrypoints will all be
// relation entrypoints.
return graph, addSubjectLinks(graph, core.ReachabilityEntrypoint_DIRECT_OPERATION_RESULT, targetRelation, def)
}
func computeRewriteReachability(ctx context.Context, graph *core.ReachabilityGraph, rewrite *core.UsersetRewrite, operationResultState core.ReachabilityEntrypoint_EntrypointResultStatus, targetRelation *core.Relation, def *Definition, option reachabilityOption) error {
switch rw := rewrite.RewriteOperation.(type) {
case *core.UsersetRewrite_Union:
return computeRewriteOpReachability(ctx, rw.Union.Child, operationResultState, graph, targetRelation, def, option)
case *core.UsersetRewrite_Intersection:
// If optimized mode is set, only return the first child of the intersection.
if option == reachabilityFirst {
return computeRewriteOpReachability(ctx, rw.Intersection.Child[0:1], core.ReachabilityEntrypoint_REACHABLE_CONDITIONAL_RESULT, graph, targetRelation, def, option)
}
return computeRewriteOpReachability(ctx, rw.Intersection.Child, core.ReachabilityEntrypoint_REACHABLE_CONDITIONAL_RESULT, graph, targetRelation, def, option)
case *core.UsersetRewrite_Exclusion:
// If optimized mode is set, only return the first child of the exclusion.
if option == reachabilityFirst {
return computeRewriteOpReachability(ctx, rw.Exclusion.Child[0:1], core.ReachabilityEntrypoint_REACHABLE_CONDITIONAL_RESULT, graph, targetRelation, def, option)
}
return computeRewriteOpReachability(ctx, rw.Exclusion.Child, core.ReachabilityEntrypoint_REACHABLE_CONDITIONAL_RESULT, graph, targetRelation, def, option)
default:
return fmt.Errorf("unknown kind of userset rewrite in reachability computation: %T", rw)
}
}
func computeRewriteOpReachability(ctx context.Context, children []*core.SetOperation_Child, operationResultState core.ReachabilityEntrypoint_EntrypointResultStatus, graph *core.ReachabilityGraph, targetRelation *core.Relation, def *Definition, option reachabilityOption) error {
rr := &core.RelationReference{
Namespace: def.nsDef.Name,
Relation: targetRelation.Name,
}
for _, childOneof := range children {
switch child := childOneof.ChildType.(type) {
case *core.SetOperation_Child_XThis:
return fmt.Errorf("use of _this is unsupported; please rewrite your schema")
case *core.SetOperation_Child_ComputedUserset:
// A computed userset adds an entrypoint indicating that the relation is rewritten.
err := addSubjectEntrypoint(graph, def.nsDef.Name, child.ComputedUserset.Relation, &core.ReachabilityEntrypoint{
Kind: core.ReachabilityEntrypoint_COMPUTED_USERSET_ENTRYPOINT,
TargetRelation: rr,
ComputedUsersetRelation: child.ComputedUserset.Relation,
ResultStatus: operationResultState,
})
if err != nil {
return err
}
case *core.SetOperation_Child_UsersetRewrite:
err := computeRewriteReachability(ctx, graph, child.UsersetRewrite, operationResultState, targetRelation, def, option)
if err != nil {
return err
}
case *core.SetOperation_Child_TupleToUserset:
tuplesetRelation := child.TupleToUserset.Tupleset.Relation
computedUsersetRelation := child.TupleToUserset.ComputedUserset.Relation
if err := computeTTUReachability(ctx, graph, tuplesetRelation, computedUsersetRelation, operationResultState, rr, def); err != nil {
return err
}
case *core.SetOperation_Child_FunctionedTupleToUserset:
tuplesetRelation := child.FunctionedTupleToUserset.Tupleset.Relation
computedUsersetRelation := child.FunctionedTupleToUserset.ComputedUserset.Relation
switch child.FunctionedTupleToUserset.Function {
case core.FunctionedTupleToUserset_FUNCTION_ANY:
// Nothing to change.
case core.FunctionedTupleToUserset_FUNCTION_ALL:
// Mark as a conditional result.
operationResultState = core.ReachabilityEntrypoint_REACHABLE_CONDITIONAL_RESULT
default:
return spiceerrors.MustBugf("unknown function type `%T` in reachability graph building", child.FunctionedTupleToUserset.Function)
}
if err := computeTTUReachability(ctx, graph, tuplesetRelation, computedUsersetRelation, operationResultState, rr, def); err != nil {
return err
}
case *core.SetOperation_Child_XNil:
// nil has no entrypoints.
return nil
default:
return spiceerrors.MustBugf("unknown set operation child `%T` in reachability graph building", child)
}
}
return nil
}
func computeTTUReachability(
ctx context.Context,
graph *core.ReachabilityGraph,
tuplesetRelation string,
computedUsersetRelation string,
operationResultState core.ReachabilityEntrypoint_EntrypointResultStatus,
rr *core.RelationReference,
def *Definition,
) error {
directRelationTypes, err := def.AllowedDirectRelationsAndWildcards(tuplesetRelation)
if err != nil {
return err
}
for _, allowedRelationType := range directRelationTypes {
// For each namespace allowed to be found on the right hand side of the
// tupleset relation, include the *computed userset* relation as an entrypoint.
//
// For example, given a schema:
//
// ```
// definition user {}
//
// definition parent1 {
// relation somerel: user
// }
//
// definition parent2 {
// relation somerel: user
// }
//
// definition child {
// relation parent: parent1 | parent2
// permission someperm = parent->somerel
// }
// ```
//
// We will add an entrypoint for the arrow itself, keyed to the relation type
// included from the computed userset.
//
// Using the above example, this will add entrypoints for `parent1#somerel`
// and `parent2#somerel`, which are the subjects reached after resolving the
// right side of the arrow.
// Check if the relation does exist on the allowed type, and only add the entrypoint if present.
relDef, err := def.ts.GetDefinition(ctx, allowedRelationType.Namespace)
if err != nil {
return err
}
if relDef.HasRelation(computedUsersetRelation) {
err := addSubjectEntrypoint(graph, allowedRelationType.Namespace, computedUsersetRelation, &core.ReachabilityEntrypoint{
Kind: core.ReachabilityEntrypoint_TUPLESET_TO_USERSET_ENTRYPOINT,
TargetRelation: rr,
ResultStatus: operationResultState,
ComputedUsersetRelation: computedUsersetRelation,
TuplesetRelation: tuplesetRelation,
})
if err != nil {
return err
}
}
}
return nil
}
func addSubjectEntrypoint(graph *core.ReachabilityGraph, namespaceName string, relationName string, entrypoint *core.ReachabilityEntrypoint) error {
key := tuple.JoinRelRef(namespaceName, relationName)
if relationName == "" {
return spiceerrors.MustBugf("found empty relation name for subject entrypoint")
}
if graph.EntrypointsBySubjectRelation[key] == nil {
graph.EntrypointsBySubjectRelation[key] = &core.ReachabilityEntrypoints{
Entrypoints: []*core.ReachabilityEntrypoint{},
SubjectRelation: &core.RelationReference{
Namespace: namespaceName,
Relation: relationName,
},
}
}
graph.EntrypointsBySubjectRelation[key].Entrypoints = append(
graph.EntrypointsBySubjectRelation[key].Entrypoints,
entrypoint,
)
return nil
}
func addSubjectLinks(graph *core.ReachabilityGraph, operationResultState core.ReachabilityEntrypoint_EntrypointResultStatus, relation *core.Relation, def *Definition) error {
typeInfo := relation.GetTypeInformation()
if typeInfo == nil {
return fmt.Errorf("missing type information for relation %s#%s", def.nsDef.Name, relation.Name)
}
rr := &core.RelationReference{
Namespace: def.nsDef.Name,
Relation: relation.Name,
}
allowedDirectRelations := typeInfo.GetAllowedDirectRelations()
for _, directRelation := range allowedDirectRelations {
// If the allowed relation is a wildcard, add it as a subject *type* entrypoint, rather than
// a subject relation.
if directRelation.GetPublicWildcard() != nil {
if graph.EntrypointsBySubjectType[directRelation.Namespace] == nil {
graph.EntrypointsBySubjectType[directRelation.Namespace] = &core.ReachabilityEntrypoints{
Entrypoints: []*core.ReachabilityEntrypoint{},
SubjectType: directRelation.Namespace,
}
}
graph.EntrypointsBySubjectType[directRelation.Namespace].Entrypoints = append(
graph.EntrypointsBySubjectType[directRelation.Namespace].Entrypoints,
&core.ReachabilityEntrypoint{
Kind: core.ReachabilityEntrypoint_RELATION_ENTRYPOINT,
TargetRelation: rr,
ResultStatus: operationResultState,
},
)
continue
}
err := addSubjectEntrypoint(graph, directRelation.Namespace, directRelation.GetRelation(), &core.ReachabilityEntrypoint{
Kind: core.ReachabilityEntrypoint_RELATION_ENTRYPOINT,
TargetRelation: rr,
ResultStatus: operationResultState,
})
if err != nil {
return err
}
}
return nil
}
|