* func (e *Handler) SetNodeKeyManagerClient(nodeKeyManagerClient encryption.NodeKeyManagerClient) { e.nodeKeyManagerClient = nodeKeyManagerClient } */ GetObject enriches request metadata for GetObject with Encryption Materials, if required by the datasource.
(ctx context.Context, node *tree.Node, requestData *models.GetRequestData)
| 72 | |
| 73 | // GetObject enriches request metadata for GetObject with Encryption Materials, if required by the datasource. |
| 74 | func (e *Handler) GetObject(ctx context.Context, node *tree.Node, requestData *models.GetRequestData) (io.ReadCloser, error) { |
| 75 | if strings.HasSuffix(node.Path, common.PydioSyncHiddenFile) { |
| 76 | return e.Next.GetObject(ctx, node, requestData) |
| 77 | } |
| 78 | |
| 79 | branchInfo, er := nodes.GetBranchInfo(ctx, "in") |
| 80 | if er != nil || branchInfo.EncryptionMode != object.EncryptionMode_MASTER { |
| 81 | return e.Next.GetObject(ctx, node, requestData) |
| 82 | } |
| 83 | |
| 84 | clone := node.Clone() |
| 85 | |
| 86 | if len(node.Uuid) == 0 || node.Size == 0 { |
| 87 | rsp, readErr := e.Next.ReadNode(ctx, &tree.ReadNodeRequest{ |
| 88 | Node: node, |
| 89 | }) |
| 90 | if readErr != nil { |
| 91 | return nil, readErr |
| 92 | } |
| 93 | clone.Uuid = rsp.Node.Uuid |
| 94 | clone.Size = rsp.Node.Size |
| 95 | } |
| 96 | |
| 97 | if len(clone.Uuid) == 0 || clone.Size == 0 { |
| 98 | return nil, errors.WithMessage(errors.NodeNotFound, node.Path) |
| 99 | } |
| 100 | |
| 101 | dsName := clone.GetStringMeta(common.MetaNamespaceDatasourceName) |
| 102 | if dsName == "" { |
| 103 | if branchInfo.Root != nil { |
| 104 | dsName = branchInfo.Root.GetStringMeta(common.MetaNamespaceDatasourceName) |
| 105 | } else if branchInfo.DataSource != nil && branchInfo.Name != "" { |
| 106 | dsName = branchInfo.Name |
| 107 | } else { |
| 108 | return nil, errors.WithMessage(errors.StatusInternalServerError, "cannot find datasource name") |
| 109 | } |
| 110 | clone.MustSetMeta(common.MetaNamespaceDatasourceName, dsName) |
| 111 | } |
| 112 | |
| 113 | info, offset, length, skipBytesCount, err := e.getNodeInfoForRead(ctx, clone, requestData) |
| 114 | if err != nil { |
| 115 | log.Logger(ctx).Error("views.handler.encryption.GetObject: failed to get node info", zap.Error(err)) |
| 116 | return nil, err |
| 117 | } |
| 118 | |
| 119 | if offset == 0 && length == 0 { |
| 120 | length = -1 |
| 121 | } |
| 122 | |
| 123 | keyProtectionTool, err := e.getKeyProtectionTool(ctx) |
| 124 | if err != nil { |
| 125 | log.Logger(ctx).Error("views.handler.encryption.GetObject: failed to load key tool", zap.Error(err)) |
| 126 | return nil, err |
| 127 | } |
| 128 | |
| 129 | plainKey, err := keyProtectionTool.GetDecrypted(ctx, branchInfo.EncryptionKey, info.NodeKey.KeyData) |
| 130 | if err != nil { |
| 131 | log.Logger(ctx).Error("views.handler.encryption.GetObject: failed to decrypt materials key", zap.String("user", dsName), zap.Error(err)) |