-
Notifications
You must be signed in to change notification settings - Fork 180
/
idm.go
588 lines (483 loc) · 16 KB
/
idm.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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
/*
* Copyright (c) 2019. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
package permissions
import (
"context"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/any"
"github.com/micro/go-micro/errors"
"github.com/micro/go-micro/metadata"
"github.com/patrickmn/go-cache"
"go.uber.org/zap"
"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"
service "github.com/pydio/cells/common/service/proto"
)
var (
usersCache *cache.Cache
)
func init() {
usersCache = cache.New(5*time.Second, 30*time.Second)
}
// GetRolesForUser loads the roles of a given user.
func GetRolesForUser(ctx context.Context, user *idm.User, createMissing bool) []*idm.Role {
var roles []*idm.Role
var foundRoles = map[string]*idm.Role{}
var roleIds []string
for _, r := range user.Roles {
roleIds = append(roleIds, r.Uuid)
}
if len(roleIds) == 0 {
return roles
}
roleClient := idm.NewRoleServiceClient(common.SERVICE_GRPC_NAMESPACE_+common.SERVICE_ROLE, defaults.NewClient())
query, _ := ptypes.MarshalAny(&idm.RoleSingleQuery{
Uuid: roleIds,
})
if stream, err := roleClient.SearchRole(ctx, &idm.SearchRoleRequest{
Query: &service.Query{
SubQueries: []*any.Any{query},
},
}); err != nil {
log.Logger(ctx).Error("failed to retrieve roles", zap.Error(err))
return nil
} else {
defer stream.Close()
for {
response, err := stream.Recv()
if err != nil {
break
}
foundRoles[response.GetRole().GetUuid()] = response.GetRole()
}
}
for _, role := range user.Roles {
if loaded, ok := foundRoles[role.Uuid]; ok {
roles = append(roles, loaded)
} else if createMissing && (role.GroupRole || role.UserRole) {
// Create missing role now
var label string
if role.GroupRole {
label = "Group " + role.Label
} else {
label = "User " + user.Login
}
resp, e := roleClient.CreateRole(ctx, &idm.CreateRoleRequest{Role: &idm.Role{
Uuid: role.Uuid,
GroupRole: role.GroupRole,
UserRole: role.UserRole,
Label: label,
}})
if e == nil {
roles = append(roles, resp.Role)
} else {
log.Logger(ctx).Error("Error creating special role", zap.Error(e))
}
} else {
roles = append(roles, role) // Still put empty role here
}
}
return roles
}
// GetRoles Objects from a list of role names.
func GetRoles(ctx context.Context, names []string) []*idm.Role {
var roles []*idm.Role
if len(names) == 0 {
return roles
}
query, _ := ptypes.MarshalAny(&idm.RoleSingleQuery{Uuid: names})
roleClient := idm.NewRoleServiceClient(common.SERVICE_GRPC_NAMESPACE_+common.SERVICE_ROLE, defaults.NewClient())
stream, err := roleClient.SearchRole(ctx, &idm.SearchRoleRequest{Query: &service.Query{SubQueries: []*any.Any{query}}})
if err != nil {
if !strings.Contains(err.Error(), "context canceled") {
log.Logger(ctx).Error("Failed to retrieve roles", zap.Error(err), zap.Any("c", ctx))
}
return nil
}
defer stream.Close()
for {
response, err := stream.Recv()
if err != nil {
break
}
roles = append(roles, response.GetRole())
}
var sorted []*idm.Role
for _, name := range names {
for _, role := range roles {
if role.Uuid == name {
sorted = append(sorted, role)
}
}
}
//log.Logger(ctx).Debug("GetRoles", zap.Any("roles", sorted))
return sorted
}
// GetACLsForRoles compiles ALCs for a list of roles.
func GetACLsForRoles(ctx context.Context, roles []*idm.Role, actions ...*idm.ACLAction) []*idm.ACL {
var acls []*idm.ACL
if len(roles) == 0 {
return acls
}
// First we retrieve the roleIDs from the role names
var roleIDs []string
for _, role := range roles {
roleIDs = append(roleIDs, role.Uuid)
}
q1, q2 := new(idm.ACLSingleQuery), new(idm.ACLSingleQuery)
q1.Actions = actions
q2.RoleIDs = roleIDs
q1Any, err := ptypes.MarshalAny(q1)
if err != nil {
return acls
}
q2Any, err := ptypes.MarshalAny(q2)
if err != nil {
return acls
}
//s := time.Now()
aclClient := idm.NewACLServiceClient(common.SERVICE_GRPC_NAMESPACE_+common.SERVICE_ACL, defaults.NewClient())
stream, err := aclClient.SearchACL(ctx, &idm.SearchACLRequest{
Query: &service.Query{
SubQueries: []*any.Any{q1Any, q2Any},
Operation: service.OperationType_AND,
},
})
if err != nil {
log.Logger(ctx).Error("GetACLsForRoles", zap.Error(err))
return nil
}
defer stream.Close()
for {
response, err := stream.Recv()
if err != nil {
break
}
acls = append(acls, response.GetACL())
}
//log.Logger(ctx).Debug("GetACLsForRoles", zap.Any("acls", acls), zap.Any("roles", roles), zap.Any("actions", actions), zap.Duration("t", time.Now().Sub(s)))
return acls
}
// GetACLsForWorkspace compiles ACLs list attached to a given workspace.
func GetACLsForWorkspace(ctx context.Context, workspaceIds []string, actions ...*idm.ACLAction) (acls []*idm.ACL, err error) {
var subQueries []*any.Any
q1, _ := ptypes.MarshalAny(&idm.ACLSingleQuery{WorkspaceIDs: workspaceIds})
q2, _ := ptypes.MarshalAny(&idm.ACLSingleQuery{Actions: actions})
subQueries = append(subQueries, q1, q2)
aclClient := idm.NewACLServiceClient(common.SERVICE_GRPC_NAMESPACE_+common.SERVICE_ACL, defaults.NewClient())
stream, err := aclClient.SearchACL(ctx, &idm.SearchACLRequest{
Query: &service.Query{
SubQueries: subQueries,
Operation: service.OperationType_AND,
},
})
if err != nil {
log.Logger(ctx).Error("GetACLsForWorkspace", zap.Error(err))
return nil, err
}
defer stream.Close()
for {
response, err := stream.Recv()
if err != nil {
break
}
acls = append(acls, response.GetACL())
}
//log.Logger(ctx).Debug("GetACLsForWorkspace", zap.Any("acls", acls), zap.Any("wsId", workspaceId), zap.Any("action", action))
return acls, nil
}
// GetWorkspacesForACLs computes a list of accessible workspaces, given a set of Read and Deny ACLs.
func GetWorkspacesForACLs(ctx context.Context, list *AccessList) []*idm.Workspace {
var workspaces []*idm.Workspace
workspaceNodes := list.GetWorkspacesNodes()
if len(workspaceNodes) == 0 {
// DO NOT PERFORM SEARCH, OR IT WILL RETRIEVE ALL WORKSPACES
return workspaces
}
workspaceClient := idm.NewWorkspaceServiceClient(common.SERVICE_GRPC_NAMESPACE_+common.SERVICE_WORKSPACE, defaults.NewClient())
var queries []*any.Any
for workspaceID := range workspaceNodes {
query, _ := ptypes.MarshalAny(&idm.WorkspaceSingleQuery{Uuid: workspaceID})
queries = append(queries, query)
}
stream, err := workspaceClient.SearchWorkspace(ctx, &idm.SearchWorkspaceRequest{
Query: &service.Query{
SubQueries: queries,
Operation: service.OperationType_OR,
},
})
if err != nil {
log.Logger(ctx).Error("search workspace request has failed", zap.Error(err))
return nil
}
defer stream.Close()
for {
response, err := stream.Recv()
if err != nil {
break
}
ws := response.GetWorkspace()
for nodeUuid := range workspaceNodes[ws.UUID] {
ws.RootUUIDs = append(ws.RootUUIDs, nodeUuid)
}
workspaces = append(workspaces, ws)
}
return workspaces
}
func GetACLsForActions(ctx context.Context, actions ...*idm.ACLAction) (acls []*idm.ACL, err error) {
var subQueries []*any.Any
q1, _ := ptypes.MarshalAny(&idm.ACLSingleQuery{Actions: actions})
subQueries = append(subQueries, q1)
aclClient := idm.NewACLServiceClient(common.SERVICE_GRPC_NAMESPACE_+common.SERVICE_ACL, defaults.NewClient())
stream, err := aclClient.SearchACL(ctx, &idm.SearchACLRequest{
Query: &service.Query{
SubQueries: subQueries,
},
})
if err != nil {
log.Logger(ctx).Error("GetACLsForActions", zap.Error(err))
return nil, err
}
defer stream.Close()
for {
response, err := stream.Recv()
if err != nil {
break
}
acls = append(acls, response.GetACL())
}
return acls, nil
}
func FindUserNameInContext(ctx context.Context) (string, claim.Claims) {
var userName string
var claims claim.Claims
if ctx.Value(claim.ContextKey) != nil {
claims = ctx.Value(claim.ContextKey).(claim.Claims)
userName = claims.Name
} else if ctx.Value(common.PYDIO_CONTEXT_USER_KEY) != nil {
userName = ctx.Value(common.PYDIO_CONTEXT_USER_KEY).(string)
} else if ctx.Value(strings.ToLower(common.PYDIO_CONTEXT_USER_KEY)) != nil {
userName = ctx.Value(strings.ToLower(common.PYDIO_CONTEXT_USER_KEY)).(string)
} else if meta, ok := metadata.FromContext(ctx); ok {
if value, exists := meta[common.PYDIO_CONTEXT_USER_KEY]; exists {
userName = value
} else if value, exists := meta[strings.ToLower(common.PYDIO_CONTEXT_USER_KEY)]; exists {
userName = value
}
}
return userName, claims
}
// AccessListForLockedNodes builds a flattened node list containing all currently locked nodes
func AccessListForLockedNodes(ctx context.Context, resolver VirtualPathResolver) (accessList *AccessList, err error) {
accessList = NewAccessList([]*idm.Role{})
acls, _ := GetACLsForActions(ctx, AclLock)
accessList.Append(acls)
accessList.NodesAcls = make(map[string]Bitmask)
b := Bitmask{}
b.AddFlag(FlagLock)
for _, acl := range acls {
accessList.NodesAcls[acl.NodeID] = b
}
accessList.LoadNodePathsAcls(ctx, resolver)
return accessList, nil
}
// AccessListFromContextClaims uses package function to compile ACL and Workspaces for a given user ( = list of roles inside the Claims)
func AccessListFromContextClaims(ctx context.Context) (accessList *AccessList, err error) {
claims, ok := ctx.Value(claim.ContextKey).(claim.Claims)
if !ok {
log.Logger(ctx).Debug("No Claims in Context, workspaces will be empty - probably anonymous user")
accessList = NewAccessList([]*idm.Role{})
return accessList, nil
}
roles := GetRoles(ctx, strings.Split(claims.Roles, ","))
accessList = NewAccessList(roles)
accessList.Append(GetACLsForRoles(ctx, roles, AclRead, AclDeny, AclWrite, AclLock, AclPolicy))
ResolvePolicyRequest = func(ctx context.Context, request *idm.PolicyEngineRequest) (*idm.PolicyEngineResponse, error) {
cli := idm.NewPolicyEngineServiceClient(common.SERVICE_GRPC_NAMESPACE_+common.SERVICE_POLICY, defaults.NewClient())
return cli.IsAllowed(ctx, request)
}
accessList.Flatten(ctx)
idmWorkspaces := GetWorkspacesForACLs(ctx, accessList)
for _, workspace := range idmWorkspaces {
accessList.Workspaces[workspace.UUID] = workspace
}
return accessList, nil
}
func AccessListFromUser(ctx context.Context, userNameOrUuid string, isUuid bool) (accessList *AccessList, user *idm.User, err error) {
if isUuid {
user, err = SearchUniqueUser(ctx, "", userNameOrUuid)
} else {
user, err = SearchUniqueUser(ctx, userNameOrUuid, "")
}
if err != nil {
return
}
accessList, err = AccessListFromRoles(ctx, user.Roles, false, true)
return
}
// SearchUniqueUser provides a shortcurt to search user services for one specific user.
func SearchUniqueUser(ctx context.Context, login string, uuid string, queries ...*idm.UserSingleQuery) (user *idm.User, err error) {
if login != "" && len(queries) == 0 {
if u, ok := usersCache.Get(login); ok {
if us, o := u.(*idm.User); o {
return us, nil
}
}
} else if uuid != "" && len(queries) == 0 {
if u, ok := usersCache.Get(uuid); ok {
if us, o := u.(*idm.User); o {
return us, nil
}
}
}
userCli := idm.NewUserServiceClient(common.SERVICE_GRPC_NAMESPACE_+common.SERVICE_USER, defaults.NewClient())
var searchRequests []*any.Any
if uuid != "" {
searchRequest, _ := ptypes.MarshalAny(&idm.UserSingleQuery{Uuid: uuid})
searchRequests = append(searchRequests, searchRequest)
} else if login != "" {
searchRequest, _ := ptypes.MarshalAny(&idm.UserSingleQuery{Login: login})
searchRequests = append(searchRequests, searchRequest)
}
for _, q := range queries {
searchRequest, _ := ptypes.MarshalAny(q)
searchRequests = append(searchRequests, searchRequest)
}
if len(searchRequests) == 0 {
return nil, fmt.Errorf("please provide at least one of login, uuid or queries")
}
streamer, err := userCli.SearchUser(ctx, &idm.SearchUserRequest{
Query: &service.Query{SubQueries: searchRequests, Operation: service.OperationType_AND},
})
if err != nil {
return
}
defer streamer.Close()
for {
resp, e := streamer.Recv()
if e != nil {
break
}
if resp == nil {
continue
}
user = resp.GetUser()
break
}
if user == nil {
return nil, errors.NotFound(common.SERVICE_USER, "Cannot find user with this login or uuid")
}
// Store to quick cache
if len(queries) == 0 {
usersCache.Set(login, user, cache.DefaultExpiration)
usersCache.Set(uuid, user, cache.DefaultExpiration)
}
return
}
// IsUserLocked checks if the passed user has a logout attribute defined.
func IsUserLocked(user *idm.User) bool {
var hasLock bool
if user.Attributes != nil {
if l, ok := user.Attributes["locks"]; ok {
var locks []string
if e := json.Unmarshal([]byte(l), &locks); e == nil {
for _, lock := range locks {
if lock == "logout" {
hasLock = true
break
}
}
}
}
}
return hasLock
}
// AccessListFromRoles loads the Acls and flatten them, eventually loading the discovered workspaces.
func AccessListFromRoles(ctx context.Context, roles []*idm.Role, countPolicies bool, loadWorkspaces bool) (accessList *AccessList, err error) {
accessList = NewAccessList(roles)
search := []*idm.ACLAction{AclRead, AclDeny, AclWrite}
if countPolicies {
search = append(search, AclPolicy)
ResolvePolicyRequest = func(ctx context.Context, request *idm.PolicyEngineRequest) (*idm.PolicyEngineResponse, error) {
return &idm.PolicyEngineResponse{Allowed: true}, nil
}
}
accessList.Append(GetACLsForRoles(ctx, roles, search...))
accessList.Flatten(ctx)
if loadWorkspaces {
idmWorkspaces := GetWorkspacesForACLs(ctx, accessList)
for _, workspace := range idmWorkspaces {
accessList.Workspaces[workspace.UUID] = workspace
}
}
return
}
func AccessListLoadFrontValues(ctx context.Context, accessList *AccessList) error {
values := GetACLsForRoles(ctx, accessList.OrderedRoles, AclFrontAction_, AclFrontParam_)
accessList.FrontPluginsValues = values
//log.Logger(ctx).Debug("Frontend ACL Values", zap.Any("values", values), zap.Any("flattenedValues", accessList.FlattenedFrontValues()))
return nil
}
// CheckContentLock finds if there is a global lock registered in ACLs.
func CheckContentLock(ctx context.Context, node *tree.Node) error {
if node.Uuid == "" {
return nil
}
var userName string
if claims, ok := ctx.Value(claim.ContextKey).(claim.Claims); ok {
userName = claims.Name
}
aclClient := idm.NewACLServiceClient(common.SERVICE_GRPC_NAMESPACE_+common.SERVICE_ACL, defaults.NewClient())
// Look for "quota" ACLs on this node
singleQ := &idm.ACLSingleQuery{NodeIDs: []string{node.Uuid}, Actions: []*idm.ACLAction{{Name: AclContentLock.Name}}}
//log.Logger(ctx).Debug("SEARCHING FOR LOCKS IN ACLS", zap.Any("q", singleQ))
q, _ := ptypes.MarshalAny(singleQ)
stream, err := aclClient.SearchACL(ctx, &idm.SearchACLRequest{Query: &service.Query{SubQueries: []*any.Any{q}}})
if err != nil {
return err
}
defer stream.Close()
for {
rsp, e := stream.Recv()
if e != nil {
break
}
if rsp == nil {
continue
}
acl := rsp.ACL
if userName == "" || acl.Action.Value != userName {
return errors.Forbidden("file.locked", "This file is locked by another user")
}
break
}
return nil
}
func ForceClearUserCache(login string) {
usersCache.Delete(login)
}