2015-06-30 23:42:29 +02:00
|
|
|
/*
|
2015-07-25 02:51:40 +02:00
|
|
|
* Minio Cloud Storage, (C) 2015 Minio, Inc.
|
2015-06-30 23:42:29 +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-06-30 23:42:29 +02:00
|
|
|
|
|
|
|
import (
|
2015-10-16 20:26:01 +02:00
|
|
|
"encoding/hex"
|
|
|
|
"io/ioutil"
|
2015-06-30 23:42:29 +02:00
|
|
|
"net/http"
|
|
|
|
|
2015-08-23 03:34:00 +02:00
|
|
|
"github.com/gorilla/mux"
|
2015-10-16 20:26:01 +02:00
|
|
|
"github.com/minio/minio-xl/pkg/crypto/sha256"
|
|
|
|
"github.com/minio/minio-xl/pkg/probe"
|
2015-10-16 22:47:44 +02:00
|
|
|
"github.com/minio/minio/pkg/fs"
|
2015-06-30 23:42:29 +02:00
|
|
|
)
|
|
|
|
|
2015-07-01 05:15:48 +02:00
|
|
|
// ListMultipartUploadsHandler - GET Bucket (List Multipart uploads)
|
2015-06-30 23:42:29 +02:00
|
|
|
// -------------------------
|
|
|
|
// This operation lists in-progress multipart uploads. An in-progress
|
|
|
|
// multipart upload is a multipart upload that has been initiated,
|
2015-10-17 04:09:35 +02:00
|
|
|
// 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.
|
2015-06-30 23:42:29 +02:00
|
|
|
//
|
2015-10-18 04:17:33 +02:00
|
|
|
func (api CloudStorageAPI) ListMultipartUploadsHandler(w http.ResponseWriter, req *http.Request) {
|
2015-10-17 04:09:35 +02:00
|
|
|
vars := mux.Vars(req)
|
|
|
|
bucket := vars["bucket"]
|
|
|
|
|
|
|
|
if !api.Anonymous {
|
|
|
|
if isRequestRequiresACLCheck(req) {
|
2015-11-14 05:07:39 +01:00
|
|
|
writeErrorResponse(w, req, AccessDenied, req.URL.Path)
|
|
|
|
return
|
2015-10-17 04:09:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-30 23:42:29 +02:00
|
|
|
resources := getBucketMultipartResources(req.URL.Query())
|
2015-07-17 02:22:45 +02:00
|
|
|
if resources.MaxUploads < 0 {
|
2015-10-04 09:27:49 +02:00
|
|
|
writeErrorResponse(w, req, InvalidMaxUploads, req.URL.Path)
|
2015-07-17 02:22:45 +02:00
|
|
|
return
|
|
|
|
}
|
2015-06-30 23:42:29 +02:00
|
|
|
if resources.MaxUploads == 0 {
|
|
|
|
resources.MaxUploads = maxObjectList
|
|
|
|
}
|
|
|
|
|
2015-10-16 20:26:01 +02:00
|
|
|
resources, err := api.Filesystem.ListMultipartUploads(bucket, resources)
|
2015-09-19 12:20:07 +02:00
|
|
|
if err != nil {
|
|
|
|
errorIf(err.Trace(), "ListMultipartUploads failed.", nil)
|
|
|
|
switch err.ToGoError().(type) {
|
2015-10-16 20:26:01 +02:00
|
|
|
case fs.BucketNotFound:
|
2015-10-04 09:27:49 +02:00
|
|
|
writeErrorResponse(w, req, NoSuchBucket, req.URL.Path)
|
2015-09-19 12:20:07 +02:00
|
|
|
default:
|
2015-10-04 09:27:49 +02:00
|
|
|
writeErrorResponse(w, req, InternalError, req.URL.Path)
|
2015-09-19 12:20:07 +02:00
|
|
|
}
|
2015-08-04 01:17:21 +02:00
|
|
|
return
|
|
|
|
}
|
2015-09-19 12:20:07 +02:00
|
|
|
// generate response
|
|
|
|
response := generateListMultipartUploadsResponse(bucket, resources)
|
2015-10-04 09:27:49 +02:00
|
|
|
encodedSuccessResponse := encodeSuccessResponse(response)
|
2015-09-19 12:20:07 +02:00
|
|
|
// write headers
|
2015-10-04 09:27:49 +02:00
|
|
|
setCommonHeaders(w, len(encodedSuccessResponse))
|
2015-09-19 12:20:07 +02:00
|
|
|
// write body
|
|
|
|
w.Write(encodedSuccessResponse)
|
2015-06-30 23:42:29 +02:00
|
|
|
}
|
|
|
|
|
2015-07-01 05:15:48 +02:00
|
|
|
// ListObjectsHandler - GET Bucket (List Objects)
|
2015-06-30 23:42:29 +02:00
|
|
|
// -------------------------
|
|
|
|
// 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.
|
|
|
|
//
|
2015-10-18 04:17:33 +02:00
|
|
|
func (api CloudStorageAPI) ListObjectsHandler(w http.ResponseWriter, req *http.Request) {
|
2015-10-17 04:09:35 +02:00
|
|
|
vars := mux.Vars(req)
|
|
|
|
bucket := vars["bucket"]
|
2015-06-30 23:42:29 +02:00
|
|
|
|
2015-10-17 04:09:35 +02:00
|
|
|
if !api.Anonymous {
|
|
|
|
if isRequestRequiresACLCheck(req) {
|
|
|
|
if api.Filesystem.IsPrivateBucket(bucket) {
|
|
|
|
writeErrorResponse(w, req, AccessDenied, req.URL.Path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-30 23:42:29 +02:00
|
|
|
resources := getBucketResources(req.URL.Query())
|
2015-07-17 02:22:45 +02:00
|
|
|
if resources.Maxkeys < 0 {
|
2015-10-04 09:27:49 +02:00
|
|
|
writeErrorResponse(w, req, InvalidMaxKeys, req.URL.Path)
|
2015-07-17 02:22:45 +02:00
|
|
|
return
|
|
|
|
}
|
2015-06-30 23:42:29 +02:00
|
|
|
if resources.Maxkeys == 0 {
|
|
|
|
resources.Maxkeys = maxObjectList
|
|
|
|
}
|
|
|
|
|
2015-10-16 20:26:01 +02:00
|
|
|
objects, resources, err := api.Filesystem.ListObjects(bucket, resources)
|
2015-08-04 01:17:21 +02:00
|
|
|
if err == nil {
|
2015-07-03 05:31:22 +02:00
|
|
|
// generate response
|
|
|
|
response := generateListObjectsResponse(bucket, objects, resources)
|
2015-10-04 09:27:49 +02:00
|
|
|
encodedSuccessResponse := encodeSuccessResponse(response)
|
2015-07-03 05:31:22 +02:00
|
|
|
// write headers
|
2015-10-04 09:27:49 +02:00
|
|
|
setCommonHeaders(w, len(encodedSuccessResponse))
|
2015-07-03 05:31:22 +02:00
|
|
|
// write body
|
|
|
|
w.Write(encodedSuccessResponse)
|
2015-08-04 01:17:21 +02:00
|
|
|
return
|
|
|
|
}
|
2015-08-19 04:30:17 +02:00
|
|
|
switch err.ToGoError().(type) {
|
2015-10-16 20:26:01 +02:00
|
|
|
case fs.BucketNameInvalid:
|
2015-10-04 09:27:49 +02:00
|
|
|
writeErrorResponse(w, req, InvalidBucketName, req.URL.Path)
|
2015-10-16 20:26:01 +02:00
|
|
|
case fs.BucketNotFound:
|
2015-10-04 09:27:49 +02:00
|
|
|
writeErrorResponse(w, req, NoSuchBucket, req.URL.Path)
|
2015-10-16 20:26:01 +02:00
|
|
|
case fs.ObjectNotFound:
|
2015-10-04 09:27:49 +02:00
|
|
|
writeErrorResponse(w, req, NoSuchKey, req.URL.Path)
|
2015-10-16 20:26:01 +02:00
|
|
|
case fs.ObjectNameInvalid:
|
2015-10-04 09:27:49 +02:00
|
|
|
writeErrorResponse(w, req, NoSuchKey, req.URL.Path)
|
2015-07-03 05:31:22 +02:00
|
|
|
default:
|
2015-09-19 12:20:07 +02:00
|
|
|
errorIf(err.Trace(), "ListObjects failed.", nil)
|
2015-10-04 09:27:49 +02:00
|
|
|
writeErrorResponse(w, req, InternalError, req.URL.Path)
|
2015-07-03 05:31:22 +02:00
|
|
|
}
|
2015-06-30 23:42:29 +02:00
|
|
|
}
|
|
|
|
|
2015-07-01 05:15:48 +02:00
|
|
|
// ListBucketsHandler - GET Service
|
2015-06-30 23:42:29 +02:00
|
|
|
// -----------
|
|
|
|
// This implementation of the GET operation returns a list of all buckets
|
|
|
|
// owned by the authenticated sender of the request.
|
2015-10-18 04:17:33 +02:00
|
|
|
func (api CloudStorageAPI) ListBucketsHandler(w http.ResponseWriter, req *http.Request) {
|
2015-10-17 04:09:35 +02:00
|
|
|
if !api.Anonymous {
|
|
|
|
if isRequestRequiresACLCheck(req) {
|
|
|
|
writeErrorResponse(w, req, AccessDenied, req.URL.Path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2015-10-16 20:26:01 +02:00
|
|
|
buckets, err := api.Filesystem.ListBuckets()
|
2015-08-04 01:17:21 +02:00
|
|
|
if err == nil {
|
2015-07-03 05:31:22 +02:00
|
|
|
// generate response
|
|
|
|
response := generateListBucketsResponse(buckets)
|
2015-10-04 09:27:49 +02:00
|
|
|
encodedSuccessResponse := encodeSuccessResponse(response)
|
2015-07-03 05:31:22 +02:00
|
|
|
// write headers
|
2015-10-04 09:27:49 +02:00
|
|
|
setCommonHeaders(w, len(encodedSuccessResponse))
|
2015-07-03 05:31:22 +02:00
|
|
|
// write response
|
|
|
|
w.Write(encodedSuccessResponse)
|
2015-08-04 01:17:21 +02:00
|
|
|
return
|
|
|
|
}
|
2015-10-04 21:06:14 +02:00
|
|
|
errorIf(err.Trace(), "ListBuckets failed.", nil)
|
|
|
|
writeErrorResponse(w, req, InternalError, req.URL.Path)
|
2015-06-30 23:42:29 +02:00
|
|
|
}
|
|
|
|
|
2015-07-01 05:15:48 +02:00
|
|
|
// PutBucketHandler - PUT Bucket
|
2015-06-30 23:42:29 +02:00
|
|
|
// ----------
|
|
|
|
// This implementation of the PUT operation creates a new bucket for authenticated request
|
2015-10-18 04:17:33 +02:00
|
|
|
func (api CloudStorageAPI) PutBucketHandler(w http.ResponseWriter, req *http.Request) {
|
2015-10-17 04:09:35 +02:00
|
|
|
vars := mux.Vars(req)
|
|
|
|
bucket := vars["bucket"]
|
|
|
|
|
|
|
|
if !api.Anonymous {
|
|
|
|
if isRequestRequiresACLCheck(req) {
|
|
|
|
writeErrorResponse(w, req, AccessDenied, req.URL.Path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-30 23:42:29 +02:00
|
|
|
// read from 'x-amz-acl'
|
|
|
|
aclType := getACLType(req)
|
|
|
|
if aclType == unsupportedACLType {
|
2015-10-04 09:27:49 +02:00
|
|
|
writeErrorResponse(w, req, NotImplemented, req.URL.Path)
|
2015-06-30 23:42:29 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-10-16 20:26:01 +02:00
|
|
|
var signature *fs.Signature
|
2015-10-07 08:32:20 +02:00
|
|
|
if !api.Anonymous {
|
2015-10-17 04:09:35 +02:00
|
|
|
// Init signature V4 verification
|
|
|
|
if isRequestSignatureV4(req) {
|
2015-10-07 08:32:20 +02:00
|
|
|
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
|
|
|
|
}
|
2015-07-10 04:34:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-14 18:17:30 +02:00
|
|
|
// 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
|
2015-10-16 20:26:01 +02:00
|
|
|
if req.Header.Get("Content-Length") == "" {
|
2015-10-04 09:27:49 +02:00
|
|
|
writeErrorResponse(w, req, MissingContentLength, req.URL.Path)
|
2015-07-14 18:17:30 +02:00
|
|
|
return
|
|
|
|
}
|
2015-10-16 20:26:01 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-14 18:17:30 +02:00
|
|
|
}
|
|
|
|
|
2015-10-16 20:26:01 +02:00
|
|
|
err := api.Filesystem.MakeBucket(bucket, getACLTypeString(aclType))
|
2015-09-19 12:20:07 +02:00
|
|
|
if err != nil {
|
|
|
|
errorIf(err.Trace(), "MakeBucket failed.", nil)
|
|
|
|
switch err.ToGoError().(type) {
|
2015-10-16 20:26:01 +02:00
|
|
|
case fs.BucketNameInvalid:
|
2015-10-04 09:27:49 +02:00
|
|
|
writeErrorResponse(w, req, InvalidBucketName, req.URL.Path)
|
2015-10-16 20:26:01 +02:00
|
|
|
case fs.BucketExists:
|
2015-10-04 09:27:49 +02:00
|
|
|
writeErrorResponse(w, req, BucketAlreadyExists, req.URL.Path)
|
2015-09-19 12:20:07 +02:00
|
|
|
default:
|
2015-10-04 09:27:49 +02:00
|
|
|
writeErrorResponse(w, req, InternalError, req.URL.Path)
|
2015-09-19 12:20:07 +02:00
|
|
|
}
|
2015-08-04 01:17:21 +02:00
|
|
|
return
|
|
|
|
}
|
2015-09-19 12:20:07 +02:00
|
|
|
// Make sure to add Location information here only for bucket
|
|
|
|
w.Header().Set("Location", "/"+bucket)
|
2015-10-04 09:27:49 +02:00
|
|
|
writeSuccessResponse(w)
|
2015-06-30 23:42:29 +02:00
|
|
|
}
|
|
|
|
|
2015-10-02 08:51:17 +02:00
|
|
|
// PostPolicyBucketHandler - POST policy
|
|
|
|
// ----------
|
|
|
|
// This implementation of the POST operation handles object creation with a specified
|
|
|
|
// signature policy in multipart/form-data
|
2015-10-18 04:17:33 +02:00
|
|
|
func (api CloudStorageAPI) PostPolicyBucketHandler(w http.ResponseWriter, req *http.Request) {
|
2015-10-07 08:32:20 +02:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-02 08:51:17 +02:00
|
|
|
// 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)
|
2015-10-04 09:27:49 +02:00
|
|
|
writeErrorResponse(w, req, MalformedPOSTRequest, req.URL.Path)
|
2015-10-02 08:51:17 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fileBody, formValues, perr := extractHTTPFormValues(reader)
|
|
|
|
if perr != nil {
|
|
|
|
errorIf(perr.Trace(), "Unable to parse form values.", nil)
|
2015-10-04 09:27:49 +02:00
|
|
|
writeErrorResponse(w, req, MalformedPOSTRequest, req.URL.Path)
|
2015-10-02 08:51:17 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
bucket := mux.Vars(req)["bucket"]
|
|
|
|
formValues["Bucket"] = bucket
|
2015-10-06 19:12:06 +02:00
|
|
|
object := formValues["Key"]
|
2015-10-02 08:51:17 +02:00
|
|
|
signature, perr := initPostPresignedPolicyV4(formValues)
|
|
|
|
if perr != nil {
|
|
|
|
errorIf(perr.Trace(), "Unable to initialize post policy presigned.", nil)
|
2015-10-04 09:27:49 +02:00
|
|
|
writeErrorResponse(w, req, MalformedPOSTRequest, req.URL.Path)
|
2015-10-02 08:51:17 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
var ok bool
|
|
|
|
if ok, perr = signature.DoesPolicySignatureMatch(formValues["X-Amz-Date"]); perr != nil {
|
|
|
|
errorIf(perr.Trace(), "Unable to verify signature.", nil)
|
2015-10-04 09:27:49 +02:00
|
|
|
writeErrorResponse(w, req, SignatureDoesNotMatch, req.URL.Path)
|
2015-10-02 08:51:17 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if ok == false {
|
2015-10-04 09:27:49 +02:00
|
|
|
writeErrorResponse(w, req, SignatureDoesNotMatch, req.URL.Path)
|
2015-10-02 08:51:17 +02:00
|
|
|
return
|
|
|
|
}
|
2015-10-06 19:12:06 +02:00
|
|
|
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
|
|
|
|
}
|
2015-10-16 20:26:01 +02:00
|
|
|
metadata, perr := api.Filesystem.CreateObject(bucket, object, "", 0, fileBody, nil)
|
2015-10-02 08:51:17 +02:00
|
|
|
if perr != nil {
|
|
|
|
errorIf(perr.Trace(), "CreateObject failed.", nil)
|
|
|
|
switch perr.ToGoError().(type) {
|
2015-10-18 04:17:33 +02:00
|
|
|
case fs.RootPathFull:
|
|
|
|
writeErrorResponse(w, req, RootPathFull, req.URL.Path)
|
2015-10-16 20:26:01 +02:00
|
|
|
case fs.BucketNotFound:
|
2015-10-04 09:27:49 +02:00
|
|
|
writeErrorResponse(w, req, NoSuchBucket, req.URL.Path)
|
2015-10-16 20:26:01 +02:00
|
|
|
case fs.BucketNameInvalid:
|
2015-10-04 09:27:49 +02:00
|
|
|
writeErrorResponse(w, req, InvalidBucketName, req.URL.Path)
|
2015-10-16 20:26:01 +02:00
|
|
|
case fs.BadDigest:
|
2015-10-04 09:27:49 +02:00
|
|
|
writeErrorResponse(w, req, BadDigest, req.URL.Path)
|
2015-10-16 20:26:01 +02:00
|
|
|
case fs.SignatureDoesNotMatch:
|
2015-10-04 09:27:49 +02:00
|
|
|
writeErrorResponse(w, req, SignatureDoesNotMatch, req.URL.Path)
|
2015-10-16 20:26:01 +02:00
|
|
|
case fs.IncompleteBody:
|
2015-10-04 09:27:49 +02:00
|
|
|
writeErrorResponse(w, req, IncompleteBody, req.URL.Path)
|
2015-10-16 20:26:01 +02:00
|
|
|
case fs.EntityTooLarge:
|
2015-10-04 09:27:49 +02:00
|
|
|
writeErrorResponse(w, req, EntityTooLarge, req.URL.Path)
|
2015-10-16 20:26:01 +02:00
|
|
|
case fs.InvalidDigest:
|
2015-10-04 09:27:49 +02:00
|
|
|
writeErrorResponse(w, req, InvalidDigest, req.URL.Path)
|
2015-10-02 08:51:17 +02:00
|
|
|
default:
|
2015-10-04 09:27:49 +02:00
|
|
|
writeErrorResponse(w, req, InternalError, req.URL.Path)
|
2015-10-02 08:51:17 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2015-10-16 20:26:01 +02:00
|
|
|
w.Header().Set("ETag", "\""+metadata.Md5+"\"")
|
2015-10-04 09:27:49 +02:00
|
|
|
writeSuccessResponse(w)
|
2015-10-02 08:51:17 +02:00
|
|
|
}
|
|
|
|
|
2015-07-01 05:15:48 +02:00
|
|
|
// PutBucketACLHandler - PUT Bucket ACL
|
2015-06-30 23:42:29 +02:00
|
|
|
// ----------
|
|
|
|
// This implementation of the PUT operation modifies the bucketACL for authenticated request
|
2015-10-18 04:17:33 +02:00
|
|
|
func (api CloudStorageAPI) PutBucketACLHandler(w http.ResponseWriter, req *http.Request) {
|
2015-10-17 04:09:35 +02:00
|
|
|
vars := mux.Vars(req)
|
|
|
|
bucket := vars["bucket"]
|
|
|
|
|
|
|
|
if !api.Anonymous {
|
|
|
|
if isRequestRequiresACLCheck(req) {
|
|
|
|
writeErrorResponse(w, req, AccessDenied, req.URL.Path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-30 23:42:29 +02:00
|
|
|
// read from 'x-amz-acl'
|
|
|
|
aclType := getACLType(req)
|
|
|
|
if aclType == unsupportedACLType {
|
2015-10-04 09:27:49 +02:00
|
|
|
writeErrorResponse(w, req, NotImplemented, req.URL.Path)
|
2015-06-30 23:42:29 +02:00
|
|
|
return
|
|
|
|
}
|
2015-10-16 20:26:01 +02:00
|
|
|
err := api.Filesystem.SetBucketMetadata(bucket, map[string]string{"acl": getACLTypeString(aclType)})
|
2015-09-19 12:20:07 +02:00
|
|
|
if err != nil {
|
|
|
|
errorIf(err.Trace(), "PutBucketACL failed.", nil)
|
|
|
|
switch err.ToGoError().(type) {
|
2015-10-16 20:26:01 +02:00
|
|
|
case fs.BucketNameInvalid:
|
2015-10-04 09:27:49 +02:00
|
|
|
writeErrorResponse(w, req, InvalidBucketName, req.URL.Path)
|
2015-10-16 20:26:01 +02:00
|
|
|
case fs.BucketNotFound:
|
2015-10-04 09:27:49 +02:00
|
|
|
writeErrorResponse(w, req, NoSuchBucket, req.URL.Path)
|
2015-09-19 12:20:07 +02:00
|
|
|
default:
|
2015-10-04 09:27:49 +02:00
|
|
|
writeErrorResponse(w, req, InternalError, req.URL.Path)
|
2015-09-19 12:20:07 +02:00
|
|
|
}
|
2015-08-04 01:17:21 +02:00
|
|
|
return
|
|
|
|
}
|
2015-10-04 09:27:49 +02:00
|
|
|
writeSuccessResponse(w)
|
2015-06-30 23:42:29 +02:00
|
|
|
}
|
|
|
|
|
2015-10-08 20:12:02 +02:00
|
|
|
// 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.
|
2015-10-18 04:17:33 +02:00
|
|
|
func (api CloudStorageAPI) GetBucketACLHandler(w http.ResponseWriter, req *http.Request) {
|
2015-10-08 20:12:02 +02:00
|
|
|
vars := mux.Vars(req)
|
|
|
|
bucket := vars["bucket"]
|
|
|
|
|
2015-10-17 04:09:35 +02:00
|
|
|
if !api.Anonymous {
|
|
|
|
if isRequestRequiresACLCheck(req) {
|
|
|
|
writeErrorResponse(w, req, AccessDenied, req.URL.Path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-16 20:26:01 +02:00
|
|
|
bucketMetadata, err := api.Filesystem.GetBucketMetadata(bucket)
|
2015-10-08 20:12:02 +02:00
|
|
|
if err != nil {
|
|
|
|
errorIf(err.Trace(), "GetBucketMetadata failed.", nil)
|
|
|
|
switch err.ToGoError().(type) {
|
2015-10-16 20:26:01 +02:00
|
|
|
case fs.BucketNotFound:
|
2015-10-08 20:12:02 +02:00
|
|
|
writeErrorResponse(w, req, NoSuchBucket, req.URL.Path)
|
2015-10-16 20:26:01 +02:00
|
|
|
case fs.BucketNameInvalid:
|
2015-10-08 20:12:02 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2015-07-01 05:15:48 +02:00
|
|
|
// HeadBucketHandler - HEAD Bucket
|
2015-06-30 23:42:29 +02:00
|
|
|
// ----------
|
|
|
|
// 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.
|
2015-10-18 04:17:33 +02:00
|
|
|
func (api CloudStorageAPI) HeadBucketHandler(w http.ResponseWriter, req *http.Request) {
|
2015-06-30 23:42:29 +02:00
|
|
|
vars := mux.Vars(req)
|
|
|
|
bucket := vars["bucket"]
|
2015-07-03 05:31:22 +02:00
|
|
|
|
2015-10-17 04:09:35 +02:00
|
|
|
if !api.Anonymous {
|
|
|
|
if isRequestRequiresACLCheck(req) {
|
|
|
|
if api.Filesystem.IsPrivateBucket(bucket) {
|
|
|
|
writeErrorResponse(w, req, AccessDenied, req.URL.Path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-16 20:26:01 +02:00
|
|
|
_, err := api.Filesystem.GetBucketMetadata(bucket)
|
2015-09-19 12:20:07 +02:00
|
|
|
if err != nil {
|
|
|
|
errorIf(err.Trace(), "GetBucketMetadata failed.", nil)
|
|
|
|
switch err.ToGoError().(type) {
|
2015-10-16 20:26:01 +02:00
|
|
|
case fs.BucketNotFound:
|
2015-10-04 09:27:49 +02:00
|
|
|
writeErrorResponse(w, req, NoSuchBucket, req.URL.Path)
|
2015-10-16 20:26:01 +02:00
|
|
|
case fs.BucketNameInvalid:
|
2015-10-04 09:27:49 +02:00
|
|
|
writeErrorResponse(w, req, InvalidBucketName, req.URL.Path)
|
2015-09-19 12:20:07 +02:00
|
|
|
default:
|
2015-10-04 09:27:49 +02:00
|
|
|
writeErrorResponse(w, req, InternalError, req.URL.Path)
|
2015-09-19 12:20:07 +02:00
|
|
|
}
|
2015-08-04 01:17:21 +02:00
|
|
|
return
|
|
|
|
}
|
2015-10-04 09:27:49 +02:00
|
|
|
writeSuccessResponse(w)
|
2015-06-30 23:42:29 +02:00
|
|
|
}
|
2015-10-16 20:26:01 +02:00
|
|
|
|
|
|
|
// DeleteBucketHandler - Delete bucket
|
2015-10-18 04:17:33 +02:00
|
|
|
func (api CloudStorageAPI) DeleteBucketHandler(w http.ResponseWriter, req *http.Request) {
|
2015-10-16 20:26:01 +02:00
|
|
|
vars := mux.Vars(req)
|
|
|
|
bucket := vars["bucket"]
|
|
|
|
|
2015-10-17 04:09:35 +02:00
|
|
|
if !api.Anonymous {
|
|
|
|
if isRequestRequiresACLCheck(req) {
|
|
|
|
writeErrorResponse(w, req, AccessDenied, req.URL.Path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-16 20:26:01 +02:00
|
|
|
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
|
|
|
|
}
|
2015-10-17 05:02:37 +02:00
|
|
|
writeSuccessNoContent(w)
|
2015-10-16 20:26:01 +02:00
|
|
|
}
|