-
Notifications
You must be signed in to change notification settings - Fork 0
/
objects.go
467 lines (394 loc) · 15.8 KB
/
objects.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
package servers
import (
"fmt"
"net"
"strconv"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"strings"
"github.com/ellcrys/util"
"github.com/fatih/structs"
"github.com/ellcrys/patchain"
"github.com/ellcrys/patchain/cockroach/tables"
"github.com/ellcrys/safehold/servers/common"
"github.com/ellcrys/safehold/servers/proto_rpc"
"github.com/ellcrys/safehold/session"
"github.com/pkg/errors"
"golang.org/x/net/context"
)
var (
// MaxObjectPerPut is the maximum number of objects allowed in a single PUT operation
MaxObjectPerPut = 25
)
// CreateObjects creates one or more objects using a session.
// If session id is provide, it checks whether the session exists locally or
// forwards the request to the remote session host.
// Supports the use of mapping to unmap custom fields in the objects to be created.
func (s *RPC) CreateObjects(ctx context.Context, req *proto_rpc.CreateObjectsMsg) (*proto_rpc.GetObjectsResponse, error) {
var err error
sessionID := util.FromIncomingMD(ctx, "session_id")
developerID := ctx.Value(CtxIdentity).(string)
authorization := util.FromIncomingMD(ctx, "authorization")
localOnly := util.FromIncomingMD(ctx, "local-only") == "true"
// Handle session
if len(sessionID) > 0 {
if !s.dbSession.HasSession(sessionID) {
// abort further operations
if localOnly {
return nil, fmt.Errorf("session not found")
}
// find session in the registry
sessionItem, err := s.sessionReg.Get(sessionID)
if err != nil {
if err == session.ErrNotFound {
return nil, common.NewSingleAPIErr(404, "", "", "session not found", nil)
}
return nil, common.ServerError
}
// check if session is owned by the developer, if not, return permission error
if sessionItem.Meta["identity"] != developerID {
return nil, common.NewSingleAPIErr(401, "", "", "permission denied: you don't have permission to perform this operation", nil)
}
sessionHostAddr := net.JoinHostPort(sessionItem.Address, strconv.Itoa(sessionItem.Port))
client, err := grpc.Dial(sessionHostAddr, grpc.WithInsecure())
if err != nil {
return nil, common.ServerError
}
defer client.Close()
// make call to the session host. Include the session_id, auth token of the current request
// and set local-only to force the RPC method to only perform local object creation
server := proto_rpc.NewAPIClient(client)
ctx = metadata.NewOutgoingContext(ctx, metadata.Pairs("session_id", sessionID, "authorization", authorization, "local-only", "true"))
resp, err := server.CreateObjects(ctx, req)
if err != nil {
if grpc.ErrorDesc(err) == "session not found" {
return nil, common.NewSingleAPIErr(404, "", "", "session not found", nil)
}
logRPC.Errorf("%+v", err)
return nil, common.ServerError
}
return resp, nil
}
// check if session is owned by the developer, if not, return permission error
if sessionAgent := s.dbSession.GetAgent(sessionID); sessionAgent != nil {
if sessionAgent.OwnerID != developerID {
return nil, common.NewSingleAPIErr(401, "", "", "permission denied: you don't have permission to perform this operation", nil)
}
}
} else { // session id not provided, create local, unregistered session
sessionID = util.UUID4()
s.dbSession.CreateUnregisteredSession(sessionID, developerID)
defer s.dbSession.CommitEnd(sessionID)
}
var objs []map[string]interface{}
if err := util.FromJSON(req.Objects, &objs); err != nil {
s.dbSession.RollbackEnd(sessionID)
return nil, common.NewSingleAPIErr(400, "", "", "failed to parse objects", nil)
}
// add creator_id and include developer as the owner_id if any object is missing it
for _, obj := range objs {
obj["creator_id"] = developerID
if obj["owner_id"] == nil {
obj["owner_id"] = developerID
}
}
var mapping map[string]string
// if mapping name is provided, get the mapping and unmap the objects
if len(req.Mapping) > 0 {
mapping, err = s.getMappingWithSession(sessionID, req.Mapping, developerID)
if err != nil {
s.dbSession.RollbackEnd(sessionID)
return nil, err
}
if err := common.UnMapFields(mapping, objs); err != nil {
s.dbSession.RollbackEnd(sessionID)
logRPC.Errorf("%+v", errors.Wrap(err, "failed to unmap object"))
return nil, common.ServerError
}
}
vErrs, err := s.validateObjects(objs, mapping)
if err != nil {
s.dbSession.RollbackEnd(sessionID)
logRPC.Errorf("%+v", errors.Wrap(err, "failed to validate objects"))
return nil, common.ServerError
}
if len(vErrs) > 0 {
s.dbSession.RollbackEnd(sessionID)
return nil, common.NewMultiAPIErr(400, "validation errors", vErrs)
}
// ensure caller has permission to PUT objects on behalf of the object owner
if developerID != objs[0]["owner_id"].(string) {
s.dbSession.RollbackEnd(sessionID)
return nil, common.NewSingleAPIErr(401, "", "", "permission denied: you are not authorized to create objects for the owner", nil)
}
var patchainObjs []*tables.Object
// create final patchain objects
for _, obj := range objs {
var o tables.Object
util.CopyToStruct(&o, obj)
patchainObjs = append(patchainObjs, &o)
}
if err := session.SendPutOpWithSession(s.dbSession, sessionID, patchainObjs); err != nil {
logRPC.Errorf("%+v", err)
s.dbSession.RollbackEnd(sessionID)
return nil, common.NewSingleAPIErr(500, common.CodePutError, "objects", err.Error(), nil)
}
// get the updated object and re-apply mapping
objs = nil
for _, updatedObj := range patchainObjs {
obj := structs.New(updatedObj).Map()
common.MapFields(mapping, obj)
objs = append(objs, obj)
}
return &proto_rpc.GetObjectsResponse{
Objects: util.MustStringify(objs),
}, nil
}
// orderByList takes a slice of *proto_rpc.OrderBy and returns
// a concatenated gorm.Order compatible string
func orderByToString(orderByList []*proto_rpc.OrderBy) string {
var s []string
for _, o := range orderByList {
ord := "asc"
if o.Order <= 0 {
ord = "desc"
}
s = append(s, fmt.Sprintf("%s %s", o.Field, ord))
}
return strings.Join(s, ", ")
}
// GetObjects fetches objects belonging to an identity
func (s *RPC) GetObjects(ctx context.Context, req *proto_rpc.GetObjectMsg) (*proto_rpc.GetObjectsResponse, error) {
var err error
authorization := util.FromIncomingMD(ctx, "authorization")
developerID := ctx.Value(CtxIdentity).(string)
sessionID := util.FromIncomingMD(ctx, "session_id")
localOnly := util.FromIncomingMD(ctx, "local-only") == "true"
// Handle session if provided
if len(sessionID) > 0 {
// if session id does not exist locally, find it in the session registry and forward
// request to the session host
if !s.dbSession.HasSession(sessionID) {
// abort further operations
if localOnly {
return nil, fmt.Errorf("session not found")
}
// find session in the registry
sessionItem, err := s.sessionReg.Get(sessionID)
if err != nil {
if err == session.ErrNotFound {
return nil, common.NewSingleAPIErr(404, "", "", "session not found", nil)
}
return nil, common.ServerError
}
// check if session is owned by the developer, if not, return permission error
if sessionItem.Meta["identity"] != developerID {
return nil, common.NewSingleAPIErr(401, "", "", "permission denied: you don't have permission to perform this operation", nil)
}
sessionHostAddr := net.JoinHostPort(sessionItem.Address, strconv.Itoa(sessionItem.Port))
client, err := grpc.Dial(sessionHostAddr, grpc.WithInsecure())
if err != nil {
return nil, common.ServerError
}
defer client.Close()
// make call to the session host server.
// include the session_id, auth token of the current request
// and set local-only to force the RPC method
// to only perform local object creation
server := proto_rpc.NewAPIClient(client)
ctx = metadata.NewOutgoingContext(ctx, metadata.Pairs("session_id", sessionID, "authorization", authorization, "local-only", "true"))
resp, err := server.GetObjects(ctx, req)
if err != nil {
if grpc.ErrorDesc(err) == "session not found" {
return nil, common.NewSingleAPIErr(404, "", "", "session not found", nil)
}
logRPC.Errorf("%+v", err)
return nil, common.ServerError
}
return resp, nil
}
// check if session is owned by the developer, if not, return permission error
if sessionAgent := s.dbSession.GetAgent(sessionID); sessionAgent != nil {
if sessionAgent.OwnerID != developerID {
return nil, common.NewSingleAPIErr(401, "", "", "permission denied: you don't have permission to perform this operation", nil)
}
}
} else { // session id not provided, create local, unregistered session
sessionID = util.UUID4()
s.dbSession.CreateUnregisteredSession(sessionID, developerID)
defer s.dbSession.CommitEnd(sessionID)
}
// if owner is set and not the same as the developer id, check if it exists
if len(req.Owner) > 0 && req.Owner != developerID {
if err = session.SendQueryOpWithSession(s.dbSession, sessionID, "", &tables.Object{ID: req.Owner}, 1, "", &tables.Object{}); err != nil {
if err == patchain.ErrNotFound {
return nil, common.NewSingleAPIErr(404, "", "", "owner not found", nil)
}
logRPC.Errorf("%+v", err)
return nil, common.ServerError
}
}
// if creator is set and not the same as the developer id, check if it exists
if len(req.Creator) > 0 && req.Creator != developerID {
if err = session.SendQueryOpWithSession(s.dbSession, sessionID, "", &tables.Object{ID: req.Creator}, 1, "", &tables.Object{}); err != nil {
if err == patchain.ErrNotFound {
return nil, common.NewSingleAPIErr(404, "", "", "creator not found", nil)
}
logRPC.Errorf("%+v", err)
return nil, common.ServerError
}
}
// set default owner as the developer if not set
if len(req.Owner) == 0 {
req.Owner = developerID
}
// set default creator as the developer if not set
if len(req.Creator) == 0 {
req.Creator = developerID
}
// developer is not the owner, this action requires permission
// TODO: ensure auth token must be a user token from the owner
// and the token authorizes access to the object created by the creator for this developer
if developerID != req.Owner {
return nil, common.NewSingleAPIErr(401, "", "", "permission denied: you are not authorized to access objects for the owner", nil)
}
// include owner and creator filters
var query map[string]interface{}
util.FromJSON(req.Query, &query)
query["owner_id"] = req.Owner
query["creator_id"] = req.Creator
var mapping map[string]string
// if mapping is provided in request, fetch it
if len(req.Mapping) > 0 {
mapping, err = s.getMappingWithSession(sessionID, req.Mapping, developerID)
if err != nil {
return nil, err
}
}
// unmap query fields
common.UnMapFields(mapping, query)
var fetchedObjs []*tables.Object
if err = session.SendQueryOpWithSession(s.dbSession, sessionID, string(util.MustStringify(query)), nil, int(req.Limit), orderByToString(req.Order), &fetchedObjs); err != nil {
if strings.Contains(err.Error(), "unknown query field") {
return nil, common.NewSingleAPIErr(400, common.CodeInvalidParam, "query", err.Error(), nil)
}
logRPC.Errorf("%+v", err)
return nil, common.ServerError
}
// convert fetched objects to map and re-apply mapping
var objs []map[string]interface{}
for _, obj := range fetchedObjs {
mObj := structs.New(obj).Map()
common.MapFields(mapping, mObj)
objs = append(objs, mObj)
}
return &proto_rpc.GetObjectsResponse{
Objects: util.MustStringify(objs),
}, nil
}
// CountObjects counts the number of objects that match a query
func (s *RPC) CountObjects(ctx context.Context, req *proto_rpc.GetObjectMsg) (*proto_rpc.ObjectCountResponse, error) {
authorization := util.FromIncomingMD(ctx, "authorization")
developerID := ctx.Value(CtxIdentity).(string)
sessionID := util.FromIncomingMD(ctx, "session_id")
localOnly := util.FromIncomingMD(ctx, "local-only") == "true"
// check if session exist in the in-memory session cache,
// use it else check if it exist on the session registry. If it does,
// forward the request to the associated host
if sessionID != "" {
if !s.dbSession.HasSession(sessionID) {
if localOnly {
return nil, fmt.Errorf("session not found")
}
// find session in the registry
sessionItem, err := s.sessionReg.Get(sessionID)
if err != nil {
if err == session.ErrNotFound {
return nil, common.NewSingleAPIErr(404, "", "", "session not found", nil)
}
return nil, common.ServerError
}
// check if session is owned by the developer, if not, return permission error
if sessionItem.Meta["identity"] != developerID {
return nil, common.NewSingleAPIErr(401, "", "", "permission denied: you don't have permission to perform this operation", nil)
}
sessionHostAddr := net.JoinHostPort(sessionItem.Address, strconv.Itoa(sessionItem.Port))
client, err := grpc.Dial(sessionHostAddr, grpc.WithInsecure())
if err != nil {
return nil, common.ServerError
}
defer client.Close()
// make call to the session host server.
// include the session_id, auth token of the current request
// and set local-only to force the RPC method
// to only perform local object creation
server := proto_rpc.NewAPIClient(client)
ctx = metadata.NewOutgoingContext(ctx, metadata.Pairs("session_id", sessionID, "authorization", authorization, "local-only", "true"))
resp, err := server.CountObjects(ctx, req)
if err != nil {
if grpc.ErrorDesc(err) == "session not found" {
return nil, common.NewSingleAPIErr(404, "", "", "session not found", nil)
}
logRPC.Errorf("%+v", err)
return nil, common.ServerError
}
return resp, nil
}
// check if session is owned by the developer, if not, return permission error
if sessionAgent := s.dbSession.GetAgent(sessionID); sessionAgent != nil {
if sessionAgent.OwnerID != developerID {
return nil, common.NewSingleAPIErr(401, "", "", "permission denied: you don't have permission to perform this operation", nil)
}
}
} else {
sessionID = util.UUID4()
s.dbSession.CreateUnregisteredSession(sessionID, developerID)
defer s.dbSession.CommitEnd(sessionID)
}
// if owner is set and not the same as the developer id, check if it exists
if len(req.Owner) > 0 && req.Owner != developerID {
if err := session.SendQueryOpWithSession(s.dbSession, sessionID, "", &tables.Object{ID: req.Owner}, 1, "", &tables.Object{}); err != nil {
if err == patchain.ErrNotFound {
return nil, common.NewSingleAPIErr(404, "", "", "owner not found", nil)
}
logRPC.Errorf("%+v", err)
return nil, common.ServerError
}
}
// if creator is set and not the same as the developer id, check if it exists
if len(req.Creator) > 0 && req.Creator != developerID {
if err := session.SendQueryOpWithSession(s.dbSession, sessionID, "", &tables.Object{ID: req.Creator}, 1, "", &tables.Object{}); err != nil {
if err == patchain.ErrNotFound {
return nil, common.NewSingleAPIErr(404, "", "", "creator not found", nil)
}
logRPC.Errorf("%+v", err)
return nil, common.ServerError
}
}
// default owner and creator
if len(req.Owner) == 0 {
req.Owner = developerID
}
if len(req.Creator) == 0 {
req.Creator = developerID
}
// developer is not the owner, this action requires permission
// TODO: ensure auth token must be a user token from the owner
// and the token authorizes access to the object created by the creator for this developer
if developerID != req.Owner {
return nil, common.NewSingleAPIErr(401, "", "", "permission denied: you are not authorized to access objects for the owner", nil)
}
// include owner and creator filters
var query map[string]interface{}
util.FromJSON(req.Query, &query)
query["owner_id"] = req.Owner
query["creator_id"] = req.Creator
var count int64
if err := session.SendCountOpWithSession(s.dbSession, sessionID, string(util.MustStringify(query)), nil, &count); err != nil {
logRPC.Errorf("%+v", err)
return nil, common.ServerError
}
return &proto_rpc.ObjectCountResponse{
Count: count,
}, nil
}