-
Notifications
You must be signed in to change notification settings - Fork 180
/
common.go
215 lines (188 loc) · 7.27 KB
/
common.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
/*
* 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 views provides high-level clients for talking to the main data tree in certain context.
//
// It follows the "wrapper" pattern of http handlers to filter all requests inputs and outputs. The "Router" is object
// is used by all services or gateways when accessing to data as a given user.
// Between others, it will
// - Load ACLs and perform checks to make sure user is allowed to read/write the data
// - Perform other meta-related or acl-related checks like Quota management, locks, etc..
// - Perform encryption/decryption of actual data on the fly
// - Compress / Decompress archives,
// - Add metadata collected from any services on the nodes outputted by the responses,
// - etc...
package views
import (
"context"
"io"
"github.com/micro/go-micro/metadata"
"github.com/pkg/errors"
"github.com/pydio/cells/common"
"github.com/pydio/cells/common/proto/idm"
"github.com/pydio/cells/common/proto/object"
"github.com/pydio/cells/common/proto/tree"
"github.com/pydio/cells/common/utils"
"github.com/pydio/minio-go"
"github.com/pydio/minio-go/pkg/encrypt"
)
const (
VIEWS_LIBRARY_NAME = "pydio.lib.views"
)
var (
// IsUnitTestEnv flag prevents among others the ClientPool to look for declared
// datasources in the registry. As none is present while running unit tests, it
// otherwise times out.
IsUnitTestEnv = false
)
// These keys may be enriched in Context depending on the middleware
type (
ctxUserAccessListKey struct{}
ctxAdminContextKey struct{}
ctxBranchInfoKey struct{}
LoadedSource struct {
object.DataSource
Client *minio.Core
}
BranchInfo struct {
LoadedSource
idm.Workspace
Root *tree.Node
Binary bool
AncestorsList []*tree.Node
}
PutRequestData struct {
Size int64
Md5Sum []byte
Sha256Sum []byte
Metadata map[string]string
EncryptionMaterial encrypt.Materials
MultipartUploadID string
MultipartPartID int
}
GetRequestData struct {
StartOffset int64
Length int64
EncryptionMaterial encrypt.Materials
VersionId string
}
CopyRequestData struct {
Metadata map[string]string
srcEncryptionMaterial encrypt.Materials
destEncryptionMaterial encrypt.Materials
SrcVersionId string
}
MultipartRequestData struct {
Metadata map[string]string
ListKeyMarker string
ListUploadIDMarker string
ListDelimiter string
ListMaxUploads int
}
)
type NodeFilter func(ctx context.Context, inputNode *tree.Node, identifier string) (context.Context, *tree.Node, error)
type NodesCallback func(inputFilter NodeFilter, outputFilter NodeFilter) error
type Handler interface {
tree.NodeProviderClient
tree.NodeReceiverClient
GetObject(ctx context.Context, node *tree.Node, requestData *GetRequestData) (io.ReadCloser, error)
PutObject(ctx context.Context, node *tree.Node, reader io.Reader, requestData *PutRequestData) (int64, error)
CopyObject(ctx context.Context, from *tree.Node, to *tree.Node, requestData *CopyRequestData) (int64, error)
MultipartCreate(ctx context.Context, target *tree.Node, requestData *MultipartRequestData) (string, error)
MultipartPutObjectPart(ctx context.Context, target *tree.Node, uploadID string, partNumberMarker int, reader io.Reader, requestData *PutRequestData) (minio.ObjectPart, error)
MultipartList(ctx context.Context, prefix string, requestData *MultipartRequestData) (minio.ListMultipartUploadsResult, error)
MultipartAbort(ctx context.Context, target *tree.Node, uploadID string, requestData *MultipartRequestData) error
MultipartComplete(ctx context.Context, target *tree.Node, uploadID string, uploadedParts []minio.CompletePart) (minio.ObjectInfo, error)
MultipartListObjectParts(ctx context.Context, target *tree.Node, uploadID string, partNumberMarker int, maxParts int) (minio.ListObjectPartsResult, error)
ExecuteWrapped(inputFilter NodeFilter, outputFilter NodeFilter, provider NodesCallback) error
SetNextHandler(h Handler)
SetClientsPool(p *ClientsPool)
}
func WithBranchInfo(ctx context.Context, identifier string, branchInfo BranchInfo) context.Context {
//log.Logger(ctx).Error("branchInfo", zap.Any("branchInfo", branchInfo))
value := ctx.Value(ctxBranchInfoKey{})
var data map[string]BranchInfo
if value != nil {
data = value.(map[string]BranchInfo)
} else {
data = make(map[string]BranchInfo)
}
data[identifier] = branchInfo
return context.WithValue(ctx, ctxBranchInfoKey{}, data)
}
func GetBranchInfo(ctx context.Context, identifier string) (BranchInfo, bool) {
value := ctx.Value(ctxBranchInfoKey{})
if value != nil {
data := value.(map[string]BranchInfo)
if info, ok := data[identifier]; ok {
return info, true
}
}
return BranchInfo{}, false
}
func UserWorkspacesFromContext(ctx context.Context) map[string]*idm.Workspace {
if value := ctx.Value(ctxUserAccessListKey{}); value != nil {
accessList := value.(*utils.AccessList)
return accessList.Workspaces
} else {
return make(map[string]*idm.Workspace)
}
}
func AccessListFromContext(ctx context.Context) (*utils.AccessList, error) {
if value := ctx.Value(ctxUserAccessListKey{}); value != nil {
accessList := value.(*utils.AccessList)
return accessList, nil
} else {
return nil, errors.New("Cannot find access list in context")
}
}
func AncestorsListFromContext(ctx context.Context, node *tree.Node, identifier string, p *ClientsPool, orParents bool) (updatedContext context.Context, parentsList []*tree.Node, e error) {
branchInfo, hasBranchInfo := GetBranchInfo(ctx, identifier)
if hasBranchInfo && len(branchInfo.AncestorsList) > 0 {
return ctx, branchInfo.AncestorsList, nil
}
searchFunc := utils.BuildAncestorsList
if orParents {
searchFunc = utils.BuildAncestorsListOrParent
}
if parents, err := searchFunc(ctx, p.GetTreeClient(), node); err != nil {
return ctx, nil, err
} else {
if hasBranchInfo {
branchInfo.AncestorsList = parents
ctx = WithBranchInfo(ctx, identifier, branchInfo)
}
return ctx, parents, nil
}
}
// MinioMetaFromContext prepares metadata for minio client, merging context medata
// and eventually the Context User Key value (X-Pydio-User). Used to prepare metadata
// sent by Minio Clients
func MinioMetaFromContext(ctx context.Context) (md map[string]string, ok bool) {
md = make(map[string]string)
if meta, mOk := metadata.FromContext(ctx); mOk {
for k, v := range meta {
md[k] = v
}
}
if user := ctx.Value(common.PYDIO_CONTEXT_USER_KEY); user != nil {
md[common.PYDIO_CONTEXT_USER_KEY] = user.(string)
}
return md, len(md) > 0
}