-
Notifications
You must be signed in to change notification settings - Fork 245
/
expectedrelations.go
247 lines (203 loc) · 8.15 KB
/
expectedrelations.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
package blocks
import (
"fmt"
"regexp"
"strings"
"github.com/jzelinskie/stringz"
yamlv3 "gopkg.in/yaml.v3"
core "github.com/authzed/spicedb/pkg/proto/core/v1"
"github.com/authzed/spicedb/pkg/spiceerrors"
"github.com/authzed/spicedb/pkg/tuple"
)
// ParsedExpectedRelations represents the expected relations defined in the validation
// file.
type ParsedExpectedRelations struct {
// ValidationMap is the parsed expected relations validation map.
ValidationMap ValidationMap
// SourcePosition is the position of the expected relations in the file.
SourcePosition spiceerrors.SourcePosition
}
// UnmarshalYAML is a custom unmarshaller.
func (per *ParsedExpectedRelations) UnmarshalYAML(node *yamlv3.Node) error {
err := node.Decode(&per.ValidationMap)
if err != nil {
return convertYamlError(err)
}
per.SourcePosition = spiceerrors.SourcePosition{LineNumber: node.Line, ColumnPosition: node.Column}
return nil
}
// ValidationMap is a map from an Object Relation (as a Relationship) to the
// validation strings containing the Subjects for that Object Relation.
type ValidationMap map[ObjectRelation][]ExpectedSubject
// ObjectRelation represents an ONR defined as a string in the key for
// the ValidationMap.
type ObjectRelation struct {
// ObjectRelationString is the string form of the object relation.
ObjectRelationString string
// ObjectAndRelation is the parsed object and relation.
ObjectAndRelation *core.ObjectAndRelation
// SourcePosition is the position of the expected relations in the file.
SourcePosition spiceerrors.SourcePosition
}
// UnmarshalYAML is a custom unmarshaller.
func (ors *ObjectRelation) UnmarshalYAML(node *yamlv3.Node) error {
err := node.Decode(&ors.ObjectRelationString)
if err != nil {
return convertYamlError(err)
}
parsed := tuple.ParseONR(ors.ObjectRelationString)
if parsed == nil {
return spiceerrors.NewErrorWithSource(
fmt.Errorf("could not parse %s", ors.ObjectRelationString),
ors.ObjectRelationString,
uint64(node.Line),
uint64(node.Column),
)
}
ors.ObjectAndRelation = parsed
ors.SourcePosition = spiceerrors.SourcePosition{LineNumber: node.Line, ColumnPosition: node.Column}
return nil
}
var (
vsSubjectRegex = regexp.MustCompile(`(.*?)\[(?P<user_str>.*)](.*?)`)
vsObjectAndRelationRegex = regexp.MustCompile(`(.*?)<(?P<onr_str>[^>]+)>(.*?)`)
vsSubjectWithExceptionsOrCaveatRegex = regexp.MustCompile(`^(?P<subject_onr>[^]\s]+)(?P<caveat>\[\.\.\.])?(\s+-\s+\{(?P<exceptions>[^}]+)})?$`)
)
// ExpectedSubject is a subject expected for the ObjectAndRelation.
type ExpectedSubject struct {
// ValidationString holds a validation string containing a Subject and one or
// more Relations to the parent Object.
// Example: `[tenant/user:someuser#...] is <tenant/document:example#viewer>`
ValidationString ValidationString
// Subject is the subject expected. May be nil if not defined in the line.
SubjectWithExceptions *SubjectWithExceptions
// Resources are the resources under which the subject is found.
Resources []*core.ObjectAndRelation
// SourcePosition is the position of the expected subject in the file.
SourcePosition spiceerrors.SourcePosition
}
// SubjectAndCaveat returns a subject and whether it is caveated.
type SubjectAndCaveat struct {
// Subject is the subject found.
Subject *core.ObjectAndRelation
// IsCaveated indicates whether the subject is caveated.
IsCaveated bool
}
// SubjectWithExceptions returns the subject found in a validation string, along with any exceptions.
type SubjectWithExceptions struct {
// Subject is the subject found.
Subject SubjectAndCaveat
// Exceptions are those subjects removed from the subject, if it is a wildcard.
Exceptions []SubjectAndCaveat
}
// UnmarshalYAML is a custom unmarshaller.
func (es *ExpectedSubject) UnmarshalYAML(node *yamlv3.Node) error {
err := node.Decode(&es.ValidationString)
if err != nil {
return convertYamlError(err)
}
subjectWithExceptions, subErr := es.ValidationString.Subject()
if subErr != nil {
return spiceerrors.NewErrorWithSource(
subErr,
subErr.SourceCodeString,
uint64(node.Line)+subErr.LineNumber,
uint64(node.Column)+subErr.ColumnPosition,
)
}
onrs, onrErr := es.ValidationString.ONRS()
if onrErr != nil {
return spiceerrors.NewErrorWithSource(
onrErr,
onrErr.SourceCodeString,
uint64(node.Line)+onrErr.LineNumber,
uint64(node.Column)+onrErr.ColumnPosition,
)
}
es.SubjectWithExceptions = subjectWithExceptions
es.SourcePosition = spiceerrors.SourcePosition{LineNumber: node.Line, ColumnPosition: node.Column}
es.Resources = onrs
return nil
}
// ValidationString holds a validation string containing a Subject and one or
// more Relations to the parent Object.
// Example: `[tenant/user:someuser#...] is <tenant/document:example#viewer>`
type ValidationString string
// SubjectString returns the subject contained in the ValidationString, if any.
func (vs ValidationString) SubjectString() (string, bool) {
result := vsSubjectRegex.FindStringSubmatch(string(vs))
if len(result) != 4 {
return "", false
}
return result[2], true
}
// Subject returns the subject contained in the ValidationString, if any. If
// none, returns nil.
func (vs ValidationString) Subject() (*SubjectWithExceptions, *spiceerrors.ErrorWithSource) {
subjectStr, ok := vs.SubjectString()
if !ok {
return nil, nil
}
subjectStr = strings.TrimSpace(subjectStr)
groups := vsSubjectWithExceptionsOrCaveatRegex.FindStringSubmatch(subjectStr)
if len(groups) == 0 {
bracketedSubjectString := "[" + subjectStr + "]"
return nil, spiceerrors.NewErrorWithSource(fmt.Errorf("invalid subject: `%s`", subjectStr), bracketedSubjectString, 0, 0)
}
subjectONRString := groups[stringz.SliceIndex(vsSubjectWithExceptionsOrCaveatRegex.SubexpNames(), "subject_onr")]
subjectONR := tuple.ParseSubjectONR(subjectONRString)
if subjectONR == nil {
return nil, spiceerrors.NewErrorWithSource(fmt.Errorf("invalid subject: `%s`", subjectONRString), subjectONRString, 0, 0)
}
exceptionsString := strings.TrimSpace(groups[stringz.SliceIndex(vsSubjectWithExceptionsOrCaveatRegex.SubexpNames(), "exceptions")])
var exceptions []SubjectAndCaveat
if len(exceptionsString) > 0 {
exceptionsStringsSlice := strings.Split(exceptionsString, ",")
exceptions = make([]SubjectAndCaveat, 0, len(exceptionsStringsSlice))
for _, exceptionString := range exceptionsStringsSlice {
isCaveated := false
if strings.HasSuffix(exceptionString, "[...]") {
exceptionString = strings.TrimSuffix(exceptionString, "[...]")
isCaveated = true
}
exceptionONR := tuple.ParseSubjectONR(strings.TrimSpace(exceptionString))
if exceptionONR == nil {
return nil, spiceerrors.NewErrorWithSource(fmt.Errorf("invalid subject: `%s`", exceptionString), exceptionString, 0, 0)
}
exceptions = append(exceptions, SubjectAndCaveat{exceptionONR, isCaveated})
}
}
isCaveated := len(strings.TrimSpace(groups[stringz.SliceIndex(vsSubjectWithExceptionsOrCaveatRegex.SubexpNames(), "caveat")])) > 0
return &SubjectWithExceptions{SubjectAndCaveat{subjectONR, isCaveated}, exceptions}, nil
}
// ONRStrings returns the ONRs contained in the ValidationString, if any.
func (vs ValidationString) ONRStrings() []string {
results := vsObjectAndRelationRegex.FindAllStringSubmatch(string(vs), -1)
onrStrings := []string{}
for _, result := range results {
onrStrings = append(onrStrings, result[2])
}
return onrStrings
}
// ONRS returns the subject ONRs in the ValidationString, if any.
func (vs ValidationString) ONRS() ([]*core.ObjectAndRelation, *spiceerrors.ErrorWithSource) {
onrStrings := vs.ONRStrings()
onrs := []*core.ObjectAndRelation{}
for _, onrString := range onrStrings {
found := tuple.ParseONR(onrString)
if found == nil {
return nil, spiceerrors.NewErrorWithSource(fmt.Errorf("invalid resource and relation: `%s`", onrString), onrString, 0, 0)
}
onrs = append(onrs, found)
}
return onrs, nil
}
// ParseExpectedRelationsBlock parses the given contents as an expected relations block.
func ParseExpectedRelationsBlock(contents []byte) (*ParsedExpectedRelations, error) {
per := ParsedExpectedRelations{}
err := yamlv3.Unmarshal(contents, &per)
if err != nil {
return nil, convertYamlError(err)
}
return &per, nil
}