From ff3862d1cc532f2af682c90f3a5febaac8f364b7 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Sun, 23 Apr 2017 15:34:46 -0700 Subject: [PATCH] core: Implement native GetBucketPolicy, PutBucketPolicy This is implemented to address the problems of applications wanting to validate the entire bucket policy in a custom manner. Fixes #659 Refer https://github.com/minio/minio/issues/4131 --- api-get-policy.go | 6 +++--- api-put-bucket.go | 4 +++- core.go | 16 +++++++++++++++- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/api-get-policy.go b/api-get-policy.go index da0a409cd..23c43178e 100644 --- a/api-get-policy.go +++ b/api-get-policy.go @@ -34,7 +34,7 @@ func (c Client) GetBucketPolicy(bucketName, objectPrefix string) (bucketPolicy p if err := isValidObjectPrefix(objectPrefix); err != nil { return policy.BucketPolicyNone, err } - policyInfo, err := c.getBucketPolicy(bucketName, objectPrefix) + policyInfo, err := c.getBucketPolicy(bucketName) if err != nil { return policy.BucketPolicyNone, err } @@ -50,7 +50,7 @@ func (c Client) ListBucketPolicies(bucketName, objectPrefix string) (bucketPolic if err := isValidObjectPrefix(objectPrefix); err != nil { return map[string]policy.BucketPolicy{}, err } - policyInfo, err := c.getBucketPolicy(bucketName, objectPrefix) + policyInfo, err := c.getBucketPolicy(bucketName) if err != nil { return map[string]policy.BucketPolicy{}, err } @@ -58,7 +58,7 @@ func (c Client) ListBucketPolicies(bucketName, objectPrefix string) (bucketPolic } // Request server for current bucket policy. -func (c Client) getBucketPolicy(bucketName string, objectPrefix string) (policy.BucketAccessPolicy, error) { +func (c Client) getBucketPolicy(bucketName string) (policy.BucketAccessPolicy, error) { // Get resources properly escaped and lined up before // using them in http request. urlValues := make(url.Values) diff --git a/api-put-bucket.go b/api-put-bucket.go index 7c7e03f49..fa0170900 100644 --- a/api-put-bucket.go +++ b/api-put-bucket.go @@ -157,10 +157,12 @@ func (c Client) SetBucketPolicy(bucketName string, objectPrefix string, bucketPo if err := isValidObjectPrefix(objectPrefix); err != nil { return err } + if !bucketPolicy.IsValidBucketPolicy() { return ErrInvalidArgument(fmt.Sprintf("Invalid bucket policy provided. %s", bucketPolicy)) } - policyInfo, err := c.getBucketPolicy(bucketName, objectPrefix) + + policyInfo, err := c.getBucketPolicy(bucketName) if err != nil { return err } diff --git a/core.go b/core.go index 5d207648e..90154d945 100644 --- a/core.go +++ b/core.go @@ -16,7 +16,11 @@ package minio -import "io" +import ( + "io" + + "github.com/minio/minio-go/pkg/policy" +) // Core - Inherits Client and adds new methods to expose the low level S3 APIs. type Core struct { @@ -84,3 +88,13 @@ func (c Core) CompleteMultipartUpload(bucket, object, uploadID string, parts []C func (c Core) AbortMultipartUpload(bucket, object, uploadID string) error { return c.abortMultipartUpload(bucket, object, uploadID) } + +// GetBucketPolicy - fetches bucket access policy for a given bucket. +func (c Core) GetBucketPolicy(bucket string) (policy.BucketAccessPolicy, error) { + return c.getBucketPolicy(bucket) +} + +// PutBucketPolicy - applies a new bucket access policy for a given bucket. +func (c Core) PutBucketPolicy(bucket string, bucketPolicy policy.BucketAccessPolicy) error { + return c.putBucketPolicy(bucket, bucketPolicy) +}