-
Notifications
You must be signed in to change notification settings - Fork 180
/
ws-roots.go
433 lines (404 loc) · 12.1 KB
/
ws-roots.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
package rest
import (
"context"
"strings"
json "github.com/pydio/cells/x/jsonx"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/any"
"github.com/pydio/cells/common"
"github.com/pydio/cells/common/auth/claim"
"github.com/pydio/cells/common/log"
defaults "github.com/pydio/cells/common/micro"
"github.com/pydio/cells/common/proto/idm"
"github.com/pydio/cells/common/proto/tree"
"github.com/pydio/cells/common/registry"
service "github.com/pydio/cells/common/service/proto"
"github.com/pydio/cells/common/utils/permissions"
"github.com/pydio/cells/common/views"
)
func (h *WorkspaceHandler) loadRootNodesForWorkspaces(ctx context.Context, wsUUIDs []string, wss map[string]*idm.Workspace) error {
acls, err := permissions.GetACLsForWorkspace(ctx, wsUUIDs, &idm.ACLAction{Name: permissions.AclWsrootActionName})
if err != nil {
return err
}
wsAcls := make(map[string][]*idm.ACL, len(wsUUIDs))
for _, a := range acls {
wsAcls[a.WorkspaceID] = append(wsAcls[a.WorkspaceID], a)
}
streamer := tree.NewNodeProviderStreamerClient(registry.GetClient(common.ServiceTree))
c, e := streamer.ReadNodeStream(ctx)
if e != nil {
return e
}
defer c.Close()
vManager := views.GetVirtualNodesManager()
localCache := make(map[string]*tree.Node)
for uuid, ws := range wss {
aa, o := wsAcls[uuid]
if !o {
continue
}
for _, a := range aa {
if n, o := localCache[a.NodeID]; o {
if ws.RootNodes == nil {
ws.RootNodes = make(map[string]*tree.Node)
}
ws.RootNodes[a.NodeID] = n
}
c.Send(&tree.ReadNodeRequest{Node: &tree.Node{Uuid: a.NodeID}})
r, e := c.Recv()
if e != nil {
break
}
if r != nil && r.Success {
if ws.RootNodes == nil {
ws.RootNodes = make(map[string]*tree.Node)
}
ws.RootNodes[a.NodeID] = r.Node.WithoutReservedMetas()
localCache[a.NodeID] = r.Node.WithoutReservedMetas()
} else {
// May be a virtual node
if node, ok := vManager.ByUuid(a.NodeID); ok {
if ws.RootNodes == nil {
ws.RootNodes = make(map[string]*tree.Node)
}
ws.RootNodes[a.NodeID] = node.WithoutReservedMetas()
localCache[a.NodeID] = node.WithoutReservedMetas()
}
}
}
}
return nil
}
// LoadRootNodesForWorkspace loads all root nodes for this workspace
func (h *WorkspaceHandler) loadRootNodesForWorkspace(ctx context.Context, ws *idm.Workspace) error {
acls, err := permissions.GetACLsForWorkspace(ctx, []string{ws.UUID}, &idm.ACLAction{Name: permissions.AclWsrootActionName})
if err != nil {
return err
}
ws.RootNodes = make(map[string]*tree.Node)
if len(acls) == 0 {
return nil
}
treeClient := tree.NewNodeProviderClient(registry.GetClient(common.ServiceTree))
for _, a := range acls {
r, e := treeClient.ReadNode(ctx, &tree.ReadNodeRequest{Node: &tree.Node{Uuid: a.NodeID}})
if e == nil && r != nil {
ws.RootNodes[a.NodeID] = r.Node.WithoutReservedMetas()
} else {
// May be a virtual node
if node, ok := views.GetVirtualNodesManager().ByUuid(a.NodeID); ok {
ws.RootNodes[a.NodeID] = node
}
}
}
return nil
}
func (h *WorkspaceHandler) storeRootNodesAsACLs(ctx context.Context, ws *idm.Workspace, update bool) error {
reassign := make(map[string][]*idm.ACLAction)
aclClient := idm.NewACLServiceClient(common.ServiceGrpcNamespace_+common.ServiceAcl, defaults.NewClient())
if update {
// Delete current Root Nodes ACLs
q, _ := ptypes.MarshalAny(&idm.ACLSingleQuery{
WorkspaceIDs: []string{ws.UUID},
Actions: []*idm.ACLAction{{Name: permissions.AclWsrootActionName}, {Name: permissions.AclRecycleRoot.Name}},
})
_, e := aclClient.DeleteACL(ctx, &idm.DeleteACLRequest{Query: &service.Query{SubQueries: []*any.Any{q}}})
if e != nil {
return e
}
// Search ACLs to reassign, then delete them
q2, _ := ptypes.MarshalAny(&idm.ACLSingleQuery{
WorkspaceIDs: []string{ws.UUID},
})
q3, _ := ptypes.MarshalAny(&idm.ACLSingleQuery{
NodeIDs: []string{"-1"},
Not: true,
})
q4, _ := ptypes.MarshalAny(&idm.ACLSingleQuery{
RoleIDs: []string{"-1"},
Not: true,
})
query := &service.Query{SubQueries: []*any.Any{q2, q3, q4}, Operation: service.OperationType_AND}
sClient, e := aclClient.SearchACL(ctx, &idm.SearchACLRequest{Query: query})
if e != nil {
return e
}
defer sClient.Close()
for {
r, e := sClient.Recv()
if e != nil {
break
}
reassign[r.ACL.RoleID] = append(reassign[r.ACL.RoleID], r.ACL.Action)
}
_, e = aclClient.DeleteACL(ctx, &idm.DeleteACLRequest{Query: query})
if e != nil {
return e
}
}
if ws.RootNodes == nil {
ws.RootNodes = map[string]*tree.Node{}
}
// Now store new roots as ACLs
for nodeId, node := range ws.RootNodes {
// Roots
if _, e := aclClient.CreateACL(ctx, &idm.CreateACLRequest{ACL: &idm.ACL{
WorkspaceID: ws.UUID,
NodeID: nodeId,
Action: &idm.ACLAction{Name: permissions.AclWsrootActionName, Value: node.GetPath()},
}}); e != nil {
return e
}
// Recycle Roots
if _, e := aclClient.CreateACL(ctx, &idm.CreateACLRequest{ACL: &idm.ACL{
WorkspaceID: ws.UUID,
NodeID: nodeId,
Action: permissions.AclRecycleRoot,
}}); e != nil {
return e
}
// Reassign if necessary
if update && len(reassign) > 0 {
for roleId, actions := range reassign {
for _, action := range actions {
acl := &idm.ACL{
WorkspaceID: ws.UUID,
RoleID: roleId,
NodeID: nodeId,
Action: action,
}
if _, e := aclClient.CreateACL(ctx, &idm.CreateACLRequest{ACL: acl}); e != nil {
return e
}
}
}
}
}
return nil
}
func (h *WorkspaceHandler) extractDefaultRights(ctx context.Context, workspace *idm.Workspace) (string, string) {
var rightsValue, quotaValue string
if workspace.Attributes != "" {
var atts map[string]interface{}
if e := json.Unmarshal([]byte(workspace.Attributes), &atts); e == nil {
var modif bool
if passed, ok := atts["DEFAULT_RIGHTS"]; ok {
rightsValue = passed.(string)
delete(atts, "DEFAULT_RIGHTS")
modif = true
}
if q, ok := atts["QUOTA"]; ok {
quotaValue = q.(string)
delete(atts, "QUOTA")
modif = true
}
if modif {
jsonAttributes, _ := json.Marshal(atts)
workspace.Attributes = string(jsonAttributes)
}
}
}
return rightsValue, quotaValue
}
func (h *WorkspaceHandler) bulkReadDefaultRights(ctx context.Context, uuids []string, wss map[string]*idm.Workspace) error {
aclClient := idm.NewACLServiceClient(common.ServiceGrpcNamespace_+common.ServiceAcl, defaults.NewClient())
// Load RootRole ACLs and append to Attributes
q1, _ := ptypes.MarshalAny(&idm.ACLSingleQuery{
WorkspaceIDs: uuids,
RoleIDs: []string{"ROOT_GROUP"},
})
q2, _ := ptypes.MarshalAny(&idm.ACLSingleQuery{
Actions: []*idm.ACLAction{permissions.AclRead, permissions.AclWrite, permissions.AclQuota},
})
stream, err := aclClient.SearchACL(ctx, &idm.SearchACLRequest{
Query: &service.Query{
SubQueries: []*any.Any{q1, q2},
Operation: service.OperationType_AND,
},
})
if err != nil {
return err
}
defer stream.Close()
rightStrings := make(map[string]string, len(uuids))
quotaStrings := make(map[string]string, len(uuids))
for {
r, e := stream.Recv()
if e != nil {
break
}
st := ""
wsID := r.ACL.WorkspaceID
if s, o := rightStrings[wsID]; o {
st = s
}
switch r.ACL.Action.Name {
case permissions.AclRead.Name:
st += "r"
case permissions.AclWrite.Name:
st += "w"
case permissions.AclQuota.Name:
quotaStrings[wsID] = r.ACL.Action.Value
}
if st != "" {
rightStrings[wsID] = st
}
}
for uuid, workspace := range wss {
attributes := make(map[string]interface{})
if workspace.Attributes != "" {
var atts map[string]interface{}
if e := json.Unmarshal([]byte(workspace.Attributes), &atts); e == nil {
attributes = atts
}
}
// Apply permission if found
if r, o := rightStrings[uuid]; o {
attributes["DEFAULT_RIGHTS"] = r
}
// Apply quota if found
if q, o := quotaStrings[uuid]; o {
attributes["QUOTA"] = q
}
jsonAttributes, _ := json.Marshal(attributes)
workspace.Attributes = string(jsonAttributes)
}
return nil
}
func (h *WorkspaceHandler) manageDefaultRights(ctx context.Context, workspace *idm.Workspace, read bool, rightsValue string, newQuota string) error {
aclClient := idm.NewACLServiceClient(common.ServiceGrpcNamespace_+common.ServiceAcl, defaults.NewClient())
if read {
// Load RootRole ACLs and append to Attributes
q1, _ := ptypes.MarshalAny(&idm.ACLSingleQuery{
WorkspaceIDs: []string{workspace.UUID},
RoleIDs: []string{"ROOT_GROUP"},
})
q2, _ := ptypes.MarshalAny(&idm.ACLSingleQuery{
Actions: []*idm.ACLAction{permissions.AclRead, permissions.AclWrite, permissions.AclQuota},
})
stream, err := aclClient.SearchACL(ctx, &idm.SearchACLRequest{
Query: &service.Query{
SubQueries: []*any.Any{q1, q2},
Operation: service.OperationType_AND,
},
})
if err != nil {
return err
}
defer stream.Close()
var read, write bool
var strQuota string
for {
r, e := stream.Recv()
if e != nil {
break
}
if r.ACL.Action.Name == permissions.AclRead.Name {
read = true
}
if r.ACL.Action.Name == permissions.AclWrite.Name {
write = true
}
if r.ACL.Action.Name == permissions.AclQuota.Name {
strQuota = r.ACL.Action.Value
}
}
s := ""
if read {
s += "r"
}
if write {
s += "w"
}
attributes := make(map[string]interface{}, 1)
if workspace.Attributes != "" {
var atts map[string]interface{}
if e := json.Unmarshal([]byte(workspace.Attributes), &atts); e == nil {
attributes = atts
}
}
attributes["DEFAULT_RIGHTS"] = s
if strQuota != "" {
attributes["QUOTA"] = strQuota
}
jsonAttributes, _ := json.Marshal(attributes)
workspace.Attributes = string(jsonAttributes)
} else {
log.Logger(ctx).Debug("Manage default Rights: " + rightsValue)
// Delete RootRole values first
q1, _ := ptypes.MarshalAny(&idm.ACLSingleQuery{
WorkspaceIDs: []string{workspace.UUID},
RoleIDs: []string{"ROOT_GROUP"},
})
q2, _ := ptypes.MarshalAny(&idm.ACLSingleQuery{
Actions: []*idm.ACLAction{permissions.AclRead, permissions.AclWrite, permissions.AclQuota},
})
_, err := aclClient.DeleteACL(ctx, &idm.DeleteACLRequest{
Query: &service.Query{
SubQueries: []*any.Any{q1, q2},
Operation: service.OperationType_AND,
},
})
if err != nil {
return err
}
// Now Update RootRole
if rightsValue == "" && newQuota == "" {
return nil
}
read := strings.Contains(rightsValue, "r")
write := strings.Contains(rightsValue, "w")
for _, node := range workspace.RootNodes {
// Create ACLs for root group
if read {
aclClient.CreateACL(ctx, &idm.CreateACLRequest{ACL: &idm.ACL{
WorkspaceID: workspace.UUID,
RoleID: "ROOT_GROUP",
NodeID: node.Uuid,
Action: permissions.AclRead,
}})
}
if write {
aclClient.CreateACL(ctx, &idm.CreateACLRequest{ACL: &idm.ACL{
WorkspaceID: workspace.UUID,
RoleID: "ROOT_GROUP",
NodeID: node.Uuid,
Action: permissions.AclWrite,
}})
}
if newQuota != "" && newQuota != "0" {
aclClient.CreateACL(ctx, &idm.CreateACLRequest{ACL: &idm.ACL{
Action: &idm.ACLAction{Name: permissions.AclQuota.Name, Value: newQuota},
RoleID: "ROOT_GROUP",
WorkspaceID: workspace.UUID,
NodeID: node.Uuid,
}})
}
}
}
return nil
}
func (h *WorkspaceHandler) allowCurrentUser(ctx context.Context, workspace *idm.Workspace) error {
aclClient := idm.NewACLServiceClient(common.ServiceGrpcNamespace_+common.ServiceAcl, defaults.NewClient())
if ctx.Value(claim.ContextKey) != nil {
claims := ctx.Value(claim.ContextKey).(claim.Claims)
userId := claims.Subject
for _, node := range workspace.RootNodes {
// Create ACLs for user id
aclClient.CreateACL(ctx, &idm.CreateACLRequest{ACL: &idm.ACL{
WorkspaceID: workspace.UUID,
RoleID: userId,
NodeID: node.Uuid,
Action: permissions.AclRead,
}})
aclClient.CreateACL(ctx, &idm.CreateACLRequest{ACL: &idm.ACL{
WorkspaceID: workspace.UUID,
RoleID: userId,
NodeID: node.Uuid,
Action: permissions.AclWrite,
}})
}
}
return nil
}