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-03 19:46:56 -05:00
|
|
|
/*
|
2018-03-15 16:03:41 -04:00
|
|
|
* Minio Cloud Storage, (C) 2015, 2016, 2017, 2018 Minio, Inc.
|
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-03 19:46:56 -05: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.
|
|
|
|
*/
|
|
|
|
|
2016-08-18 19:23:42 -04:00
|
|
|
package cmd
|
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-03 19:46:56 -05:00
|
|
|
|
|
|
|
import (
|
2016-07-28 23:49:08 -04:00
|
|
|
"bytes"
|
2018-03-15 16:27:16 -04:00
|
|
|
"context"
|
2016-10-13 12:19:04 -04:00
|
|
|
"encoding/json"
|
2016-08-10 23:10:48 -04:00
|
|
|
"io"
|
2017-10-27 19:14:06 -04:00
|
|
|
"reflect"
|
2016-09-26 17:28:35 -04:00
|
|
|
"sync"
|
2017-10-22 01:30:34 -04:00
|
|
|
|
2017-10-27 19:14:06 -04:00
|
|
|
"github.com/minio/minio-go/pkg/policy"
|
2018-04-05 18:04:40 -04:00
|
|
|
"github.com/minio/minio/cmd/logger"
|
2017-11-25 14:58:29 -05:00
|
|
|
"github.com/minio/minio/pkg/errors"
|
2017-10-22 01:30:34 -04:00
|
|
|
"github.com/minio/minio/pkg/hash"
|
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-03 19:46:56 -05:00
|
|
|
)
|
|
|
|
|
2017-01-10 19:43:48 -05:00
|
|
|
const (
|
|
|
|
// Static prefix to be used while constructing bucket ARN.
|
|
|
|
// refer to S3 docs for more info.
|
2018-03-15 16:03:41 -04:00
|
|
|
bucketARNPrefix = "arn:aws:s3:::"
|
2017-01-16 20:05:00 -05:00
|
|
|
|
|
|
|
// Bucket policy config name.
|
|
|
|
bucketPolicyConfig = "policy.json"
|
2017-01-10 19:43:48 -05:00
|
|
|
)
|
|
|
|
|
2016-09-26 17:28:35 -04:00
|
|
|
// Global bucket policies list, policies are enforced on each bucket looking
|
|
|
|
// through the policies here.
|
|
|
|
type bucketPolicies struct {
|
|
|
|
rwMutex *sync.RWMutex
|
|
|
|
|
|
|
|
// Collection of 'bucket' policies.
|
2017-10-27 19:14:06 -04:00
|
|
|
bucketPolicyConfigs map[string]policy.BucketAccessPolicy
|
2016-09-26 17:28:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch bucket policy for a given bucket.
|
2017-10-27 19:14:06 -04:00
|
|
|
func (bp bucketPolicies) GetBucketPolicy(bucket string) policy.BucketAccessPolicy {
|
2016-09-26 17:28:35 -04:00
|
|
|
bp.rwMutex.RLock()
|
|
|
|
defer bp.rwMutex.RUnlock()
|
|
|
|
return bp.bucketPolicyConfigs[bucket]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set a new bucket policy for a bucket, this operation will overwrite
|
2016-10-13 12:19:04 -04:00
|
|
|
// any previous bucket policies for the bucket.
|
2018-02-09 18:19:30 -05:00
|
|
|
func (bp *bucketPolicies) SetBucketPolicy(bucket string, newpolicy policy.BucketAccessPolicy) error {
|
2016-09-26 17:28:35 -04:00
|
|
|
bp.rwMutex.Lock()
|
|
|
|
defer bp.rwMutex.Unlock()
|
2016-10-13 12:19:04 -04:00
|
|
|
|
2018-02-09 18:19:30 -05:00
|
|
|
if reflect.DeepEqual(newpolicy, emptyBucketPolicy) {
|
|
|
|
return errInvalidArgument
|
2016-09-26 17:28:35 -04:00
|
|
|
}
|
2018-02-09 18:19:30 -05:00
|
|
|
bp.bucketPolicyConfigs[bucket] = newpolicy
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete bucket policy from struct for a given bucket.
|
|
|
|
func (bp *bucketPolicies) DeleteBucketPolicy(bucket string) error {
|
|
|
|
bp.rwMutex.Lock()
|
|
|
|
defer bp.rwMutex.Unlock()
|
|
|
|
delete(bp.bucketPolicyConfigs, bucket)
|
2016-09-26 17:28:35 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-09 18:19:30 -05:00
|
|
|
// Intialize all bucket policies.
|
2018-02-15 20:45:57 -05:00
|
|
|
func initBucketPolicies(objAPI ObjectLayer) (*bucketPolicies, error) {
|
2018-02-09 18:19:30 -05:00
|
|
|
if objAPI == nil {
|
2018-02-15 20:45:57 -05:00
|
|
|
return nil, errInvalidArgument
|
2018-02-09 18:19:30 -05:00
|
|
|
}
|
2018-02-15 20:45:57 -05:00
|
|
|
|
2016-09-26 17:28:35 -04:00
|
|
|
// List buckets to proceed loading all notification configuration.
|
2018-03-15 16:27:16 -04:00
|
|
|
buckets, err := objAPI.ListBuckets(context.Background())
|
2016-09-26 17:28:35 -04:00
|
|
|
if err != nil {
|
2018-02-15 20:45:57 -05:00
|
|
|
return nil, errors.Cause(err)
|
2016-09-26 17:28:35 -04:00
|
|
|
}
|
|
|
|
|
2018-02-09 18:19:30 -05:00
|
|
|
policies := make(map[string]policy.BucketAccessPolicy)
|
2016-09-26 17:28:35 -04:00
|
|
|
// Loads bucket policy.
|
|
|
|
for _, bucket := range buckets {
|
2018-02-20 15:21:12 -05:00
|
|
|
bp, pErr := ReadBucketPolicy(bucket.Name, objAPI)
|
2016-09-26 17:28:35 -04:00
|
|
|
if pErr != nil {
|
2016-11-23 23:05:04 -05:00
|
|
|
// net.Dial fails for rpc client or any
|
|
|
|
// other unexpected errors during net.Dial.
|
2017-11-25 14:58:29 -05:00
|
|
|
if !errors.IsErrIgnored(pErr, errDiskNotFound) {
|
2016-11-19 20:37:57 -05:00
|
|
|
if !isErrBucketPolicyNotFound(pErr) {
|
2018-02-15 20:45:57 -05:00
|
|
|
return nil, errors.Cause(pErr)
|
2016-11-19 20:37:57 -05:00
|
|
|
}
|
2016-09-26 17:28:35 -04:00
|
|
|
}
|
|
|
|
// Continue to load other bucket policies if possible.
|
|
|
|
continue
|
|
|
|
}
|
2017-10-27 19:14:06 -04:00
|
|
|
policies[bucket.Name] = bp
|
2016-09-26 17:28:35 -04:00
|
|
|
}
|
2018-02-15 20:45:57 -05:00
|
|
|
|
|
|
|
// Return all bucket policies.
|
|
|
|
return &bucketPolicies{
|
2016-09-26 17:28:35 -04:00
|
|
|
rwMutex: &sync.RWMutex{},
|
|
|
|
bucketPolicyConfigs: policies,
|
2018-02-15 20:45:57 -05:00
|
|
|
}, nil
|
2016-09-26 17:28:35 -04:00
|
|
|
}
|
|
|
|
|
2016-08-30 13:04:50 -04:00
|
|
|
// readBucketPolicyJSON - reads bucket policy for an input bucket, returns BucketPolicyNotFound
|
|
|
|
// if bucket policy is not found.
|
|
|
|
func readBucketPolicyJSON(bucket string, objAPI ObjectLayer) (bucketPolicyReader io.Reader, err error) {
|
2017-01-16 20:05:00 -05:00
|
|
|
policyPath := pathJoin(bucketConfigPrefix, bucket, bucketPolicyConfig)
|
2016-12-10 19:15:12 -05:00
|
|
|
|
2016-07-28 23:49:08 -04:00
|
|
|
var buffer bytes.Buffer
|
2018-04-05 18:04:40 -04:00
|
|
|
ctx := logger.SetReqInfo(context.Background(), &logger.ReqInfo{BucketName: bucket})
|
|
|
|
err = objAPI.GetObject(ctx, minioMetaBucket, policyPath, 0, -1, &buffer, "")
|
2016-07-28 23:49:08 -04:00
|
|
|
if err != nil {
|
2016-11-19 20:37:57 -05:00
|
|
|
if isErrObjectNotFound(err) || isErrIncompleteBody(err) {
|
2018-02-09 18:19:30 -05:00
|
|
|
return nil, PolicyNotFound{Bucket: bucket}
|
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-03 19:46:56 -05:00
|
|
|
}
|
2017-11-25 14:58:29 -05:00
|
|
|
return nil, errors.Cause(err)
|
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-03 19:46:56 -05:00
|
|
|
}
|
2016-08-30 13:04:50 -04:00
|
|
|
|
|
|
|
return &buffer, nil
|
|
|
|
}
|
|
|
|
|
2018-02-20 15:21:12 -05:00
|
|
|
// ReadBucketPolicy - reads bucket policy for an input bucket, returns BucketPolicyNotFound
|
2016-08-30 13:04:50 -04:00
|
|
|
// if bucket policy is not found. This function also parses the bucket policy into an object.
|
2018-02-20 15:21:12 -05:00
|
|
|
func ReadBucketPolicy(bucket string, objAPI ObjectLayer) (policy.BucketAccessPolicy, error) {
|
2016-08-30 13:04:50 -04:00
|
|
|
// Read bucket policy JSON.
|
|
|
|
bucketPolicyReader, err := readBucketPolicyJSON(bucket, objAPI)
|
|
|
|
if err != nil {
|
2017-10-27 19:14:06 -04:00
|
|
|
return emptyBucketPolicy, err
|
2016-08-30 13:04:50 -04:00
|
|
|
}
|
|
|
|
|
2016-08-10 23:10:48 -04:00
|
|
|
// Parse the saved policy.
|
2017-10-27 19:14:06 -04:00
|
|
|
var bp policy.BucketAccessPolicy
|
|
|
|
if err = parseBucketPolicy(bucketPolicyReader, &bp); err != nil {
|
|
|
|
return emptyBucketPolicy, err
|
2016-08-10 23:10:48 -04:00
|
|
|
|
|
|
|
}
|
2017-10-27 19:14:06 -04:00
|
|
|
return bp, nil
|
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-03 19:46:56 -05:00
|
|
|
}
|
|
|
|
|
2016-08-10 23:10:48 -04:00
|
|
|
// removeBucketPolicy - removes any previously written bucket policy. Returns BucketPolicyNotFound
|
|
|
|
// if no policies are found.
|
2018-04-05 18:04:40 -04:00
|
|
|
func removeBucketPolicy(ctx context.Context, bucket string, objAPI ObjectLayer) error {
|
2017-01-16 20:05:00 -05:00
|
|
|
policyPath := pathJoin(bucketConfigPrefix, bucket, bucketPolicyConfig)
|
2018-04-05 18:04:40 -04:00
|
|
|
err := objAPI.DeleteObject(ctx, minioMetaBucket, policyPath)
|
2017-10-27 19:14:06 -04:00
|
|
|
if err != nil {
|
2017-11-25 14:58:29 -05:00
|
|
|
err = errors.Cause(err)
|
2016-07-28 23:49:08 -04:00
|
|
|
if _, ok := err.(ObjectNotFound); ok {
|
2016-04-29 17:24:10 -04:00
|
|
|
return BucketPolicyNotFound{Bucket: bucket}
|
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-03 19:46:56 -05:00
|
|
|
}
|
2016-04-29 17:24:10 -04:00
|
|
|
return err
|
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-03 19:46:56 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-19 20:37:57 -05:00
|
|
|
// writeBucketPolicy - save a bucket policy that is assumed to be validated.
|
2018-04-05 18:04:40 -04:00
|
|
|
func writeBucketPolicy(ctx context.Context, bucket string, objAPI ObjectLayer, bpy policy.BucketAccessPolicy) error {
|
2016-10-13 12:19:04 -04:00
|
|
|
buf, err := json.Marshal(bpy)
|
2018-04-05 18:04:40 -04:00
|
|
|
|
2016-10-13 12:19:04 -04:00
|
|
|
if err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
2016-10-13 12:19:04 -04:00
|
|
|
return err
|
|
|
|
}
|
2017-01-16 20:05:00 -05:00
|
|
|
policyPath := pathJoin(bucketConfigPrefix, bucket, bucketPolicyConfig)
|
2017-10-22 01:30:34 -04:00
|
|
|
hashReader, err := hash.NewReader(bytes.NewReader(buf), int64(len(buf)), "", getSHA256Hash(buf))
|
|
|
|
if err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
2017-11-25 14:58:29 -05:00
|
|
|
return errors.Cause(err)
|
2017-10-22 01:30:34 -04:00
|
|
|
}
|
|
|
|
|
2018-04-05 18:04:40 -04:00
|
|
|
if _, err = objAPI.PutObject(ctx, minioMetaBucket, policyPath, hashReader, nil); err != nil {
|
2017-11-25 14:58:29 -05:00
|
|
|
return errors.Cause(err)
|
2016-08-25 12:39:01 -04:00
|
|
|
}
|
|
|
|
return nil
|
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-03 19:46:56 -05:00
|
|
|
}
|
2016-12-10 19:15:12 -05:00
|
|
|
|
|
|
|
// persistAndNotifyBucketPolicyChange - takes a policyChange argument,
|
|
|
|
// persists it to storage, and notify nodes in the cluster about the
|
|
|
|
// change. In-memory state is updated in response to the notification.
|
2018-04-05 18:04:40 -04:00
|
|
|
func persistAndNotifyBucketPolicyChange(ctx context.Context, bucket string, isRemove bool, bktPolicy policy.BucketAccessPolicy, objAPI ObjectLayer) error {
|
2018-02-09 18:19:30 -05:00
|
|
|
if isRemove {
|
2018-04-05 18:04:40 -04:00
|
|
|
err := removeBucketPolicy(ctx, bucket, objAPI)
|
2017-10-27 19:14:06 -04:00
|
|
|
if err != nil {
|
2016-12-10 19:15:12 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2018-02-09 18:19:30 -05:00
|
|
|
if reflect.DeepEqual(bktPolicy, emptyBucketPolicy) {
|
2016-12-10 19:15:12 -05:00
|
|
|
return errInvalidArgument
|
|
|
|
}
|
2018-04-05 18:04:40 -04:00
|
|
|
if err := writeBucketPolicy(ctx, bucket, objAPI, bktPolicy); err != nil {
|
2016-12-10 19:15:12 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|