-
Notifications
You must be signed in to change notification settings - Fork 173
/
handler.go
557 lines (497 loc) · 16 KB
/
handler.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
/*
* Copyright (c) 2018. 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 grpc
import (
"context"
"fmt"
"path"
"sort"
"strings"
"sync"
"time"
"go.uber.org/zap"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
"github.com/pydio/cells/v4/common"
"github.com/pydio/cells/v4/common/broker"
"github.com/pydio/cells/v4/common/client/grpc"
"github.com/pydio/cells/v4/common/log"
"github.com/pydio/cells/v4/common/proto/idm"
"github.com/pydio/cells/v4/common/proto/jobs"
service "github.com/pydio/cells/v4/common/proto/service"
"github.com/pydio/cells/v4/common/proto/tree"
"github.com/pydio/cells/v4/common/runtime"
servicecontext "github.com/pydio/cells/v4/common/service/context"
"github.com/pydio/cells/v4/common/service/context/metadata"
"github.com/pydio/cells/v4/common/service/errors"
"github.com/pydio/cells/v4/common/sql/resources"
"github.com/pydio/cells/v4/common/utils/cache"
json "github.com/pydio/cells/v4/common/utils/jsonx"
"github.com/pydio/cells/v4/common/utils/permissions"
"github.com/pydio/cells/v4/idm/user"
"github.com/pydio/cells/v4/scheduler/tasks"
)
var (
defaultPolicies = []*service.ResourcePolicy{
{Subject: "profile:standard", Action: service.ResourcePolicyAction_READ, Effect: service.ResourcePolicy_allow},
{Subject: "profile:admin", Action: service.ResourcePolicyAction_WRITE, Effect: service.ResourcePolicy_allow},
}
autoAppliesCache cache.Cache
)
// ByOverride implements sort.Interface for []Role based on the ForceOverride field.
type ByOverride []*idm.Role
func (a ByOverride) Len() int { return len(a) }
func (a ByOverride) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByOverride) Less(i, j int) bool { return !a[i].ForceOverride && a[j].ForceOverride }
// Handler definition
type Handler struct {
ctx context.Context
idm.UnimplementedUserServiceServer
service.UnimplementedLoginModifierServer
dao user.DAO
}
func NewHandler(ctx context.Context, dao user.DAO) idm.NamedUserServiceServer {
return &Handler{ctx: ctx, dao: dao}
}
func (h *Handler) Name() string {
return ServiceName
}
// BindUser binds a user with login/password
func (h *Handler) BindUser(ctx context.Context, req *idm.BindUserRequest) (*idm.BindUserResponse, error) {
u, err := h.dao.Bind(req.UserName, req.Password)
if err != nil {
return nil, err
}
u.Password = ""
resp := &idm.BindUserResponse{
User: u,
}
return resp, nil
}
// CreateUser adds or creates a user or a group in the underlying database.
func (h *Handler) CreateUser(ctx context.Context, req *idm.CreateUserRequest) (*idm.CreateUserResponse, error) {
passChange := req.User.Password
// Create or update user
newUser, createdNodes, err := h.dao.Add(req.User)
if err != nil {
log.Logger(ctx).Error("cannot put user "+req.User.Login, req.User.ZapUuid(), zap.Error(err))
return nil, err
}
resp := &idm.CreateUserResponse{}
out := newUser.(*idm.User)
var movedGroup string
if out.Attributes != nil && out.Attributes["original_group"] != "" {
movedGroup = out.Attributes["original_group"]
delete(out.Attributes, "original_group")
}
if passChange != "" {
// Check if it is a "force pass change operation".
ctxLogin, _ := permissions.FindUserNameInContext(ctx)
if l, ok := out.Attributes["locks"]; ok && strings.Contains(l, "pass_change") && ctxLogin == out.Login {
if req.User.OldPassword == out.Password {
return nil, fmt.Errorf("new password is the same as the old password, please use a different one")
}
var locks, newLocks []string
_ = json.Unmarshal([]byte(l), &locks)
for _, lock := range locks {
if lock != "pass_change" {
newLocks = append(newLocks, lock)
}
}
marsh, _ := json.Marshal(newLocks)
out.Attributes["locks"] = string(marsh)
if _, _, e := h.dao.Add(out); e == nil {
log.Logger(ctx).Info("user "+req.User.Login+" successfully updated his password", req.User.ZapUuid())
}
}
}
out.Password = ""
resp.User = out
if len(req.User.Policies) == 0 {
var userPolicies []*service.ResourcePolicy
userPolicies = append(userPolicies, defaultPolicies...)
if !req.User.IsGroup {
// A user must be able to edit his own profile!
userPolicies = append(userPolicies, &service.ResourcePolicy{
Subject: "user:" + out.Login,
Action: service.ResourcePolicyAction_WRITE,
Effect: service.ResourcePolicy_allow,
})
}
req.User.Policies = userPolicies
}
log.Logger(ctx).Debug("ADDING POLICIES NOW", zap.Int("p length", len(req.User.Policies)), zap.Int("createdNodes length", len(createdNodes)))
if err := h.dao.AddPolicies(len(createdNodes) == 0, out.Uuid, req.User.Policies); err != nil {
return nil, err
}
for _, g := range createdNodes {
if g.GetUuid() != out.Uuid && g.GetType() == tree.NodeType_COLLECTION {
// Groups where created in the process, add default policies on them
log.Logger(ctx).Info("Setting Default Policies on groups that were created automatically", zap.String("groupPath", g.GetPath()))
if err := h.dao.AddPolicies(false, g.GetUuid(), defaultPolicies); err != nil {
return nil, err
}
}
}
// Fix output GroupPath for Groups
publishUser := proto.Clone(out).(*idm.User)
if out.IsGroup {
publishUser.GroupPath, publishUser.GroupLabel = path.Split(publishUser.GroupPath)
}
if len(createdNodes) == 0 {
// Propagate creation event
cEvent := &idm.ChangeEvent{
Type: idm.ChangeEventType_UPDATE,
User: publishUser,
Attributes: map[string]string{},
}
if movedGroup != "" {
cEvent.Attributes["original_group"] = movedGroup
}
if cu, _ := permissions.FindUserNameInContext(ctx); cu != "" {
cEvent.Attributes["ctx_username"] = cu
}
broker.MustPublish(ctx, common.TopicIdmEvent, cEvent)
if out.IsGroup {
log.Auditer(ctx).Info(
fmt.Sprintf("Updated group [%s]", out.GroupLabel),
log.GetAuditId(common.AuditGroupUpdate),
out.ZapUuid(),
)
} else {
log.Auditer(ctx).Info(
fmt.Sprintf("Updated user [%s]", out.Login),
log.GetAuditId(common.AuditUserUpdate),
out.ZapUuid(),
)
}
} else {
// Propagate creation event
broker.MustPublish(ctx, common.TopicIdmEvent, &idm.ChangeEvent{
Type: idm.ChangeEventType_CREATE,
User: publishUser,
})
if out.IsGroup {
log.Auditer(ctx).Info(
fmt.Sprintf("Created group [%s]", out.GroupPath),
log.GetAuditId(common.AuditGroupCreate),
out.ZapUuid(),
)
} else {
log.Auditer(ctx).Info(
fmt.Sprintf("Created user [%s]", path.Join(out.GroupPath, out.Login)),
log.GetAuditId(common.AuditUserCreate),
out.ZapUuid(),
zap.String("GroupPath", out.GroupPath),
)
}
}
return resp, nil
}
// DeleteUser from database
func (h *Handler) DeleteUser(ctx context.Context, req *idm.DeleteUserRequest) (*idm.DeleteUserResponse, error) {
usersChan := make(chan *idm.User)
done := make(chan bool)
var autoClient *tasks.ReconnectingClient
var task *jobs.Task
var taskChan chan interface{}
uName, _ := permissions.FindUserNameInContext(ctx)
if tU, ok := metadata.CanonicalMeta(ctx, servicecontext.ContextMetaTaskUuid); ok {
jU, _ := metadata.CanonicalMeta(ctx, servicecontext.ContextMetaJobUuid)
task = &jobs.Task{
JobID: jU,
ID: tU,
TriggerOwner: uName,
Status: jobs.TaskStatus_Running,
HasProgress: true,
}
taskChan = make(chan interface{})
autoClient = tasks.NewTaskReconnectingClient(ctx)
autoClient.StartListening(taskChan)
defer autoClient.Stop()
}
i, err := h.dao.Count(req.Query, true)
if err != nil {
return nil, err
}
total := float32(i)
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
var crt float32
for {
select {
case deleted := <-usersChan:
if deleted == nil {
continue
}
if pp, er := h.dao.GetPoliciesForResource(deleted.Uuid); er == nil {
deleted.Policies = pp
} else {
log.Logger(ctx).Warn("cannot load policies on user deletion", zap.Error(er))
}
_ = h.dao.DeletePoliciesForResource(deleted.Uuid)
if deleted.IsGroup {
_ = h.dao.DeletePoliciesBySubject(fmt.Sprintf("role:%s", deleted.Uuid))
} else {
_ = h.dao.DeletePoliciesBySubject(fmt.Sprintf("user:%s", deleted.Uuid))
}
// Propagate deletion event
broker.MustPublish(ctx, common.TopicIdmEvent, &idm.ChangeEvent{
Type: idm.ChangeEventType_DELETE,
User: deleted,
})
var msg string
if deleted.IsGroup {
msg = fmt.Sprintf("Deleted group [%s]", path.Join(deleted.GroupPath, deleted.GroupLabel))
log.Auditer(ctx).Info(msg, log.GetAuditId(common.AuditGroupDelete), deleted.ZapUuid())
} else {
msg = fmt.Sprintf("Deleted user [%s] from [%s]", deleted.Login, deleted.GroupPath)
log.Auditer(ctx).Info(msg, log.GetAuditId(common.AuditUserDelete), deleted.ZapUuid())
}
crt++
percent := crt / total
if task != nil {
task.Progress = percent
task.StatusMessage = msg
task.Status = jobs.TaskStatus_Running
log.TasksLogger(ctx).Info(msg)
taskChan <- task
}
case <-done:
if task != nil {
task.Progress = 1
task.StatusMessage = "Background delete complete"
task.EndTime = int32(time.Now().Unix())
task.Status = jobs.TaskStatus_Finished
taskChan <- task
}
return
}
}
}()
numRows, err := h.dao.Del(req.Query, usersChan)
close(done)
close(usersChan)
if err != nil {
if task != nil {
task.Progress = 1
task.StatusMessage = err.Error()
task.Status = jobs.TaskStatus_Error
taskChan <- task
}
return nil, err
}
wg.Wait()
if taskChan != nil {
close(taskChan)
}
return &idm.DeleteUserResponse{RowsDeleted: numRows}, nil
}
// SearchUser in database
func (h *Handler) SearchUser(request *idm.SearchUserRequest, response idm.UserService_SearchUserServer) error {
ctx := response.Context()
autoApplies, er := h.loadAutoAppliesRoles(ctx)
if er != nil {
return er
}
usersGroups := new([]interface{})
if err := h.dao.Search(request.Query, usersGroups); err != nil {
return err
}
var e error
for _, in := range *usersGroups {
if usr, ok := in.(*idm.User); ok {
usr.Password = ""
if usr.Policies, e = h.dao.GetPoliciesForResource(usr.Uuid); e != nil {
log.Logger(ctx).Error("cannot load policies for user "+usr.Uuid, zap.Error(e))
continue
}
h.applyAutoApplies(usr, autoApplies)
response.Send(&idm.SearchUserResponse{User: usr})
} else {
return errors.InternalServerError(common.ServiceUser, "wrong type received, should have been idm.User or idm.Group")
}
}
return nil
}
// CountUser in database
func (h *Handler) CountUser(ctx context.Context, request *idm.SearchUserRequest) (*idm.CountUserResponse, error) {
total, err := h.dao.Count(request.Query)
if err != nil {
return nil, err
}
return &idm.CountUserResponse{Count: int32(total)}, nil
}
// StreamUser from database
func (h *Handler) StreamUser(streamer idm.UserService_StreamUserServer) error {
ctx := streamer.Context()
autoApplies, e := h.loadAutoAppliesRoles(ctx)
if e != nil {
return e
}
for {
incoming, err := streamer.Recv()
if incoming == nil || err != nil {
break
}
users := new([]interface{})
if err := h.dao.Search(incoming.Query, users); err != nil {
return err
}
for _, in := range *users {
if usr, ok := in.(*idm.User); ok {
usr.Password = ""
h.applyAutoApplies(usr, autoApplies)
streamer.Send(&idm.SearchUserResponse{User: usr})
}
}
streamer.Send(nil)
}
return nil
}
// applyAutoApplies tries to find if some roles must be added to this user.
// If necessary, it adds role to the list AFTER the groupRoles and before other roles.
func (h *Handler) applyAutoApplies(usr *idm.User, autoApplies map[string][]*idm.Role) {
// Apply automatically role for profile
if usr.Attributes != nil {
if profile, ok := usr.Attributes[idm.UserAttrProfile]; ok {
// For shared users, disable group roles inheritance
if profile == common.PydioProfileShared || profile == common.PydioProfileAnon {
var newRoles []*idm.Role
for _, role := range usr.Roles {
if !role.GroupRole {
newRoles = append(newRoles, role)
}
}
usr.Roles = newRoles
}
if len(autoApplies) == 0 {
return
}
// Apply AutoApply role if any
if applies, has := autoApplies[profile]; has {
var newRoles []*idm.Role
var added = false
for _, crt := range usr.Roles {
if !added && !crt.GroupRole {
newRoles = append(newRoles, applies...)
added = true
}
newRoles = append(newRoles, crt)
}
usr.Roles = newRoles
}
}
}
// Sort so that ForceOverride are at the bottom of the list
var head, body, foot []*idm.Role
for _, r := range usr.Roles {
if r.GroupRole {
head = append(head, r)
} else if r.UserRole {
foot = append(foot, r)
} else {
body = append(body, r)
}
}
sort.Sort(ByOverride(body))
all := append(head, body...)
all = append(all, foot...)
usr.Roles = all
}
// LoadAutoAppliesRoles performs a request to the Roles service to find all roles that have a non-empty list
// of "AutoApplies" : these roles must be applied to user with a given profile.
func (h *Handler) loadAutoAppliesRoles(ctx context.Context) (autoApplies map[string][]*idm.Role, err error) {
// Check if it's not already cached
if autoAppliesCache != nil {
if autoAppliesCache.Get("autoApplies", &autoApplies) {
return
}
}
autoApplies = make(map[string][]*idm.Role)
roleCli := idm.NewRoleServiceClient(grpc.GetClientConnFromCtx(h.ctx, common.ServiceRole))
q, _ := anypb.New(&idm.RoleSingleQuery{HasAutoApply: true})
stream, e := roleCli.SearchRole(ctx, &idm.SearchRoleRequest{Query: &service.Query{SubQueries: []*anypb.Any{q}}})
if e != nil {
return autoApplies, e
}
for {
resp, e := stream.Recv()
if e != nil {
break
}
applies := resp.Role.AutoApplies
for _, profileId := range applies {
var data []*idm.Role
var ok bool
if data, ok = autoApplies[profileId]; !ok {
data = []*idm.Role{}
}
data = append(data, resp.Role)
autoApplies[profileId] = data
}
}
// Save to cache
if autoAppliesCache == nil {
c, _ := cache.OpenCache(context.TODO(), runtime.ShortCacheURL("evictionTime", "10s", "cleanWindow", "20s"))
autoAppliesCache = c
}
autoAppliesCache.Set("autoApplies", autoApplies)
return
}
func (h *Handler) ModifyLogin(ctx context.Context, req *service.ModifyLoginRequest) (resp *service.ModifyLoginResponse, err error) {
var mm []string
if req.GetDryRun() {
// Check user in Tree
usersGroups := new([]interface{})
uQ, _ := anypb.New(&idm.UserSingleQuery{Login: req.OldLogin})
er := h.dao.Search(&service.Query{SubQueries: []*anypb.Any{uQ}}, usersGroups)
if er != nil {
return nil, er
}
for _, us := range *usersGroups {
if usr, ok := us.(*idm.User); ok {
mm = append(mm, "Found user "+usr.GetLogin()+" ("+usr.GetUuid()+") in group "+usr.GetGroupPath())
}
}
} else {
// Apply change in Tree
if uCount, er := h.dao.UpdateNameInPlace(req.OldLogin, req.NewLogin, "", -1); er != nil {
return nil, er
} else {
mm = append(mm, fmt.Sprintf("Replace %d user(s) in index table", uCount))
}
// Apply change in Attributes
if aCount, er := h.dao.LoginModifiedAttr(req.OldLogin, req.NewLogin); er != nil {
return nil, er
} else {
mm = append(mm, fmt.Sprintf("Replace %d user(s) in attributes table", aCount))
}
}
// Apply Policies
if ppr, er := resources.ModifyLogin(ctx, h.dao, req); er != nil {
return nil, er
} else {
mm = append(mm, ppr.Messages...)
}
return &service.ModifyLoginResponse{Messages: mm, Success: true}, nil
}