2015-07-01 05:15:48 +02:00
|
|
|
/*
|
2015-10-16 20:26:01 +02:00
|
|
|
* Minio Cloud Storage, (C) 2015 Minio, Inc.
|
2015-07-01 05:15:48 +02: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 09:52:01 +02:00
|
|
|
package main
|
2015-07-01 05:15:48 +02:00
|
|
|
|
|
|
|
import (
|
2016-01-25 07:26:53 +01:00
|
|
|
"net"
|
2015-07-01 05:15:48 +02:00
|
|
|
"net/http"
|
|
|
|
|
2015-08-23 03:34:00 +02:00
|
|
|
router "github.com/gorilla/mux"
|
2016-01-24 04:44:32 +01:00
|
|
|
jsonrpc "github.com/gorilla/rpc/v2"
|
|
|
|
"github.com/gorilla/rpc/v2/json"
|
2016-01-25 07:26:53 +01:00
|
|
|
"github.com/minio/minio-go"
|
|
|
|
"github.com/minio/minio-xl/pkg/probe"
|
2015-10-16 20:26:01 +02:00
|
|
|
"github.com/minio/minio/pkg/fs"
|
2015-07-01 05:15:48 +02:00
|
|
|
)
|
|
|
|
|
2016-01-22 01:28:15 +01:00
|
|
|
// CloudStorageAPI container for S3 compatible API.
|
2015-10-19 21:15:19 +02:00
|
|
|
type CloudStorageAPI struct {
|
2016-01-25 07:26:53 +01: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 21:15:19 +02:00
|
|
|
Filesystem fs.Filesystem
|
|
|
|
}
|
|
|
|
|
2016-01-22 01:28:15 +01:00
|
|
|
// WebAPI container for Web API.
|
|
|
|
type WebAPI struct {
|
2016-01-26 21:08:45 +01:00
|
|
|
// FSPath filesystem path.
|
|
|
|
FSPath string
|
2016-01-25 07:26:53 +01:00
|
|
|
// Once true log all incoming request.
|
|
|
|
AccessLog bool
|
|
|
|
// Minio client instance.
|
|
|
|
Client minio.CloudStorageClient
|
2016-01-27 10:52:54 +01: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-22 01:28:15 +01:00
|
|
|
}
|
|
|
|
|
2016-01-24 04:44:32 +01:00
|
|
|
func getWebAPIHandler(web *WebAPI) http.Handler {
|
2016-01-22 01:28:15 +01:00
|
|
|
var mwHandlers = []MiddlewareHandler{
|
2016-01-24 04:44:32 +01:00
|
|
|
TimeValidityHandler, // Validate time.
|
2016-01-27 10:52:54 +01:00
|
|
|
AuthHandler, // Authentication handler for verifying tokens.
|
2016-01-28 18:23:05 +01:00
|
|
|
CorsHandler, // CORS added only for testing purposes.
|
2016-01-22 01:28:15 +01:00
|
|
|
}
|
|
|
|
if web.AccessLog {
|
|
|
|
mwHandlers = append(mwHandlers, AccessLogHandler)
|
|
|
|
}
|
2016-01-24 04:44:32 +01: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-22 01:28:15 +01:00
|
|
|
mux := router.NewRouter()
|
2016-01-31 03:33:33 +01:00
|
|
|
// Root router.
|
|
|
|
root := mux.NewRoute().PathPrefix("/").Subrouter()
|
|
|
|
root.Handle("/rpc", s)
|
|
|
|
|
2016-01-24 04:44:32 +01:00
|
|
|
// Enable this when we add assets.
|
2016-01-31 03:33:33 +01:00
|
|
|
// root.PathPrefix("/login").Handler(http.StripPrefix("/login", http.FileServer(assetFS())))
|
|
|
|
// root.Handle("/{file:.*}", http.FileServer(assetFS()))
|
2016-01-22 01:28:15 +01:00
|
|
|
return registerCustomMiddleware(mux, mwHandlers...)
|
|
|
|
}
|
|
|
|
|
2015-10-18 04:17:33 +02:00
|
|
|
// registerCloudStorageAPI - register all the handlers to their respective paths
|
|
|
|
func registerCloudStorageAPI(mux *router.Router, a CloudStorageAPI) {
|
2015-11-07 23:15:22 +01:00
|
|
|
// root Router
|
2015-10-25 16:00:39 +01:00
|
|
|
root := mux.NewRoute().PathPrefix("/").Subrouter()
|
2015-11-07 23:15:22 +01:00
|
|
|
// Bucket router
|
2015-10-25 16:00:39 +01:00
|
|
|
bucket := root.PathPrefix("/{bucket}").Subrouter()
|
2015-07-01 05:15:48 +02:00
|
|
|
|
2015-11-07 23:15:22 +01:00
|
|
|
// Object operations
|
2015-10-25 16:00:39 +01: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 23:15:22 +01:00
|
|
|
// Bucket operations
|
2015-12-27 08:38:38 +01:00
|
|
|
bucket.Methods("GET").HandlerFunc(a.GetBucketLocationHandler).Queries("location", "")
|
2015-10-25 16:00:39 +01: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 23:15:22 +01:00
|
|
|
// Root operation
|
2015-10-25 16:00:39 +01:00
|
|
|
root.Methods("GET").HandlerFunc(a.ListBucketsHandler)
|
2015-07-01 09:37:43 +02:00
|
|
|
}
|
|
|
|
|
2016-01-22 01:28:15 +01:00
|
|
|
// getNewWebAPI instantiate a new WebAPI.
|
2016-01-24 04:44:32 +01:00
|
|
|
func getNewWebAPI(conf cloudServerConfig) *WebAPI {
|
2016-01-25 07:26:53 +01:00
|
|
|
// Split host port.
|
2016-01-27 10:52:54 +01:00
|
|
|
host, port, e := net.SplitHostPort(conf.Address)
|
2016-01-25 07:26:53 +01:00
|
|
|
fatalIf(probe.NewError(e), "Unable to parse web addess.", nil)
|
|
|
|
|
2016-01-27 10:52:54 +01:00
|
|
|
// Default host is 'localhost', if no host present.
|
|
|
|
if host == "" {
|
|
|
|
host = "localhost"
|
|
|
|
}
|
2016-01-25 07:26:53 +01:00
|
|
|
|
|
|
|
// Initialize minio client for AWS Signature Version '4'
|
2016-01-27 10:52:54 +01: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 07:26:53 +01:00
|
|
|
fatalIf(probe.NewError(e), "Unable to initialize minio client", nil)
|
|
|
|
|
2016-01-24 04:44:32 +01:00
|
|
|
web := &WebAPI{
|
2016-01-27 10:52:54 +01:00
|
|
|
FSPath: conf.Path,
|
|
|
|
AccessLog: conf.AccessLog,
|
|
|
|
Client: client,
|
|
|
|
inSecure: inSecure,
|
|
|
|
apiAddress: conf.Address,
|
|
|
|
accessKeyID: conf.AccessKeyID,
|
|
|
|
secretAccessKey: conf.SecretAccessKey,
|
2016-01-22 01:28:15 +01:00
|
|
|
}
|
2016-01-24 04:44:32 +01:00
|
|
|
return web
|
2016-01-22 01:28:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// getNewCloudStorageAPI instantiate a new CloudStorageAPI.
|
2015-10-20 09:33:53 +02:00
|
|
|
func getNewCloudStorageAPI(conf cloudServerConfig) CloudStorageAPI {
|
2015-12-06 23:31:20 +01:00
|
|
|
fs, err := fs.New(conf.Path)
|
2015-11-07 23:15:22 +01:00
|
|
|
fatalIf(err.Trace(), "Initializing filesystem failed.", nil)
|
2015-09-19 11:36:50 +02:00
|
|
|
|
2015-10-18 04:17:33 +02:00
|
|
|
fs.SetMinFreeDisk(conf.MinFreeDisk)
|
2015-10-18 23:47:56 +02:00
|
|
|
if conf.Expiry > 0 {
|
|
|
|
go fs.AutoExpiryThread(conf.Expiry)
|
|
|
|
}
|
2015-10-18 04:17:33 +02:00
|
|
|
return CloudStorageAPI{
|
2015-10-16 20:26:01 +02:00
|
|
|
Filesystem: fs,
|
2015-10-18 04:17:33 +02:00
|
|
|
Anonymous: conf.Anonymous,
|
2015-10-19 21:15:19 +02:00
|
|
|
AccessLog: conf.AccessLog,
|
2015-09-19 11:36:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-18 04:17:33 +02:00
|
|
|
func getCloudStorageAPIHandler(api CloudStorageAPI) http.Handler {
|
2015-09-19 09:52:01 +02:00
|
|
|
var mwHandlers = []MiddlewareHandler{
|
|
|
|
TimeValidityHandler,
|
|
|
|
IgnoreResourcesHandler,
|
2015-12-10 00:38:40 +01:00
|
|
|
IgnoreSignatureV2RequestHandler,
|
2015-09-16 01:59:43 +02:00
|
|
|
}
|
2015-10-18 04:17:33 +02:00
|
|
|
if !api.Anonymous {
|
2015-10-07 08:32:20 +02:00
|
|
|
mwHandlers = append(mwHandlers, SignatureHandler)
|
|
|
|
}
|
2015-10-19 21:15:19 +02:00
|
|
|
if api.AccessLog {
|
|
|
|
mwHandlers = append(mwHandlers, AccessLogHandler)
|
|
|
|
}
|
2016-01-28 18:23:05 +01:00
|
|
|
mwHandlers = append(mwHandlers, CorsHandler)
|
2015-07-01 05:15:48 +02:00
|
|
|
mux := router.NewRouter()
|
2015-10-18 04:17:33 +02:00
|
|
|
registerCloudStorageAPI(mux, api)
|
|
|
|
return registerCustomMiddleware(mux, mwHandlers...)
|
2015-07-01 05:15:48 +02:00
|
|
|
}
|