forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction-etl-p8-workspaces.go
297 lines (264 loc) · 7.94 KB
/
action-etl-p8-workspaces.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
package actions
import (
"context"
"encoding/json"
"fmt"
"github.com/pydio/cells/common/forms"
"github.com/micro/go-micro/client"
"go.uber.org/zap"
"github.com/pydio/cells/common/etl"
"github.com/pydio/cells/common/etl/models"
"github.com/pydio/cells/common/etl/stores"
"github.com/pydio/cells/common/log"
"github.com/pydio/cells/common/proto/idm"
"github.com/pydio/cells/common/proto/jobs"
"github.com/pydio/cells/common/utils/permissions"
"github.com/pydio/cells/scheduler/actions"
)
type SyncWorkspacesAction struct {
etlAction
}
var (
SyncWorkspacesActionName = "actions.etl.p8-workspaces"
)
// Unique identifier
func (c *SyncWorkspacesAction) GetName() string {
return SyncWorkspacesActionName
}
func (c *SyncWorkspacesAction) GetDescription(lang ...string) actions.ActionDescription {
return actions.ActionDescription{
ID: SyncWorkspacesActionName,
Label: "Sync. workspaces",
Icon: "",
Description: "Diff and merge workspaces from stores",
Category: actions.ActionCategoryETL,
SummaryTemplate: "",
HasForm: true,
}
}
func (c *SyncWorkspacesAction) GetParametersForm() *forms.Form {
return &forms.Form{Groups: []*forms.Group{
{
Fields: []forms.Field{
&forms.FormField{
Name: "left",
Type: forms.ParamString,
Label: "Left store",
Description: "Type of left users store",
},
&forms.FormField{
Name: "right",
Type: forms.ParamString,
Label: "Right store",
Description: "Type of right users store",
},
&forms.FormField{
Name: "splitUserRoles",
Type: forms.ParamString,
Label: "Split users and roles",
Description: "Sequentially import users then roles",
},
&forms.FormField{
Name: "cellAdmin",
Type: forms.ParamString,
Label: "Cells admin",
Description: "Login of cells administrator",
},
},
},
}}
}
// Pass parameters
func (c *SyncWorkspacesAction) Init(job *jobs.Job, cl client.Client, action *jobs.Action) error {
if err := c.ParseStores(action.Parameters); err != nil {
return err
}
if _, ok := action.Parameters["cellAdmin"]; !ok {
return fmt.Errorf("task workspaces must take a cellAdmin parameter")
}
return nil
}
func (c *SyncWorkspacesAction) migratePydio8(ctx context.Context, mapping map[string]string, progress chan etl.MergeOperation) {
options := stores.CreateOptions(ctx, c.params, jobs.ActionMessage{})
left, err := stores.LoadReadableStore(c.leftType, options)
if err != nil {
progress <- etl.MergeOperation{Error: err}
return
}
right, err := stores.LoadWritableStore(c.rightType, options)
if err != nil {
progress <- etl.MergeOperation{Error: err}
return
}
// First we retrieve the acls from the cells store
acls, _ := right.ListACLs(ctx, nil)
type info struct {
WorkspaceID string
NodeID string
}
// From that list we get workspace pathes
wsMapping := make(map[string]info)
for _, acl := range acls {
if acl.Action.Name == "workspace-path" {
if v, ok := mapping[acl.WorkspaceID]; ok {
wsMapping[v] = info{
WorkspaceID: acl.WorkspaceID,
NodeID: acl.NodeID,
}
}
}
}
var pydio8ACLs []*idm.ACL
// All groups must already have been migrated
apiGroups, er := right.ListGroups(ctx, nil)
if er != nil {
progress <- etl.MergeOperation{Error: er}
} else {
progress <- etl.MergeOperation{Description: "Loaded all groups for syncing ACLs"}
for i, apiGroup := range apiGroups {
pydio8GroupACLs, _ := left.ListACLs(ctx, map[string]interface{}{
"path": "AJXP_GRP_/" + apiGroup.GetGroupLabel(),
"roleID": apiGroup.GetUuid(),
})
for _, acl := range pydio8GroupACLs {
if a, ok := wsMapping[acl.GetWorkspaceID()]; ok {
acl.RoleID = apiGroup.GetUuid()
acl.WorkspaceID = a.WorkspaceID
acl.NodeID = a.NodeID
pydio8ACLs = append(pydio8ACLs, acl)
}
}
progress <- etl.MergeOperation{
Description: "Loaded ACLs for group " + apiGroup.GetGroupLabel(),
Cursor: i,
Total: len(apiGroups),
}
}
}
// All groups must already have been migrated
apiUsers, err := right.ListUsers(ctx, nil, nil)
if err != nil {
progress <- etl.MergeOperation{Error: err}
} else {
progress <- etl.MergeOperation{Description: "Loading all users roles for syncing ACLs"}
var i int
for _, apiUser := range apiUsers {
if apiUser.GetLogin() == c.params["cellAdmin"] {
// Manually make sure that cellAdmin has access to all workspaces
for wsId, wsInfo := range wsMapping {
pydio8ACLs = append(pydio8ACLs, &idm.ACL{
RoleID: apiUser.GetUuid(),
WorkspaceID: wsId,
NodeID: wsInfo.NodeID,
Action: permissions.AclRead,
}, &idm.ACL{
RoleID: apiUser.GetUuid(),
WorkspaceID: wsId,
NodeID: wsInfo.NodeID,
Action: permissions.AclWrite,
})
}
} else {
leftUsersACLs, _ := left.ListACLs(ctx, map[string]interface{}{
"path": "AJXP_USR_/" + apiUser.GetLogin(),
"roleID": apiUser.GetUuid(),
})
for _, acl := range leftUsersACLs {
if a, ok := wsMapping[acl.GetWorkspaceID()]; ok {
acl.RoleID = apiUser.GetUuid()
acl.WorkspaceID = a.WorkspaceID
acl.NodeID = a.NodeID
pydio8ACLs = append(pydio8ACLs, acl)
}
}
}
progress <- etl.MergeOperation{
Description: "Loaded ACLs for user " + apiUser.GetLogin(),
Cursor: i,
Total: len(apiUsers),
}
i++
}
}
// We pass a nil readableStore as we are not loading teams and it will not be used
apiRoles, e := right.ListRoles(ctx, nil, map[string]interface{}{})
if e != nil {
progress <- etl.MergeOperation{Error: e}
} else {
progress <- etl.MergeOperation{Description: "Loading all roles for syncing ACLs"}
for i, apiRole := range apiRoles {
leftRolesAcls, _ := left.ListACLs(ctx, map[string]interface{}{
"path": apiRole.GetUuid(),
"roleID": apiRole.GetUuid(),
})
log.Logger(ctx).Info("Loaded ACLs for role "+apiRole.GetUuid(), zap.Any("acls", leftRolesAcls))
for _, acl := range leftRolesAcls {
if a, ok := wsMapping[acl.GetWorkspaceID()]; ok {
acl.RoleID = apiRole.GetUuid()
acl.WorkspaceID = a.WorkspaceID
acl.NodeID = a.NodeID
pydio8ACLs = append(pydio8ACLs, acl)
}
}
progress <- etl.MergeOperation{
Description: "Loaded ACLs for role " + apiRole.GetLabel(),
Cursor: i,
Total: len(apiRoles),
}
}
}
merger := etl.NewMerger(left, right, &models.MergeOptions{})
defer merger.Close()
aclsDiff := new(models.ACLDiff)
merger.Diff(pydio8ACLs, acls, aclsDiff)
log.Logger(ctx).Info("After merge ACLs", zap.Any("add", aclsDiff.ToAdd()))
merger.Save(ctx, aclsDiff, progress)
}
// Run the actual action code
func (c *SyncWorkspacesAction) Run(ctx context.Context, channels *actions.RunnableChannels, input jobs.ActionMessage) (jobs.ActionMessage, error) {
channels.StatusMsg <- "Initializing workspaces list for diff/merge..."
progress := make(chan etl.MergeOperation)
finished := make(chan bool)
defer close(progress)
defer close(finished)
var pgErrors []error
go func() {
for {
select {
case op := <-progress:
channels.StatusMsg <- op.Description
if op.Total > 0 {
channels.Progress <- float32(op.Cursor) / float32(op.Total)
}
if op.Error != nil {
pgErrors = append(pgErrors, op.Error)
}
case <-finished:
return
}
}
}()
var mapping map[string]string
mappingJSON, ok := c.params["mapping"]
if !ok {
return input, fmt.Errorf("missing mapping parameter")
}
if err := json.Unmarshal([]byte(mappingJSON), &mapping); err != nil {
return input, err
}
c.migratePydio8(ctx, mapping, progress)
finished <- true
output := input
output.AppendOutput(&jobs.ActionOutput{
Success: true,
StringBody: "Succesfully synced workspaces",
})
var gE error
if len(pgErrors) > 0 {
gE = pgErrors[0]
for _, err := range pgErrors {
output = output.WithError(err)
}
}
return output, gE
}