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
|
package hints
import (
core "github.com/authzed/spicedb/pkg/proto/core/v1"
v1 "github.com/authzed/spicedb/pkg/proto/dispatch/v1"
"github.com/authzed/spicedb/pkg/schema"
"github.com/authzed/spicedb/pkg/spiceerrors"
"github.com/authzed/spicedb/pkg/tuple"
)
// CheckHintForComputedUserset creates a CheckHint for a relation and a subject.
func CheckHintForComputedUserset(resourceType string, resourceID string, relation string, subject tuple.ObjectAndRelation, result *v1.ResourceCheckResult) *v1.CheckHint {
return &v1.CheckHint{
Resource: &core.ObjectAndRelation{
Namespace: resourceType,
ObjectId: resourceID,
Relation: relation,
},
Subject: subject.ToCoreONR(),
Result: result,
}
}
// CheckHintForArrow creates a CheckHint for an arrow and a subject.
func CheckHintForArrow(resourceType string, resourceID string, tuplesetRelation string, computedUsersetRelation string, subject tuple.ObjectAndRelation, result *v1.ResourceCheckResult) *v1.CheckHint {
return &v1.CheckHint{
Resource: &core.ObjectAndRelation{
Namespace: resourceType,
ObjectId: resourceID,
Relation: tuplesetRelation,
},
TtuComputedUsersetRelation: computedUsersetRelation,
Subject: subject.ToCoreONR(),
Result: result,
}
}
// AsCheckHintForComputedUserset returns the resourceID if the checkHint is for the given relation and subject.
func AsCheckHintForComputedUserset(checkHint *v1.CheckHint, resourceType string, relationName string, subject tuple.ObjectAndRelation) (string, bool) {
if checkHint.TtuComputedUsersetRelation != "" {
return "", false
}
if checkHint.Resource.Namespace == resourceType && checkHint.Resource.Relation == relationName && checkHint.Subject.EqualVT(subject.ToCoreONR()) {
return checkHint.Resource.ObjectId, true
}
return "", false
}
// AsCheckHintForArrow returns the resourceID if the checkHint is for the given arrow and subject.
func AsCheckHintForArrow(checkHint *v1.CheckHint, resourceType string, tuplesetRelation string, computedUsersetRelation string, subject tuple.ObjectAndRelation) (string, bool) {
if checkHint.TtuComputedUsersetRelation != computedUsersetRelation {
return "", false
}
if checkHint.Resource.Namespace == resourceType && checkHint.Resource.Relation == tuplesetRelation && checkHint.Subject.EqualVT(subject.ToCoreONR()) {
return checkHint.Resource.ObjectId, true
}
return "", false
}
// HintForEntrypoint returns a CheckHint for the given reachability graph entrypoint and associated subject and result.
func HintForEntrypoint(re schema.ReachabilityEntrypoint, resourceID string, subject tuple.ObjectAndRelation, result *v1.ResourceCheckResult) (*v1.CheckHint, error) {
switch re.EntrypointKind() {
case core.ReachabilityEntrypoint_RELATION_ENTRYPOINT:
return nil, spiceerrors.MustBugf("cannot call CheckHintForResource for kind %v", re.EntrypointKind())
case core.ReachabilityEntrypoint_TUPLESET_TO_USERSET_ENTRYPOINT:
namespace := re.TargetNamespace()
tuplesetRelation, err := re.TuplesetRelation()
if err != nil {
return nil, err
}
computedUsersetRelation, err := re.ComputedUsersetRelation()
if err != nil {
return nil, err
}
return CheckHintForArrow(namespace, resourceID, tuplesetRelation, computedUsersetRelation, subject, result), nil
case core.ReachabilityEntrypoint_COMPUTED_USERSET_ENTRYPOINT:
namespace := re.TargetNamespace()
relation, err := re.ComputedUsersetRelation()
if err != nil {
return nil, err
}
return CheckHintForComputedUserset(namespace, resourceID, relation, subject, result), nil
default:
return nil, spiceerrors.MustBugf("unknown relation entrypoint kind")
}
}
|