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"
|
2016-02-16 21:50:36 -05:00
|
|
|
"path/filepath"
|
2015-06-30 23:15:48 -04:00
|
|
|
|
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"
|
2016-02-11 13:11:43 -05:00
|
|
|
"github.com/gorilla/rpc/v2/json2"
|
2016-01-25 01:26:53 -05:00
|
|
|
"github.com/minio/minio-go"
|
2015-10-16 14:26:01 -04:00
|
|
|
"github.com/minio/minio/pkg/fs"
|
2016-02-10 19:40:09 -05:00
|
|
|
"github.com/minio/minio/pkg/probe"
|
2016-02-15 20:42:39 -05:00
|
|
|
signV4 "github.com/minio/minio/pkg/signature"
|
2015-06-30 23:15:48 -04:00
|
|
|
)
|
|
|
|
|
2016-02-18 05:13:52 -05:00
|
|
|
// storageAPI container for S3 compatible API.
|
|
|
|
type storageAPI struct {
|
2016-01-25 01:26:53 -05:00
|
|
|
// Once true log all incoming requests.
|
|
|
|
AccessLog bool
|
|
|
|
// Filesystem instance.
|
2015-10-19 15:15:19 -04:00
|
|
|
Filesystem fs.Filesystem
|
2016-02-15 20:42:39 -05:00
|
|
|
// Signature instance.
|
|
|
|
Signature *signV4.Signature
|
|
|
|
// Region instance.
|
|
|
|
Region string
|
2015-10-19 15:15:19 -04:00
|
|
|
}
|
|
|
|
|
2016-02-18 05:13:52 -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.
|
|
|
|
apiAddress string // api destination address.
|
|
|
|
// accessKeys kept to be used internally.
|
|
|
|
accessKeyID string
|
|
|
|
secretAccessKey string
|
2016-01-21 19:28:15 -05:00
|
|
|
}
|
|
|
|
|
2016-02-18 05:13:52 -05:00
|
|
|
// registerAPIHandlers - register all the handlers to their respective paths
|
|
|
|
func registerAPIHandlers(mux *router.Router, a storageAPI, w *webAPI) {
|
2016-02-16 21:50:36 -05:00
|
|
|
// Minio rpc router
|
|
|
|
minio := mux.NewRoute().PathPrefix(privateBucket).Subrouter()
|
2016-01-23 22:44:32 -05:00
|
|
|
|
2016-02-16 21:50:36 -05:00
|
|
|
// Initialize json rpc handlers.
|
|
|
|
rpc := jsonrpc.NewServer()
|
2016-02-11 13:11:43 -05:00
|
|
|
codec := json2.NewCodec()
|
2016-02-16 21:50:36 -05:00
|
|
|
rpc.RegisterCodec(codec, "application/json")
|
|
|
|
rpc.RegisterCodec(codec, "application/json; charset=UTF-8")
|
|
|
|
rpc.RegisterService(w, "Web")
|
2016-01-30 21:33:33 -05:00
|
|
|
|
2016-02-16 21:50:36 -05:00
|
|
|
// RPC handler at URI - /minio/rpc
|
|
|
|
minio.Path("/rpc").Handler(rpc)
|
|
|
|
|
|
|
|
// Web handler assets at URI - /minio/login
|
|
|
|
minio.Path("/login").Handler(http.StripPrefix(filepath.Join(privateBucket, "login"), http.FileServer(assetFS())))
|
|
|
|
minio.Path("/{file:.*}").Handler(http.StripPrefix(privateBucket, http.FileServer(assetFS())))
|
|
|
|
|
|
|
|
// API Router
|
|
|
|
api := mux.NewRoute().PathPrefix("/").Subrouter()
|
2016-01-21 19:28:15 -05:00
|
|
|
|
2015-11-07 17:15:22 -05:00
|
|
|
// Bucket router
|
2016-02-16 21:50:36 -05:00
|
|
|
bucket := api.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
|
2016-02-16 21:50:36 -05:00
|
|
|
api.Methods("GET").HandlerFunc(a.ListBucketsHandler)
|
2015-07-01 03:37:43 -04:00
|
|
|
}
|
|
|
|
|
2016-02-18 05:13:52 -05:00
|
|
|
// initWeb instantiate a new Web.
|
|
|
|
func initWeb(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-02-18 05:13:52 -05:00
|
|
|
w := &webAPI{
|
2016-01-27 04:52:54 -05:00
|
|
|
FSPath: conf.Path,
|
|
|
|
AccessLog: conf.AccessLog,
|
|
|
|
Client: client,
|
|
|
|
apiAddress: conf.Address,
|
|
|
|
accessKeyID: conf.AccessKeyID,
|
|
|
|
secretAccessKey: conf.SecretAccessKey,
|
2016-01-21 19:28:15 -05:00
|
|
|
}
|
2016-02-16 21:50:36 -05:00
|
|
|
return w
|
2016-01-21 19:28:15 -05:00
|
|
|
}
|
|
|
|
|
2016-02-18 05:13:52 -05:00
|
|
|
// initAPI instantiate a new StorageAPI.
|
|
|
|
func initAPI(conf cloudServerConfig) storageAPI {
|
2016-02-06 21:22:50 -05:00
|
|
|
fs, err := fs.New(conf.Path, conf.MinFreeDisk)
|
2015-11-07 17:15:22 -05:00
|
|
|
fatalIf(err.Trace(), "Initializing filesystem failed.", nil)
|
2015-09-19 05:36:50 -04:00
|
|
|
|
2016-02-15 20:42:39 -05:00
|
|
|
sign, err := signV4.New(conf.AccessKeyID, conf.SecretAccessKey, conf.Region)
|
|
|
|
fatalIf(err.Trace(conf.AccessKeyID, conf.SecretAccessKey, conf.Region), "Initializing signature version '4' failed.", nil)
|
|
|
|
|
2016-02-18 05:13:52 -05:00
|
|
|
return storageAPI{
|
2015-10-19 15:15:19 -04:00
|
|
|
AccessLog: conf.AccessLog,
|
2016-02-15 20:42:39 -05:00
|
|
|
Filesystem: fs,
|
|
|
|
Signature: sign,
|
|
|
|
Region: conf.Region,
|
2015-09-19 05:36:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-18 05:13:52 -05:00
|
|
|
// server handler returns final handler before initializing server.
|
|
|
|
func serverHandler(conf cloudServerConfig) http.Handler {
|
|
|
|
// Initialize API.
|
|
|
|
api := initAPI(conf)
|
|
|
|
|
|
|
|
// Initialize Web.
|
|
|
|
web := initWeb(conf)
|
|
|
|
|
2016-02-04 17:57:20 -05:00
|
|
|
var handlerFns = []HandlerFunc{
|
2016-02-16 21:50:36 -05:00
|
|
|
// Redirect some pre-defined browser request paths to a static
|
|
|
|
// location prefix.
|
|
|
|
setBrowserRedirectHandler,
|
|
|
|
// Validates if incoming request is for restricted buckets.
|
|
|
|
setPrivateBucketHandler,
|
|
|
|
// Adds cache control for all browser requests.
|
|
|
|
setBrowserCacheControlHandler,
|
|
|
|
// Validates all incoming requests to have a valid date header.
|
2016-02-04 17:57:20 -05:00
|
|
|
setTimeValidityHandler,
|
2016-02-16 21:50:36 -05:00
|
|
|
// CORS setting for all browser API requests.
|
|
|
|
setCorsHandler,
|
|
|
|
// Validates all incoming URL resources, for invalid/unsupported
|
|
|
|
// resources client receives a HTTP error.
|
2016-02-04 17:57:20 -05:00
|
|
|
setIgnoreResourcesHandler,
|
2016-02-16 21:50:36 -05:00
|
|
|
// Auth handler verifies incoming authorization headers and
|
|
|
|
// routes them accordingly. Client receives a HTTP error for
|
|
|
|
// invalid/unsupported signatures.
|
2016-02-15 20:42:39 -05:00
|
|
|
setAuthHandler,
|
2015-10-07 02:32:20 -04:00
|
|
|
}
|
2016-02-18 05:13:52 -05:00
|
|
|
|
|
|
|
// Initialize router.
|
2015-06-30 23:15:48 -04:00
|
|
|
mux := router.NewRouter()
|
2016-02-18 05:13:52 -05:00
|
|
|
|
|
|
|
// Register all API handlers.
|
|
|
|
registerAPIHandlers(mux, api, web)
|
|
|
|
|
|
|
|
// Register rest of the handlers.
|
2016-02-04 17:57:20 -05:00
|
|
|
return registerHandlers(mux, handlerFns...)
|
2015-06-30 23:15:48 -04:00
|
|
|
}
|