-
Notifications
You must be signed in to change notification settings - Fork 247
/
builder.go
243 lines (215 loc) · 8.18 KB
/
builder.go
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
package namespace
import (
"github.com/authzed/spicedb/pkg/caveats"
core "github.com/authzed/spicedb/pkg/proto/core/v1"
iv1 "github.com/authzed/spicedb/pkg/proto/impl/v1"
"github.com/authzed/spicedb/pkg/spiceerrors"
)
// Namespace creates a namespace definition with one or more defined relations.
func Namespace(name string, relations ...*core.Relation) *core.NamespaceDefinition {
return &core.NamespaceDefinition{
Name: name,
Relation: relations,
}
}
// WithComment creates a namespace definition with one or more defined relations.
func WithComment(name string, comment string, relations ...*core.Relation) *core.NamespaceDefinition {
nd := Namespace(name, relations...)
nd.Metadata, _ = AddComment(nd.Metadata, comment)
return nd
}
// MustRelation creates a relation definition with an optional rewrite definition.
func MustRelation(name string, rewrite *core.UsersetRewrite, allowedDirectRelations ...*core.AllowedRelation) *core.Relation {
r, err := Relation(name, rewrite, allowedDirectRelations...)
if err != nil {
panic(err)
}
return r
}
// Relation creates a relation definition with an optional rewrite definition.
func Relation(name string, rewrite *core.UsersetRewrite, allowedDirectRelations ...*core.AllowedRelation) (*core.Relation, error) {
var typeInfo *core.TypeInformation
if len(allowedDirectRelations) > 0 {
typeInfo = &core.TypeInformation{
AllowedDirectRelations: allowedDirectRelations,
}
}
rel := &core.Relation{
Name: name,
UsersetRewrite: rewrite,
TypeInformation: typeInfo,
}
switch {
case rewrite != nil && len(allowedDirectRelations) == 0:
if err := SetRelationKind(rel, iv1.RelationMetadata_PERMISSION); err != nil {
return nil, spiceerrors.MustBugf("failed to set relation kind: %s", err.Error())
}
case rewrite == nil && len(allowedDirectRelations) > 0:
if err := SetRelationKind(rel, iv1.RelationMetadata_RELATION); err != nil {
return nil, spiceerrors.MustBugf("failed to set relation kind: %s", err.Error())
}
default:
// By default we do not set a relation kind on the relation. Relations without any
// information, or relations with both rewrites and types are legacy relations from
// before the DSL schema and, as such, do not have a defined "kind".
}
return rel, nil
}
// MustRelationWithComment creates a relation definition with an optional rewrite definition.
func MustRelationWithComment(name string, comment string, rewrite *core.UsersetRewrite, allowedDirectRelations ...*core.AllowedRelation) *core.Relation {
rel := MustRelation(name, rewrite, allowedDirectRelations...)
rel.Metadata, _ = AddComment(rel.Metadata, comment)
return rel
}
// AllowedRelation creates a relation reference to an allowed relation.
func AllowedRelation(namespaceName string, relationName string) *core.AllowedRelation {
return &core.AllowedRelation{
Namespace: namespaceName,
RelationOrWildcard: &core.AllowedRelation_Relation{
Relation: relationName,
},
}
}
// AllowedRelationWithCaveat creates a relation reference to an allowed relation.
func AllowedRelationWithCaveat(namespaceName string, relationName string, withCaveat *core.AllowedCaveat) *core.AllowedRelation {
return &core.AllowedRelation{
Namespace: namespaceName,
RelationOrWildcard: &core.AllowedRelation_Relation{
Relation: relationName,
},
RequiredCaveat: withCaveat,
}
}
// AllowedPublicNamespace creates a relation reference to an allowed public namespace.
func AllowedPublicNamespace(namespaceName string) *core.AllowedRelation {
return &core.AllowedRelation{
Namespace: namespaceName,
RelationOrWildcard: &core.AllowedRelation_PublicWildcard_{
PublicWildcard: &core.AllowedRelation_PublicWildcard{},
},
}
}
// AllowedCaveat creates a caveat reference.
func AllowedCaveat(name string) *core.AllowedCaveat {
return &core.AllowedCaveat{
CaveatName: name,
}
}
// CaveatDefinition returns a new caveat definition.
func CaveatDefinition(env *caveats.Environment, name string, expr string) (*core.CaveatDefinition, error) {
compiled, err := caveats.CompileCaveatWithName(env, expr, name)
if err != nil {
return nil, err
}
return CompiledCaveatDefinition(env, name, compiled)
}
// CompiledCaveatDefinition returns a new caveat definition.
func CompiledCaveatDefinition(env *caveats.Environment, name string, compiled *caveats.CompiledCaveat) (*core.CaveatDefinition, error) {
serialized, err := compiled.Serialize()
if err != nil {
return nil, err
}
return &core.CaveatDefinition{
Name: name,
SerializedExpression: serialized,
ParameterTypes: env.EncodedParametersTypes(),
}, nil
}
// MustCaveatDefinition returns a new caveat definition.
func MustCaveatDefinition(env *caveats.Environment, name string, expr string) *core.CaveatDefinition {
cd, err := CaveatDefinition(env, name, expr)
if err != nil {
panic(err)
}
return cd
}
// AllowedPublicNamespaceWithCaveat creates a relation reference to an allowed public namespace.
func AllowedPublicNamespaceWithCaveat(namespaceName string, withCaveat *core.AllowedCaveat) *core.AllowedRelation {
return &core.AllowedRelation{
Namespace: namespaceName,
RelationOrWildcard: &core.AllowedRelation_PublicWildcard_{
PublicWildcard: &core.AllowedRelation_PublicWildcard{},
},
RequiredCaveat: withCaveat,
}
}
// RelationReference creates a relation reference.
func RelationReference(namespaceName string, relationName string) *core.RelationReference {
return &core.RelationReference{
Namespace: namespaceName,
Relation: relationName,
}
}
// Union creates a rewrite definition that combines/considers usersets in all children.
func Union(firstChild *core.SetOperation_Child, rest ...*core.SetOperation_Child) *core.UsersetRewrite {
return &core.UsersetRewrite{
RewriteOperation: &core.UsersetRewrite_Union{
Union: setOperation(firstChild, rest),
},
}
}
// Intersection creates a rewrite definition that returns/considers only usersets present in all children.
func Intersection(firstChild *core.SetOperation_Child, rest ...*core.SetOperation_Child) *core.UsersetRewrite {
return &core.UsersetRewrite{
RewriteOperation: &core.UsersetRewrite_Intersection{
Intersection: setOperation(firstChild, rest),
},
}
}
// Exclusion creates a rewrite definition that starts with the usersets of the first child
// and iteratively removes usersets that appear in the remaining children.
func Exclusion(firstChild *core.SetOperation_Child, rest ...*core.SetOperation_Child) *core.UsersetRewrite {
return &core.UsersetRewrite{
RewriteOperation: &core.UsersetRewrite_Exclusion{
Exclusion: setOperation(firstChild, rest),
},
}
}
func setOperation(firstChild *core.SetOperation_Child, rest []*core.SetOperation_Child) *core.SetOperation {
children := append([]*core.SetOperation_Child{firstChild}, rest...)
return &core.SetOperation{
Child: children,
}
}
// Nil creates a child for a set operation that references the empty set.
func Nil() *core.SetOperation_Child {
return &core.SetOperation_Child{
ChildType: &core.SetOperation_Child_XNil{},
}
}
// ComputesUserset creates a child for a set operation that follows a relation on the given starting object.
func ComputedUserset(relation string) *core.SetOperation_Child {
return &core.SetOperation_Child{
ChildType: &core.SetOperation_Child_ComputedUserset{
ComputedUserset: &core.ComputedUserset{
Relation: relation,
},
},
}
}
// TupleToUserset creates a child which first loads all tuples with the specific relation,
// and then unions all children on the usersets found by following a relation on those loaded
// tuples.
func TupleToUserset(tuplesetRelation, usersetRelation string) *core.SetOperation_Child {
return &core.SetOperation_Child{
ChildType: &core.SetOperation_Child_TupleToUserset{
TupleToUserset: &core.TupleToUserset{
Tupleset: &core.TupleToUserset_Tupleset{
Relation: tuplesetRelation,
},
ComputedUserset: &core.ComputedUserset{
Relation: usersetRelation,
Object: core.ComputedUserset_TUPLE_USERSET_OBJECT,
},
},
},
}
}
// Rewrite wraps a rewrite as a set operation child of another rewrite.
func Rewrite(rewrite *core.UsersetRewrite) *core.SetOperation_Child {
return &core.SetOperation_Child{
ChildType: &core.SetOperation_Child_UsersetRewrite{
UsersetRewrite: rewrite,
},
}
}