-
Notifications
You must be signed in to change notification settings - Fork 3
/
context.go
388 lines (330 loc) · 8.54 KB
/
context.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
// Copyright 2017 Keydap. All rights reserved.
// Licensed under the Apache License, Version 2.0, see LICENSE.
package base
import (
"sparrow/schema"
)
type OpContext struct {
Session *RbacSession
Sso bool
UpdatedSession bool
ClientIP string
Endpoint string
}
type CreateContext struct {
Repl bool // adding here instead of in OpContext so that creation of OpContext can be avoided
InRes *Resource
*OpContext // the operation context
}
type GetContext struct {
Rid string
Username string
Rt *schema.ResourceType
ParamAttrs string
ParamExclAttrs string
*OpContext // the operation context
}
type DeleteContext struct {
Rid string
Repl bool
Rt *schema.ResourceType
DeleteCsn string // a new CSN generated during delete operation, this helps in ordering replication event
*OpContext // the operation context
}
type ReplaceContext struct {
InRes *Resource
IfMatch string
Rt *schema.ResourceType
Res *Resource // the resource after replacing
Repl bool
Cloning bool
ReplVersion string
*OpContext // the operation context
}
type PatchContext struct {
Repl bool
ReplVersion string // to be used only by a replication event
Rid string
Pr *PatchReq
Rt *schema.ResourceType
Res *Resource
*OpContext // the operation context
}
type SearchContext struct {
MaxResults int // the maximum number of results returned for a search request
Filter *FilterNode // the search filter
ResTypes []*schema.ResourceType // the resource types
Attrs []string // attributes to sent
RawReq *SearchRequest
*OpContext // the operation context
}
type ChangePasswordContext struct {
Rid string
NewPassword string
HashAlgo string
Res *Resource
*OpContext // the operation context
}
type ListResponse struct {
TotalResults int64
Resources []*Resource
StartIndex int64
ItemsPerPage int
}
type AttributeParam struct {
Name string
SchemaId string
SubAts map[string]string // simplifies searching and eliminates iteration while filtering denied attributes
}
// https://tools.ietf.org/html/rfc7644#section-3.4.3
type SearchRequest struct {
Schemas []string `json:"schemas"`
Attributes string `json:"attributes,omitempty"`
ExcludedAttributes string `json:"excludedAttributes,omitempty"`
Filter string `json:"filter"`
SortBy string `json:"sortBy,omitempty"`
SortOrder string `json:"sortOrder,omitempty"`
StartIndex int `json:"startIndex,omitempty"`
Count int `json:"count,omitempty"`
}
type AuthRequest struct {
Username string `json:"username"`
Domain string `json:"domain"`
Password string `json:"password"`
ClientIP string `json:"-"`
FromLdap bool `json:"-"`
}
type VerifyOtpRequest struct {
Rid string
Otp string
ClientIP string
FromLdap bool
}
type ChangePasswordRequest struct {
Rid string
Password string
ClientIP string
FromLdap bool
}
type ModifyGroupsOfUserRequest struct {
UserRid string `json:"userrid"` // user resource's ID
AddGids []string `json:"addgids"` // list of group resources' IDs
RemoveGids []string `json:"removegids"` // list of group resources' IDs
UserVersion string
*OpContext
}
type OpDecision struct {
Allow bool
Deny bool
EvalWithoutFetch bool
EvalFilter bool
}
func NewSearchRequest(filter string, attrs string, include bool) *SearchRequest {
req := &SearchRequest{}
req.Schemas = []string{"urn:ietf:params:scim:api:messages:2.0:SearchRequest"}
req.Filter = filter
if len(attrs) > 0 {
if include {
req.Attributes = attrs
} else {
req.ExcludedAttributes = attrs
}
}
return req
}
func (cc *CreateContext) AllowOp() bool {
rt := cc.InRes.resType
rp := cc.Session.EffPerms[rt.Name]
if rp == nil {
return false
}
if rp.WritePerm.OnAnyResource && rp.WritePerm.AllowAll {
return true
}
entryOk := rp.WritePerm.EvalFilter(cc.InRes)
return entryOk && rp.WritePerm.AllowAll
}
func (dc *DeleteContext) EvalDelete(res *Resource) bool {
rp := dc.Session.EffPerms[dc.Rt.Name]
if rp == nil {
return false
}
if !rp.ReadPerm.AllowAll {
return false
}
return rp.ReadPerm.EvalFilter(res)
}
func (dc *DeleteContext) GetDecision() OpDecision {
od := OpDecision{}
rp := dc.Session.EffPerms[dc.Rt.Name]
if rp == nil {
od.Deny = true
return od
}
if rp.WritePerm.OnAnyResource && rp.WritePerm.AllowAll {
od.Allow = true
} else {
od.EvalFilter = true
}
return od
}
func (pc *PatchContext) GetDecision() OpDecision {
od := OpDecision{}
// special case for patching self
if pc.Rid == pc.Session.Sub {
od.EvalFilter = true
return od
}
rp := pc.Session.EffPerms[pc.Rt.Name]
if rp == nil {
od.Deny = true
return od
}
if rp.WritePerm.OnAnyResource && rp.WritePerm.AllowAll {
od.Allow = true
} else if rp.WritePerm.OnAnyResource {
od.EvalWithoutFetch = true
} else {
od.EvalFilter = true
}
return od
}
func (pc *PatchContext) EvalPatch(res *Resource) bool {
// special case for allowing self-password change
if pc.Rid == pc.Session.Sub {
if len(pc.Pr.Operations) == 1 { // there must be only one operation
op := pc.Pr.Operations[0]
if op.Path == "password" {
return true
}
}
}
rp := pc.Session.EffPerms[pc.Rt.Name]
if rp.WritePerm == nil {
return false
} else if rp.WritePerm.AllowAll {
return true
}
entryOk := true
if res != nil { // will be nil IF EvalWithoutFetch is true
entryOk = rp.WritePerm.EvalFilter(res)
}
if entryOk && rp.WritePerm.AllowAll {
return true
}
allowAttrs := rp.WritePerm.AllowAttrs
for _, po := range pc.Pr.Operations {
if po.ParsedPath.ParentType != nil {
at := allowAttrs[po.ParsedPath.ParentType.NormName]
if at == nil {
return false
} else {
if at.SubAts != nil { // check sub attributes are allowed
_, ok := at.SubAts[po.ParsedPath.AtType.NormName]
if !ok {
return false
}
}
}
} else {
at := allowAttrs[po.ParsedPath.AtType.NormName]
if at == nil {
return false
}
}
}
return entryOk
}
func (gc *GetContext) GetDecision() OpDecision {
od := OpDecision{}
rp := gc.Session.EffPerms[gc.Rt.Name]
if rp == nil {
od.Deny = true
return od
}
if rp.ReadPerm.OnAnyResource {
od.Allow = true
} else {
od.EvalFilter = true
}
return od
}
func (gc *GetContext) AllowRead(res *Resource) bool {
rp := gc.Session.EffPerms[gc.Rt.Name]
if rp == nil {
return false
}
if !rp.ReadPerm.AllowAll && rp.ReadPerm.AllowAttrs == nil {
return false
}
return rp.ReadPerm.EvalFilter(res)
}
func (rc *ReplaceContext) AllowOp() bool {
rp := rc.Session.EffPerms[rc.Rt.Name]
if rp == nil {
return false
}
if !rp.WritePerm.AllowAll {
return false
}
if rp.WritePerm.OnAnyResource {
return true
}
return rp.WritePerm.EvalFilter(rc.InRes)
}
func (sc *SearchContext) CanDenyOp() (bool, *FilterNode) {
rtLen := len(sc.ResTypes)
// use 1 less than the count cause the
// for loop runs from 0 to rtLen-1
powTwo := (1 << uint(rtLen-1))
flipped := powTwo
var fn *FilterNode
for i, rt := range sc.ResTypes {
efp := sc.Session.EffPerms[rt.Name]
if efp != nil && efp.ReadPerm.AllowAll {
// valid case
} else if efp == nil {
// flip the bit at ith index
flipped ^= (1 << uint(i))
//} else if efp.ReadPerm.AllowAttrs == nil && !efp.ReadPerm.AllowAll {
/// how to perform filter level permission checks? e.g how to prevent a comparison of emails.value when user has no read access?
//return false, nil
} else if !efp.ReadPerm.OnAnyResource {
tmp := efp.ReadPerm.Filter
if tmp == nil {
flipped ^= (1 << uint(i))
continue
}
tmp = tmp.Clone()
if fn == nil {
if rtLen > 1 {
fn = &FilterNode{Op: "OR"}
fn.Children = make([]*FilterNode, 1)
fn.Children[0] = tmp
} else {
fn = tmp
}
} else {
fn.Children = append(fn.Children, tmp)
}
}
}
deny := ((flipped & powTwo) == 0)
return deny, fn
}
// only checks for permissions on User resourcetype even
// though the associated operation updates groups as well
func (auth ModifyGroupsOfUserRequest) AllowOp(res *Resource) bool {
rt := res.resType
rp := auth.Session.EffPerms[rt.Name]
if rp == nil {
return false
}
if rp.WritePerm.OnAnyResource && rp.WritePerm.AllowAll {
return true
}
entryOk := rp.WritePerm.EvalFilter(res)
// only allow the one who has write access to *all*
// attributes of User (a.k.a an admin)
return entryOk && rp.WritePerm.AllowAll
}