-
Notifications
You must be signed in to change notification settings - Fork 13
/
json.go
311 lines (289 loc) · 7.44 KB
/
json.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
package authr
import (
"encoding/json"
"fmt"
"strconv"
"strings"
)
const (
propAccess = "access"
propWhere = "where"
propWhereRsrcType = "rsrc_type"
propWhereRsrcMatch = "rsrc_match"
propWhereAction = "action"
propMeta = "$meta"
jtypeBool = "JSON boolean"
jtypeNumber = "JSON number"
jtypeString = "JSON string"
jtypeArray = "JSON array"
jtypeObject = "JSON object"
jtypeNull = "JSON null"
)
func (r *Rule) UnmarshalJSON(data []byte) error {
*r = Rule{}
var v interface{}
if err := json.Unmarshal(data, &v); err != nil {
return err
}
o, ok := v.(map[string]interface{})
if !ok {
return Error(fmt.Sprintf("expecting %s for rule definition, got %s", jtypeObject, typename(v)))
}
if ai, ok := o[propAccess]; ok {
a, ok := ai.(string)
if !ok {
return jsonInvalidType([]string{propAccess}, ai, jtypeString)
}
switch a {
case "allow":
r.access = Allow
case "deny":
r.access = Deny
default:
return jsonInvalidPropValue([]string{propAccess}, `"allow" or "deny"`, fmt.Sprintf(`"%s"`, a))
}
} else {
return jsonMissingProperty([]string{propAccess})
}
if wi, ok := o[propWhere]; ok {
w, ok := wi.(map[string]interface{})
if !ok {
return jsonInvalidType([]string{propWhere}, wi, jtypeObject)
}
var err error
err = unmarshalSlugSet(&r.where.resourceType, propWhereRsrcType, w)
if err != nil {
return err
}
err = unmarshalSlugSet(&r.where.action, propWhereAction, w)
if err != nil {
return err
}
csi, ok := w[propWhereRsrcMatch]
if !ok {
return jsonMissingProperty([]string{propWhere, propWhereRsrcMatch})
}
r.where.resourceMatch, err = unmarshalConditionSet([]string{propWhere, propWhereRsrcMatch}, csi)
if err != nil {
return err
}
} else {
return jsonMissingProperty([]string{propWhere})
}
if meta, ok := o[propMeta]; ok {
r.meta = meta
}
return nil
}
func unmarshalConditionSet(path []string, csi interface{}) (ConditionSet, error) {
cs := ConditionSet{}
cs.evaluators = []Evaluator{}
switch _cs := csi.(type) {
case map[string]interface{}:
logic, csinneri, err := unwrapKeywordMap(path, _cs, logicalAnd.String(), logicalOr.String())
if err != nil {
return ConditionSet{}, err
}
switch logic {
case logicalAnd.String():
cs.conj = logicalAnd
case logicalOr.String():
cs.conj = logicalOr
}
path = append(path, logic)
switch csinner := csinneri.(type) {
case []interface{}:
var err error
cs.evaluators, err = unmarshalNestedConditions(
append(path, cs.conj.String()),
csinner,
)
if err != nil {
return ConditionSet{}, err
}
default:
return ConditionSet{}, jsonInvalidType(path, csinneri, jtypeArray)
}
case []interface{}:
cs.conj = logicalAnd
var err error
cs.evaluators, err = unmarshalNestedConditions(path, _cs)
if err != nil {
return ConditionSet{}, err
}
default:
return ConditionSet{}, jsonInvalidType(path, csi, jtypeObject, jtypeArray)
}
return cs, nil
}
func unmarshalNestedConditions(path []string, csinner []interface{}) ([]Evaluator, error) {
evals := make([]Evaluator, len(csinner))
for i, v := range csinner {
if jarr, ok := v.([]interface{}); ok && len(jarr) == 3 && isstring(jarr[1]) {
// smells like a condition!
evals[i] = Cond(jarr[0], jarr[1].(string), jarr[2])
continue
}
var err error
evals[i], err = unmarshalConditionSet(append(path, strconv.Itoa(i)), v)
if err != nil {
return nil, err
}
}
return evals, nil
}
func isstring(v interface{}) bool {
_, ok := v.(string)
return ok
}
func unwrapKeywordMap(path []string, msi map[string]interface{}, validKeys ...string) (string, interface{}, error) {
if len(msi) == 1 {
var k string
for _k := range msi {
k = _k
break
}
for _, vk := range validKeys {
if k == vk {
return k, msi[k], nil
}
}
}
err := Error(
fmt.Sprintf(
`invalid value for property "%s": expected %s with only one of the these key(s): "%s"`,
strings.Join(path, "."),
jtypeObject,
strings.Join(validKeys, `", "`),
),
)
return "", nil, err
}
func unmarshalSlugSet(ss *SlugSet, prop string, w map[string]interface{}) error {
path := []string{propWhere, prop}
ssi, ok := w[prop]
if !ok {
return jsonMissingProperty(path)
}
switch _ss := ssi.(type) {
case map[string]interface{}:
_, ssni, err := unwrapKeywordMap(path, _ss, "$not")
if err != nil {
return err
}
path = append(path, "$not")
ss.mode = blocklist
switch ssn := ssni.(type) {
case []interface{}:
// empty slug set IS allowed if the slugset is a blocklist.
err := unmarshalStringSlice(path, ss, ssn)
if err != nil {
return err
}
case string:
ss.elements = []string{ssn}
default:
return jsonInvalidType(path, ssni, jtypeArray, jtypeString)
}
case []interface{}:
// empty slug set is NOT allowed if the slug set is not a blocklist
// the rule would never match anything
if len(_ss) == 0 {
return jsonInvalidPropValue(path, "non-empty array", "empty array")
}
err := unmarshalStringSlice(path, ss, _ss)
if err != nil {
return err
}
case string:
if _ss == "*" {
ss.mode = wildcard
ss.elements = []string{}
} else {
ss.elements = []string{_ss}
}
default:
return jsonInvalidType(path, ssi, jtypeObject, jtypeArray, jtypeString)
}
return nil
}
func unmarshalStringSlice(path []string, ss *SlugSet, jarr []interface{}) error {
ss.elements = make([]string, len(jarr))
for i, v := range jarr {
// TODO(nick): check for empty strings
s, ok := v.(string)
if !ok {
return jsonInvalidType(append(path, fmt.Sprintf("%v", i)), v, jtypeString)
}
ss.elements[i] = s
}
return nil
}
func lexicalJoin(a []string) string {
switch len(a) {
case 0:
return ""
case 1:
return a[0]
case 2:
return a[0] + " or " + a[1]
default:
return strings.Join(a[:len(a)-1], ", ") + " or " + a[len(a)-1]
}
}
type ruleUnmarshalError struct {
path []string
}
type jsonInvalidTypeError struct {
ruleUnmarshalError
needTypes []string
v interface{}
}
func (j jsonInvalidTypeError) Error() string {
return fmt.Sprintf(`expecting %s for property "%s", got %s`, lexicalJoin(j.needTypes), strings.Join(j.path, "."), typename(j.v))
}
type jsonMissingPropertyError struct {
ruleUnmarshalError
}
func (j jsonMissingPropertyError) Error() string {
return fmt.Sprintf(`invalid rule; missing required property "%s"`, strings.Join(j.path, "."))
}
func jsonMissingProperty(path []string) error {
return jsonMissingPropertyError{
ruleUnmarshalError: ruleUnmarshalError{path: path},
}
}
type jsonInvalidPropertyValueError struct {
ruleUnmarshalError
expecting, got string
}
func (j jsonInvalidPropertyValueError) Error() string {
return fmt.Sprintf(`invalid value for property "%s", expecting %s, got %s`, strings.Join(j.path, "."), j.expecting, j.got)
}
func jsonInvalidPropValue(path []string, e, g string) error {
return jsonInvalidPropertyValueError{
ruleUnmarshalError: ruleUnmarshalError{path: path},
expecting: e,
got: g,
}
}
func jsonInvalidType(path []string, v interface{}, needType ...string) error {
return jsonInvalidTypeError{ruleUnmarshalError: ruleUnmarshalError{path: path}, needTypes: needType, v: v}
}
func typename(v interface{}) string {
switch v.(type) {
case bool:
return jtypeBool
case float64:
return jtypeNumber
case string:
return jtypeString
case []interface{}:
return jtypeArray
case map[string]interface{}:
return jtypeObject
case nil:
return jtypeNull
}
panic(fmt.Sprintf("unexpected go type found in unmarshaled value: %T", v))
}