/
tuple.go
325 lines (280 loc) · 9.83 KB
/
tuple.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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package tuple
import (
"fmt"
"regexp"
core "github.com/authzed/spicedb/pkg/proto/core/v1"
v1 "github.com/authzed/authzed-go/proto/authzed/api/v1"
"github.com/jzelinskie/stringz"
)
const (
// Ellipsis is the Ellipsis relation in v0 style subjects.
Ellipsis = "..."
// PublicWildcard is the wildcard value for subject object IDs that indicates public access
// for the subject type.
PublicWildcard = "*"
)
const (
namespaceNameExpr = "([a-z][a-z0-9_]{1,61}[a-z0-9]/)?[a-z][a-z0-9_]{1,62}[a-z0-9]"
resourceIDExpr = "[a-zA-Z0-9_][a-zA-Z0-9/_|-]{0,127}"
subjectIDExpr = "([a-zA-Z0-9_][a-zA-Z0-9/_|-]{0,127})|\\*"
relationExpr = "[a-z][a-z0-9_]{1,62}[a-z0-9]"
)
var onrExpr = fmt.Sprintf(
`(?P<resourceType>(%s)):(?P<resourceID>%s)#(?P<resourceRel>%s)`,
namespaceNameExpr,
resourceIDExpr,
relationExpr,
)
var subjectExpr = fmt.Sprintf(
`(?P<subjectType>(%s)):(?P<subjectID>%s)(#(?P<subjectRel>%s|\.\.\.))?`,
namespaceNameExpr,
subjectIDExpr,
relationExpr,
)
var (
onrRegex = regexp.MustCompile(fmt.Sprintf("^%s$", onrExpr))
subjectRegex = regexp.MustCompile(fmt.Sprintf("^%s$", subjectExpr))
resourceIDRegex = regexp.MustCompile(fmt.Sprintf("^%s$", resourceIDExpr))
subjectIDRegex = regexp.MustCompile(fmt.Sprintf("^%s$", subjectIDExpr))
)
var parserRegex = regexp.MustCompile(
fmt.Sprintf(
`^%s@%s$`,
onrExpr,
subjectExpr,
),
)
// ValidateResourceID ensures that the given resource ID is valid. Returns an error if not.
func ValidateResourceID(objectID string) error {
if !resourceIDRegex.MatchString(objectID) {
return fmt.Errorf("invalid resource id; must be alphanumeric and between 1 and 127 characters")
}
return nil
}
// ValidateSubjectID ensures that the given object ID (under a subject reference) is valid. Returns an error if not.
func ValidateSubjectID(subjectID string) error {
if !subjectIDRegex.MatchString(subjectID) {
return fmt.Errorf("invalid subject id; must be alphanumeric and between 1 and 127 characters or a star for public")
}
return nil
}
// String converts a tuple to a string. If the tuple is nil or empty, returns empty string.
func String(tpl *core.RelationTuple) string {
if tpl == nil || tpl.ResourceAndRelation == nil || tpl.Subject == nil {
return ""
}
return fmt.Sprintf("%s@%s", StringONR(tpl.ResourceAndRelation), StringONR(tpl.Subject))
}
// MustRelString converts a relationship into a string. Will panic if
// the Relationship does not validate.
func MustRelString(tpl *v1.Relationship) string {
return String(MustFromRelationship(tpl))
}
// MustParse wraps Parse such that any failures panic rather than returning
// nil.
func MustParse(tpl string) *core.RelationTuple {
if parsed := Parse(tpl); parsed != nil {
return parsed
}
panic("failed to parse tuple")
}
// RelString converts a relationship into a string.
func RelString(tpl *v1.Relationship) string {
return String(FromRelationship(tpl))
}
// Parse unmarshals the string form of a Tuple and returns nil if there is a
// failure.
//
// This function treats both missing and Ellipsis relations equally.
func Parse(tpl string) *core.RelationTuple {
groups := parserRegex.FindStringSubmatch(tpl)
if len(groups) == 0 {
return nil
}
subjectRelation := Ellipsis
subjectRelIndex := stringz.SliceIndex(parserRegex.SubexpNames(), "subjectRel")
if len(groups[subjectRelIndex]) > 0 {
subjectRelation = groups[subjectRelIndex]
}
return &core.RelationTuple{
ResourceAndRelation: &core.ObjectAndRelation{
Namespace: groups[stringz.SliceIndex(parserRegex.SubexpNames(), "resourceType")],
ObjectId: groups[stringz.SliceIndex(parserRegex.SubexpNames(), "resourceID")],
Relation: groups[stringz.SliceIndex(parserRegex.SubexpNames(), "resourceRel")],
},
Subject: &core.ObjectAndRelation{
Namespace: groups[stringz.SliceIndex(parserRegex.SubexpNames(), "subjectType")],
ObjectId: groups[stringz.SliceIndex(parserRegex.SubexpNames(), "subjectID")],
Relation: subjectRelation,
},
}
}
func ParseRel(rel string) *v1.Relationship {
tpl := Parse(rel)
if tpl == nil {
return nil
}
return ToRelationship(tpl)
}
func Create(tpl *core.RelationTuple) *core.RelationTupleUpdate {
return &core.RelationTupleUpdate{
Operation: core.RelationTupleUpdate_CREATE,
Tuple: tpl,
}
}
func Touch(tpl *core.RelationTuple) *core.RelationTupleUpdate {
return &core.RelationTupleUpdate{
Operation: core.RelationTupleUpdate_TOUCH,
Tuple: tpl,
}
}
func Delete(tpl *core.RelationTuple) *core.RelationTupleUpdate {
return &core.RelationTupleUpdate{
Operation: core.RelationTupleUpdate_DELETE,
Tuple: tpl,
}
}
// MustToRelationship converts a RelationTuple into a Relationship. Will panic if
// the RelationTuple does not validate.
func MustToRelationship(tpl *core.RelationTuple) *v1.Relationship {
if err := tpl.Validate(); err != nil {
panic(fmt.Sprintf("invalid tuple: %#v %s", tpl, err))
}
return ToRelationship(tpl)
}
// ToRelationship converts a RelationTuple into a Relationship.
func ToRelationship(tpl *core.RelationTuple) *v1.Relationship {
return &v1.Relationship{
Resource: &v1.ObjectReference{
ObjectType: tpl.ResourceAndRelation.Namespace,
ObjectId: tpl.ResourceAndRelation.ObjectId,
},
Relation: tpl.ResourceAndRelation.Relation,
Subject: &v1.SubjectReference{
Object: &v1.ObjectReference{
ObjectType: tpl.Subject.Namespace,
ObjectId: tpl.Subject.ObjectId,
},
OptionalRelation: stringz.Default(tpl.Subject.Relation, "", Ellipsis),
},
}
}
// MustToFilter converts a RelationTuple into a RelationshipFilter. Will panic if
// the RelationTuple does not validate.
func MustToFilter(tpl *core.RelationTuple) *v1.RelationshipFilter {
if err := tpl.Validate(); err != nil {
panic(fmt.Sprintf("invalid tuple: %#v %s", tpl, err))
}
return ToFilter(tpl)
}
// ToFilter converts a RelationTuple into a RelationshipFilter.
func ToFilter(tpl *core.RelationTuple) *v1.RelationshipFilter {
return &v1.RelationshipFilter{
ResourceType: tpl.ResourceAndRelation.Namespace,
OptionalResourceId: tpl.ResourceAndRelation.ObjectId,
OptionalRelation: tpl.ResourceAndRelation.Relation,
OptionalSubjectFilter: UsersetToSubjectFilter(tpl.Subject),
}
}
// UsersetToSubjectFilter converts a userset to the equivalent exact SubjectFilter.
func UsersetToSubjectFilter(userset *core.ObjectAndRelation) *v1.SubjectFilter {
return &v1.SubjectFilter{
SubjectType: userset.Namespace,
OptionalSubjectId: userset.ObjectId,
OptionalRelation: &v1.SubjectFilter_RelationFilter{
Relation: stringz.Default(userset.Relation, "", Ellipsis),
},
}
}
// MustRelToFilter converts a Relationship into a RelationshipFilter. Will panic if
// the Relationship does not validate.
func MustRelToFilter(rel *v1.Relationship) *v1.RelationshipFilter {
if err := rel.Validate(); err != nil {
panic(fmt.Sprintf("invalid tuple: %#v %s", rel, err))
}
return RelToFilter(rel)
}
// RelToFilter converts a Relationship into a RelationshipFilter.
func RelToFilter(rel *v1.Relationship) *v1.RelationshipFilter {
return &v1.RelationshipFilter{
ResourceType: rel.Resource.ObjectType,
OptionalResourceId: rel.Resource.ObjectId,
OptionalRelation: rel.Relation,
OptionalSubjectFilter: &v1.SubjectFilter{
SubjectType: rel.Subject.Object.ObjectType,
OptionalSubjectId: rel.Subject.Object.ObjectId,
OptionalRelation: &v1.SubjectFilter_RelationFilter{
Relation: rel.Subject.OptionalRelation,
},
},
}
}
// UpdatesToRelationshipUpdates converts a slice of RelationTupleUpdate into a
// slice of RelationshipUpdate.
func UpdatesToRelationshipUpdates(updates []*core.RelationTupleUpdate) []*v1.RelationshipUpdate {
relationshipUpdates := make([]*v1.RelationshipUpdate, 0, len(updates))
for _, update := range updates {
relationshipUpdates = append(relationshipUpdates, UpdateToRelationshipUpdate(update))
}
return relationshipUpdates
}
// UpdateToRelationshipUpdate converts a RelationTupleUpdate into a
// RelationshipUpdate.
func UpdateToRelationshipUpdate(update *core.RelationTupleUpdate) *v1.RelationshipUpdate {
var op v1.RelationshipUpdate_Operation
switch update.Operation {
case core.RelationTupleUpdate_CREATE:
op = v1.RelationshipUpdate_OPERATION_CREATE
case core.RelationTupleUpdate_DELETE:
op = v1.RelationshipUpdate_OPERATION_DELETE
case core.RelationTupleUpdate_TOUCH:
op = v1.RelationshipUpdate_OPERATION_TOUCH
default:
panic("unknown tuple mutation")
}
return &v1.RelationshipUpdate{
Operation: op,
Relationship: ToRelationship(update.Tuple),
}
}
// MustFromRelationship converts a Relationship into a RelationTuple.
func MustFromRelationship(r *v1.Relationship) *core.RelationTuple {
if err := r.Validate(); err != nil {
panic(fmt.Sprintf("invalid relationship: %#v %s", r, err))
}
return FromRelationship(r)
}
// FromRelationship converts a Relationship into a RelationTuple.
func FromRelationship(r *v1.Relationship) *core.RelationTuple {
return &core.RelationTuple{
ResourceAndRelation: &core.ObjectAndRelation{
Namespace: r.Resource.ObjectType,
ObjectId: r.Resource.ObjectId,
Relation: r.Relation,
},
Subject: &core.ObjectAndRelation{
Namespace: r.Subject.Object.ObjectType,
ObjectId: r.Subject.Object.ObjectId,
Relation: stringz.DefaultEmpty(r.Subject.OptionalRelation, Ellipsis),
},
}
}
// UpdateFromRelationshipUpdate converts a RelationshipUpdate into a
// RelationTupleUpdate.
func UpdateFromRelationshipUpdate(update *v1.RelationshipUpdate) *core.RelationTupleUpdate {
var op core.RelationTupleUpdate_Operation
switch update.Operation {
case v1.RelationshipUpdate_OPERATION_CREATE:
op = core.RelationTupleUpdate_CREATE
case v1.RelationshipUpdate_OPERATION_DELETE:
op = core.RelationTupleUpdate_DELETE
case v1.RelationshipUpdate_OPERATION_TOUCH:
op = core.RelationTupleUpdate_TOUCH
default:
panic("unknown tuple mutation")
}
return &core.RelationTupleUpdate{
Operation: op,
Tuple: FromRelationship(update.Relationship),
}
}