forked from sagecontinuum/sage-storage-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
1072 lines (848 loc) · 27.8 KB
/
handlers.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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"bufio"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"path"
"strconv"
"strings"
"time"
// "bytes"
// "mime/multipart"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/gorilla/mux"
"database/sql"
"github.com/go-sql-driver/mysql"
_ "github.com/go-sql-driver/mysql"
)
// SAGEBucket _
type SAGEBucket struct {
ErrorStruct `json:",inline"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"` // optional name (might be indentical to only file in bucket) username/bucketname
Owner string `json:"owner,omitempty"`
DataType string `json:"type,omitempty"`
TimeCreated *time.Time `json:"time_created,omitempty"`
TimeUpdated *time.Time `json:"time_last_updated,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
}
// SageFile simple response object
type SageFile struct {
ErrorStruct `json:",inline"`
Bucket string `json:"bucket-id,omitempty"`
Key string `json:"key,omitempty"`
}
// SAGEBucketPermission _
type SAGEBucketPermission struct {
ErrorStruct `json:",inline"`
GranteeType string `json:"granteeType,omitempty"`
Grantee string `json:"grantee,omitempty"`
Permission string `json:"permission,omitempty"`
}
// DeleteRespsonse _
type DeleteRespsonse struct {
ErrorStruct `json:",inline"`
Deleted []string `json:"deleted"`
}
// ErrorStruct _
type ErrorStruct struct {
Error string `json:"error,omitempty"`
}
// getSageBucket
// return either a bucket/folder/ listing or a file
func getSageBucketGeneric(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
sageBucketID := vars["bucket"]
if len(sageBucketID) != 36 {
respondJSONError(w, http.StatusInternalServerError, "bucket id (%s) invalid (%d)", sageBucketID, len(sageBucketID))
return
}
username := vars["username"]
// r.URL.Path example: /api/v1/objects/6dd46856-c871-4089-b1bc-a12b44e92c81
//pathArray := strings.SplitN(r.URL.Path, "/", 6)
//keyPath := pathArray[5]
_, sagePath, err := getSagePath(r.URL.Path)
if err != nil {
respondJSONError(w, http.StatusBadRequest, err.Error())
return
}
// bucket permission
rawQuery := strings.ToLower(r.URL.RawQuery) // this is ugle, .Values() does not handle ?permissions as it has no value
//log.Printf("rawQuery: %s", rawQuery)
if sagePath == "" && strings.Contains(rawQuery, "permissions") {
allowed, err := userHasBucketPermission(username, sageBucketID, "READ_ACP")
if err != nil {
respondJSONError(w, http.StatusBadRequest, err.Error())
return
}
if !allowed {
respondJSONError(w, http.StatusUnauthorized, "Access to bucket permissions denied (%s, %s)", username, sageBucketID)
return
}
permissions, err := ListBucketPermissions(sageBucketID)
if err != nil {
respondJSONError(w, http.StatusBadRequest, err.Error())
return
}
respondJSON(w, http.StatusOK, permissions)
return
}
// bucket of directory listing
allowed, err := userHasBucketPermission(username, sageBucketID, "READ")
if err != nil {
respondJSONError(w, http.StatusBadRequest, err.Error())
return
}
if !allowed {
respondJSONError(w, http.StatusUnauthorized, "Read access to bucket denied (username: \"%s\", sageBucketID: \"%s\")", username, sageBucketID)
return
}
if sagePath == "" {
// bucket listing
bucket, err := GetSageBucket(sageBucketID)
if err != nil {
respondJSONError(w, http.StatusBadRequest, err.Error())
return
}
respondJSON(w, http.StatusOK, bucket)
return
}
// directory listing
if strings.HasSuffix(sagePath, "/") {
// _, sagePath, err := getSagePath(r.URL.Path)
// if err != nil {
// respondJSONError(w, http.StatusBadRequest, err.Error())
// return
// }
recursive := false
if strings.Contains(rawQuery, "recursive") {
if !strings.Contains(rawQuery, "recursive=false") {
recursive = true
}
}
continuationToken, err := getQueryField(r, "ContinuationToken")
if err != nil {
continuationToken = ""
}
limit, err := getQueryFieldInt64(r, "limit", 0)
if err != nil {
respondJSONError(w, http.StatusInternalServerError, "error parsing query field limit: %s", err.Error())
return
}
if false {
files, directories, newContinuationToken, err := listSageBucketContentDepreccated(sageBucketID, sagePath, recursive, 0, "", continuationToken)
if err != nil {
respondJSONError(w, http.StatusInternalServerError, "error listing bucket contents (sageBucketID: %s, sagePath: %s): %s", sageBucketID, sagePath, err.Error())
return
}
files = append(files, directories...)
// Repsonse has some similiarity to https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListObjectsV2.html#API_ListObjectsV2_ResponseSyntax
data := make(map[string]interface{})
data["Contents"] = files
if newContinuationToken != "" {
data["NextContinuationToken"] = newContinuationToken
data["IsTruncated"] = true
} else {
data["IsTruncated"] = false
}
respondJSON(w, http.StatusOK, data)
}
listObject, err := listSageBucketContent(sageBucketID, sagePath, recursive, limit, "", continuationToken)
if err != nil {
respondJSONError(w, http.StatusInternalServerError, "error listing bucket contents (sageBucketID: %s, sagePath: %s): %s", sageBucketID, sagePath, err.Error())
return
}
respondJSON(w, http.StatusOK, listObject)
return
}
// a file download
//uuidStr := vars["bucket"]
//sageKey := vars["key"]
// convert SAGE specifiers to S3 specifiers
//sageBucketID := s3BucketPrefix + sageBucketID[0:2]
s3BucketID := getS3BucketID(sageBucketID) //s3BucketPrefix + sageBucketID[0:2]
s3key := path.Join(sageBucketID, sagePath)
sageFilename := path.Base(sagePath)
if sageFilename == "." || sageFilename == "/" {
respondJSONError(w, http.StatusInternalServerError, "Invalid filename (%s)", sageFilename)
return
}
objectInput := s3.GetObjectInput{
Bucket: &s3BucketID,
Key: &s3key,
}
out, err := svc.GetObject(&objectInput)
if err != nil {
respondJSONError(w, http.StatusInternalServerError, "Error getting data, svc.GetObject returned: %s", err.Error())
return
}
defer out.Body.Close()
w.Header().Set("Content-Disposition", "attachment; filename="+sageFilename)
//w.Header().Set("Content-Length", FileSize)
buffer := make([]byte, 1024*1024)
w.WriteHeader(http.StatusOK)
for {
n, err := out.Body.Read(buffer)
if err != nil {
if err == io.EOF {
w.Write(buffer[:n]) //should handle any remainding bytes.
fileDownloadByteSize.Add(float64(n))
break
}
respondJSONError(w, http.StatusInternalServerError, "Error getting data: %s", err.Error())
return
}
w.Write(buffer[0:n])
fileDownloadByteSize.Add(float64(n))
}
return
}
func listSageBucketRequest(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
username := vars["username"]
filter_owner, _ := getQueryField(r, "owner")
filter_name, _ := getQueryField(r, "name")
//if err != nil {
// respondJSONError(w, http.StatusInternalServerError, "error parsiong query: %s", err.Error())
// return
//}
buckets, err := listSageBuckets(username, filter_owner, filter_name)
if err != nil {
respondJSONError(w, http.StatusInternalServerError, "error getting list of buckets: %s", err.Error())
return
}
respondJSON(w, http.StatusOK, buckets)
}
func getQueryFieldBool(r *http.Request, fieldName string) (value bool, err error) {
value = false
valueStr, err := getQueryField(r, fieldName)
if err != nil {
return
}
if valueStr == "true" || valueStr == "1" {
value = true
}
return
}
func getQueryFieldInt64(r *http.Request, fieldName string, defaultValue int64) (value int64, err error) {
query := r.URL.Query()
_, ok := query[fieldName]
if !ok {
value = defaultValue
return
}
valueStr, err := getQueryField(r, fieldName)
if err != nil {
return
}
value, err = strconv.ParseInt(valueStr, 10, 64)
//value, err = strconv.Atoi(valueStr)
if err != nil {
return
}
return
}
// only gets the fist value, even if there are multiple values
func getQueryField(r *http.Request, fieldName string) (value string, err error) {
query := r.URL.Query()
dataTypeArray, ok := query[fieldName]
if !ok {
err = fmt.Errorf("Field %s not found", fieldName)
return
}
if len(dataTypeArray) == 0 {
err = fmt.Errorf("Field %s not found", fieldName)
return
}
value = dataTypeArray[0]
if value == "" {
err = fmt.Errorf("Field %s is empty", fieldName)
return
}
return
}
// POST /objects creates SAGE bucket (and a S3 bucket if needed) and returns identifier
func createBucketRequest(w http.ResponseWriter, r *http.Request) {
log.Printf("received bucket creation request")
vars := mux.Vars(r)
username := vars["username"]
log.Printf("username: %s", username)
dataType, err := getQueryField(r, "type")
if err != nil {
respondJSONError(w, http.StatusInternalServerError, err.Error(), dataType)
return
}
_, ok := validDataTypes[dataType]
if !ok {
respondJSONError(w, http.StatusInternalServerError, "Data type %s not supported", dataType)
return
}
bucketName, _ := getQueryField(r, "name")
isPublic, _ := getQueryFieldBool(r, "public")
bucketObject, err := createSageBucket(username, dataType, bucketName, isPublic)
if err != nil {
respondJSONError(w, http.StatusInternalServerError, "bucket creation failed: %s", err.Error())
return
}
// TODO store owner info in mysql
respondJSON(w, http.StatusOK, bucketObject)
}
func getSagePath(urlPath string) (bucket string, path string, err error) {
pathParsed := strings.SplitN(urlPath, "/", 6)
if len(pathParsed) != 6 && len(pathParsed) != 5 {
err = fmt.Errorf("error parsing URL (%d)", len(pathParsed))
return
}
if pathParsed[3] != "objects" {
err = fmt.Errorf("error parsing URL (%s)", pathParsed[3])
return
}
// bucket
bucket = pathParsed[4]
if len(pathParsed) == 6 {
path = "/" + pathParsed[5] // folder or file
} else {
path = "" // bucket
}
log.Printf("getSagePath, path: %s", path)
return
}
// patchBucket Modify bucket metadata values
// Only bucket fields specified in request body will overwrite existing values.
func patchBucket(w http.ResponseWriter, r *http.Request) {
pathParams := mux.Vars(r)
sageBucketID := pathParams["bucket"]
//objectKey := pathParams["key"]
vars := mux.Vars(r)
username := vars["username"]
//rawQuery := r.URL.RawQuery
// normal bucket metadata
allowed, err := userHasBucketPermission(username, sageBucketID, "FULL_CONTROL")
if err != nil {
respondJSONError(w, http.StatusBadRequest, err.Error())
return
}
if !allowed {
respondJSONError(w, http.StatusUnauthorized, "Write access to bucket metadata denied (%s, %s)", username, sageBucketID)
return
}
var deltaBucket map[string]string
err = json.NewDecoder(r.Body).Decode(&deltaBucket)
if err != nil {
err = fmt.Errorf("Could not parse json: %s", err.Error())
respondJSONError(w, http.StatusBadRequest, err.Error())
return
}
log.Printf("got: %v", deltaBucket)
db, err := sql.Open("mysql", mysqlDSN)
if err != nil {
err = fmt.Errorf("Unable to connect to database: %v", err)
return
}
defer db.Close()
newBucketname, ok := deltaBucket["name"]
if ok {
insertQueryStr := "UPDATE Buckets SET name=? WHERE id=UUID_TO_BIN(?) ;"
_, err = db.Exec(insertQueryStr, newBucketname, sageBucketID)
if err != nil {
err = fmt.Errorf("Bucket creation in mysql failed: %s", err.Error())
return
}
}
// return should return real bucket
newBucket, err := GetSageBucket(sageBucketID)
if err != nil {
err = fmt.Errorf("GetSageBucket returned: %s", err.Error())
respondJSONError(w, http.StatusBadRequest, err.Error())
return
}
respondJSON(w, http.StatusOK, newBucket)
//bucket fields:
//metadata , name , type, (owner, change requires permission change)
//permission
}
// putBucket only for permissions
func putBucket(w http.ResponseWriter, r *http.Request) {
pathParams := mux.Vars(r)
sageBucketID := pathParams["bucket"]
//objectKey := pathParams["key"]
vars := mux.Vars(r)
username := vars["username"]
rawQuery := r.URL.RawQuery
if strings.Contains(rawQuery, "permission") {
allowed, err := userHasBucketPermission(username, sageBucketID, "WRITE_ACP")
if err != nil {
respondJSONError(w, http.StatusBadRequest, err.Error())
return
}
if !allowed {
respondJSONError(w, http.StatusUnauthorized, "Write access to bucket permissions denied (%s, %s)", username, sageBucketID)
return
}
bucketObject, err := GetSageBucket(sageBucketID)
if err != nil {
respondJSONError(w, http.StatusBadRequest, err.Error())
return
}
var newPerm SAGEBucketPermission
err = json.NewDecoder(r.Body).Decode(&newPerm)
if err != nil {
err = fmt.Errorf("Could not parse json: %s", err.Error())
respondJSONError(w, http.StatusBadRequest, err.Error())
return
}
if bucketObject.Owner == newPerm.Grantee {
respondJSONError(w, http.StatusBadRequest, "You cannot change your own permissons.")
return
}
if newPerm.GranteeType == "" {
respondJSONError(w, http.StatusBadRequest, "GranteeType missing")
return
}
if newPerm.GranteeType == "GROUP" && newPerm.Grantee == "AllUsers" && newPerm.Permission != "READ" {
respondJSONError(w, http.StatusBadRequest, "Buckets can be made public only for READ access.")
return
}
//do something
db, err := sql.Open("mysql", mysqlDSN)
if err != nil {
err = fmt.Errorf("Unable to connect to database: %v", err)
return
}
defer db.Close()
insertQueryStr := "INSERT INTO BucketPermissions (id, granteeType, grantee, permission) VALUES ( UUID_TO_BIN(?), ? , ?, ?) ;"
_, err = db.Exec(insertQueryStr, sageBucketID, newPerm.GranteeType, newPerm.Grantee, newPerm.Permission)
if err != nil {
me, ok := err.(*mysql.MySQLError)
if ok {
if me.Number == 1062 {
// entry already exists, quietly respond OK
respondJSON(w, http.StatusOK, newPerm)
return
}
}
err = fmt.Errorf("Adding bucket permissions failed: %s", err.Error())
respondJSONError(w, http.StatusUnauthorized, err.Error())
return
}
respondJSON(w, http.StatusOK, newPerm)
return
}
respondJSONError(w, http.StatusUnauthorized, "Only query ?permissions supported")
return
//respondJSON(w, http.StatusOK, newBucket)
//bucket fields:
//metadata , name , type, (owner, change requires permission change)
//permission
}
// deleteBucket deletes bucket, files, and bucket permissions
func deleteBucket(w http.ResponseWriter, r *http.Request) {
//pathParams := mux.Vars(r)
//sageBucketID := pathParams["bucket"]
//objectKey := pathParams["key"]
vars := mux.Vars(r)
username := vars["username"]
sageBucketID := vars["bucket"]
if len(sageBucketID) != 36 {
respondJSONError(w, http.StatusInternalServerError, "bucket id (%s) invalid (%d)", sageBucketID, len(sageBucketID))
return
}
_, sagePath, err := getSagePath(r.URL.Path)
if err != nil {
respondJSONError(w, http.StatusBadRequest, err.Error())
return
}
rawQuery := r.URL.RawQuery
if (sagePath == "") && strings.Contains(rawQuery, "permission") {
allowed, err := userHasBucketPermission(username, sageBucketID, "WRITE_ACP")
if err != nil {
respondJSONError(w, http.StatusBadRequest, err.Error())
return
}
if !allowed {
respondJSONError(w, http.StatusUnauthorized, "Write access to bucket permissions denied (%s, %s)", username, sageBucketID)
return
}
bucketObject, err := GetSageBucket(sageBucketID)
if err != nil {
respondJSONError(w, http.StatusBadRequest, err.Error())
return
}
values := r.URL.Query()
grantees, ok := values["grantee"]
if !ok {
respondJSONError(w, http.StatusBadRequest, "query field \"grantee\" missing")
return
}
if len(grantees) == 0 {
respondJSONError(w, http.StatusBadRequest, "query field \"grantee\" missing")
return
}
dr := DeleteRespsonse{}
dr.Deleted = []string{}
for _, grantee := range grantees {
granteeType := "USER"
granteeArray := strings.SplitN(grantee, ":", 3)
deletePermission := ""
if len(granteeArray) >= 2 {
granteeType = granteeArray[0]
grantee = granteeArray[1]
}
if len(granteeArray) == 3 {
deletePermission = granteeArray[2]
}
if bucketObject.Owner == grantee {
respondJSONError(w, http.StatusBadRequest, "You cannot change your own permissons.")
return
}
//do something
db, err := sql.Open("mysql", mysqlDSN)
if err != nil {
err = fmt.Errorf("Unable to connect to database: %v", err)
return
}
defer db.Close()
insertQueryStr := ""
var result sql.Result
if deletePermission == "" {
insertQueryStr = "DELETE FROM BucketPermissions WHERE id=UUID_TO_BIN(?) AND granteeType=? AND grantee=? ;"
result, err = db.Exec(insertQueryStr, sageBucketID, granteeType, grantee)
} else {
insertQueryStr = "DELETE FROM BucketPermissions WHERE id=UUID_TO_BIN(?) AND granteeType=? AND grantee=? AND permission=?;"
result, err = db.Exec(insertQueryStr, sageBucketID, granteeType, grantee, deletePermission)
}
if err != nil {
err = fmt.Errorf("Removing bucket permissions failed: %s", err.Error())
respondJSONError(w, http.StatusUnauthorized, err.Error())
return
}
deletedNumber, err := result.RowsAffected()
if err != nil {
err = fmt.Errorf("result.RowsAffected returned: %s", err.Error())
respondJSONError(w, http.StatusUnauthorized, err.Error())
return
}
if deletedNumber > 0 {
if deletePermission == "" {
dr.Deleted = append(dr.Deleted, granteeType+":"+grantee)
} else {
dr.Deleted = append(dr.Deleted, granteeType+":"+grantee+":"+deletePermission)
}
}
}
respondJSON(w, http.StatusOK, dr)
return
}
// delete bucket or file
allowed, err := userHasBucketPermission(username, sageBucketID, "WRITE")
if err != nil {
respondJSONError(w, http.StatusBadRequest, err.Error())
return
}
if !allowed {
respondJSONError(w, http.StatusUnauthorized, "Write access to bucket denied (%s, %s)", username, sageBucketID)
return
}
// delete bucket
if sagePath == "" {
// delete bucket and all its contents
// 1) check if bucket exists
sageBucket, err := GetSageBucket(sageBucketID)
if err != nil {
respondJSONError(w, http.StatusUnauthorized, err.Error())
return
}
_ = sageBucket
// 2) delete files in bucket
totalDeleted, err := deleteAllFiles(sageBucketID)
if err != nil {
respondJSONError(w, http.StatusInternalServerError, err.Error())
return
}
_ = totalDeleted
db, err := sql.Open("mysql", mysqlDSN)
if err != nil {
err = fmt.Errorf("Unable to connect to database: %v", err)
respondJSONError(w, http.StatusInternalServerError, err.Error())
return
}
defer db.Close()
// 3) delete bucket
insertQueryStr := "DELETE FROM Buckets WHERE id=UUID_TO_BIN(?) ;"
_, err = db.Exec(insertQueryStr, sageBucketID)
if err != nil {
err = fmt.Errorf("Bucket creation in mysql failed: %s", err.Error())
respondJSONError(w, http.StatusInternalServerError, err.Error())
return
}
data := DeleteRespsonse{}
data.Deleted = []string{sageBucketID} // fmt.Sprintf("totalDeleted: %d", totalDeleted)
respondJSON(w, http.StatusOK, data)
return
}
// delete file
deleted, err := deleteSAGEFiles(sageBucketID, []string{sagePath})
if err != nil {
err = fmt.Errorf("Deleting files failed: %s", err.Error())
respondJSONError(w, http.StatusInternalServerError, err.Error())
}
data := DeleteRespsonse{}
data.Deleted = deleted
respondJSON(w, http.StatusOK, data)
return
//respondJSON(w, http.StatusOK, newBucket)
//bucket fields:
//metadata , name , type, (owner, change requires permission change)
//permission
}
// PUT /objects/{bucket-id}/key... (woth or without filename in key)
func uploadObject(w http.ResponseWriter, r *http.Request) {
pathParams := mux.Vars(r)
sageBucketID := pathParams["bucket"]
//objectKey := pathParams["key"]
vars := mux.Vars(r)
username := vars["username"]
allowed, err := userHasBucketPermission(username, sageBucketID, "WRITE")
if err != nil {
respondJSONError(w, http.StatusBadRequest, err.Error())
return
}
if !allowed {
respondJSONError(w, http.StatusUnauthorized, "Write access to bucket denied (%s, %s)", username, sageBucketID)
return
}
log.Printf("r.URL.Path: %s", r.URL.Path)
// example: /api/v1/objects/cbc2c709-2ef7-4852-8f5e-038fdc7f2304/test3/test3.jpg
_, preliminarySageKey, err := getSagePath(r.URL.Path)
if err != nil {
respondJSONError(w, http.StatusBadRequest, err.Error())
return
}
isDirectory := false
if preliminarySageKey == "" || strings.HasSuffix(preliminarySageKey, "/") {
isDirectory = true
}
log.Printf("preliminarySageKey: %s", preliminarySageKey)
preliminaryS3Key := path.Join(sageBucketID, preliminarySageKey)
log.Printf("preliminaryS3Key: %s", preliminaryS3Key)
s3BucketName := getS3BucketID(sageBucketID) //s3BucketPrefix + sageBucketID[0:2] // first two characters of uuid
log.Printf("s3BucketName: %s", s3BucketName)
mReader, err := r.MultipartReader()
if err != nil {
respondJSONError(w, http.StatusBadRequest, "MultipartReader returned: %s", err.Error())
return
}
//chunk := make([]byte, 4096)
for {
part, err := mReader.NextPart()
if err != nil {
if err != io.EOF {
respondJSONError(w, http.StatusInternalServerError, "error reading part: %s", err.Error())
} else {
log.Printf("Hit last part of multipart upload")
w.WriteHeader(200)
fmt.Fprintf(w, "Upload completed")
}
return
}
// process part
//formName := part.FormName()
s3Key := ""
sageKey := ""
if isDirectory {
filename := part.FileName()
if filename == "" {
respondJSONError(w, http.StatusBadRequest, "part upload has no filename and no key was specified")
return
}
s3Key = path.Join(preliminaryS3Key, filename)
sageKey = path.Join(preliminarySageKey, filename)
} else {
s3Key = preliminaryS3Key
sageKey = preliminarySageKey
}
sageKey = strings.TrimPrefix(sageKey, "/")
log.Printf("s3Key: %s", s3Key)
log.Printf("sageKey: %s", sageKey)
bufferedPartReader := bufio.NewReaderSize(part, 32768)
var objectMetadata map[string]*string
objectMetadata = make(map[string]*string)
data := SageFile{}
//data.Metadata = objectMetadata
//key := filename // use filename unless key has been specified
objectMetadata["owner"] = &username
//objectMetadata["type"] = &dataType
data.Key = sageKey
upParams := &s3manager.UploadInput{
Bucket: aws.String(s3BucketName),
Key: aws.String(s3Key),
Body: bufferedPartReader,
Metadata: objectMetadata,
}
uploader := s3manager.NewUploader(newSession)
_, err = uploader.Upload(upParams)
if err != nil {
// Print the error and exit.
respondJSONError(w, http.StatusInternalServerError, "Upload to S3 backend failed: %s", err.Error())
return
}
data.Bucket = sageBucketID
//log.Printf("Upload - Bucket: %v and Object: %v\n", bucketName, objectName)
log.Printf("user upload successful")
fileUploadCounter.Inc()
respondJSON(w, http.StatusOK, data)
break // not doing multiple files yet
}
return
}
func downloadObject(w http.ResponseWriter, r *http.Request) {
pathParams := mux.Vars(r)
uuidStr := pathParams["bucket"]
sageKey := pathParams["key"]
// convert SAGE specifiers to S3 specifiers
sageBucketID := getS3BucketID(uuidStr) //s3BucketPrefix + uuidStr[0:2]
key := path.Join(uuidStr, sageKey)
log.Printf("sageBucketID: %s key: %s", sageBucketID, key)
}
func respondJSON(w http.ResponseWriter, statusCode int, data interface{}) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
// json.NewEncoder(w).Encode(data)
s, err := json.MarshalIndent(data, "", " ")
if err == nil {
w.Write(s)
}
}
func respondJSONError(w http.ResponseWriter, statusCode int, msg string, args ...interface{}) {
errorStr := fmt.Sprintf(msg, args...)
log.Printf("Reply to client: %s", errorStr)
respondJSON(w, statusCode, ErrorStruct{Error: errorStr})
}
func authMW(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
vars := mux.Vars(r)
vars["username"] = ""
authorization := r.Header.Get("Authorization")
if authorization == "" {
next(w, r)
//respondJSONError(w, http.StatusInternalServerError, "Authorization header is missing")
return
}
log.Printf("authorization: %s", authorization)
authorizationArray := strings.Split(authorization, " ")
if len(authorizationArray) != 2 {
respondJSONError(w, http.StatusInternalServerError, "Authorization field must be of form \"sage <token>\"")
return
}
if strings.ToLower(authorizationArray[0]) != "sage" {
respondJSONError(w, http.StatusInternalServerError, "Only bearer \"sage\" supported")
return
}
//tokenStr := r.FormValue("token")
tokenStr := authorizationArray[1]
log.Printf("tokenStr: %s", tokenStr)
if disableAuth {
if strings.HasPrefix(tokenStr, "user:") {
username := strings.TrimPrefix(tokenStr, "user:")
vars["username"] = username
} else {
vars["username"] = "user-auth-disabled"
}
next(w, r)
return
}
url := tokenInfoEndpoint
log.Printf("url: %s", url)
payload := strings.NewReader("token=" + tokenStr)
client := &http.Client{
Timeout: time.Second * 5,
}
req, err := http.NewRequest("POST", url, payload)
if err != nil {
log.Print("NewRequest returned: " + err.Error())
//http.Error(w, err.Error(), http.StatusInternalServerError)
respondJSONError(w, http.StatusInternalServerError, err.Error())
return
}
auth := tokenInfoUser + ":" + tokenInfoPassword
//fmt.Printf("auth: %s", auth)
authEncoded := base64.StdEncoding.EncodeToString([]byte(auth))
req.Header.Add("Authorization", "Basic "+authEncoded)
req.Header.Add("Accept", "application/json; indent=4")
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
res, err := client.Do(req)
if err != nil {
log.Print(err)
//http.Error(w, err.Error(), http.StatusInternalServerError)
respondJSONError(w, http.StatusInternalServerError, err.Error())
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
//http.Error(w, err.Error(), http.StatusInternalServerError)