Allow anonymous putObject() over publicly writable buckets
This commit is contained in:
+2
-2
@@ -41,8 +41,8 @@
|
||||
"canonical": "github.com/minio/minio-go",
|
||||
"comment": "",
|
||||
"local": "vendor/github.com/minio/minio-go",
|
||||
"revision": "ca58a5b7e02c2e0adf5fb404df3545f0ddc45b29",
|
||||
"revisionTime": "2015-08-22T15:40:20-07:00"
|
||||
"revision": "7860bcf566f9e641f94a4d85ac60a4f3a0883f46",
|
||||
"revisionTime": "2015-08-28T00:38:53-07:00"
|
||||
},
|
||||
{
|
||||
"canonical": "github.com/minio/minio/pkg/probe",
|
||||
|
||||
+53
@@ -469,6 +469,59 @@ func (a apiV1) deleteBucket(bucket string) error {
|
||||
|
||||
/// Object Read/Write/Stat Operations
|
||||
|
||||
func (a apiV1) putObjectUnAuthenticatedRequest(bucket, object, contentType string, size int64, body io.Reader) (*request, error) {
|
||||
if strings.TrimSpace(contentType) == "" {
|
||||
contentType = "application/octet-stream"
|
||||
}
|
||||
encodedObject, err := urlEncodeName(object)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
op := &operation{
|
||||
HTTPServer: a.config.Endpoint,
|
||||
HTTPMethod: "PUT",
|
||||
HTTPPath: separator + bucket + separator + encodedObject,
|
||||
}
|
||||
r, err := newUnauthenticatedRequest(op, a.config, body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Content-MD5 is not set consciously
|
||||
r.Set("Content-Type", contentType)
|
||||
r.req.ContentLength = size
|
||||
return r, nil
|
||||
}
|
||||
|
||||
// putObjectUnAuthenticated - add an object to a bucket
|
||||
// NOTE: You must have WRITE permissions on a bucket to add an object to it.
|
||||
func (a apiV1) putObjectUnAuthenticated(bucket, object, contentType string, size int64, body io.Reader) (ObjectStat, error) {
|
||||
req, err := a.putObjectUnAuthenticatedRequest(bucket, object, contentType, size, body)
|
||||
if err != nil {
|
||||
return ObjectStat{}, err
|
||||
}
|
||||
resp, err := req.Do()
|
||||
defer closeResp(resp)
|
||||
if err != nil {
|
||||
return ObjectStat{}, err
|
||||
}
|
||||
if resp != nil {
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return ObjectStat{}, BodyToErrorResponse(resp.Body, a.config.AcceptType)
|
||||
}
|
||||
}
|
||||
var metadata ObjectStat
|
||||
metadata.ETag = strings.Trim(resp.Header.Get("ETag"), "\"") // trim off the odd double quotes
|
||||
if metadata.ETag == "" {
|
||||
return ObjectStat{}, ErrorResponse{
|
||||
Code: "InternalError",
|
||||
Message: "Missing Etag, please report this issue at https://github.com/minio/minio-go/issues",
|
||||
RequestID: resp.Header.Get("x-amz-request-id"),
|
||||
HostID: resp.Header.Get("x-amz-id-2"),
|
||||
}
|
||||
}
|
||||
return metadata, nil
|
||||
}
|
||||
|
||||
// putObjectRequest wrapper creates a new PutObject request
|
||||
func (a apiV1) putObjectRequest(bucket, object, contentType string, md5SumBytes []byte, size int64, body io.ReadSeeker) (*request, error) {
|
||||
if strings.TrimSpace(contentType) == "" {
|
||||
|
||||
+20
-11
@@ -128,13 +128,13 @@ var regions = map[string]string{
|
||||
}
|
||||
|
||||
// getRegion returns a region based on its endpoint mapping.
|
||||
func getRegion(host string) (region string, err error) {
|
||||
if regions[host] != "" {
|
||||
return regions[host], nil
|
||||
func getRegion(host string) (region string) {
|
||||
if _, ok := regions[host]; ok {
|
||||
return regions[host]
|
||||
}
|
||||
// Region cannot be empty according to Amazon S3.
|
||||
// So we address all the four quadrants of our galaxy.
|
||||
return "milkyway", nil
|
||||
return "milkyway"
|
||||
}
|
||||
|
||||
// Config - main configuration struct used by all to set endpoint, credentials, and other options for requests.
|
||||
@@ -199,7 +199,7 @@ type apiV2 struct {
|
||||
|
||||
// New - instantiate a new minio api client
|
||||
func New(config Config) (API, error) {
|
||||
if config.Region == "" {
|
||||
if strings.TrimSpace(config.Region) == "" || len(config.Region) == 0 {
|
||||
u, err := url.Parse(config.Endpoint)
|
||||
if err != nil {
|
||||
return apiV2{}, err
|
||||
@@ -210,11 +210,7 @@ func New(config Config) (API, error) {
|
||||
hostSplits := strings.SplitN(u.Host, ".", 2)
|
||||
u.Host = hostSplits[1]
|
||||
}
|
||||
region, err := getRegion(u.Host)
|
||||
if err != nil {
|
||||
return apiV2{}, err
|
||||
}
|
||||
config.Region = region
|
||||
config.Region = getRegion(u.Host)
|
||||
}
|
||||
config.SetUserAgent(LibraryName, LibraryVersion, runtime.GOOS, runtime.GOARCH)
|
||||
config.isUserAgentSet = false // default
|
||||
@@ -510,6 +506,19 @@ func (a apiV2) PutObject(bucket, object, contentType string, size int64, data io
|
||||
if err := invalidArgumentError(object); err != nil {
|
||||
return err
|
||||
}
|
||||
// for un-authenticated requests do not initiated multipart operation
|
||||
//
|
||||
// NOTE: this behavior is only kept valid for S3, since S3 doesn't
|
||||
// allow unauthenticated multipart requests
|
||||
if a.config.Region != "milkyway" {
|
||||
if a.config.AccessKeyID == "" || a.config.SecretAccessKey == "" {
|
||||
_, err := a.putObjectUnAuthenticated(bucket, object, contentType, size, data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
switch {
|
||||
case size < minimumPartSize:
|
||||
// Single Part use case, use PutObject directly
|
||||
@@ -600,7 +609,7 @@ func (a apiV2) MakeBucket(bucket string, acl BucketACL) error {
|
||||
if !acl.isValidBucketACL() {
|
||||
return invalidArgumentError("")
|
||||
}
|
||||
location, _ := getRegion(a.config.Endpoint)
|
||||
location := a.config.Region
|
||||
if location == "milkyway" {
|
||||
location = ""
|
||||
}
|
||||
|
||||
+2
-8
@@ -45,17 +45,11 @@ func TestUserAgent(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetRegion(t *testing.T) {
|
||||
region, err := getRegion("s3.amazonaws.com")
|
||||
if err != nil {
|
||||
t.Fatalf("Error")
|
||||
}
|
||||
region := getRegion("s3.amazonaws.com")
|
||||
if region != "us-east-1" {
|
||||
t.Fatalf("Error")
|
||||
}
|
||||
region, err = getRegion("localhost:9000")
|
||||
if err != nil {
|
||||
t.Fatalf("Error")
|
||||
}
|
||||
region = getRegion("localhost:9000")
|
||||
if region != "milkyway" {
|
||||
t.Fatalf("Error")
|
||||
}
|
||||
|
||||
+40
@@ -223,6 +223,46 @@ func newPresignedRequest(op *operation, config *Config, expires string) (*reques
|
||||
return r, nil
|
||||
}
|
||||
|
||||
// newUnauthenticatedRequest - instantiate a new unauthenticated request
|
||||
func newUnauthenticatedRequest(op *operation, config *Config, body io.Reader) (*request, error) {
|
||||
// if no method default to POST
|
||||
method := op.HTTPMethod
|
||||
if method == "" {
|
||||
method = "POST"
|
||||
}
|
||||
|
||||
u := op.getRequestURL(*config)
|
||||
|
||||
// get a new HTTP request, for the requested method
|
||||
req, err := httpNewRequest(method, u, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// set UserAgent
|
||||
req.Header.Set("User-Agent", config.userAgent)
|
||||
|
||||
// set Accept header for response encoding style, if available
|
||||
if config.AcceptType != "" {
|
||||
req.Header.Set("Accept", config.AcceptType)
|
||||
}
|
||||
|
||||
// add body
|
||||
switch {
|
||||
case body == nil:
|
||||
req.Body = nil
|
||||
default:
|
||||
req.Body = ioutil.NopCloser(body)
|
||||
}
|
||||
|
||||
// save for subsequent use
|
||||
r := new(request)
|
||||
r.req = req
|
||||
r.config = config
|
||||
|
||||
return r, nil
|
||||
}
|
||||
|
||||
// newRequest - instantiate a new request
|
||||
func newRequest(op *operation, config *Config, body io.ReadSeeker) (*request, error) {
|
||||
// if no method default to POST
|
||||
|
||||
Reference in New Issue
Block a user