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-03-04 01:39:19 +01:00
|
|
|
"fmt"
|
2016-01-25 07:26:53 +01:00
|
|
|
"net"
|
2015-07-01 05:15:48 +02:00
|
|
|
"net/http"
|
|
|
|
|
2016-02-23 22:05:47 +01:00
|
|
|
"github.com/elazarl/go-bindata-assetfs"
|
2016-03-04 03:15:56 +01:00
|
|
|
"github.com/gorilla/handlers"
|
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"
|
2016-02-11 19:11:43 +01:00
|
|
|
"github.com/gorilla/rpc/v2/json2"
|
2016-01-25 07:26:53 +01:00
|
|
|
"github.com/minio/minio-go"
|
2015-10-16 20:26:01 +02:00
|
|
|
"github.com/minio/minio/pkg/fs"
|
2016-02-11 01:40:09 +01:00
|
|
|
"github.com/minio/minio/pkg/probe"
|
2016-02-22 02:57:05 +01:00
|
|
|
"github.com/minio/minio/pkg/s3/signature4"
|
2016-02-23 22:05:47 +01:00
|
|
|
"github.com/minio/miniobrowser"
|
2015-07-01 05:15:48 +02:00
|
|
|
)
|
|
|
|
|
2016-02-18 11:13:52 +01:00
|
|
|
// storageAPI container for S3 compatible API.
|
|
|
|
type storageAPI struct {
|
2016-01-25 07:26:53 +01:00
|
|
|
// Once true log all incoming requests.
|
|
|
|
AccessLog bool
|
|
|
|
// Filesystem instance.
|
2015-10-19 21:15:19 +02:00
|
|
|
Filesystem fs.Filesystem
|
2016-02-16 02:42:39 +01:00
|
|
|
// Signature instance.
|
2016-02-22 02:57:05 +01:00
|
|
|
Signature *signature4.Sign
|
2016-02-16 02:42:39 +01:00
|
|
|
// Region instance.
|
|
|
|
Region string
|
2015-10-19 21:15:19 +02:00
|
|
|
}
|
|
|
|
|
2016-02-18 11:13:52 +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.
|
2016-03-10 23:22:28 +01:00
|
|
|
Client *minio.Client
|
2016-01-27 10:52:54 +01:00
|
|
|
|
|
|
|
// private params.
|
|
|
|
apiAddress string // api destination address.
|
|
|
|
// accessKeys kept to be used internally.
|
|
|
|
accessKeyID string
|
|
|
|
secretAccessKey string
|
2016-01-22 01:28:15 +01:00
|
|
|
}
|
|
|
|
|
2016-02-23 19:37:27 +01:00
|
|
|
// indexHandler - Handler to serve index.html
|
|
|
|
type indexHandler struct {
|
|
|
|
handler http.Handler
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.URL.Path = privateBucket + "/"
|
|
|
|
h.handler.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
2016-02-23 22:05:47 +01:00
|
|
|
const assetPrefix = "production"
|
|
|
|
|
|
|
|
func assetFS() *assetfs.AssetFS {
|
|
|
|
return &assetfs.AssetFS{
|
|
|
|
Asset: miniobrowser.Asset,
|
|
|
|
AssetDir: miniobrowser.AssetDir,
|
|
|
|
AssetInfo: miniobrowser.AssetInfo,
|
|
|
|
Prefix: assetPrefix,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-04 01:39:19 +01:00
|
|
|
// specialAssets are files which are unique files not embedded inside index_bundle.js.
|
|
|
|
const specialAssets = "loader.css|logo.svg|firefox.png|safari.png|chrome.png|favicon.ico"
|
|
|
|
|
2016-02-18 11:13:52 +01:00
|
|
|
// registerAPIHandlers - register all the handlers to their respective paths
|
|
|
|
func registerAPIHandlers(mux *router.Router, a storageAPI, w *webAPI) {
|
2016-02-17 03:50:36 +01:00
|
|
|
// Minio rpc router
|
|
|
|
minio := mux.NewRoute().PathPrefix(privateBucket).Subrouter()
|
2016-01-24 04:44:32 +01:00
|
|
|
|
2016-02-17 03:50:36 +01:00
|
|
|
// Initialize json rpc handlers.
|
|
|
|
rpc := jsonrpc.NewServer()
|
2016-02-11 19:11:43 +01:00
|
|
|
codec := json2.NewCodec()
|
2016-02-17 03:50:36 +01:00
|
|
|
rpc.RegisterCodec(codec, "application/json")
|
|
|
|
rpc.RegisterCodec(codec, "application/json; charset=UTF-8")
|
|
|
|
rpc.RegisterService(w, "Web")
|
2016-01-31 03:33:33 +01:00
|
|
|
|
2016-02-17 03:50:36 +01:00
|
|
|
// RPC handler at URI - /minio/rpc
|
|
|
|
minio.Path("/rpc").Handler(rpc)
|
2016-03-04 01:39:19 +01:00
|
|
|
// Serve all assets.
|
2016-03-04 03:15:56 +01:00
|
|
|
minio.Path(fmt.Sprintf("/{assets:[^/]+.js|%s}", specialAssets)).Handler(handlers.CompressHandler(http.StripPrefix(privateBucket, http.FileServer(assetFS()))))
|
2016-02-23 19:37:27 +01:00
|
|
|
// Serve index.html for rest of the requests
|
|
|
|
minio.Path("/{index:.*}").Handler(indexHandler{http.StripPrefix(privateBucket, http.FileServer(assetFS()))})
|
2016-02-17 03:50:36 +01:00
|
|
|
|
|
|
|
// API Router
|
|
|
|
api := mux.NewRoute().PathPrefix("/").Subrouter()
|
2016-01-22 01:28:15 +01:00
|
|
|
|
2015-11-07 23:15:22 +01:00
|
|
|
// Bucket router
|
2016-02-17 03:50:36 +01:00
|
|
|
bucket := api.PathPrefix("/{bucket}").Subrouter()
|
2015-07-01 05:15:48 +02:00
|
|
|
|
2016-02-27 12:04:52 +01:00
|
|
|
/// Object operations
|
|
|
|
|
|
|
|
// HeadObject
|
2015-10-25 16:00:39 +01:00
|
|
|
bucket.Methods("HEAD").Path("/{object:.+}").HandlerFunc(a.HeadObjectHandler)
|
2016-02-27 12:04:52 +01:00
|
|
|
// PutObjectPart
|
2015-10-25 16:00:39 +01:00
|
|
|
bucket.Methods("PUT").Path("/{object:.+}").HandlerFunc(a.PutObjectPartHandler).Queries("partNumber", "{partNumber:[0-9]+}", "uploadId", "{uploadId:.*}")
|
2016-02-27 12:04:52 +01:00
|
|
|
// ListObjectPxarts
|
2015-10-25 16:00:39 +01:00
|
|
|
bucket.Methods("GET").Path("/{object:.+}").HandlerFunc(a.ListObjectPartsHandler).Queries("uploadId", "{uploadId:.*}")
|
2016-02-27 12:04:52 +01:00
|
|
|
// CompleteMultipartUpload
|
2015-10-25 16:00:39 +01:00
|
|
|
bucket.Methods("POST").Path("/{object:.+}").HandlerFunc(a.CompleteMultipartUploadHandler).Queries("uploadId", "{uploadId:.*}")
|
2016-02-27 12:04:52 +01:00
|
|
|
// NewMultipartUpload
|
2015-10-25 16:00:39 +01:00
|
|
|
bucket.Methods("POST").Path("/{object:.+}").HandlerFunc(a.NewMultipartUploadHandler).Queries("uploads", "")
|
2016-02-27 12:04:52 +01:00
|
|
|
// AbortMultipartUpload
|
2015-10-25 16:00:39 +01:00
|
|
|
bucket.Methods("DELETE").Path("/{object:.+}").HandlerFunc(a.AbortMultipartUploadHandler).Queries("uploadId", "{uploadId:.*}")
|
2016-02-27 12:04:52 +01:00
|
|
|
// GetObject
|
2015-10-25 16:00:39 +01:00
|
|
|
bucket.Methods("GET").Path("/{object:.+}").HandlerFunc(a.GetObjectHandler)
|
2016-02-27 12:04:52 +01:00
|
|
|
// CopyObject
|
|
|
|
bucket.Methods("PUT").Path("/{object:.+}").HeadersRegexp("X-Amz-Copy-Source", ".*?(\\/).*?").HandlerFunc(a.CopyObjectHandler)
|
|
|
|
// PutObject
|
2015-10-25 16:00:39 +01:00
|
|
|
bucket.Methods("PUT").Path("/{object:.+}").HandlerFunc(a.PutObjectHandler)
|
2016-02-27 12:04:52 +01:00
|
|
|
// DeleteObject
|
2015-10-25 16:00:39 +01:00
|
|
|
bucket.Methods("DELETE").Path("/{object:.+}").HandlerFunc(a.DeleteObjectHandler)
|
|
|
|
|
2016-02-27 12:04:52 +01:00
|
|
|
/// Bucket operations
|
|
|
|
|
|
|
|
// GetBucketLocation
|
2015-12-27 08:38:38 +01:00
|
|
|
bucket.Methods("GET").HandlerFunc(a.GetBucketLocationHandler).Queries("location", "")
|
accessPolicy: Implement Put, Get, Delete access policy.
This patch implements Get,Put,Delete bucket policies
Supporting - http://docs.aws.amazon.com/AmazonS3/latest/dev/access-policy-language-overview.html
Currently supports following actions.
"*": true,
"s3:*": true,
"s3:GetObject": true,
"s3:ListBucket": true,
"s3:PutObject": true,
"s3:CreateBucket": true,
"s3:GetBucketLocation": true,
"s3:DeleteBucket": true,
"s3:DeleteObject": true,
"s3:AbortMultipartUpload": true,
"s3:ListBucketMultipartUploads": true,
"s3:ListMultipartUploadParts": true,
following conditions for "StringEquals" and "StringNotEquals"
"s3:prefix", "s3:max-keys"
2016-02-04 01:46:56 +01:00
|
|
|
// GetBucketPolicy
|
|
|
|
bucket.Methods("GET").HandlerFunc(a.GetBucketPolicyHandler).Queries("policy", "")
|
2016-02-27 12:04:52 +01:00
|
|
|
// ListMultipartUploads
|
2015-10-25 16:00:39 +01:00
|
|
|
bucket.Methods("GET").HandlerFunc(a.ListMultipartUploadsHandler).Queries("uploads", "")
|
2016-02-27 12:04:52 +01:00
|
|
|
// ListObjects
|
2015-10-25 16:00:39 +01:00
|
|
|
bucket.Methods("GET").HandlerFunc(a.ListObjectsHandler)
|
accessPolicy: Implement Put, Get, Delete access policy.
This patch implements Get,Put,Delete bucket policies
Supporting - http://docs.aws.amazon.com/AmazonS3/latest/dev/access-policy-language-overview.html
Currently supports following actions.
"*": true,
"s3:*": true,
"s3:GetObject": true,
"s3:ListBucket": true,
"s3:PutObject": true,
"s3:CreateBucket": true,
"s3:GetBucketLocation": true,
"s3:DeleteBucket": true,
"s3:DeleteObject": true,
"s3:AbortMultipartUpload": true,
"s3:ListBucketMultipartUploads": true,
"s3:ListMultipartUploadParts": true,
following conditions for "StringEquals" and "StringNotEquals"
"s3:prefix", "s3:max-keys"
2016-02-04 01:46:56 +01:00
|
|
|
// PutBucketPolicy
|
|
|
|
bucket.Methods("PUT").HandlerFunc(a.PutBucketPolicyHandler).Queries("policy", "")
|
2016-02-27 12:04:52 +01:00
|
|
|
// PutBucket
|
2015-10-25 16:00:39 +01:00
|
|
|
bucket.Methods("PUT").HandlerFunc(a.PutBucketHandler)
|
2016-02-27 12:04:52 +01:00
|
|
|
// HeadBucket
|
2015-10-25 16:00:39 +01:00
|
|
|
bucket.Methods("HEAD").HandlerFunc(a.HeadBucketHandler)
|
2016-03-06 01:43:48 +01:00
|
|
|
// DeleteMultipleObjects
|
|
|
|
bucket.Methods("POST").HandlerFunc(a.DeleteMultipleObjectsHandler)
|
2016-02-27 12:04:52 +01:00
|
|
|
// PostPolicy
|
accessPolicy: Implement Put, Get, Delete access policy.
This patch implements Get,Put,Delete bucket policies
Supporting - http://docs.aws.amazon.com/AmazonS3/latest/dev/access-policy-language-overview.html
Currently supports following actions.
"*": true,
"s3:*": true,
"s3:GetObject": true,
"s3:ListBucket": true,
"s3:PutObject": true,
"s3:CreateBucket": true,
"s3:GetBucketLocation": true,
"s3:DeleteBucket": true,
"s3:DeleteObject": true,
"s3:AbortMultipartUpload": true,
"s3:ListBucketMultipartUploads": true,
"s3:ListMultipartUploadParts": true,
following conditions for "StringEquals" and "StringNotEquals"
"s3:prefix", "s3:max-keys"
2016-02-04 01:46:56 +01:00
|
|
|
bucket.Methods("POST").Headers("Content-Type", "multipart/form-data").HandlerFunc(a.PostPolicyBucketHandler)
|
|
|
|
// DeleteBucketPolicy
|
|
|
|
bucket.Methods("DELETE").HandlerFunc(a.DeleteBucketPolicyHandler).Queries("policy", "")
|
2016-02-27 12:04:52 +01:00
|
|
|
// DeleteBucket
|
2015-10-25 16:00:39 +01:00
|
|
|
bucket.Methods("DELETE").HandlerFunc(a.DeleteBucketHandler)
|
|
|
|
|
2016-02-27 12:04:52 +01:00
|
|
|
/// Root operation
|
|
|
|
|
|
|
|
// ListBuckets
|
2016-02-17 03:50:36 +01:00
|
|
|
api.Methods("GET").HandlerFunc(a.ListBucketsHandler)
|
2015-07-01 09:37:43 +02:00
|
|
|
}
|
|
|
|
|
2016-02-18 11:13:52 +01:00
|
|
|
// initWeb instantiate a new Web.
|
|
|
|
func initWeb(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-02-18 11:13:52 +01:00
|
|
|
w := &webAPI{
|
2016-01-27 10:52:54 +01:00
|
|
|
FSPath: conf.Path,
|
|
|
|
AccessLog: conf.AccessLog,
|
|
|
|
Client: client,
|
|
|
|
apiAddress: conf.Address,
|
|
|
|
accessKeyID: conf.AccessKeyID,
|
|
|
|
secretAccessKey: conf.SecretAccessKey,
|
2016-01-22 01:28:15 +01:00
|
|
|
}
|
2016-02-17 03:50:36 +01:00
|
|
|
return w
|
2016-01-22 01:28:15 +01:00
|
|
|
}
|
|
|
|
|
2016-02-18 11:13:52 +01:00
|
|
|
// initAPI instantiate a new StorageAPI.
|
|
|
|
func initAPI(conf cloudServerConfig) storageAPI {
|
2016-02-07 03:22:50 +01:00
|
|
|
fs, err := fs.New(conf.Path, conf.MinFreeDisk)
|
2015-11-07 23:15:22 +01:00
|
|
|
fatalIf(err.Trace(), "Initializing filesystem failed.", nil)
|
2015-09-19 11:36:50 +02:00
|
|
|
|
2016-02-22 02:57:05 +01:00
|
|
|
sign, err := signature4.New(conf.AccessKeyID, conf.SecretAccessKey, conf.Region)
|
2016-02-16 02:42:39 +01:00
|
|
|
fatalIf(err.Trace(conf.AccessKeyID, conf.SecretAccessKey, conf.Region), "Initializing signature version '4' failed.", nil)
|
|
|
|
|
2016-02-18 11:13:52 +01:00
|
|
|
return storageAPI{
|
2015-10-19 21:15:19 +02:00
|
|
|
AccessLog: conf.AccessLog,
|
2016-02-16 02:42:39 +01:00
|
|
|
Filesystem: fs,
|
|
|
|
Signature: sign,
|
|
|
|
Region: conf.Region,
|
2015-09-19 11:36:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-18 11:13:52 +01: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 23:57:20 +01:00
|
|
|
var handlerFns = []HandlerFunc{
|
2016-02-17 03:50:36 +01: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 23:57:20 +01:00
|
|
|
setTimeValidityHandler,
|
2016-02-17 03:50:36 +01: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 23:57:20 +01:00
|
|
|
setIgnoreResourcesHandler,
|
2016-02-17 03:50:36 +01:00
|
|
|
// Auth handler verifies incoming authorization headers and
|
|
|
|
// routes them accordingly. Client receives a HTTP error for
|
|
|
|
// invalid/unsupported signatures.
|
2016-02-16 02:42:39 +01:00
|
|
|
setAuthHandler,
|
2015-10-07 08:32:20 +02:00
|
|
|
}
|
2016-02-18 11:13:52 +01:00
|
|
|
|
|
|
|
// Initialize router.
|
2015-07-01 05:15:48 +02:00
|
|
|
mux := router.NewRouter()
|
2016-02-18 11:13:52 +01:00
|
|
|
|
|
|
|
// Register all API handlers.
|
|
|
|
registerAPIHandlers(mux, api, web)
|
|
|
|
|
|
|
|
// Register rest of the handlers.
|
2016-02-04 23:57:20 +01:00
|
|
|
return registerHandlers(mux, handlerFns...)
|
2015-07-01 05:15:48 +02:00
|
|
|
}
|