You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
5.9 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package s3api
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strings"
  6. "github.com/gorilla/mux"
  7. "google.golang.org/grpc"
  8. )
  9. type S3ApiServerOption struct {
  10. Filer string
  11. Port int
  12. FilerGrpcAddress string
  13. Config string
  14. DomainName string
  15. BucketsPath string
  16. GrpcDialOption grpc.DialOption
  17. }
  18. type S3ApiServer struct {
  19. option *S3ApiServerOption
  20. iam *IdentityAccessManagement
  21. }
  22. func NewS3ApiServer(router *mux.Router, option *S3ApiServerOption) (s3ApiServer *S3ApiServer, err error) {
  23. s3ApiServer = &S3ApiServer{
  24. option: option,
  25. iam: NewIdentityAccessManagement(option.Config, option.DomainName),
  26. }
  27. s3ApiServer.registerRouter(router)
  28. return s3ApiServer, nil
  29. }
  30. func (s3a *S3ApiServer) registerRouter(router *mux.Router) {
  31. // API Router
  32. apiRouter := router.PathPrefix("/").Subrouter()
  33. var routers []*mux.Router
  34. if s3a.option.DomainName != "" {
  35. domainNames := strings.Split(s3a.option.DomainName, ",")
  36. for _, domainName := range domainNames {
  37. routers = append(routers, apiRouter.Host(
  38. fmt.Sprintf("%s.%s:%d", "{bucket:.+}", domainName, s3a.option.Port)).Subrouter())
  39. routers = append(routers, apiRouter.Host(
  40. fmt.Sprintf("%s.%s", "{bucket:.+}", domainName)).Subrouter())
  41. }
  42. }
  43. routers = append(routers, apiRouter.PathPrefix("/{bucket}").Subrouter())
  44. for _, bucket := range routers {
  45. // HeadObject
  46. bucket.Methods("HEAD").Path("/{object:.+}").HandlerFunc(track(s3a.iam.Auth(s3a.HeadObjectHandler, ACTION_READ), "GET"))
  47. // HeadBucket
  48. bucket.Methods("HEAD").HandlerFunc(track(s3a.iam.Auth(s3a.HeadBucketHandler, ACTION_ADMIN), "GET"))
  49. // CopyObjectPart
  50. bucket.Methods("PUT").Path("/{object:.+}").HeadersRegexp("X-Amz-Copy-Source", ".*?(\\/|%2F).*?").HandlerFunc(track(s3a.iam.Auth(s3a.CopyObjectPartHandler, ACTION_WRITE), "PUT")).Queries("partNumber", "{partNumber:[0-9]+}", "uploadId", "{uploadId:.*}")
  51. // PutObjectPart
  52. bucket.Methods("PUT").Path("/{object:.+}").HandlerFunc(track(s3a.iam.Auth(s3a.PutObjectPartHandler, ACTION_WRITE), "PUT")).Queries("partNumber", "{partNumber:[0-9]+}", "uploadId", "{uploadId:.*}")
  53. // CompleteMultipartUpload
  54. bucket.Methods("POST").Path("/{object:.+}").HandlerFunc(track(s3a.iam.Auth(s3a.CompleteMultipartUploadHandler, ACTION_WRITE), "POST")).Queries("uploadId", "{uploadId:.*}")
  55. // NewMultipartUpload
  56. bucket.Methods("POST").Path("/{object:.+}").HandlerFunc(track(s3a.iam.Auth(s3a.NewMultipartUploadHandler, ACTION_WRITE), "POST")).Queries("uploads", "")
  57. // AbortMultipartUpload
  58. bucket.Methods("DELETE").Path("/{object:.+}").HandlerFunc(track(s3a.iam.Auth(s3a.AbortMultipartUploadHandler, ACTION_WRITE), "DELETE")).Queries("uploadId", "{uploadId:.*}")
  59. // ListObjectParts
  60. bucket.Methods("GET").Path("/{object:.+}").HandlerFunc(track(s3a.iam.Auth(s3a.ListObjectPartsHandler, ACTION_READ), "GET")).Queries("uploadId", "{uploadId:.*}")
  61. // ListMultipartUploads
  62. bucket.Methods("GET").HandlerFunc(track(s3a.iam.Auth(s3a.ListMultipartUploadsHandler, ACTION_READ), "GET")).Queries("uploads", "")
  63. // GetObjectTagging
  64. bucket.Methods("GET").Path("/{object:.+}").HandlerFunc(track(s3a.iam.Auth(s3a.GetObjectTaggingHandler, ACTION_READ), "GET")).Queries("tagging", "")
  65. // PutObjectTagging
  66. bucket.Methods("PUT").Path("/{object:.+}").HandlerFunc(track(s3a.iam.Auth(s3a.PutObjectTaggingHandler, ACTION_TAGGING), "PUT")).Queries("tagging", "")
  67. // DeleteObjectTagging
  68. bucket.Methods("DELETE").Path("/{object:.+}").HandlerFunc(track(s3a.iam.Auth(s3a.DeleteObjectTaggingHandler, ACTION_TAGGING), "DELETE")).Queries("tagging", "")
  69. // CopyObject
  70. bucket.Methods("PUT").Path("/{object:.+}").HeadersRegexp("X-Amz-Copy-Source", ".*?(\\/|%2F).*?").HandlerFunc(track(s3a.iam.Auth(s3a.CopyObjectHandler, ACTION_WRITE), "COPY"))
  71. // PutObject
  72. bucket.Methods("PUT").Path("/{object:.+}").HandlerFunc(track(s3a.iam.Auth(s3a.PutObjectHandler, ACTION_WRITE), "PUT"))
  73. // PutBucket
  74. bucket.Methods("PUT").HandlerFunc(track(s3a.iam.Auth(s3a.PutBucketHandler, ACTION_ADMIN), "PUT"))
  75. // DeleteObject
  76. bucket.Methods("DELETE").Path("/{object:.+}").HandlerFunc(track(s3a.iam.Auth(s3a.DeleteObjectHandler, ACTION_WRITE), "DELETE"))
  77. // DeleteBucket
  78. bucket.Methods("DELETE").HandlerFunc(track(s3a.iam.Auth(s3a.DeleteBucketHandler, ACTION_WRITE), "DELETE"))
  79. // ListObjectsV2
  80. bucket.Methods("GET").HandlerFunc(track(s3a.iam.Auth(s3a.ListObjectsV2Handler, ACTION_LIST), "LIST")).Queries("list-type", "2")
  81. // GetObject, but directory listing is not supported
  82. bucket.Methods("GET").Path("/{object:.+}").HandlerFunc(track(s3a.iam.Auth(s3a.GetObjectHandler, ACTION_READ), "GET"))
  83. // ListObjectsV1 (Legacy)
  84. bucket.Methods("GET").HandlerFunc(track(s3a.iam.Auth(s3a.ListObjectsV1Handler, ACTION_LIST), "LIST"))
  85. // PostPolicy
  86. bucket.Methods("POST").HeadersRegexp("Content-Type", "multipart/form-data*").HandlerFunc(track(s3a.iam.Auth(s3a.PostPolicyBucketHandler, ACTION_WRITE), "POST"))
  87. // DeleteMultipleObjects
  88. bucket.Methods("POST").HandlerFunc(track(s3a.iam.Auth(s3a.DeleteMultipleObjectsHandler, ACTION_WRITE), "DELETE")).Queries("delete", "")
  89. /*
  90. // not implemented
  91. // GetBucketLocation
  92. bucket.Methods("GET").HandlerFunc(s3a.GetBucketLocationHandler).Queries("location", "")
  93. // GetBucketPolicy
  94. bucket.Methods("GET").HandlerFunc(s3a.GetBucketPolicyHandler).Queries("policy", "")
  95. // GetObjectACL
  96. bucket.Methods("GET").Path("/{object:.+}").HandlerFunc(s3a.GetObjectACLHandler).Queries("acl", "")
  97. // GetBucketACL
  98. bucket.Methods("GET").HandlerFunc(s3a.GetBucketACLHandler).Queries("acl", "")
  99. // PutBucketPolicy
  100. bucket.Methods("PUT").HandlerFunc(s3a.PutBucketPolicyHandler).Queries("policy", "")
  101. // DeleteBucketPolicy
  102. bucket.Methods("DELETE").HandlerFunc(s3a.DeleteBucketPolicyHandler).Queries("policy", "")
  103. */
  104. }
  105. // ListBuckets
  106. apiRouter.Methods("GET").Path("/").HandlerFunc(track(s3a.iam.Auth(s3a.ListBucketsHandler, ACTION_ADMIN), "LIST"))
  107. // NotFound
  108. apiRouter.NotFoundHandler = http.HandlerFunc(notFoundHandler)
  109. }