-
Notifications
You must be signed in to change notification settings - Fork 246
/
validation.go
267 lines (233 loc) · 9.96 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
package development
import (
"fmt"
"sort"
"strings"
"github.com/google/go-cmp/cmp"
yaml "gopkg.in/yaml.v2"
"github.com/authzed/spicedb/internal/developmentmembership"
devinterface "github.com/authzed/spicedb/pkg/proto/developer/v1"
v1 "github.com/authzed/spicedb/pkg/proto/dispatch/v1"
"github.com/authzed/spicedb/pkg/tuple"
"github.com/authzed/spicedb/pkg/validationfile/blocks"
)
// RunValidation runs the parsed validation block against the data in the dev context.
func RunValidation(devContext *DevContext, validation *blocks.ParsedExpectedRelations) (*developmentmembership.Set, []*devinterface.DeveloperError, error) {
var failures []*devinterface.DeveloperError
membershipSet := developmentmembership.NewMembershipSet()
ctx := devContext.Ctx
for onrKey, expectedSubjects := range validation.ValidationMap {
if onrKey.ObjectAndRelation == nil {
return nil, nil, fmt.Errorf("got nil ObjectAndRelation for key %s", onrKey.ObjectRelationString)
}
// Run a full recursive expansion over the ONR.
er, derr := devContext.Dispatcher.DispatchExpand(ctx, &v1.DispatchExpandRequest{
ResourceAndRelation: onrKey.ObjectAndRelation,
Metadata: &v1.ResolverMeta{
AtRevision: devContext.Revision.String(),
DepthRemaining: maxDispatchDepth,
},
ExpansionMode: v1.DispatchExpandRequest_RECURSIVE,
})
if derr != nil {
devErr, wireErr := DistinguishGraphError(devContext, derr, devinterface.DeveloperError_VALIDATION_YAML, 0, 0, onrKey.ObjectRelationString)
if wireErr != nil {
return nil, nil, wireErr
}
failures = append(failures, devErr)
continue
}
// Add the ONR and its expansion to the membership set.
foundSubjects, _, aerr := membershipSet.AddExpansion(onrKey.ObjectAndRelation, er.TreeNode)
if aerr != nil {
devErr, wireErr := DistinguishGraphError(devContext, aerr, devinterface.DeveloperError_VALIDATION_YAML, 0, 0, onrKey.ObjectRelationString)
if wireErr != nil {
return nil, nil, wireErr
}
failures = append(failures, devErr)
continue
}
// Compare the terminal subjects found to those specified.
errs := validateSubjects(onrKey, foundSubjects, expectedSubjects)
failures = append(failures, errs...)
}
if len(failures) > 0 {
return membershipSet, failures, nil
}
return membershipSet, nil, nil
}
func wrapRelationships(onrStrings []string) []string {
wrapped := make([]string, 0, len(onrStrings))
for _, str := range onrStrings {
wrapped = append(wrapped, "<"+str+">")
}
// Sort to ensure stability.
sort.Strings(wrapped)
return wrapped
}
func validateSubjects(onrKey blocks.ObjectRelation, fs developmentmembership.FoundSubjects, expectedSubjects []blocks.ExpectedSubject) []*devinterface.DeveloperError {
onr := onrKey.ObjectAndRelation
var failures []*devinterface.DeveloperError
// Verify that every referenced subject is found in the membership.
encounteredSubjects := map[string]struct{}{}
for _, expectedSubject := range expectedSubjects {
subjectWithExceptions := expectedSubject.SubjectWithExceptions
if subjectWithExceptions == nil {
failures = append(failures, &devinterface.DeveloperError{
Message: fmt.Sprintf("For object and permission/relation `%s`, no expected subject specified in `%s`", tuple.StringONR(onr), expectedSubject.ValidationString),
Source: devinterface.DeveloperError_VALIDATION_YAML,
Kind: devinterface.DeveloperError_MISSING_EXPECTED_RELATIONSHIP,
Context: string(expectedSubject.ValidationString),
Line: uint32(expectedSubject.SourcePosition.LineNumber),
Column: uint32(expectedSubject.SourcePosition.ColumnPosition),
})
continue
}
encounteredSubjects[tuple.StringONR(subjectWithExceptions.Subject.Subject)] = struct{}{}
subject, ok := fs.LookupSubject(subjectWithExceptions.Subject.Subject)
if !ok {
failures = append(failures, &devinterface.DeveloperError{
Message: fmt.Sprintf("For object and permission/relation `%s`, missing expected subject `%s`", tuple.StringONR(onr), tuple.StringONR(subjectWithExceptions.Subject.Subject)),
Source: devinterface.DeveloperError_VALIDATION_YAML,
Kind: devinterface.DeveloperError_MISSING_EXPECTED_RELATIONSHIP,
Context: string(expectedSubject.ValidationString),
Line: uint32(expectedSubject.SourcePosition.LineNumber),
Column: uint32(expectedSubject.SourcePosition.ColumnPosition),
})
continue
}
foundRelationships := subject.Relationships()
// Verify that the relationships are the same.
expectedONRStrings := tuple.StringsONRs(expectedSubject.Resources)
foundONRStrings := tuple.StringsONRs(foundRelationships)
if !cmp.Equal(expectedONRStrings, foundONRStrings) {
failures = append(failures, &devinterface.DeveloperError{
Message: fmt.Sprintf("For object and permission/relation `%s`, found different relationships for subject `%s`: Specified: `%s`, Computed: `%s`",
tuple.StringONR(onr),
tuple.StringONR(subjectWithExceptions.Subject.Subject),
strings.Join(wrapRelationships(expectedONRStrings), "/"),
strings.Join(wrapRelationships(foundONRStrings), "/"),
),
Source: devinterface.DeveloperError_VALIDATION_YAML,
Kind: devinterface.DeveloperError_MISSING_EXPECTED_RELATIONSHIP,
Context: string(expectedSubject.ValidationString),
Line: uint32(expectedSubject.SourcePosition.LineNumber),
Column: uint32(expectedSubject.SourcePosition.ColumnPosition),
})
}
// Verify exclusions are the same, if any.
foundExcludedSubjects, isWildcard := subject.ExcludedSubjectsFromWildcard()
expectedExcludedSubjects := subjectWithExceptions.Exceptions
if isWildcard {
expectedExcludedStrings := toExpectedRelationshipsStrings(expectedExcludedSubjects)
foundExcludedONRStrings := toFoundRelationshipsStrings(foundExcludedSubjects)
if !cmp.Equal(expectedExcludedStrings, foundExcludedONRStrings) {
failures = append(failures, &devinterface.DeveloperError{
Message: fmt.Sprintf("For object and permission/relation `%s`, found different excluded subjects for subject `%s`: Specified: `%s`, Computed: `%s`",
tuple.StringONR(onr),
tuple.StringONR(subjectWithExceptions.Subject.Subject),
strings.Join(wrapRelationships(expectedExcludedStrings), ", "),
strings.Join(wrapRelationships(foundExcludedONRStrings), ", "),
),
Source: devinterface.DeveloperError_VALIDATION_YAML,
Kind: devinterface.DeveloperError_MISSING_EXPECTED_RELATIONSHIP,
Context: string(expectedSubject.ValidationString),
Line: uint32(expectedSubject.SourcePosition.LineNumber),
Column: uint32(expectedSubject.SourcePosition.ColumnPosition),
})
}
} else {
if len(expectedExcludedSubjects) > 0 {
failures = append(failures, &devinterface.DeveloperError{
Message: fmt.Sprintf("For object and permission/relation `%s`, found unexpected excluded subjects",
tuple.StringONR(onr),
),
Source: devinterface.DeveloperError_VALIDATION_YAML,
Kind: devinterface.DeveloperError_EXTRA_RELATIONSHIP_FOUND,
Context: string(expectedSubject.ValidationString),
Line: uint32(expectedSubject.SourcePosition.LineNumber),
Column: uint32(expectedSubject.SourcePosition.ColumnPosition),
})
}
}
// Verify caveats.
if (subject.GetCaveatExpression() != nil) != subjectWithExceptions.Subject.IsCaveated {
failures = append(failures, &devinterface.DeveloperError{
Message: fmt.Sprintf("For object and permission/relation `%s`, found caveat mismatch",
tuple.StringONR(onr),
),
Source: devinterface.DeveloperError_VALIDATION_YAML,
Kind: devinterface.DeveloperError_MISSING_EXPECTED_RELATIONSHIP,
Context: string(expectedSubject.ValidationString),
Line: uint32(expectedSubject.SourcePosition.LineNumber),
Column: uint32(expectedSubject.SourcePosition.ColumnPosition),
})
}
}
// Verify that every subject found was referenced.
for _, foundSubject := range fs.ListFound() {
_, ok := encounteredSubjects[tuple.StringONR(foundSubject.Subject())]
if !ok {
failures = append(failures, &devinterface.DeveloperError{
Message: fmt.Sprintf("For object and permission/relation `%s`, subject `%s` found but missing from specified",
tuple.StringONR(onr),
tuple.StringONR(foundSubject.Subject()),
),
Source: devinterface.DeveloperError_VALIDATION_YAML,
Kind: devinterface.DeveloperError_EXTRA_RELATIONSHIP_FOUND,
Context: tuple.StringONR(onr),
Line: uint32(onrKey.SourcePosition.LineNumber),
Column: uint32(onrKey.SourcePosition.ColumnPosition),
})
}
}
return failures
}
// GenerateValidation generates the validation block based on a membership set.
func GenerateValidation(membershipSet *developmentmembership.Set) (string, error) {
validationMap := map[string][]string{}
subjectsByONR := membershipSet.SubjectsByONR()
onrStrings := make([]string, 0, len(subjectsByONR))
for onrString := range subjectsByONR {
onrStrings = append(onrStrings, onrString)
}
// Sort to ensure stability of output.
sort.Strings(onrStrings)
for _, onrString := range onrStrings {
foundSubjects := subjectsByONR[onrString]
var strs []string
for _, fs := range foundSubjects.ListFound() {
strs = append(strs,
fmt.Sprintf("[%s] is %s",
fs.ToValidationString(),
strings.Join(wrapRelationships(tuple.StringsONRs(fs.Relationships())), "/"),
))
}
// Sort to ensure stability of output.
sort.Strings(strs)
validationMap[onrString] = strs
}
contents, err := yaml.Marshal(validationMap)
if err != nil {
return "", err
}
return string(contents), nil
}
func toExpectedRelationshipsStrings(subs []blocks.SubjectAndCaveat) []string {
mapped := make([]string, 0, len(subs))
for _, sub := range subs {
if sub.IsCaveated {
mapped = append(mapped, tuple.StringONR(sub.Subject)+"[...]")
} else {
mapped = append(mapped, tuple.StringONR(sub.Subject))
}
}
return mapped
}
func toFoundRelationshipsStrings(subs []developmentmembership.FoundSubject) []string {
mapped := make([]string, 0, len(subs))
for _, sub := range subs {
mapped = append(mapped, sub.ToValidationString())
}
return mapped
}