-
Notifications
You must be signed in to change notification settings - Fork 180
/
userspace.go
567 lines (505 loc) · 16.6 KB
/
userspace.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
/*
* 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 rest
import (
"context"
"encoding/json"
"fmt"
"net/url"
"path"
"strings"
"time"
"github.com/pydio/cells/common/config"
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/errors"
"github.com/pborman/uuid"
"go.uber.org/zap"
"github.com/pydio/cells/common"
"github.com/pydio/cells/common/auth/claim"
"github.com/pydio/cells/common/log"
"github.com/pydio/cells/common/micro"
"github.com/pydio/cells/common/proto/jobs"
"github.com/pydio/cells/common/proto/tree"
"github.com/pydio/cells/common/registry"
"github.com/pydio/cells/common/views"
"github.com/pydio/cells/scheduler/lang"
)
func compress(ctx context.Context, selectedPathes []string, targetNodePath string, format string, languages ...string) (string, error) {
T := lang.Bundle().GetTranslationFunc(languages...)
jobUuid := "compress-folders-" + uuid.New()
claims := ctx.Value(claim.ContextKey).(claim.Claims)
userName := claims.Name
err := getRouter().WrapCallback(func(inputFilter views.NodeFilter, outputFilter views.NodeFilter) error {
var targetSize int64
for i, p := range selectedPathes {
node := &tree.Node{Path: p}
_, node, nodeErr := inputFilter(ctx, node, "in")
log.Logger(ctx).Debug("Filtering Input Node", zap.Any("node", node), zap.Error(nodeErr))
if nodeErr != nil {
return nodeErr
}
if resp, e := getRouter().GetClientsPool().GetTreeClient().ReadNode(ctx, &tree.ReadNodeRequest{Node: node}); e != nil {
return e
} else {
targetSize += resp.GetNode().GetSize()
}
selectedPathes[i] = node.Path
}
if targetNodePath != "" {
node := &tree.Node{Path: targetNodePath}
targetCtx, node, nodeErr := inputFilter(ctx, node, "in")
if nodeErr != nil {
log.Logger(ctx).Error("Filtering Input Node", zap.Any("node", node), zap.Error(nodeErr))
return nodeErr
}
// Assume archive size will be as big as all files (we cannot not know in advance)
node.Size = targetSize
if err := getRouter().WrappedCanApply(nil, targetCtx, &tree.NodeChangeEvent{Type: tree.NodeChangeEvent_CREATE, Target: node}); err != nil {
return err
}
targetNodePath = node.Path
}
log.Logger(ctx).Debug("Submitting selected pathes for compression", zap.Any("pathes", selectedPathes))
job := &jobs.Job{
ID: jobUuid,
Owner: userName,
Label: T("Jobs.User.Compress"),
Inactive: false,
Languages: languages,
MaxConcurrency: 1,
AutoStart: true,
AutoClean: true,
Actions: []*jobs.Action{
{
ID: "actions.archive.compress",
Parameters: map[string]string{
"format": format,
"target": targetNodePath,
},
NodesSelector: &jobs.NodesSelector{
Collect: true,
Pathes: selectedPathes,
},
},
},
}
cli := jobs.NewJobServiceClient(registry.GetClient(common.SERVICE_JOBS))
_, er := cli.PutJob(ctx, &jobs.PutJobRequest{Job: job})
return er
})
return jobUuid, err
}
func extract(ctx context.Context, selectedNode string, targetPath string, format string, languages ...string) (string, error) {
jobUuid := "extract-archive-" + uuid.New()
claims := ctx.Value(claim.ContextKey).(claim.Claims)
userName := claims.Name
T := lang.Bundle().GetTranslationFunc(languages...)
err := getRouter().WrapCallback(func(inputFilter views.NodeFilter, outputFilter views.NodeFilter) error {
node := &tree.Node{Path: selectedNode}
srcCtx, node, nodeErr := inputFilter(ctx, node, "in")
if nodeErr != nil {
log.Logger(ctx).Error("Filtering Input Node", zap.Any("node", node), zap.Error(nodeErr))
return nodeErr
}
archiveNode := node.Path
var archiveSize int64
if resp, e := getRouter().GetClientsPool().GetTreeClient().ReadNode(srcCtx, &tree.ReadNodeRequest{Node: node}); e != nil {
return e
} else {
archiveSize = resp.GetNode().GetSize()
}
targetNode := &tree.Node{Path: targetPath}
if targetPath == "" {
targetNode.Path = path.Dir(selectedNode)
}
targetCtx, realNode, nodeErr := inputFilter(ctx, targetNode, "in")
if nodeErr != nil {
log.Logger(ctx).Error("Filtering Input Node", zap.Any("node", targetNode), zap.Error(nodeErr))
return nodeErr
}
if targetPath != "" {
targetPath = realNode.Path
}
realNode.Size = archiveSize
if err := getRouter().WrappedCanApply(nil, targetCtx, &tree.NodeChangeEvent{Type: tree.NodeChangeEvent_CREATE, Target: realNode}); err != nil {
return err
}
job := &jobs.Job{
ID: jobUuid,
Owner: userName,
Label: T("Jobs.User.Extract"),
Inactive: false,
Languages: languages,
MaxConcurrency: 1,
AutoStart: true,
AutoClean: true,
Actions: []*jobs.Action{
{
ID: "actions.archive.extract",
Parameters: map[string]string{
"format": format,
"target": targetPath,
},
NodesSelector: &jobs.NodesSelector{
Pathes: []string{archiveNode},
},
},
},
}
cli := jobs.NewJobServiceClient(registry.GetClient(common.SERVICE_JOBS))
_, err := cli.PutJob(ctx, &jobs.PutJobRequest{Job: job})
return err
})
return jobUuid, err
}
func dirCopy(ctx context.Context, selectedPathes []string, targetNodePath string, targetIsParent bool, move bool, languages ...string) (string, error) {
T := lang.Bundle().GetTranslationFunc(languages...)
taskType := "copy"
taskLabel := T("Jobs.User.DirCopy")
if move {
taskType = "move"
taskLabel = T("Jobs.User.DirMove")
}
jobUuid := "copy-move-" + uuid.New()
claims := ctx.Value(claim.ContextKey).(claim.Claims)
userName := claims.Name
sourceId := "in"
targetId := "in"
if move {
sourceId = "from"
targetId = "to"
}
err := getRouter().WrapCallback(func(inputFilter views.NodeFilter, outputFilter views.NodeFilter) error {
var dir, base string
if targetIsParent {
dir = targetNodePath
} else {
dir, base = path.Split(targetNodePath)
}
targetNode := &tree.Node{Path: dir}
targetCtx, targetNode, nodeErr := inputFilter(ctx, targetNode, targetId)
if nodeErr != nil {
log.Logger(ctx).Error("Filtering Input Node Parent", zap.Any("node", targetNode), zap.Error(nodeErr))
return nodeErr
}
var targetParent = ""
if targetIsParent {
targetNodePath = targetNode.Path
targetParent = "true"
} else {
targetNodePath = targetNode.Path + "/" + base
}
var createSize int64
var loadedNodes []*tree.Node
for i, p := range selectedPathes {
node := &tree.Node{Path: p}
srcCtx, node, nodeErr := inputFilter(ctx, node, sourceId)
if nodeErr != nil {
return nodeErr
}
r, e := getRouter().GetClientsPool().GetTreeClient().ReadNode(ctx, &tree.ReadNodeRequest{Node: &tree.Node{Path: node.Path}})
if e != nil {
return e
}
createSize += r.Node.Size
targetNode.Size = r.Node.Size
checkNode := targetNode.Clone()
if !targetIsParent { // Use real target to eventually check for name / extension
checkNode.Path = targetNodePath
}
if move {
if sErr := getRouter().WrappedCanApply(srcCtx, targetCtx, &tree.NodeChangeEvent{Type: tree.NodeChangeEvent_UPDATE_PATH, Source: r.Node, Target: checkNode}); sErr != nil {
return sErr
}
} else {
if er := getRouter().WrappedCanApply(nil, targetCtx, &tree.NodeChangeEvent{Type: tree.NodeChangeEvent_CREATE, Target: checkNode}); er != nil {
return er
}
}
loadedNodes = append(loadedNodes, r.Node)
selectedPathes[i] = node.Path
}
if len(loadedNodes) > 1 {
if move {
taskLabel = T("Jobs.User.MultipleMove")
} else {
taskLabel = T("Jobs.User.MultipleCopy")
// Additional pre-check for full copy size
targetNode.Size = createSize
if er := router.WrappedCanApply(nil, targetCtx, &tree.NodeChangeEvent{Type: tree.NodeChangeEvent_CREATE, Target: targetNode}); er != nil {
return er
}
}
} else if len(loadedNodes) == 1 && loadedNodes[0].IsLeaf() {
if move {
taskLabel = T("Jobs.User.FileMove")
} else {
taskLabel = T("Jobs.User.FileCopy")
}
}
log.Logger(ctx).Info("Creating copy/move job", zap.Any("paths", selectedPathes), zap.String("target", targetNodePath))
if move && strings.Contains(targetNodePath, common.RECYCLE_BIN_NAME) {
// Update node meta before moving
metaClient := tree.NewNodeReceiverClient(common.SERVICE_GRPC_NAMESPACE_+common.SERVICE_META, defaults.NewClient())
for _, n := range loadedNodes {
metaNode := &tree.Node{Uuid: n.GetUuid()}
metaNode.SetMeta(common.META_NAMESPACE_RECYCLE_RESTORE, n.Path)
_, e := metaClient.CreateNode(ctx, &tree.CreateNodeRequest{Node: metaNode})
if e != nil {
log.Logger(ctx).Error("Error while saving recycle_restore meta", zap.Error(e))
}
}
}
job := &jobs.Job{
ID: jobUuid,
Owner: userName,
Label: taskLabel,
Inactive: false,
Languages: languages,
MaxConcurrency: 1,
AutoStart: true,
AutoClean: true,
Actions: []*jobs.Action{
{
ID: "actions.tree.copymove",
Parameters: map[string]string{
"type": taskType,
"target": targetNodePath,
"targetParent": targetParent,
"recursive": "true",
"create": "true",
},
NodesSelector: &jobs.NodesSelector{
//Collect: true,
Pathes: selectedPathes,
},
},
},
}
cli := jobs.NewJobServiceClient(registry.GetClient(common.SERVICE_JOBS))
_, er := cli.PutJob(ctx, &jobs.PutJobRequest{Job: job})
return er
})
return jobUuid, err
}
func syncDatasource(ctx context.Context, dsName string, languages ...string) (string, error) {
T := lang.Bundle().GetTranslationFunc(languages...)
jobUuid := "resync-ds-" + dsName
cli := jobs.NewJobServiceClient(registry.GetClient(common.SERVICE_JOBS))
if resp, er := cli.GetJob(ctx, &jobs.GetJobRequest{JobID: jobUuid}); er == nil && resp.Job != nil {
client.Publish(ctx, client.NewPublication(common.TOPIC_TIMER_EVENT, &jobs.JobTriggerEvent{
JobID: jobUuid,
RunNow: true,
}))
return jobUuid, nil
}
job := &jobs.Job{
ID: jobUuid,
Owner: common.PYDIO_SYSTEM_USERNAME,
Label: T("Jobs.User.ResyncDS", map[string]string{"DsName": dsName}),
Inactive: false,
Languages: languages,
MaxConcurrency: 1,
AutoStart: true,
Actions: []*jobs.Action{
{
ID: "actions.cmd.resync",
Parameters: map[string]string{
"service": common.SERVICE_GRPC_NAMESPACE_ + common.SERVICE_DATA_SYNC_ + dsName,
},
},
},
}
_, er := cli.PutJob(ctx, &jobs.PutJobRequest{Job: job})
return jobUuid, er
}
// wgetTasks launch one or many background task for downloading URL to the storage
func wgetTasks(ctx context.Context, parentPath string, urls []string, languages ...string) ([]string, error) {
T := lang.Bundle().GetTranslationFunc(languages...)
taskLabel := T("Jobs.User.Wget")
claims := ctx.Value(claim.ContextKey).(claim.Claims)
userName := claims.Name
var jobUuids []string
var parentNode, fullPathParentNode *tree.Node
if resp, e := getRouter().ReadNode(ctx, &tree.ReadNodeRequest{Node: &tree.Node{Path: parentPath}}); e != nil {
return []string{}, e
} else {
parentNode = resp.Node
}
// Compute node real path, and check that its writeable as well
if err := getRouter().WrapCallback(func(inputFilter views.NodeFilter, outputFilter views.NodeFilter) error {
updateCtx, realParent, e := inputFilter(ctx, parentNode, "sel")
if e != nil {
return e
} else {
accessList, e := views.AccessListFromContext(updateCtx)
if e != nil {
return e
}
// First load ancestors or grab them from BranchInfo
ctx, parents, err := views.AncestorsListFromContext(updateCtx, realParent, "in", getRouter().GetClientsPool(), false)
if err != nil {
return err
}
if !accessList.CanWrite(ctx, parents...) {
return errors.Forbidden(common.SERVICE_JOBS, "Parent Node is not writeable")
}
fullPathParentNode = realParent
}
return nil
}); err != nil {
return jobUuids, err
}
var whiteList, blackList []string
wl := config.Get("frontend", "plugin", "uploader.http", "REMOTE_UPLOAD_WHITELIST").String("")
bl := config.Get("frontend", "plugin", "uploader.http", "REMOTE_UPLOAD_BLACKLIST").String("")
if wl != "" {
whiteList = strings.Split(wl, ",")
}
if bl != "" {
blackList = strings.Split(bl, ",")
}
cli := jobs.NewJobServiceClient(registry.GetClient(common.SERVICE_JOBS))
for _, u := range urls {
parsed, e := url.Parse(u)
if e != nil {
return nil, e
}
if (len(whiteList) > 0 && !hostListCheck(whiteList, parsed)) || (len(blackList) > 0 && hostListCheck(blackList, parsed)) {
return nil, fmt.Errorf("hostname %s is not allowed", parsed.Hostname())
}
jobUuid := "wget-" + uuid.New()
jobUuids = append(jobUuids, jobUuid)
var params = map[string]string{
"url": u,
}
cleanUrl := strings.Split(u, "?")[0]
cleanUrl = strings.Split(cleanUrl, "#")[0]
basename := path.Base(cleanUrl)
if basename == "" {
basename = jobUuid
}
// Recheck target node for policies
if err := getRouter().WrapCallback(func(inputFilter views.NodeFilter, outputFilter views.NodeFilter) error {
updateCtx, realTarget, e := inputFilter(ctx, &tree.Node{Path: path.Join(parentPath, basename)}, "sel")
if e != nil {
return e
} else {
accessList, e := views.AccessListFromContext(updateCtx)
if e != nil {
return e
}
// First load ancestors or grab them from BranchInfo
ctx, parents, err := views.AncestorsListFromContext(updateCtx, realTarget, "in", getRouter().GetClientsPool(), true)
if err != nil {
return err
}
if !accessList.CanWrite(ctx, parents...) {
return errors.Forbidden(common.SERVICE_JOBS, "Parent Node is not writeable")
}
}
return nil
}); err != nil {
return jobUuids, err
}
job := &jobs.Job{
ID: jobUuid,
Owner: userName,
Label: taskLabel,
Inactive: false,
Languages: languages,
MaxConcurrency: 5,
AutoStart: true,
Actions: []*jobs.Action{
{
ID: "actions.cmd.wget",
Parameters: params,
NodesSelector: &jobs.NodesSelector{
Pathes: []string{path.Join(fullPathParentNode.Path, basename)},
},
},
},
}
_, er := cli.PutJob(ctx, &jobs.PutJobRequest{Job: job})
if er != nil {
return jobUuids, er
}
}
return jobUuids, nil
}
func p8migration(ctx context.Context, jsonParams string) (string, error) {
claims := ctx.Value(claim.ContextKey).(claim.Claims)
if claims.Profile != common.PYDIO_PROFILE_ADMIN {
return "", errors.Forbidden("user.job.forbidden", "you are not allowed to create this job")
}
jobUuid := "pydio8-data-import"
// Parse JsonParams
type Action struct {
Name string `json:"name"`
Params map[string]string `json:"params"`
}
var data []*Action
if err := json.Unmarshal([]byte(jsonParams), &data); err != nil {
return "", err
}
job := &jobs.Job{
ID: jobUuid,
Label: "Import Data from Pydio 8",
Owner: common.PYDIO_SYSTEM_USERNAME,
Inactive: false,
//AutoStart: true,
MaxConcurrency: 1,
}
rootAction := &jobs.Action{}
log.Logger(ctx).Info("Got Actions", zap.Any("actions", data))
parentAction := rootAction
for _, a := range data {
action := &jobs.Action{
ID: a.Name,
Parameters: a.Params,
ChainedActions: []*jobs.Action{},
}
parentAction.ChainedActions = append(parentAction.ChainedActions, action)
parentAction = action
}
job.Actions = rootAction.ChainedActions
log.Logger(ctx).Info("Posting Job", zap.Any("job", job))
cli := jobs.NewJobServiceClient(registry.GetClient(common.SERVICE_JOBS))
if _, er := cli.PutJob(ctx, &jobs.PutJobRequest{Job: job}); er == nil {
<-time.After(2 * time.Second)
client.Publish(ctx, client.NewPublication(common.TOPIC_TIMER_EVENT, &jobs.JobTriggerEvent{
JobID: jobUuid,
RunNow: true,
}))
return jobUuid, nil
} else {
return "", er
}
}
// hostListCheck check if url hostname is in string
func hostListCheck(list []string, u *url.URL) bool {
h := u.Hostname()
for _, l := range list {
if strings.TrimSpace(l) == h {
return true
}
}
return false
}