2015-06-30 23:15:48 -04:00
|
|
|
/*
|
2015-10-16 14:26:01 -04:00
|
|
|
* Minio Cloud Storage, (C) 2015 Minio, Inc.
|
2015-06-30 23:15:48 -04:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-09-19 03:52:01 -04:00
|
|
|
package main
|
2015-06-30 23:15:48 -04:00
|
|
|
|
|
|
|
import (
|
2016-01-25 01:26:53 -05:00
|
|
|
"net"
|
2015-06-30 23:15:48 -04:00
|
|
|
"net/http"
|
|
|
|
|
2015-08-22 21:34:00 -04:00
|
|
|
router "github.com/gorilla/mux"
|
2016-01-23 22:44:32 -05:00
|
|
|
jsonrpc "github.com/gorilla/rpc/v2"
|
|
|
|
"github.com/gorilla/rpc/v2/json"
|
2016-01-25 01:26:53 -05:00
|
|
|
"github.com/minio/minio-go"
|
|
|
|
"github.com/minio/minio-xl/pkg/probe"
|
2015-10-16 14:26:01 -04:00
|
|
|
"github.com/minio/minio/pkg/fs"
|
2015-06-30 23:15:48 -04:00
|
|
|
)
|
|
|
|
|
2016-01-21 19:28:15 -05:00
|
|
|
// CloudStorageAPI container for S3 compatible API.
|
2015-10-19 15:15:19 -04:00
|
|
|
type CloudStorageAPI struct {
|
2016-01-25 01:26:53 -05:00
|
|
|
// Do not check for incoming signatures, allow all requests.
|
|
|
|
Anonymous bool
|
|
|
|
// Once true log all incoming requests.
|
|
|
|
AccessLog bool
|
|
|
|
// Filesystem instance.
|
2015-10-19 15:15:19 -04:00
|
|
|
Filesystem fs.Filesystem
|
|
|
|
}
|
|
|
|
|
2016-01-21 19:28:15 -05:00
|
|
|
// WebAPI container for Web API.
|
|
|
|
type WebAPI struct {
|
2016-01-26 15:08:45 -05:00
|
|
|
// FSPath filesystem path.
|
|
|
|
FSPath string
|
2016-01-25 01:26:53 -05:00
|
|
|
// Once true log all incoming request.
|
|
|
|
AccessLog bool
|
|
|
|
// Minio client instance.
|
|
|
|
Client minio.CloudStorageClient
|
2016-01-27 04:52:54 -05:00
|
|
|
|
|
|
|
// private params.
|
|
|
|
inSecure bool // Enabled if TLS is false.
|
|
|
|
apiAddress string // api destination address.
|
|
|
|
// accessKeys kept to be used internally.
|
|
|
|
accessKeyID string
|
|
|
|
secretAccessKey string
|
2016-01-21 19:28:15 -05:00
|
|
|
}
|
|
|
|
|
2016-01-23 22:44:32 -05:00
|
|
|
func getWebAPIHandler(web *WebAPI) http.Handler {
|
2016-01-21 19:28:15 -05:00
|
|
|
var mwHandlers = []MiddlewareHandler{
|
2016-01-23 22:44:32 -05:00
|
|
|
TimeValidityHandler, // Validate time.
|
2016-01-27 04:52:54 -05:00
|
|
|
AuthHandler, // Authentication handler for verifying tokens.
|
2016-01-28 12:23:05 -05:00
|
|
|
CorsHandler, // CORS added only for testing purposes.
|
2016-01-21 19:28:15 -05:00
|
|
|
}
|
|
|
|
if web.AccessLog {
|
|
|
|
mwHandlers = append(mwHandlers, AccessLogHandler)
|
|
|
|
}
|
2016-01-23 22:44:32 -05:00
|
|
|
|
|
|
|
s := jsonrpc.NewServer()
|
|
|
|
codec := json.NewCodec()
|
|
|
|
s.RegisterCodec(codec, "application/json")
|
|
|
|
s.RegisterCodec(codec, "application/json; charset=UTF-8")
|
|
|
|
s.RegisterService(web, "Web")
|
2016-01-21 19:28:15 -05:00
|
|
|
mux := router.NewRouter()
|
2016-01-23 22:44:32 -05:00
|
|
|
// Add new RPC services here
|
|
|
|
mux.Handle("/rpc", s)
|
|
|
|
// Enable this when we add assets.
|
|
|
|
// mux.Handle("/{file:.*}", http.FileServer(assetFS()))
|
2016-01-21 19:28:15 -05:00
|
|
|
return registerCustomMiddleware(mux, mwHandlers...)
|
|
|
|
}
|
|
|
|
|
2015-10-17 22:17:33 -04:00
|
|
|
// registerCloudStorageAPI - register all the handlers to their respective paths
|
|
|
|
func registerCloudStorageAPI(mux *router.Router, a CloudStorageAPI) {
|
2015-11-07 17:15:22 -05:00
|
|
|
// root Router
|
2015-10-25 11:00:39 -04:00
|
|
|
root := mux.NewRoute().PathPrefix("/").Subrouter()
|
2015-11-07 17:15:22 -05:00
|
|
|
// Bucket router
|
2015-10-25 11:00:39 -04:00
|
|
|
bucket := root.PathPrefix("/{bucket}").Subrouter()
|
2015-06-30 23:15:48 -04:00
|
|
|
|
2015-11-07 17:15:22 -05:00
|
|
|
// Object operations
|
2015-10-25 11:00:39 -04:00
|
|
|
bucket.Methods("HEAD").Path("/{object:.+}").HandlerFunc(a.HeadObjectHandler)
|
|
|
|
bucket.Methods("PUT").Path("/{object:.+}").HandlerFunc(a.PutObjectPartHandler).Queries("partNumber", "{partNumber:[0-9]+}", "uploadId", "{uploadId:.*}")
|
|
|
|
bucket.Methods("GET").Path("/{object:.+}").HandlerFunc(a.ListObjectPartsHandler).Queries("uploadId", "{uploadId:.*}")
|
|
|
|
bucket.Methods("POST").Path("/{object:.+}").HandlerFunc(a.CompleteMultipartUploadHandler).Queries("uploadId", "{uploadId:.*}")
|
|
|
|
bucket.Methods("POST").Path("/{object:.+}").HandlerFunc(a.NewMultipartUploadHandler).Queries("uploads", "")
|
|
|
|
bucket.Methods("DELETE").Path("/{object:.+}").HandlerFunc(a.AbortMultipartUploadHandler).Queries("uploadId", "{uploadId:.*}")
|
|
|
|
bucket.Methods("GET").Path("/{object:.+}").HandlerFunc(a.GetObjectHandler)
|
|
|
|
bucket.Methods("PUT").Path("/{object:.+}").HandlerFunc(a.PutObjectHandler)
|
|
|
|
bucket.Methods("DELETE").Path("/{object:.+}").HandlerFunc(a.DeleteObjectHandler)
|
|
|
|
|
2015-11-07 17:15:22 -05:00
|
|
|
// Bucket operations
|
2015-12-27 02:38:38 -05:00
|
|
|
bucket.Methods("GET").HandlerFunc(a.GetBucketLocationHandler).Queries("location", "")
|
2015-10-25 11:00:39 -04:00
|
|
|
bucket.Methods("GET").HandlerFunc(a.GetBucketACLHandler).Queries("acl", "")
|
|
|
|
bucket.Methods("GET").HandlerFunc(a.ListMultipartUploadsHandler).Queries("uploads", "")
|
|
|
|
bucket.Methods("GET").HandlerFunc(a.ListObjectsHandler)
|
|
|
|
bucket.Methods("PUT").HandlerFunc(a.PutBucketACLHandler).Queries("acl", "")
|
|
|
|
bucket.Methods("PUT").HandlerFunc(a.PutBucketHandler)
|
|
|
|
bucket.Methods("HEAD").HandlerFunc(a.HeadBucketHandler)
|
|
|
|
bucket.Methods("POST").HandlerFunc(a.PostPolicyBucketHandler)
|
|
|
|
bucket.Methods("DELETE").HandlerFunc(a.DeleteBucketHandler)
|
|
|
|
|
2015-11-07 17:15:22 -05:00
|
|
|
// Root operation
|
2015-10-25 11:00:39 -04:00
|
|
|
root.Methods("GET").HandlerFunc(a.ListBucketsHandler)
|
2015-07-01 03:37:43 -04:00
|
|
|
}
|
|
|
|
|
2016-01-21 19:28:15 -05:00
|
|
|
// getNewWebAPI instantiate a new WebAPI.
|
2016-01-23 22:44:32 -05:00
|
|
|
func getNewWebAPI(conf cloudServerConfig) *WebAPI {
|
2016-01-25 01:26:53 -05:00
|
|
|
// Split host port.
|
2016-01-27 04:52:54 -05:00
|
|
|
host, port, e := net.SplitHostPort(conf.Address)
|
2016-01-25 01:26:53 -05:00
|
|
|
fatalIf(probe.NewError(e), "Unable to parse web addess.", nil)
|
|
|
|
|
2016-01-27 04:52:54 -05:00
|
|
|
// Default host is 'localhost', if no host present.
|
|
|
|
if host == "" {
|
|
|
|
host = "localhost"
|
|
|
|
}
|
2016-01-25 01:26:53 -05:00
|
|
|
|
|
|
|
// Initialize minio client for AWS Signature Version '4'
|
2016-01-27 04:52:54 -05:00
|
|
|
inSecure := !conf.TLS // Insecure true when TLS is false.
|
|
|
|
client, e := minio.NewV4(net.JoinHostPort(host, port), conf.AccessKeyID, conf.SecretAccessKey, inSecure)
|
2016-01-25 01:26:53 -05:00
|
|
|
fatalIf(probe.NewError(e), "Unable to initialize minio client", nil)
|
|
|
|
|
2016-01-23 22:44:32 -05:00
|
|
|
web := &WebAPI{
|
2016-01-27 04:52:54 -05:00
|
|
|
FSPath: conf.Path,
|
|
|
|
AccessLog: conf.AccessLog,
|
|
|
|
Client: client,
|
|
|
|
inSecure: inSecure,
|
|
|
|
apiAddress: conf.Address,
|
|
|
|
accessKeyID: conf.AccessKeyID,
|
|
|
|
secretAccessKey: conf.SecretAccessKey,
|
2016-01-21 19:28:15 -05:00
|
|
|
}
|
2016-01-23 22:44:32 -05:00
|
|
|
return web
|
2016-01-21 19:28:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// getNewCloudStorageAPI instantiate a new CloudStorageAPI.
|
2015-10-20 03:33:53 -04:00
|
|
|
func getNewCloudStorageAPI(conf cloudServerConfig) CloudStorageAPI {
|
2015-12-06 17:31:20 -05:00
|
|
|
fs, err := fs.New(conf.Path)
|
2015-11-07 17:15:22 -05:00
|
|
|
fatalIf(err.Trace(), "Initializing filesystem failed.", nil)
|
2015-09-19 05:36:50 -04:00
|
|
|
|
2015-10-17 22:17:33 -04:00
|
|
|
fs.SetMinFreeDisk(conf.MinFreeDisk)
|
2015-10-18 17:47:56 -04:00
|
|
|
if conf.Expiry > 0 {
|
|
|
|
go fs.AutoExpiryThread(conf.Expiry)
|
|
|
|
}
|
2015-10-17 22:17:33 -04:00
|
|
|
return CloudStorageAPI{
|
2015-10-16 14:26:01 -04:00
|
|
|
Filesystem: fs,
|
2015-10-17 22:17:33 -04:00
|
|
|
Anonymous: conf.Anonymous,
|
2015-10-19 15:15:19 -04:00
|
|
|
AccessLog: conf.AccessLog,
|
2015-09-19 05:36:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-17 22:17:33 -04:00
|
|
|
func getCloudStorageAPIHandler(api CloudStorageAPI) http.Handler {
|
2015-09-19 03:52:01 -04:00
|
|
|
var mwHandlers = []MiddlewareHandler{
|
|
|
|
TimeValidityHandler,
|
|
|
|
IgnoreResourcesHandler,
|
2015-12-09 18:38:40 -05:00
|
|
|
IgnoreSignatureV2RequestHandler,
|
2015-09-15 19:59:43 -04:00
|
|
|
}
|
2015-10-17 22:17:33 -04:00
|
|
|
if !api.Anonymous {
|
2015-10-07 02:32:20 -04:00
|
|
|
mwHandlers = append(mwHandlers, SignatureHandler)
|
|
|
|
}
|
2015-10-19 15:15:19 -04:00
|
|
|
if api.AccessLog {
|
|
|
|
mwHandlers = append(mwHandlers, AccessLogHandler)
|
|
|
|
}
|
2016-01-28 12:23:05 -05:00
|
|
|
mwHandlers = append(mwHandlers, CorsHandler)
|
2015-06-30 23:15:48 -04:00
|
|
|
mux := router.NewRouter()
|
2015-10-17 22:17:33 -04:00
|
|
|
registerCloudStorageAPI(mux, api)
|
|
|
|
return registerCustomMiddleware(mux, mwHandlers...)
|
2015-06-30 23:15:48 -04:00
|
|
|
}
|