forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction-etl-users.go
247 lines (213 loc) · 6.33 KB
/
action-etl-users.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
package actions
import (
"context"
"fmt"
"time"
"github.com/micro/go-micro/client"
"go.uber.org/zap"
"github.com/pydio/cells/common"
"github.com/pydio/cells/common/auth"
"github.com/pydio/cells/common/etl"
"github.com/pydio/cells/common/etl/models"
"github.com/pydio/cells/common/forms"
"github.com/pydio/cells/common/log"
"github.com/pydio/cells/common/proto/jobs"
"github.com/pydio/cells/scheduler/actions"
)
type SyncUsersAction struct {
etlAction
}
func (c *SyncUsersAction) GetDescription(lang ...string) actions.ActionDescription {
return actions.ActionDescription{
ID: SyncUsersActionName,
Category: actions.ActionCategoryETL,
Label: "Sync. Directories",
Icon: "account-convert",
Description: "Synchronize external directories with Cells database",
SummaryTemplate: "",
HasForm: true,
}
}
func (c *SyncUsersAction) 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",
},
},
},
}}
}
type ConnectorConfigs struct {
ldapStore models.ReadableStore
mappingRules []auth.MappingRule
mergeOptions map[string]string
}
var (
SyncUsersActionName = "actions.etl.users"
)
// GetName returns the Unique Identifier of this task.
func (c *SyncUsersAction) GetName() string {
return SyncUsersActionName
}
// Init parses and validates parameters
func (c *SyncUsersAction) Init(job *jobs.Job, cl client.Client, action *jobs.Action) error {
return c.ParseStores(action.Parameters)
}
// Run the actual action code
func (c *SyncUsersAction) Run(ctx context.Context, channels *actions.RunnableChannels, input jobs.ActionMessage) (jobs.ActionMessage, error) {
msg := "Starting synchronisation with external directory"
log.TasksLogger(ctx).Info(msg)
channels.StatusMsg <- msg
channels.Status <- jobs.TaskStatus_Running
// Add a small timer for the job to be ready to be launched. Superstition?
<-time.After(2 * time.Second)
progress := make(chan etl.MergeOperation)
finished := make(chan bool)
defer close(progress)
defer close(finished)
var pgErrors []error
listProgress := make(chan float32)
ratio := float32(1)
var lastProgress float32
go func() {
// Send progress only if incremented by 0.1%
sendProgress := func(newProgress float32) bool {
if newProgress-lastProgress > 0.001 {
channels.Progress <- newProgress
lastProgress = newProgress
return true
}
return false
}
for {
select {
case p := <-listProgress:
sendProgress(p / ratio)
case op := <-progress:
log.TasksLogger(ctx).Info(op.Description)
channels.StatusMsg <- op.Description
if op.Total > 0 {
sendProgress((float32(op.Cursor) / float32(op.Total) / ratio) + 1/ratio)
}
if op.Error != nil {
pgErrors = append(pgErrors, op.Error)
}
case <-finished:
return
}
}
}()
defer func() {
finished <- true
}()
merger, err := c.initMerger(ctx, input)
if err != nil {
return input.WithError(err), err
}
defer merger.Close()
if _, ok := c.params["splitUserRoles"]; ok {
cellAdmin, ok := c.params["cellAdmin"]
if !ok {
er := fmt.Errorf("missing 'cellAdmin' parameter")
return input.WithError(er), er
}
log.Auditer(ctx).Info("Processing user import")
// FIRST SYNC ONLY USERS
usersDiff, _, err := merger.LoadAndDiffUsers(ctx, nil)
if err != nil {
return input.WithError(err), err
}
for k, u := range usersDiff.Delete {
if u.Login == cellAdmin || u.Login == common.PYDIO_S3ANON_USERNAME || u.Uuid == "ROOT_GROUP" {
delete(usersDiff.Delete, k)
}
}
switch merger.Options.SyncType {
case models.CREATEONLYSYNC:
usersDiff.Update = nil
usersDiff.Delete = nil
case models.NODELETESYNC:
usersDiff.Delete = nil
}
log.Logger(ctx).Debug("Diffs after listing users", zap.Any("users", usersDiff))
merger.SaveUsers(ctx, usersDiff, new(models.RoleDiff), progress)
// THEN SYNC ADMIN ROLES
roleDiff, e := merger.LoadAndDiffRoles(ctx, map[string]interface{}{})
if e != nil {
return input.WithError(e), e
}
log.Logger(ctx).Debug("Admin roles diff", zap.Any("roles", roleDiff))
merger.SaveUsers(ctx, models.NewUserDiff(), roleDiff, progress)
// THEN SYNC TEAM ROLES
roleDiff2, e := merger.LoadAndDiffRoles(ctx, map[string]interface{}{"teams": true})
if e != nil {
return input.WithError(e), e
}
log.Logger(ctx).Debug("Teams diff", zap.Any("teams", roleDiff2))
merger.SaveUsers(ctx, models.NewUserDiff(), roleDiff2, progress)
} else {
msg = "Comparing internal and external directories"
log.TasksLogger(ctx).Info(msg)
channels.StatusMsg <- msg
usersDiff, rolesDiff, err := merger.LoadAndDiffUsers(ctx, listProgress)
if err != nil {
return input.WithError(err), err
}
switch merger.Options.SyncType {
case models.CREATEONLYSYNC:
usersDiff.Update = nil
usersDiff.Delete = nil
rolesDiff.Updates = nil
rolesDiff.Deletes = nil
case models.NODELETESYNC:
usersDiff.Delete = nil
rolesDiff.Deletes = nil
}
msg = "Persisting modifications"
log.TasksLogger(ctx).Info(msg)
channels.StatusMsg <- msg
merger.SaveUsers(ctx, usersDiff, rolesDiff, progress)
// Add a small timer to insure success message is displayed last
<-time.After(2 * time.Second)
msg = fmt.Sprintf("Synchronisation complete")
log.TasksLogger(ctx).Info(msg)
log.Auditer(ctx).Info("Synchronization with external directory complete")
channels.StatusMsg <- msg
}
output := input
output.AppendOutput(&jobs.ActionOutput{
Success: true,
StringBody: "Successfully synchronized users",
})
var gE error
if len(pgErrors) > 0 {
gE = pgErrors[0]
for _, err := range pgErrors {
output = output.WithError(err)
}
}
return output, gE
}