forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql.go
514 lines (426 loc) · 12.5 KB
/
sql.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
/*
* 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 acl
import (
"context"
"errors"
"fmt"
"time"
"github.com/go-sql-driver/mysql"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/any"
"github.com/pydio/packr"
migrate "github.com/rubenv/sql-migrate"
"go.uber.org/zap"
goqu "gopkg.in/doug-martin/goqu.v4"
"github.com/pydio/cells/common"
"github.com/pydio/cells/common/log"
"github.com/pydio/cells/common/proto/idm"
"github.com/pydio/cells/common/sql"
)
var (
queries = map[string]string{
"AddACL": `insert into idm_acls (action_name, action_value, role_id, workspace_id, node_id) values (?, ?, ?, ?, ?)`,
"AddACLNode": `insert into idm_acl_nodes (uuid) values (?)`,
"AddACLRole": `insert into idm_acl_roles (uuid) values (?)`,
"AddACLWorkspace": `insert into idm_acl_workspaces (name) values (?)`,
"GetACLNode": `select id from idm_acl_nodes where uuid = ?`,
"GetACLRole": `select id from idm_acl_roles where uuid = ?`,
"GetACLWorkspace": `select id from idm_acl_workspaces where name = ?`,
"CleanWorkspaces": `DELETE FROM idm_acl_workspaces WHERE id != -1 and id NOT IN (select distinct(workspace_id) from idm_acls)`,
"CleanRoles": `DELETE FROM idm_acl_roles WHERE id != -1 and id NOT IN (select distinct(role_id) from idm_acls)`,
"CleanNodes": `DELETE FROM idm_acl_nodes WHERE id != -1 and id NOT IN (select distinct(node_id) from idm_acls)`,
"CleanDuplicateIfExpired": `DELETE FROM idm_acls WHERE action_name=? AND role_id=? AND workspace_id=? AND node_id=? AND expires_at IS NOT NULL AND expires_at < ?`,
}
)
type sqlimpl struct {
sql.DAO
}
// Init handler for the SQL DAO
func (dao *sqlimpl) Init(options common.ConfigValues) error {
// super
dao.DAO.Init(options)
// Doing the database migrations
migrations := &sql.PackrMigrationSource{
Box: packr.NewBox("../../idm/acl/migrations"),
Dir: dao.Driver(),
TablePrefix: dao.Prefix(),
}
_, err := sql.ExecMigration(dao.DB(), dao.Driver(), migrations, migrate.Up, "idm_acl_")
if err != nil {
return err
}
// Preparing the db statements
if options.Bool("prepare", true) {
for key, query := range queries {
if err := dao.Prepare(key, query); err != nil {
return err
}
}
}
return nil
}
// Add inserts an ACL to the underlying SQL DB
func (dao *sqlimpl) Add(in interface{}) error {
return dao.addWithDupCheck(in, true)
}
// addWithDupCheck insert and override existing value if it's a
// duplicate key and the ACL is expired
func (dao *sqlimpl) addWithDupCheck(in interface{}, check bool) error {
val, ok := in.(*idm.ACL)
if !ok {
return errors.New("Wrong type")
}
if val.Action == nil {
return errors.New("Missing action value")
}
workspaceID := "-1"
if val.WorkspaceID != "" {
id, err := dao.addWorkspace(val.WorkspaceID)
if err != nil {
return err
}
workspaceID = id
}
nodeID := "-1"
if val.NodeID != "" {
id, err := dao.addNode(val.NodeID)
if err != nil {
return err
}
nodeID = id
}
roleID := "-1"
if val.RoleID != "" {
id, err := dao.addRole(val.RoleID)
if err != nil {
return err
}
roleID = id
}
log.Logger(context.Background()).Debug("AddACL",
zap.String("r", roleID), zap.String("w", workspaceID), zap.String("n", nodeID), zap.Any("value", val))
stmt, er := dao.GetStmt("AddACL")
if er != nil {
return er
}
res, err := stmt.Exec(val.Action.Name, val.Action.Value, roleID, workspaceID, nodeID)
if err != nil {
if mErr, ok := err.(*mysql.MySQLError); ok && mErr.Number == 1062 && check {
// fmt.Println("GOT DUPLICATE ERROR", mErr.Error(), mErr.Message)
// There is a duplicate : if it is expired, we can safely ignore it and replace it
deleteStmt, dE := dao.GetStmt("CleanDuplicateIfExpired")
if dE != nil {
return dE
}
delRes, drE := deleteStmt.Exec(val.Action.Name, roleID, workspaceID, nodeID, time.Now())
if drE != nil {
return drE
}
if affected, e := delRes.RowsAffected(); e == nil && affected == 1 {
// fmt.Println("[AddACL] Replacing one duplicate row that was in fact expired")
return dao.addWithDupCheck(in, false)
}
}
return err
}
id, err := res.LastInsertId()
if err != nil {
return err
}
val.ID = fmt.Sprintf("%d", id)
return nil
}
// Search in the underlying SQL DB.
func (dao *sqlimpl) Search(query sql.Enquirer, acls *[]interface{}) error {
db := goqu.New(dao.Driver(), dao.DB())
expressions := []goqu.Expression{
goqu.I("n.id").Eq(goqu.I("a.node_id")),
goqu.I("w.id").Eq(goqu.I("a.workspace_id")),
goqu.I("r.id").Eq(goqu.I("a.role_id")),
goqu.Or(
goqu.I("a.expires_at").IsNull(),
goqu.I("a.expires_at").Gt(time.Now()),
),
}
whereExpression := sql.NewQueryBuilder(query, new(queryConverter)).Expression(dao.Driver())
if whereExpression != nil {
expressions = append(expressions, whereExpression)
}
offset, limit := int64(0), int64(-1)
if query.GetOffset() > 0 {
offset = query.GetOffset()
}
if query.GetLimit() > 0 {
limit = query.GetLimit()
}
dataset := db.From(
goqu.I("idm_acls").As("a"),
goqu.I("idm_acl_nodes").As("n"),
goqu.I("idm_acl_workspaces").As("w"),
goqu.I("idm_acl_roles").As("r"),
).Prepared(true).Select(
goqu.I("a.id").As("acl_id"),
goqu.I("n.uuid").As("node_uuid"),
goqu.I("a.action_name").As("acl_action_name"),
goqu.I("a.action_value").As("acl_action_value"),
goqu.I("r.uuid").As("role_uuid"),
goqu.I("w.name").As("workspace_name"),
)
if limit > -1 {
dataset = dataset.Offset(uint(offset)).Limit(uint(limit))
}
dataset = dataset.Where(expressions...)
var items []struct {
AclID string `db:"acl_id"`
NodeUUID string `db:"node_uuid"`
ACLActionName string `db:"acl_action_name"`
ACLActionValue string `db:"acl_action_value"`
RoleUUID string `db:"role_uuid"`
WorkspaceName string `db:"workspace_name"`
}
if err := dataset.ScanStructs(&items); err != nil {
return err
}
for _, item := range items {
val := new(idm.ACL)
action := new(idm.ACLAction)
val.ID = item.AclID
val.NodeID = item.NodeUUID
val.RoleID = item.RoleUUID
val.WorkspaceID = item.WorkspaceName
action.Name = item.ACLActionName
action.Value = item.ACLActionValue
val.Action = action
*acls = append(*acls, val)
}
return nil
}
// SetExpiry sets an expiry timestamp on the acl
func (dao *sqlimpl) SetExpiry(query sql.Enquirer, t time.Time) (int64, error) {
db := goqu.New(dao.Driver(), dao.DB())
whereExpression := sql.NewQueryBuilder(query, new(queryConverter)).Expression(dao.Driver())
dataset := db.From("idm_acls").Where(whereExpression).Update(
goqu.Record{"expires_at": t},
)
res, err := dataset.Exec()
if err != nil {
return 0, err
}
rows, err := res.RowsAffected()
if err != nil {
return 0, err
}
return rows, nil
}
// Del from the sql DB.
func (dao *sqlimpl) Del(query sql.Enquirer) (int64, error) {
whereExpression := sql.NewQueryBuilder(query, new(queryConverter)).Expression(dao.Driver())
queryString, args, err := sql.DeleteStringFromExpression("idm_acls", dao.Driver(), whereExpression)
if err != nil {
return 0, err
}
res, err := dao.DB().Exec(queryString, args...)
if err != nil {
return 0, err
}
rows, err := res.RowsAffected()
if err != nil {
return 0, err
}
if rows > 0 {
// Perform clean up
if stmt, er := dao.GetStmt("CleanWorkspaces"); er == nil {
stmt.Exec()
} else {
return 0, er
}
if stmt, er := dao.GetStmt("CleanRoles"); er == nil {
stmt.Exec()
} else {
return 0, er
}
if stmt, er := dao.GetStmt("CleanNodes"); er == nil {
stmt.Exec()
} else {
return 0, er
}
}
return rows, nil
}
func (dao *sqlimpl) addWorkspace(uuid string) (string, error) {
stmt, er := dao.GetStmt("AddACLWorkspace")
if er != nil {
return "", er
}
res, err := stmt.Exec(uuid)
if err == nil {
rows, err := res.RowsAffected()
if err != nil {
return "", err
}
if rows > 0 {
id, err := res.LastInsertId()
if err != nil {
return "", err
}
return fmt.Sprintf("%d", id), nil
}
}
var id string
stmt, er = dao.GetStmt("GetACLWorkspace")
if er != nil {
return "", er
}
row := stmt.QueryRow(uuid)
if row == nil {
return "", fmt.Errorf("cannot not find acl for workspace %s", uuid)
}
er = row.Scan(&id)
return id, er
}
func (dao *sqlimpl) addNode(uuid string) (string, error) {
stmt, er := dao.GetStmt("AddACLNode")
if er != nil {
return "", er
}
res, err := stmt.Exec(uuid)
if err == nil {
rows, err := res.RowsAffected()
if err != nil {
return "", err
}
if rows > 0 {
id, err := res.LastInsertId()
if err != nil {
return "", err
}
return fmt.Sprintf("%d", id), nil
}
}
// Checking we didn't have a duplicate
var id string
stmt, er = dao.GetStmt("GetACLNode")
if er != nil {
return "", er
}
row := stmt.QueryRow(uuid)
if row == nil {
return "", fmt.Errorf("can not find acl node %s", uuid)
}
er = row.Scan(&id)
return id, er
}
func (dao *sqlimpl) addRole(uuid string) (string, error) {
stmt, er := dao.GetStmt("AddACLRole")
if er != nil {
return "", er
}
res, err := stmt.Exec(uuid)
if err == nil {
rows, err := res.RowsAffected()
if err != nil {
return "", err
}
if rows > 0 {
id, err := res.LastInsertId()
if err != nil {
return "", err
}
return fmt.Sprintf("%d", id), nil
}
}
// Checking we didn't have a duplicate
var id string
stmt, er = dao.GetStmt("GetACLRole")
if er != nil {
return "", er
}
row := stmt.QueryRow(uuid)
if row == nil {
return "", fmt.Errorf("cannot find acl role %s", uuid)
}
er = row.Scan(&id)
return id, er
}
type queryConverter idm.ACLSingleQuery
func (c *queryConverter) Convert(val *any.Any, driver string) (goqu.Expression, bool) {
q := new(idm.ACLSingleQuery)
if err := ptypes.UnmarshalAny(val, q); err != nil {
return nil, false
}
db := goqu.New(driver, nil)
var expressions []goqu.Expression
if len(q.RoleIDs) > 0 {
dataset := db.From("idm_acl_roles").Select("id")
dataset = dataset.Where(sql.GetExpressionForString(false, "uuid", q.RoleIDs...))
str, _, err := dataset.ToSql()
if err != nil {
return nil, true
}
expressions = append(expressions, goqu.I("role_id").In(goqu.L(str)))
}
if len(q.WorkspaceIDs) > 0 {
dataset := db.From("idm_acl_workspaces").Select("id")
dataset = dataset.Where(sql.GetExpressionForString(false, "name", q.WorkspaceIDs...))
str, _, err := dataset.ToSql()
if err != nil {
return nil, true
}
expressions = append(expressions, goqu.I("workspace_id").In(goqu.L(str)))
}
if len(q.NodeIDs) > 0 {
dataset := db.From("idm_acl_nodes").Select("id")
dataset = dataset.Where(sql.GetExpressionForString(false, "uuid", q.NodeIDs...))
str, _, err := dataset.ToSql()
if err != nil {
return nil, true
}
expressions = append(expressions, goqu.I("node_id").In(goqu.L(str)))
}
// Special case for Actions
if len(q.Actions) > 0 {
actionsByName := make(map[string][]string) // actionName => actionValues
for _, act := range q.Actions {
values, exists := actionsByName[act.Name]
if !exists {
values = []string{}
}
if act.Value != "" {
values = append(values, act.Value)
}
actionsByName[act.Name] = values
}
var orExpression []goqu.Expression
//var orWheres []string
for actName, actValues := range actionsByName {
var actionAndExpression []goqu.Expression
actionAndExpression = append(actionAndExpression, sql.GetExpressionForString(false, "action_name", actName))
if len(actValues) > 0 {
actionAndExpression = append(actionAndExpression, sql.GetExpressionForString(false, "action_value", actValues...))
orExpression = append(orExpression, goqu.And(actionAndExpression...))
} else {
orExpression = append(orExpression, actionAndExpression...)
}
}
expressions = append(expressions, goqu.Or(orExpression...))
}
return goqu.And(expressions...), true
}