-
Notifications
You must be signed in to change notification settings - Fork 180
/
handler-acl-lock.go
157 lines (129 loc) · 5.23 KB
/
handler-acl-lock.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
/*
* Copyright (c) 2019-2021. 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"
"io"
"github.com/pydio/cells/v4/common/nodes"
"github.com/pydio/cells/v4/common/nodes/abstract"
"github.com/pydio/cells/v4/common/nodes/models"
"github.com/pydio/cells/v4/common/proto/tree"
"github.com/pydio/cells/v4/common/service/errors"
"github.com/pydio/cells/v4/common/utils/permissions"
)
func WithLock() nodes.Option {
return func(options *nodes.RouterOptions) {
options.Wrappers = append(options.Wrappers, &LockFilter{})
}
}
// LockFilter filters call by checking internal locks.
type LockFilter struct {
abstract.Handler
}
func (a *LockFilter) Adapt(h nodes.Handler, options nodes.RouterOptions) nodes.Handler {
a.Next = h
a.ClientsPool = options.Pool
return a
}
// UpdateNode synchronously and recursively performs a Move operation of a node
// func (h *LockFilter) CreateNode(ctx context.Context, in *tree.CreateNodeRequest, opts ...grpc.CallOption) (*tree.CreateNodeResponse, error) {
// log.Logger(ctx).Info("Going through the create lock during operation")
// return h.next.CreateNode(ctx, in, opts...)
// }
// // DeleteNode synchronously and recursively delete a node
// func (h *LockFilter) DeleteNode(ctx context.Context, in *tree.DeleteNodeRequest, opts ...grpc.CallOption) (*tree.DeleteNodeResponse, error) {
// log.Logger(ctx).Info("Going through the delete lock during operation")
// return h.next.DeleteNode(ctx, in, opts...)
// }
// // UpdateNode synchronously and recursively performs a Move operation of a node
// func (h *LockFilter) UpdateNode(ctx context.Context, in *tree.UpdateNodeRequest, opts ...grpc.CallOption) (*tree.UpdateNodeResponse, error) {
// log.Logger(ctx).Info("Going through the update lock during operation")
// return h.next.UpdateNode(ctx, in, opts...)
// }
// PutObject check locks before allowing Put operation.
func (a *LockFilter) PutObject(ctx context.Context, node *tree.Node, reader io.Reader, requestData *models.PutRequestData) (int64, error) {
branchInfo, ok := nodes.GetBranchInfo(ctx, "in")
if !ok {
return a.Next.PutObject(ctx, node, reader, requestData)
}
accessList, err := permissions.AccessListForLockedNodes(ctx, a.virtualResolver)
if err != nil {
return 0, err
}
nn := []*tree.Node{node}
for _, ancestorNodes := range branchInfo.AncestorsList {
nn = append(nn, ancestorNodes...)
}
if accessList.IsLocked(ctx, nn...) {
return 0, errors.New("parent.locked", "Node is currently locked", 423)
}
return a.Next.PutObject(ctx, node, reader, requestData)
}
func (a *LockFilter) MultipartCreate(ctx context.Context, node *tree.Node, requestData *models.MultipartRequestData) (string, error) {
branchInfo, ok := nodes.GetBranchInfo(ctx, "in")
if !ok {
return a.Next.MultipartCreate(ctx, node, requestData)
}
accessList, err := permissions.AccessListForLockedNodes(ctx, a.virtualResolver)
if err != nil {
return "", err
}
nn := []*tree.Node{node}
for _, ancestorNodes := range branchInfo.AncestorsList {
nn = append(nn, ancestorNodes...)
}
if accessList.IsLocked(ctx, nn...) {
return "", errors.New("parent.locked", "Node is currently locked", 423)
}
return a.Next.MultipartCreate(ctx, node, requestData)
}
// WrappedCanApply will perform checks on quota to make sure an operation is authorized
func (a *LockFilter) WrappedCanApply(srcCtx context.Context, targetCtx context.Context, operation *tree.NodeChangeEvent) error {
var node *tree.Node
var ctx context.Context
switch operation.GetType() {
case tree.NodeChangeEvent_READ, tree.NodeChangeEvent_UPDATE_CONTENT:
return a.Next.WrappedCanApply(srcCtx, targetCtx, operation)
case tree.NodeChangeEvent_CREATE:
node = operation.GetTarget()
ctx = targetCtx
case tree.NodeChangeEvent_DELETE, tree.NodeChangeEvent_UPDATE_PATH:
node = operation.GetSource()
ctx = srcCtx
}
// Load all nodes
accessList, err := permissions.AccessListForLockedNodes(ctx, a.virtualResolver)
if err != nil {
return err
}
// First load ancestors or grab them from BranchInfo
_, parents, err := nodes.AncestorsListFromContext(ctx, node, "in", a.ClientsPool, false)
if err != nil {
return err
}
nn := append([]*tree.Node{node}, parents...)
if accessList.IsLocked(ctx, nn...) {
return errors.New("parent.locked", "Node is currently locked", 423)
}
return a.Next.WrappedCanApply(srcCtx, targetCtx, operation)
}
func (a *LockFilter) virtualResolver(ctx context.Context, node *tree.Node) (*tree.Node, bool) {
return abstract.GetVirtualNodesManager(a.RuntimeCtx).GetResolver(false)(ctx, node)
}