-
Notifications
You must be signed in to change notification settings - Fork 180
/
handler.go
347 lines (289 loc) · 9.26 KB
/
handler.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
/*
* 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 grpc
import (
"context"
"io"
"github.com/micro/go-micro/errors"
"github.com/pydio/cells/common/log"
"github.com/pydio/cells/common/proto/encryption"
"github.com/pydio/cells/common/proto/tree"
"github.com/pydio/cells/common/service/context"
"github.com/pydio/cells/data/key"
"go.uber.org/zap"
)
const (
aesGCMTagSize = 16
)
type NodeInfoMessage struct {
Message interface{}
}
type NodeKeyManagerHandler struct{}
func getDAO(ctx context.Context) (key.DAO, error) {
dao := servicecontext.GetDAO(ctx)
if dao == nil {
return nil, errors.InternalServerError("data.key.handler", "no DAO found")
}
keyDao, ok := dao.(key.DAO)
if !ok {
return nil, errors.InternalServerError("data.key.handler", "wrong DAO type found, wrong initialization")
}
return keyDao, nil
}
func (km *NodeKeyManagerHandler) HandleTreeChanges(ctx context.Context, msg *tree.NodeChangeEvent) error {
if !msg.Optimistic && msg.Type == tree.NodeChangeEvent_DELETE {
req := &encryption.DeleteNodeRequest{
NodeId: msg.Source.Uuid,
}
return km.DeleteNode(ctx, req, &encryption.DeleteNodeResponse{})
}
return nil
}
func (km *NodeKeyManagerHandler) GetNodeInfo(ctx context.Context, req *encryption.GetNodeInfoRequest, rsp *encryption.GetNodeInfoResponse) error {
dao, err := getDAO(ctx)
if err != nil {
return err
}
rsp.NodeInfo = new(encryption.NodeInfo)
rsp.NodeInfo.Node, err = dao.GetNode(req.NodeId)
if err != nil {
return err
}
rsp.NodeInfo.NodeKey, err = dao.GetNodeKey(req.NodeId, req.UserId)
if err != nil {
log.Logger(ctx).Error("data.key.handler: failed to get node key for "+req.NodeId+" - "+req.UserId, zap.Error(err))
return err
}
if rsp.NodeInfo.Node.Legacy {
block, err := dao.GetEncryptedLegacyBlockInfo(req.NodeId)
if err != nil {
log.Logger(ctx).Error("data.key.handler: failed to load legacy block info", zap.Error(err))
return err
}
rsp.NodeInfo.Block = &encryption.Block{
BlockSize: block.BlockSize,
OwnerId: block.OwnerId,
Nonce: block.Nonce,
}
} else if req.WithRange {
cursor, err := dao.ListEncryptedBlockInfo(req.NodeId)
if err != nil {
log.Logger(ctx).Error("failed to list node blocks", zap.String("id", rsp.NodeInfo.Node.NodeId))
return err
}
defer cursor.Close()
encryptedOffsetCursor := int64(0)
encryptedLimitCursor := int64(0)
plainLimitCursor := req.PlainOffset + req.PlainLength
plainOffsetCursor := int64(0)
foundEncryptedOffset := plainOffsetCursor == int64(req.PlainOffset)
foundEncryptedLimit := req.PlainLength <= 0
done := false
for cursor.HasNext() && !done {
next, _ := cursor.Next()
b := next.(*key.RangedBlocks)
plainBlockSize := int64(b.BlockSize) - aesGCMTagSize
encryptedBlockSize := int64(b.BlockSize + b.HeaderSize)
count := int(b.SeqEnd-b.SeqStart) + 1
for i := 0; i < count; i++ {
nextPlainOffset := plainOffsetCursor + plainBlockSize
encryptedLimitCursor += encryptedBlockSize
if !foundEncryptedOffset {
if nextPlainOffset > req.PlainOffset {
foundEncryptedOffset = true
rsp.HeadSKippedPlainBytesCount = req.PlainOffset - plainOffsetCursor
rsp.EncryptedOffset = encryptedOffsetCursor
if nextPlainOffset >= plainLimitCursor {
rsp.EncryptedCount = encryptedBlockSize
foundEncryptedLimit = true
}
}
encryptedOffsetCursor += encryptedBlockSize
plainOffsetCursor = nextPlainOffset
}
if foundEncryptedOffset && !foundEncryptedLimit {
if nextPlainOffset >= plainLimitCursor {
foundEncryptedLimit = true
rsp.EncryptedCount = encryptedLimitCursor - rsp.EncryptedOffset
done = true
break
}
plainOffsetCursor = nextPlainOffset
}
}
}
if !foundEncryptedOffset {
return errors.InternalServerError("offset.not.found", "Cannot find proper offset for range %d %d", req.PlainOffset, req.PlainLength)
}
if !foundEncryptedLimit {
rsp.EncryptedCount = encryptedLimitCursor - rsp.EncryptedOffset
}
}
return err
}
func (km *NodeKeyManagerHandler) GetNodePlainSize(ctx context.Context, req *encryption.GetNodePlainSizeRequest, rsp *encryption.GetNodePlainSizeResponse) error {
dao, err := getDAO(ctx)
if err != nil {
return err
}
cursor, err := dao.ListEncryptedBlockInfo(req.NodeId)
if err != nil {
return err
}
defer cursor.Close()
rsp.Size = 0
for cursor.HasNext() {
next, _ := cursor.Next()
b := next.(*key.RangedBlocks)
plainBlockSize := int64(b.BlockSize) - aesGCMTagSize
count := int(b.SeqEnd-b.SeqStart) + 1
rsp.Size += plainBlockSize * int64(count)
}
return nil
}
func (km *NodeKeyManagerHandler) SetNodeInfo(ctx context.Context, stream encryption.NodeKeyManager_SetNodeInfoStream) error {
dao, err := getDAO(ctx)
if err != nil {
return err
}
sessionOpened := true
var rangedBlocks *key.RangedBlocks
var nodeUuid string
for sessionOpened {
var req encryption.SetNodeInfoRequest
var rsp encryption.SetNodeInfoResponse
err = stream.RecvMsg(&req)
if err != nil {
if err != io.EOF {
log.Logger(ctx).Error("data.key.handler.SetNodeInfo: failed to read SetInfoRequest", zap.Error(err))
}
break
}
switch req.Action {
case "key":
err = km.saveNodeKey(ctx, dao, req.SetNodeKey.NodeKey)
if err != nil {
rsp.ErrorText = err.Error()
log.Logger(ctx).Error("failed to save key", zap.Error(err))
}
case "clearBlocks":
err := dao.ClearNodeEncryptedBlockInfo(req.SetBlock.NodeUuid)
if err != nil {
log.Logger(ctx).Error("failed to clear old blocks", zap.Error(err))
return err
}
err = dao.UpgradeNodeVersion(req.SetBlock.NodeUuid)
if err != nil {
log.Logger(ctx).Error("failed to upgrade node version", zap.Error(err))
return err
}
case "block":
if nodeUuid == "" {
nodeUuid = req.SetBlock.NodeUuid
}
tmpRangeBlock := &key.RangedBlocks{
BlockSize: req.SetBlock.Block.BlockSize,
OwnerId: req.SetBlock.Block.OwnerId,
HeaderSize: req.SetBlock.Block.HeaderSize,
PartId: req.SetBlock.Block.PartId,
}
if rangedBlocks == nil {
rangedBlocks = tmpRangeBlock
} else if req.SetBlock.Block.BlockSize != rangedBlocks.BlockSize || req.SetBlock.Block.HeaderSize != rangedBlocks.HeaderSize {
err = dao.SaveEncryptedBlockInfo(nodeUuid, rangedBlocks)
if err != nil {
rsp.ErrorText = err.Error()
log.Logger(ctx).Error("data.key.handler.SetNodeInfo: failed to save block", zap.Error(err))
} else {
newEnd := rangedBlocks.SeqEnd + 1
tmpRangeBlock.SeqStart = newEnd
tmpRangeBlock.SeqEnd = newEnd
rangedBlocks = tmpRangeBlock
}
} else {
rangedBlocks.SeqEnd++
}
case "close":
sessionOpened = false
if rangedBlocks != nil {
err = dao.SaveEncryptedBlockInfo(nodeUuid, rangedBlocks)
rangedBlocks = nil
}
}
err = stream.SendMsg(&rsp)
if err != nil {
break
}
}
if sce := stream.Close(); sce != nil {
log.Logger(ctx).Error("data.key.handler.SetNodeInfo: failed to close micro.stream", zap.Error(sce))
}
return err
}
func (km *NodeKeyManagerHandler) CopyNodeInfo(ctx context.Context, req *encryption.CopyNodeInfoRequest, rsp *encryption.CopyNodeInfoResponse) error {
dao, err := getDAO(ctx)
if err != nil {
return err
}
return dao.CopyNode(req.NodeUuid, req.NodeCopyUuid)
}
func (km *NodeKeyManagerHandler) DeleteNode(ctx context.Context, req *encryption.DeleteNodeRequest, rsp *encryption.DeleteNodeResponse) error {
dao, err := getDAO(ctx)
if err != nil {
return err
}
return dao.DeleteNode(req.NodeId)
}
func (km *NodeKeyManagerHandler) DeleteNodeKey(ctx context.Context, req *encryption.DeleteNodeKeyRequest, rsp *encryption.DeleteNodeKeyResponse) error {
dao, err := getDAO(ctx)
if err != nil {
return err
}
return dao.DeleteNodeKey(&encryption.NodeKey{
UserId: req.UserId,
NodeId: req.NodeId,
})
}
func (km *NodeKeyManagerHandler) DeleteNodeSharedKey(ctx context.Context, req *encryption.DeleteNodeSharedKeyRequest, rsp *encryption.DeleteNodeSharedKeyResponse) error {
dao, err := getDAO(ctx)
if err != nil {
return err
}
return dao.DeleteNodeKey(&encryption.NodeKey{
UserId: req.UserId,
NodeId: req.NodeId,
})
}
func (km *NodeKeyManagerHandler) saveNodeKey(ctx context.Context, dao key.DAO, nodeKey *encryption.NodeKey) error {
err := dao.SaveNode(&encryption.Node{
NodeId: nodeKey.NodeId,
Legacy: false,
})
if err != nil {
log.Logger(ctx).Error("failed to save node info", zap.Error(err))
return err
}
err = dao.SaveNodeKey(nodeKey)
if err != nil {
log.Logger(ctx).Error("failed to save node key", zap.Error(err))
return err
}
return nil
}