-
Notifications
You must be signed in to change notification settings - Fork 246
/
validation.go
268 lines (228 loc) · 8.21 KB
/
validation.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
package relationships
import (
"context"
"github.com/samber/lo"
"github.com/authzed/spicedb/internal/namespace"
"github.com/authzed/spicedb/pkg/caveats"
"github.com/authzed/spicedb/pkg/datastore"
"github.com/authzed/spicedb/pkg/genutil/mapz"
ns "github.com/authzed/spicedb/pkg/namespace"
core "github.com/authzed/spicedb/pkg/proto/core/v1"
"github.com/authzed/spicedb/pkg/spiceerrors"
"github.com/authzed/spicedb/pkg/tuple"
"github.com/authzed/spicedb/pkg/typesystem"
)
// ValidateRelationshipUpdates performs validation on the given relationship updates, ensuring that
// they can be applied against the datastore.
func ValidateRelationshipUpdates(
ctx context.Context,
reader datastore.Reader,
updates []*core.RelationTupleUpdate,
) error {
rels := lo.Map(updates, func(item *core.RelationTupleUpdate, _ int) *core.RelationTuple {
return item.Tuple
})
// Load namespaces and caveats.
referencedNamespaceMap, referencedCaveatMap, err := loadNamespacesAndCaveats(ctx, rels, reader)
if err != nil {
return err
}
// Validate each updates's types.
for _, update := range updates {
option := ValidateRelationshipForCreateOrTouch
if update.Operation == core.RelationTupleUpdate_DELETE {
option = ValidateRelationshipForDeletion
}
if err := ValidateOneRelationship(
referencedNamespaceMap,
referencedCaveatMap,
update.Tuple,
option,
); err != nil {
return err
}
}
return nil
}
// ValidateRelationshipsForCreateOrTouch performs validation on the given relationships to be written, ensuring that
// they can be applied against the datastore.
//
// NOTE: This method *cannot* be used for relationships that will be deleted.
func ValidateRelationshipsForCreateOrTouch(
ctx context.Context,
reader datastore.Reader,
rels []*core.RelationTuple,
) error {
// Load namespaces and caveats.
referencedNamespaceMap, referencedCaveatMap, err := loadNamespacesAndCaveats(ctx, rels, reader)
if err != nil {
return err
}
// Validate each relationship's types.
for _, rel := range rels {
if err := ValidateOneRelationship(
referencedNamespaceMap,
referencedCaveatMap,
rel,
ValidateRelationshipForCreateOrTouch,
); err != nil {
return err
}
}
return nil
}
func loadNamespacesAndCaveats(ctx context.Context, rels []*core.RelationTuple, reader datastore.Reader) (map[string]*typesystem.TypeSystem, map[string]*core.CaveatDefinition, error) {
referencedNamespaceNames := mapz.NewSet[string]()
referencedCaveatNamesWithContext := mapz.NewSet[string]()
for _, rel := range rels {
referencedNamespaceNames.Insert(rel.ResourceAndRelation.Namespace)
referencedNamespaceNames.Insert(rel.Subject.Namespace)
if hasNonEmptyCaveatContext(rel) {
referencedCaveatNamesWithContext.Insert(rel.Caveat.CaveatName)
}
}
var referencedNamespaceMap map[string]*typesystem.TypeSystem
var referencedCaveatMap map[string]*core.CaveatDefinition
if !referencedNamespaceNames.IsEmpty() {
foundNamespaces, err := reader.LookupNamespacesWithNames(ctx, referencedNamespaceNames.AsSlice())
if err != nil {
return nil, nil, err
}
referencedNamespaceMap = make(map[string]*typesystem.TypeSystem, len(foundNamespaces))
for _, nsDef := range foundNamespaces {
nts, err := typesystem.NewNamespaceTypeSystem(nsDef.Definition, typesystem.ResolverForDatastoreReader(reader))
if err != nil {
return nil, nil, err
}
referencedNamespaceMap[nsDef.Definition.Name] = nts
}
}
if !referencedCaveatNamesWithContext.IsEmpty() {
foundCaveats, err := reader.LookupCaveatsWithNames(ctx, referencedCaveatNamesWithContext.AsSlice())
if err != nil {
return nil, nil, err
}
referencedCaveatMap = make(map[string]*core.CaveatDefinition, len(foundCaveats))
for _, caveatDef := range foundCaveats {
referencedCaveatMap[caveatDef.Definition.Name] = caveatDef.Definition
}
}
return referencedNamespaceMap, referencedCaveatMap, nil
}
// ValidationRelationshipRule is the rule to use for the validation.
type ValidationRelationshipRule int
const (
// ValidateRelationshipForCreateOrTouch indicates that the validation should occur for a CREATE or TOUCH operation.
ValidateRelationshipForCreateOrTouch ValidationRelationshipRule = 0
// ValidateRelationshipForDeletion indicates that the validation should occur for a DELETE operation.
ValidateRelationshipForDeletion ValidationRelationshipRule = 1
)
// ValidateOneRelationship validates a single relationship for CREATE/TOUCH or DELETE.
func ValidateOneRelationship(
namespaceMap map[string]*typesystem.TypeSystem,
caveatMap map[string]*core.CaveatDefinition,
rel *core.RelationTuple,
rule ValidationRelationshipRule,
) error {
// Validate the IDs of the resource and subject.
if err := tuple.ValidateResourceID(rel.ResourceAndRelation.ObjectId); err != nil {
return err
}
if err := tuple.ValidateSubjectID(rel.Subject.ObjectId); err != nil {
return err
}
// Validate the namespace and relation for the resource.
resourceTS, ok := namespaceMap[rel.ResourceAndRelation.Namespace]
if !ok {
return namespace.NewNamespaceNotFoundErr(rel.ResourceAndRelation.Namespace)
}
if !resourceTS.HasRelation(rel.ResourceAndRelation.Relation) {
return namespace.NewRelationNotFoundErr(rel.ResourceAndRelation.Namespace, rel.ResourceAndRelation.Relation)
}
// Validate the namespace and relation for the subject.
subjectTS, ok := namespaceMap[rel.Subject.Namespace]
if !ok {
return namespace.NewNamespaceNotFoundErr(rel.Subject.Namespace)
}
if rel.Subject.Relation != tuple.Ellipsis {
if !subjectTS.HasRelation(rel.Subject.Relation) {
return namespace.NewRelationNotFoundErr(rel.Subject.Namespace, rel.Subject.Relation)
}
}
// Validate that the relationship is not writing to a permission.
if resourceTS.IsPermission(rel.ResourceAndRelation.Relation) {
return NewCannotWriteToPermissionError(rel)
}
// Validate the subject against the allowed relation(s).
var caveat *core.AllowedCaveat
if rel.Caveat != nil {
caveat = ns.AllowedCaveat(rel.Caveat.CaveatName)
}
var relationToCheck *core.AllowedRelation
if rel.Subject.ObjectId == tuple.PublicWildcard {
relationToCheck = ns.AllowedPublicNamespaceWithCaveat(rel.Subject.Namespace, caveat)
} else {
relationToCheck = ns.AllowedRelationWithCaveat(
rel.Subject.Namespace,
rel.Subject.Relation,
caveat)
}
switch {
case rule == ValidateRelationshipForCreateOrTouch || caveat != nil:
// For writing or when the caveat was specified, the caveat must be a direct match.
isAllowed, err := resourceTS.HasAllowedRelation(
rel.ResourceAndRelation.Relation,
relationToCheck)
if err != nil {
return err
}
if isAllowed != typesystem.AllowedRelationValid {
return NewInvalidSubjectTypeError(rel, relationToCheck)
}
case rule == ValidateRelationshipForDeletion && caveat == nil:
// For deletion, the caveat *can* be ignored if not specified.
if rel.Subject.ObjectId == tuple.PublicWildcard {
isAllowed, err := resourceTS.IsAllowedPublicNamespace(rel.ResourceAndRelation.Relation, rel.Subject.Namespace)
if err != nil {
return err
}
if isAllowed != typesystem.PublicSubjectAllowed {
return NewInvalidSubjectTypeError(rel, relationToCheck)
}
} else {
isAllowed, err := resourceTS.IsAllowedDirectRelation(rel.ResourceAndRelation.Relation, rel.Subject.Namespace, rel.Subject.Relation)
if err != nil {
return err
}
if isAllowed != typesystem.DirectRelationValid {
return NewInvalidSubjectTypeError(rel, relationToCheck)
}
}
default:
return spiceerrors.MustBugf("unknown validate rule")
}
// Validate caveat and its context, if applicable.
if hasNonEmptyCaveatContext(rel) {
caveat, ok := caveatMap[rel.Caveat.CaveatName]
if !ok {
// Should ideally never happen since the caveat is type checked above, but just in case.
return NewCaveatNotFoundError(rel)
}
// Verify that the provided context information matches the types of the parameters defined.
_, err := caveats.ConvertContextToParameters(
rel.Caveat.Context.AsMap(),
caveat.ParameterTypes,
caveats.ErrorForUnknownParameters,
)
if err != nil {
return err
}
}
return nil
}
func hasNonEmptyCaveatContext(update *core.RelationTuple) bool {
return update.Caveat != nil &&
update.Caveat.CaveatName != "" &&
update.Caveat.Context != nil &&
len(update.Caveat.Context.GetFields()) > 0
}