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.
407 lines
13 KiB
407 lines
13 KiB
10 years ago
|
/*
|
||
10 years ago
|
* Minio Cloud Storage, (C) 2015 Minio, Inc.
|
||
10 years ago
|
*
|
||
|
* 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.
|
||
|
*/
|
||
|
|
||
9 years ago
|
package main
|
||
10 years ago
|
|
||
|
import (
|
||
9 years ago
|
"encoding/hex"
|
||
|
"io/ioutil"
|
||
10 years ago
|
"net/http"
|
||
|
|
||
9 years ago
|
"github.com/gorilla/mux"
|
||
9 years ago
|
"github.com/minio/minio/pkg/fs"
|
||
|
"github.com/minio/minio-xl/pkg/crypto/sha256"
|
||
|
"github.com/minio/minio-xl/pkg/probe"
|
||
10 years ago
|
)
|
||
|
|
||
10 years ago
|
// ListMultipartUploadsHandler - GET Bucket (List Multipart uploads)
|
||
10 years ago
|
// -------------------------
|
||
|
// This operation lists in-progress multipart uploads. An in-progress
|
||
|
// multipart upload is a multipart upload that has been initiated,
|
||
|
// using the Initiate Multipart Upload request, but has not yet been completed or aborted.
|
||
|
// This operation returns at most 1,000 multipart uploads in the response.
|
||
|
//
|
||
9 years ago
|
func (api API) ListMultipartUploadsHandler(w http.ResponseWriter, req *http.Request) {
|
||
10 years ago
|
resources := getBucketMultipartResources(req.URL.Query())
|
||
10 years ago
|
if resources.MaxUploads < 0 {
|
||
9 years ago
|
writeErrorResponse(w, req, InvalidMaxUploads, req.URL.Path)
|
||
10 years ago
|
return
|
||
|
}
|
||
10 years ago
|
if resources.MaxUploads == 0 {
|
||
|
resources.MaxUploads = maxObjectList
|
||
|
}
|
||
|
|
||
|
vars := mux.Vars(req)
|
||
|
bucket := vars["bucket"]
|
||
10 years ago
|
|
||
9 years ago
|
resources, err := api.Filesystem.ListMultipartUploads(bucket, resources)
|
||
9 years ago
|
if err != nil {
|
||
|
errorIf(err.Trace(), "ListMultipartUploads failed.", nil)
|
||
|
switch err.ToGoError().(type) {
|
||
9 years ago
|
case fs.BucketNotFound:
|
||
9 years ago
|
writeErrorResponse(w, req, NoSuchBucket, req.URL.Path)
|
||
9 years ago
|
default:
|
||
9 years ago
|
writeErrorResponse(w, req, InternalError, req.URL.Path)
|
||
9 years ago
|
}
|
||
10 years ago
|
return
|
||
|
}
|
||
9 years ago
|
// generate response
|
||
|
response := generateListMultipartUploadsResponse(bucket, resources)
|
||
9 years ago
|
encodedSuccessResponse := encodeSuccessResponse(response)
|
||
9 years ago
|
// write headers
|
||
9 years ago
|
setCommonHeaders(w, len(encodedSuccessResponse))
|
||
9 years ago
|
// write body
|
||
|
w.Write(encodedSuccessResponse)
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// ListObjectsHandler - GET Bucket (List Objects)
|
||
10 years ago
|
// -------------------------
|
||
|
// This implementation of the GET operation returns some or all (up to 1000)
|
||
|
// of the objects in a bucket. You can use the request parameters as selection
|
||
|
// criteria to return a subset of the objects in a bucket.
|
||
|
//
|
||
9 years ago
|
func (api API) ListObjectsHandler(w http.ResponseWriter, req *http.Request) {
|
||
10 years ago
|
if isRequestUploads(req.URL.Query()) {
|
||
10 years ago
|
api.ListMultipartUploadsHandler(w, req)
|
||
10 years ago
|
return
|
||
|
}
|
||
|
|
||
|
resources := getBucketResources(req.URL.Query())
|
||
10 years ago
|
if resources.Maxkeys < 0 {
|
||
9 years ago
|
writeErrorResponse(w, req, InvalidMaxKeys, req.URL.Path)
|
||
10 years ago
|
return
|
||
|
}
|
||
10 years ago
|
if resources.Maxkeys == 0 {
|
||
|
resources.Maxkeys = maxObjectList
|
||
|
}
|
||
|
|
||
|
vars := mux.Vars(req)
|
||
|
bucket := vars["bucket"]
|
||
|
|
||
9 years ago
|
objects, resources, err := api.Filesystem.ListObjects(bucket, resources)
|
||
10 years ago
|
if err == nil {
|
||
10 years ago
|
// generate response
|
||
|
response := generateListObjectsResponse(bucket, objects, resources)
|
||
9 years ago
|
encodedSuccessResponse := encodeSuccessResponse(response)
|
||
10 years ago
|
// write headers
|
||
9 years ago
|
setCommonHeaders(w, len(encodedSuccessResponse))
|
||
10 years ago
|
// write body
|
||
|
w.Write(encodedSuccessResponse)
|
||
10 years ago
|
return
|
||
|
}
|
||
9 years ago
|
switch err.ToGoError().(type) {
|
||
9 years ago
|
case fs.BucketNameInvalid:
|
||
9 years ago
|
writeErrorResponse(w, req, InvalidBucketName, req.URL.Path)
|
||
9 years ago
|
case fs.BucketNotFound:
|
||
9 years ago
|
writeErrorResponse(w, req, NoSuchBucket, req.URL.Path)
|
||
9 years ago
|
case fs.ObjectNotFound:
|
||
9 years ago
|
writeErrorResponse(w, req, NoSuchKey, req.URL.Path)
|
||
9 years ago
|
case fs.ObjectNameInvalid:
|
||
9 years ago
|
writeErrorResponse(w, req, NoSuchKey, req.URL.Path)
|
||
10 years ago
|
default:
|
||
9 years ago
|
errorIf(err.Trace(), "ListObjects failed.", nil)
|
||
9 years ago
|
writeErrorResponse(w, req, InternalError, req.URL.Path)
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// ListBucketsHandler - GET Service
|
||
10 years ago
|
// -----------
|
||
|
// This implementation of the GET operation returns a list of all buckets
|
||
|
// owned by the authenticated sender of the request.
|
||
9 years ago
|
func (api API) ListBucketsHandler(w http.ResponseWriter, req *http.Request) {
|
||
9 years ago
|
buckets, err := api.Filesystem.ListBuckets()
|
||
10 years ago
|
if err == nil {
|
||
10 years ago
|
// generate response
|
||
|
response := generateListBucketsResponse(buckets)
|
||
9 years ago
|
encodedSuccessResponse := encodeSuccessResponse(response)
|
||
10 years ago
|
// write headers
|
||
9 years ago
|
setCommonHeaders(w, len(encodedSuccessResponse))
|
||
10 years ago
|
// write response
|
||
|
w.Write(encodedSuccessResponse)
|
||
10 years ago
|
return
|
||
|
}
|
||
9 years ago
|
errorIf(err.Trace(), "ListBuckets failed.", nil)
|
||
|
writeErrorResponse(w, req, InternalError, req.URL.Path)
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// PutBucketHandler - PUT Bucket
|
||
10 years ago
|
// ----------
|
||
|
// This implementation of the PUT operation creates a new bucket for authenticated request
|
||
9 years ago
|
func (api API) PutBucketHandler(w http.ResponseWriter, req *http.Request) {
|
||
9 years ago
|
if _, err := stripAccessKeyID(req.Header.Get("Authorization")); err != nil {
|
||
|
writeErrorResponse(w, req, AccessDenied, req.URL.Path)
|
||
10 years ago
|
return
|
||
|
}
|
||
9 years ago
|
|
||
10 years ago
|
// read from 'x-amz-acl'
|
||
|
aclType := getACLType(req)
|
||
|
if aclType == unsupportedACLType {
|
||
9 years ago
|
writeErrorResponse(w, req, NotImplemented, req.URL.Path)
|
||
10 years ago
|
return
|
||
|
}
|
||
|
|
||
|
vars := mux.Vars(req)
|
||
|
bucket := vars["bucket"]
|
||
10 years ago
|
|
||
9 years ago
|
var signature *fs.Signature
|
||
9 years ago
|
if !api.Anonymous {
|
||
|
if _, ok := req.Header["Authorization"]; ok {
|
||
|
// Init signature V4 verification
|
||
|
var err *probe.Error
|
||
|
signature, err = initSignatureV4(req)
|
||
|
if err != nil {
|
||
|
errorIf(err.Trace(), "Initializing signature v4 failed.", nil)
|
||
|
writeErrorResponse(w, req, InternalError, req.URL.Path)
|
||
|
return
|
||
|
}
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
// if body of request is non-nil then check for validity of Content-Length
|
||
|
if req.Body != nil {
|
||
|
/// if Content-Length missing, deny the request
|
||
9 years ago
|
if req.Header.Get("Content-Length") == "" {
|
||
9 years ago
|
writeErrorResponse(w, req, MissingContentLength, req.URL.Path)
|
||
10 years ago
|
return
|
||
|
}
|
||
9 years ago
|
if signature != nil {
|
||
|
locationBytes, err := ioutil.ReadAll(req.Body)
|
||
|
if err != nil {
|
||
|
sh := sha256.New()
|
||
|
sh.Write(locationBytes)
|
||
|
ok, perr := signature.DoesSignatureMatch(hex.EncodeToString(sh.Sum(nil)))
|
||
|
if perr != nil {
|
||
|
errorIf(perr.Trace(), "MakeBucket failed.", nil)
|
||
|
writeErrorResponse(w, req, InternalError, req.URL.Path)
|
||
|
return
|
||
|
}
|
||
|
if !ok {
|
||
|
writeErrorResponse(w, req, SignatureDoesNotMatch, req.URL.Path)
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
err := api.Filesystem.MakeBucket(bucket, getACLTypeString(aclType))
|
||
9 years ago
|
if err != nil {
|
||
|
errorIf(err.Trace(), "MakeBucket failed.", nil)
|
||
|
switch err.ToGoError().(type) {
|
||
9 years ago
|
case fs.BucketNameInvalid:
|
||
9 years ago
|
writeErrorResponse(w, req, InvalidBucketName, req.URL.Path)
|
||
9 years ago
|
case fs.BucketExists:
|
||
9 years ago
|
writeErrorResponse(w, req, BucketAlreadyExists, req.URL.Path)
|
||
9 years ago
|
default:
|
||
9 years ago
|
writeErrorResponse(w, req, InternalError, req.URL.Path)
|
||
9 years ago
|
}
|
||
10 years ago
|
return
|
||
|
}
|
||
9 years ago
|
// Make sure to add Location information here only for bucket
|
||
|
w.Header().Set("Location", "/"+bucket)
|
||
9 years ago
|
writeSuccessResponse(w)
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
// PostPolicyBucketHandler - POST policy
|
||
|
// ----------
|
||
|
// This implementation of the POST operation handles object creation with a specified
|
||
|
// signature policy in multipart/form-data
|
||
|
func (api API) PostPolicyBucketHandler(w http.ResponseWriter, req *http.Request) {
|
||
9 years ago
|
// if body of request is non-nil then check for validity of Content-Length
|
||
|
if req.Body != nil {
|
||
|
/// if Content-Length missing, deny the request
|
||
|
size := req.Header.Get("Content-Length")
|
||
|
if size == "" {
|
||
|
writeErrorResponse(w, req, MissingContentLength, req.URL.Path)
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
// Here the parameter is the size of the form data that should
|
||
|
// be loaded in memory, the remaining being put in temporary
|
||
|
// files
|
||
|
reader, err := req.MultipartReader()
|
||
|
if err != nil {
|
||
|
errorIf(probe.NewError(err), "Unable to initialize multipart reader.", nil)
|
||
9 years ago
|
writeErrorResponse(w, req, MalformedPOSTRequest, req.URL.Path)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
|
fileBody, formValues, perr := extractHTTPFormValues(reader)
|
||
|
if perr != nil {
|
||
|
errorIf(perr.Trace(), "Unable to parse form values.", nil)
|
||
9 years ago
|
writeErrorResponse(w, req, MalformedPOSTRequest, req.URL.Path)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
bucket := mux.Vars(req)["bucket"]
|
||
|
formValues["Bucket"] = bucket
|
||
9 years ago
|
object := formValues["Key"]
|
||
9 years ago
|
signature, perr := initPostPresignedPolicyV4(formValues)
|
||
|
if perr != nil {
|
||
|
errorIf(perr.Trace(), "Unable to initialize post policy presigned.", nil)
|
||
9 years ago
|
writeErrorResponse(w, req, MalformedPOSTRequest, req.URL.Path)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
var ok bool
|
||
|
if ok, perr = signature.DoesPolicySignatureMatch(formValues["X-Amz-Date"]); perr != nil {
|
||
|
errorIf(perr.Trace(), "Unable to verify signature.", nil)
|
||
9 years ago
|
writeErrorResponse(w, req, SignatureDoesNotMatch, req.URL.Path)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
if ok == false {
|
||
9 years ago
|
writeErrorResponse(w, req, SignatureDoesNotMatch, req.URL.Path)
|
||
9 years ago
|
return
|
||
|
}
|
||
9 years ago
|
if perr = applyPolicy(formValues); perr != nil {
|
||
|
errorIf(perr.Trace(), "Invalid request, policy doesn't match with the endpoint.", nil)
|
||
|
writeErrorResponse(w, req, MalformedPOSTRequest, req.URL.Path)
|
||
|
return
|
||
|
}
|
||
9 years ago
|
metadata, perr := api.Filesystem.CreateObject(bucket, object, "", 0, fileBody, nil)
|
||
9 years ago
|
if perr != nil {
|
||
|
errorIf(perr.Trace(), "CreateObject failed.", nil)
|
||
|
switch perr.ToGoError().(type) {
|
||
9 years ago
|
case fs.BucketNotFound:
|
||
9 years ago
|
writeErrorResponse(w, req, NoSuchBucket, req.URL.Path)
|
||
9 years ago
|
case fs.BucketNameInvalid:
|
||
9 years ago
|
writeErrorResponse(w, req, InvalidBucketName, req.URL.Path)
|
||
9 years ago
|
case fs.BadDigest:
|
||
9 years ago
|
writeErrorResponse(w, req, BadDigest, req.URL.Path)
|
||
9 years ago
|
case fs.SignatureDoesNotMatch:
|
||
9 years ago
|
writeErrorResponse(w, req, SignatureDoesNotMatch, req.URL.Path)
|
||
9 years ago
|
case fs.IncompleteBody:
|
||
9 years ago
|
writeErrorResponse(w, req, IncompleteBody, req.URL.Path)
|
||
9 years ago
|
case fs.EntityTooLarge:
|
||
9 years ago
|
writeErrorResponse(w, req, EntityTooLarge, req.URL.Path)
|
||
9 years ago
|
case fs.InvalidDigest:
|
||
9 years ago
|
writeErrorResponse(w, req, InvalidDigest, req.URL.Path)
|
||
9 years ago
|
default:
|
||
9 years ago
|
writeErrorResponse(w, req, InternalError, req.URL.Path)
|
||
9 years ago
|
}
|
||
|
return
|
||
|
}
|
||
9 years ago
|
w.Header().Set("ETag", "\""+metadata.Md5+"\"")
|
||
9 years ago
|
writeSuccessResponse(w)
|
||
9 years ago
|
}
|
||
|
|
||
10 years ago
|
// PutBucketACLHandler - PUT Bucket ACL
|
||
10 years ago
|
// ----------
|
||
|
// This implementation of the PUT operation modifies the bucketACL for authenticated request
|
||
9 years ago
|
func (api API) PutBucketACLHandler(w http.ResponseWriter, req *http.Request) {
|
||
10 years ago
|
// read from 'x-amz-acl'
|
||
|
aclType := getACLType(req)
|
||
|
if aclType == unsupportedACLType {
|
||
9 years ago
|
writeErrorResponse(w, req, NotImplemented, req.URL.Path)
|
||
10 years ago
|
return
|
||
|
}
|
||
|
|
||
|
vars := mux.Vars(req)
|
||
|
bucket := vars["bucket"]
|
||
10 years ago
|
|
||
9 years ago
|
err := api.Filesystem.SetBucketMetadata(bucket, map[string]string{"acl": getACLTypeString(aclType)})
|
||
9 years ago
|
if err != nil {
|
||
|
errorIf(err.Trace(), "PutBucketACL failed.", nil)
|
||
|
switch err.ToGoError().(type) {
|
||
9 years ago
|
case fs.BucketNameInvalid:
|
||
9 years ago
|
writeErrorResponse(w, req, InvalidBucketName, req.URL.Path)
|
||
9 years ago
|
case fs.BucketNotFound:
|
||
9 years ago
|
writeErrorResponse(w, req, NoSuchBucket, req.URL.Path)
|
||
9 years ago
|
default:
|
||
9 years ago
|
writeErrorResponse(w, req, InternalError, req.URL.Path)
|
||
9 years ago
|
}
|
||
10 years ago
|
return
|
||
|
}
|
||
9 years ago
|
writeSuccessResponse(w)
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
// GetBucketACLHandler - GET ACL on a Bucket
|
||
|
// ----------
|
||
|
// This operation uses acl subresource to the return the ``acl``
|
||
|
// of a bucket. One must have permission to access the bucket to
|
||
|
// know its ``acl``. This operation willl return response of 404
|
||
|
// if bucket not found and 403 for invalid credentials.
|
||
|
func (api API) GetBucketACLHandler(w http.ResponseWriter, req *http.Request) {
|
||
|
vars := mux.Vars(req)
|
||
|
bucket := vars["bucket"]
|
||
|
|
||
9 years ago
|
bucketMetadata, err := api.Filesystem.GetBucketMetadata(bucket)
|
||
9 years ago
|
if err != nil {
|
||
|
errorIf(err.Trace(), "GetBucketMetadata failed.", nil)
|
||
|
switch err.ToGoError().(type) {
|
||
9 years ago
|
case fs.BucketNotFound:
|
||
9 years ago
|
writeErrorResponse(w, req, NoSuchBucket, req.URL.Path)
|
||
9 years ago
|
case fs.BucketNameInvalid:
|
||
9 years ago
|
writeErrorResponse(w, req, InvalidBucketName, req.URL.Path)
|
||
|
default:
|
||
|
writeErrorResponse(w, req, InternalError, req.URL.Path)
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
// generate response
|
||
|
response := generateAccessControlPolicyResponse(bucketMetadata.ACL)
|
||
|
encodedSuccessResponse := encodeSuccessResponse(response)
|
||
|
// write headers
|
||
|
setCommonHeaders(w, len(encodedSuccessResponse))
|
||
|
// write body
|
||
|
w.Write(encodedSuccessResponse)
|
||
|
}
|
||
|
|
||
10 years ago
|
// HeadBucketHandler - HEAD Bucket
|
||
10 years ago
|
// ----------
|
||
|
// This operation is useful to determine if a bucket exists.
|
||
|
// The operation returns a 200 OK if the bucket exists and you
|
||
|
// have permission to access it. Otherwise, the operation might
|
||
|
// return responses such as 404 Not Found and 403 Forbidden.
|
||
9 years ago
|
func (api API) HeadBucketHandler(w http.ResponseWriter, req *http.Request) {
|
||
10 years ago
|
vars := mux.Vars(req)
|
||
|
bucket := vars["bucket"]
|
||
10 years ago
|
|
||
9 years ago
|
_, err := api.Filesystem.GetBucketMetadata(bucket)
|
||
9 years ago
|
if err != nil {
|
||
|
errorIf(err.Trace(), "GetBucketMetadata failed.", nil)
|
||
|
switch err.ToGoError().(type) {
|
||
9 years ago
|
case fs.BucketNotFound:
|
||
9 years ago
|
writeErrorResponse(w, req, NoSuchBucket, req.URL.Path)
|
||
9 years ago
|
case fs.BucketNameInvalid:
|
||
9 years ago
|
writeErrorResponse(w, req, InvalidBucketName, req.URL.Path)
|
||
9 years ago
|
default:
|
||
9 years ago
|
writeErrorResponse(w, req, InternalError, req.URL.Path)
|
||
9 years ago
|
}
|
||
10 years ago
|
return
|
||
|
}
|
||
9 years ago
|
writeSuccessResponse(w)
|
||
10 years ago
|
}
|
||
9 years ago
|
|
||
|
// DeleteBucketHandler - Delete bucket
|
||
|
func (api API) DeleteBucketHandler(w http.ResponseWriter, req *http.Request) {
|
||
|
vars := mux.Vars(req)
|
||
|
bucket := vars["bucket"]
|
||
|
|
||
|
err := api.Filesystem.DeleteBucket(bucket)
|
||
|
if err != nil {
|
||
|
errorIf(err.Trace(), "DeleteBucket failed.", nil)
|
||
|
switch err.ToGoError().(type) {
|
||
|
case fs.BucketNotFound:
|
||
|
writeErrorResponse(w, req, NoSuchBucket, req.URL.Path)
|
||
|
case fs.BucketNotEmpty:
|
||
|
writeErrorResponse(w, req, BucketNotEmpty, req.URL.Path)
|
||
|
default:
|
||
|
writeErrorResponse(w, req, InternalError, req.URL.Path)
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
writeSuccessResponse(w)
|
||
|
}
|