-
Notifications
You must be signed in to change notification settings - Fork 3
/
patch.go
267 lines (223 loc) · 6.46 KB
/
patch.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
// Copyright 2017 Keydap. All rights reserved.
// Licensed under the Apache License, Version 2.0, see LICENSE.
package base
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"sparrow/schema"
"strings"
)
type PatchReq struct {
Schemas []string
IfMatch string
Operations []*PatchOp
RawReq []byte // the raw request that gets sent for replication
}
type PatchOp struct {
Index int
Op string
Path string
ParsedPath *ParsedPath
Value interface{}
}
type ParsedPath struct {
ParentType *schema.AttrType // name of the sub-attribute's parent
AtType *schema.AttrType // name of the (sub-)attribute
Schema string // the schema of the attribute
Slctr Selector // the selection filter present in the path
Text string
IsExtContainer bool
}
func NewPatchReq() *PatchReq {
pr := &PatchReq{}
pr.Schemas = []string{"urn:ietf:params:scim:api:messages:2.0:PatchOp"}
pr.Operations = make([]*PatchOp, 0)
return pr
}
func ParsePatchReq(body io.Reader, rt *schema.ResourceType) (*PatchReq, error) {
if body == nil {
return nil, NewBadRequestError("Invalid Patch request data")
}
var pr PatchReq
data, err := ioutil.ReadAll(body)
if err != nil {
log.Debugf("Failed to read the body of patch request %#v", err)
return nil, NewBadRequestError(err.Error())
}
err = json.Unmarshal(data, &pr)
if err != nil {
log.Debugf("Failed to parse the patch request %#v", err)
return nil, NewBadRequestError(err.Error())
}
pr.RawReq = data
if len(pr.Operations) == 0 {
detail := "Invalid patch request, one or more operations must be present"
log.Debugf(detail)
return nil, NewBadRequestError(detail)
}
for i, po := range pr.Operations {
po.Op = strings.ToLower(strings.TrimSpace(po.Op))
po.Path = strings.TrimSpace(po.Path)
po.Index = i
pLen := len(po.Path)
switch po.Op {
case "add", "replace":
if po.Value == nil {
detail := fmt.Sprintf("Invalid patch request, missing value in the %d operation", i)
log.Debugf(detail)
return nil, NewBadRequestError(detail)
}
case "remove":
if pLen == 0 {
detail := fmt.Sprintf("Invalid patch request, missing path in the %d operation", i)
log.Debugf(detail)
se := NewBadRequestError(detail)
se.ScimType = ST_NOTARGET
return nil, se
}
default:
detail := fmt.Sprintf("Invalid patch request, unknown operation name %s in %d operation", po.Op, i)
log.Debugf(detail)
return nil, NewBadRequestError(detail)
}
if pLen > 0 {
pp, err := ParsePath(po.Path, rt)
if err != nil {
return nil, err
}
po.ParsedPath = pp
}
}
return &pr, nil
}
func ParsePath(path string, rt *schema.ResourceType) (pp *ParsedPath, err error) {
pp = &ParsedPath{}
runningPath := path
selector := ""
defer func() {
e := recover()
if e != nil {
log.Debugf("Failed to parse path %#v", e)
pp = nil
err = e.(error)
}
}()
slctrStrtPos := strings.IndexRune(path, '[')
if slctrStrtPos == 0 {
detail := fmt.Sprintf("Invalid attribute path %s, missing parent attribute", path)
return nil, NewBadRequestError(detail)
}
if slctrStrtPos > 0 {
slctrEndPos := strings.LastIndex(path, "]")
if slctrEndPos == -1 {
detail := fmt.Sprintf("Invalid attribute path %s, missing ']' character in the path", path)
return nil, NewBadRequestError(detail)
}
selector = path[:slctrEndPos]
if len(selector) == 0 {
detail := fmt.Sprintf("Invalid attribute path %s, empty selector present", path)
return nil, NewBadRequestError(detail)
}
// now append ]
selector += "]"
runningPath = path[:slctrStrtPos]
slctrEndPos++
if slctrEndPos < len(path) {
runningPath += path[slctrEndPos:]
}
}
colonPos := strings.LastIndex(runningPath, ":")
if colonPos > 0 {
// when colon is present there are two cases
// 1. an attribute of extension container or
// 2. the extension container itself as a whole
assumedContainerPath := runningPath
uri := path[:colonPos]
if rt.Schema == uri {
uri = "" // core schema
} else {
found := false
for _, extSc := range rt.SchemaExtensions {
if uri == extSc.Schema {
found = true
pp.Schema = uri
break
} else if assumedContainerPath == extSc.Schema {
pp.IsExtContainer = true
pp.Schema = assumedContainerPath
found = true
break
}
}
if !found {
detail := fmt.Sprintf("Unknown schema URI %s in the attribute path %s", uri, path)
return nil, NewBadRequestError(detail)
}
if pp.IsExtContainer {
return pp, nil
}
}
colonPos++
if colonPos >= len(path) {
detail := fmt.Sprintf("Invalid attribute path %s", path)
return nil, NewBadRequestError(detail)
}
runningPath = path[colonPos:]
}
dotPos := strings.LastIndex(runningPath, ".")
if dotPos > colonPos {
parentName := runningPath[:dotPos]
dotPos++
if dotPos >= len(runningPath) {
detail := fmt.Sprintf("invalid attribute name in the path %s", path)
return nil, NewBadRequestError(detail)
}
pp.ParentType = rt.GetAtType(parentName)
if pp.ParentType == nil {
detail := fmt.Sprintf("Unknown complex attribute %s in the path %s", parentName, path)
return nil, NewBadRequestError(detail)
}
atName := runningPath[dotPos:]
name := parentName + "." + atName
pp.AtType = rt.GetAtType(name)
if pp.AtType == nil {
detail := fmt.Sprintf("Unknown attribute %s in the path %s", name, path)
return nil, NewBadRequestError(detail)
}
} else {
atName := runningPath
pp.AtType = rt.GetAtType(atName)
if pp.AtType == nil {
detail := fmt.Sprintf("Unknown attribute %s in the path %s", atName, path)
return nil, NewBadRequestError(detail)
}
}
if len(selector) > 0 {
slctrNode, err := ParseFilter(selector)
if err != nil {
return nil, err
}
pp.Text = selector
pp.Slctr = buildSelector(slctrNode, rt)
}
// If the target location is a multi-valued attribute and no filter
// is specified, the attribute and all values are replaced.
// from the section https://tools.ietf.org/html/rfc7644#section-3.5.2.3
// seems to be quite problematic when it comes to handling "members" of a Group
// what should we do when the path present in any given operation contains no selector
// for example "members.value" should we reject such operation??
// if pp.ParentType != nil {
// if pp.ParentType.MultiValued && (pp.Slctr == nil) {
//
// }
// }
return pp, nil
}
func (pp *ParsedPath) FQAName() string {
if pp.Schema == "" {
return pp.AtType.NormName
}
return pp.Schema + ":" + pp.AtType.NormName
}