2016-05-05 01:56:57 +02:00
|
|
|
/*
|
2019-04-09 20:39:42 +02:00
|
|
|
* MinIO Cloud Storage, (C) 2016 MinIO, Inc.
|
2016-05-05 01:56:57 +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.
|
|
|
|
*/
|
|
|
|
|
2016-08-19 01:23:42 +02:00
|
|
|
package cmd
|
2016-05-05 01:56:57 +02:00
|
|
|
|
|
|
|
import (
|
2016-07-03 04:05:16 +02:00
|
|
|
"bytes"
|
2018-03-15 21:27:16 +01:00
|
|
|
"context"
|
2016-05-05 01:56:57 +02:00
|
|
|
"fmt"
|
2016-10-10 18:29:56 +02:00
|
|
|
"io"
|
2016-07-04 07:35:30 +02:00
|
|
|
"io/ioutil"
|
2016-07-03 04:05:16 +02:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2018-04-25 00:53:30 +02:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
2016-05-05 01:56:57 +02:00
|
|
|
"testing"
|
2016-08-20 12:16:38 +02:00
|
|
|
|
2017-10-31 19:54:32 +01:00
|
|
|
"github.com/minio/minio/pkg/auth"
|
2020-01-27 23:12:34 +01:00
|
|
|
"github.com/minio/minio/pkg/bucket/policy"
|
|
|
|
"github.com/minio/minio/pkg/bucket/policy/condition"
|
2016-05-05 01:56:57 +02:00
|
|
|
)
|
|
|
|
|
2018-04-25 00:53:30 +02:00
|
|
|
func getAnonReadOnlyBucketPolicy(bucketName string) *policy.Policy {
|
|
|
|
return &policy.Policy{
|
|
|
|
Version: policy.DefaultVersion,
|
|
|
|
Statements: []policy.Statement{policy.NewStatement(
|
|
|
|
policy.Allow,
|
|
|
|
policy.NewPrincipal("*"),
|
|
|
|
policy.NewActionSet(policy.GetBucketLocationAction, policy.ListBucketAction),
|
|
|
|
policy.NewResourceSet(policy.NewResource(bucketName, "")),
|
|
|
|
condition.NewFunctions(),
|
|
|
|
)},
|
2016-05-05 01:56:57 +02:00
|
|
|
}
|
2018-04-25 00:53:30 +02:00
|
|
|
}
|
2016-05-05 01:56:57 +02:00
|
|
|
|
2018-04-25 00:53:30 +02:00
|
|
|
func getAnonWriteOnlyBucketPolicy(bucketName string) *policy.Policy {
|
|
|
|
return &policy.Policy{
|
|
|
|
Version: policy.DefaultVersion,
|
|
|
|
Statements: []policy.Statement{policy.NewStatement(
|
|
|
|
policy.Allow,
|
|
|
|
policy.NewPrincipal("*"),
|
|
|
|
policy.NewActionSet(
|
|
|
|
policy.GetBucketLocationAction,
|
|
|
|
policy.ListBucketMultipartUploadsAction,
|
|
|
|
),
|
|
|
|
policy.NewResourceSet(policy.NewResource(bucketName, "")),
|
|
|
|
condition.NewFunctions(),
|
|
|
|
)},
|
2016-05-05 01:56:57 +02:00
|
|
|
}
|
2018-04-25 00:53:30 +02:00
|
|
|
}
|
2016-05-05 01:56:57 +02:00
|
|
|
|
2018-04-25 00:53:30 +02:00
|
|
|
func getAnonReadOnlyObjectPolicy(bucketName, prefix string) *policy.Policy {
|
|
|
|
return &policy.Policy{
|
|
|
|
Version: policy.DefaultVersion,
|
|
|
|
Statements: []policy.Statement{policy.NewStatement(
|
|
|
|
policy.Allow,
|
|
|
|
policy.NewPrincipal("*"),
|
|
|
|
policy.NewActionSet(policy.GetObjectAction),
|
|
|
|
policy.NewResourceSet(policy.NewResource(bucketName, prefix)),
|
|
|
|
condition.NewFunctions(),
|
|
|
|
)},
|
2016-05-05 01:56:57 +02:00
|
|
|
}
|
|
|
|
}
|
2016-07-01 08:49:59 +02:00
|
|
|
|
2018-04-25 00:53:30 +02:00
|
|
|
func getAnonWriteOnlyObjectPolicy(bucketName, prefix string) *policy.Policy {
|
|
|
|
return &policy.Policy{
|
|
|
|
Version: policy.DefaultVersion,
|
|
|
|
Statements: []policy.Statement{policy.NewStatement(
|
|
|
|
policy.Allow,
|
|
|
|
policy.NewPrincipal("*"),
|
|
|
|
policy.NewActionSet(
|
|
|
|
policy.AbortMultipartUploadAction,
|
|
|
|
policy.DeleteObjectAction,
|
|
|
|
policy.ListMultipartUploadPartsAction,
|
|
|
|
policy.PutObjectAction,
|
|
|
|
),
|
|
|
|
policy.NewResourceSet(policy.NewResource(bucketName, prefix)),
|
|
|
|
condition.NewFunctions(),
|
|
|
|
)},
|
2016-07-01 08:49:59 +02:00
|
|
|
}
|
2016-07-03 04:05:16 +02:00
|
|
|
}
|
2016-07-01 08:49:59 +02:00
|
|
|
|
2016-07-03 04:05:16 +02:00
|
|
|
// Wrapper for calling Put Bucket Policy HTTP handler tests for both XL multiple disks and single node setup.
|
|
|
|
func TestPutBucketPolicyHandler(t *testing.T) {
|
2016-10-06 11:02:42 +02:00
|
|
|
ExecObjectLayerAPITest(t, testPutBucketPolicyHandler, []string{"PutBucketPolicy"})
|
2016-07-03 04:05:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// testPutBucketPolicyHandler - Test for Bucket policy end point.
|
2016-10-06 11:02:42 +02:00
|
|
|
func testPutBucketPolicyHandler(obj ObjectLayer, instanceType, bucketName string, apiRouter http.Handler,
|
2017-10-31 19:54:32 +01:00
|
|
|
credentials auth.Credentials, t *testing.T) {
|
2016-09-26 23:28:35 +02:00
|
|
|
|
2016-11-15 00:45:00 +01:00
|
|
|
bucketName1 := fmt.Sprintf("%s-1", bucketName)
|
2018-03-15 21:27:16 +01:00
|
|
|
if err := obj.MakeBucketWithLocation(context.Background(), bucketName1, ""); err != nil {
|
2016-11-15 00:45:00 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-07-03 04:05:16 +02:00
|
|
|
// template for constructing HTTP request body for PUT bucket policy.
|
2016-08-11 05:10:48 +02:00
|
|
|
bucketPolicyTemplate := `{"Version":"2012-10-17","Statement":[{"Sid":"","Effect":"Allow","Principal":{"AWS":["*"]},"Action":["s3:GetBucketLocation","s3:ListBucket"],"Resource":["arn:aws:s3:::%s"]},{"Sid":"","Effect":"Allow","Principal":{"AWS":["*"]},"Action":["s3:GetObject"],"Resource":["arn:aws:s3:::%s/this*"]}]}`
|
2016-07-03 04:05:16 +02:00
|
|
|
|
2018-04-28 00:02:54 +02:00
|
|
|
bucketPolicyTemplateWithoutVersion := `{"Version":"","Statement":[{"Sid":"","Effect":"Allow","Principal":{"AWS":["*"]},"Action":["s3:GetBucketLocation","s3:ListBucket"],"Resource":["arn:aws:s3:::%s"]},{"Sid":"","Effect":"Allow","Principal":{"AWS":["*"]},"Action":["s3:GetObject"],"Resource":["arn:aws:s3:::%s/this*"]}]}`
|
|
|
|
|
2016-07-03 04:05:16 +02:00
|
|
|
// test cases with sample input and expected output.
|
|
|
|
testCases := []struct {
|
|
|
|
bucketName string
|
2016-10-10 18:29:56 +02:00
|
|
|
// bucket policy to be set,
|
|
|
|
// set as request body.
|
|
|
|
bucketPolicyReader io.ReadSeeker
|
|
|
|
// length in bytes of the bucket policy being set.
|
|
|
|
policyLen int
|
|
|
|
accessKey string
|
|
|
|
secretKey string
|
2016-07-03 04:05:16 +02:00
|
|
|
// expected Response.
|
|
|
|
expectedRespStatus int
|
|
|
|
}{
|
2016-10-10 18:29:56 +02:00
|
|
|
// Test case - 1.
|
|
|
|
{
|
|
|
|
bucketName: bucketName,
|
|
|
|
bucketPolicyReader: bytes.NewReader([]byte(fmt.Sprintf(bucketPolicyTemplate, bucketName, bucketName))),
|
|
|
|
|
|
|
|
policyLen: len(fmt.Sprintf(bucketPolicyTemplate, bucketName, bucketName)),
|
2016-12-26 19:21:23 +01:00
|
|
|
accessKey: credentials.AccessKey,
|
|
|
|
secretKey: credentials.SecretKey,
|
2016-10-10 18:29:56 +02:00
|
|
|
expectedRespStatus: http.StatusNoContent,
|
|
|
|
},
|
|
|
|
// Test case - 2.
|
|
|
|
// Setting the content length to be more than max allowed size.
|
|
|
|
// Expecting StatusBadRequest (400).
|
|
|
|
{
|
|
|
|
bucketName: bucketName,
|
|
|
|
bucketPolicyReader: bytes.NewReader([]byte(fmt.Sprintf(bucketPolicyTemplate, bucketName, bucketName))),
|
|
|
|
|
2018-04-25 00:53:30 +02:00
|
|
|
policyLen: maxBucketPolicySize + 1,
|
2016-12-26 19:21:23 +01:00
|
|
|
accessKey: credentials.AccessKey,
|
|
|
|
secretKey: credentials.SecretKey,
|
2016-10-10 18:29:56 +02:00
|
|
|
expectedRespStatus: http.StatusBadRequest,
|
|
|
|
},
|
|
|
|
// Test case - 3.
|
|
|
|
// Case with content-length of the HTTP request set to 0.
|
|
|
|
// Expecting the HTTP response status to be StatusLengthRequired (411).
|
|
|
|
{
|
|
|
|
bucketName: bucketName,
|
|
|
|
bucketPolicyReader: bytes.NewReader([]byte(fmt.Sprintf(bucketPolicyTemplate, bucketName, bucketName))),
|
|
|
|
|
|
|
|
policyLen: 0,
|
2016-12-26 19:21:23 +01:00
|
|
|
accessKey: credentials.AccessKey,
|
|
|
|
secretKey: credentials.SecretKey,
|
2016-10-10 18:29:56 +02:00
|
|
|
expectedRespStatus: http.StatusLengthRequired,
|
|
|
|
},
|
|
|
|
// Test case - 4.
|
|
|
|
// setting the readSeeker to `nil`, bucket policy parser will fail.
|
|
|
|
{
|
|
|
|
bucketName: bucketName,
|
|
|
|
bucketPolicyReader: nil,
|
|
|
|
|
|
|
|
policyLen: 10,
|
2016-12-26 19:21:23 +01:00
|
|
|
accessKey: credentials.AccessKey,
|
|
|
|
secretKey: credentials.SecretKey,
|
2016-10-10 18:29:56 +02:00
|
|
|
expectedRespStatus: http.StatusBadRequest,
|
|
|
|
},
|
|
|
|
// Test case - 5.
|
|
|
|
// setting the keys to be empty.
|
|
|
|
// Expecting statusForbidden.
|
|
|
|
{
|
|
|
|
bucketName: bucketName,
|
|
|
|
bucketPolicyReader: nil,
|
|
|
|
|
|
|
|
policyLen: 10,
|
|
|
|
accessKey: "",
|
|
|
|
secretKey: "",
|
|
|
|
expectedRespStatus: http.StatusForbidden,
|
|
|
|
},
|
|
|
|
// Test case - 6.
|
|
|
|
// setting an invalid bucket policy.
|
|
|
|
// the bucket policy parser will fail.
|
|
|
|
{
|
2016-11-15 00:45:00 +01:00
|
|
|
bucketName: bucketName,
|
2016-10-10 18:29:56 +02:00
|
|
|
bucketPolicyReader: bytes.NewReader([]byte("dummy-policy")),
|
|
|
|
|
|
|
|
policyLen: len([]byte("dummy-policy")),
|
2016-12-26 19:21:23 +01:00
|
|
|
accessKey: credentials.AccessKey,
|
|
|
|
secretKey: credentials.SecretKey,
|
2016-10-10 18:29:56 +02:00
|
|
|
expectedRespStatus: http.StatusBadRequest,
|
|
|
|
},
|
|
|
|
// Test case - 7.
|
|
|
|
// Different bucket name used in the HTTP request and the policy string.
|
|
|
|
// checkBucketPolicyResources should fail.
|
|
|
|
{
|
2016-11-15 00:45:00 +01:00
|
|
|
bucketName: bucketName1,
|
2016-10-10 18:29:56 +02:00
|
|
|
bucketPolicyReader: bytes.NewReader([]byte(fmt.Sprintf(bucketPolicyTemplate, bucketName, bucketName))),
|
|
|
|
|
|
|
|
policyLen: len(fmt.Sprintf(bucketPolicyTemplate, bucketName, bucketName)),
|
2016-12-26 19:21:23 +01:00
|
|
|
accessKey: credentials.AccessKey,
|
|
|
|
secretKey: credentials.SecretKey,
|
2016-10-10 18:29:56 +02:00
|
|
|
expectedRespStatus: http.StatusBadRequest,
|
|
|
|
},
|
|
|
|
// Test case - 8.
|
|
|
|
// non-existent bucket is used.
|
|
|
|
// writing BucketPolicy should fail.
|
2018-04-28 00:02:54 +02:00
|
|
|
// should result in 404 StatusNotFound
|
2016-10-10 18:29:56 +02:00
|
|
|
{
|
|
|
|
bucketName: "non-existent-bucket",
|
|
|
|
bucketPolicyReader: bytes.NewReader([]byte(fmt.Sprintf(bucketPolicyTemplate, "non-existent-bucket", "non-existent-bucket"))),
|
|
|
|
|
|
|
|
policyLen: len(fmt.Sprintf(bucketPolicyTemplate, bucketName, bucketName)),
|
2016-12-26 19:21:23 +01:00
|
|
|
accessKey: credentials.AccessKey,
|
|
|
|
secretKey: credentials.SecretKey,
|
2016-11-15 00:45:00 +01:00
|
|
|
expectedRespStatus: http.StatusNotFound,
|
2016-10-10 18:29:56 +02:00
|
|
|
},
|
|
|
|
// Test case - 9.
|
2018-04-24 05:27:33 +02:00
|
|
|
// non-existent bucket is used (with invalid bucket name)
|
2016-10-10 18:29:56 +02:00
|
|
|
// writing BucketPolicy should fail.
|
2018-04-28 00:02:54 +02:00
|
|
|
// should result in 404 StatusNotFound
|
2016-10-10 18:29:56 +02:00
|
|
|
{
|
|
|
|
bucketName: ".invalid-bucket",
|
|
|
|
bucketPolicyReader: bytes.NewReader([]byte(fmt.Sprintf(bucketPolicyTemplate, ".invalid-bucket", ".invalid-bucket"))),
|
|
|
|
|
|
|
|
policyLen: len(fmt.Sprintf(bucketPolicyTemplate, bucketName, bucketName)),
|
2016-12-26 19:21:23 +01:00
|
|
|
accessKey: credentials.AccessKey,
|
|
|
|
secretKey: credentials.SecretKey,
|
2018-04-24 05:27:33 +02:00
|
|
|
expectedRespStatus: http.StatusNotFound,
|
2016-10-10 18:29:56 +02:00
|
|
|
},
|
2018-04-28 00:02:54 +02:00
|
|
|
// Test case - 10.
|
|
|
|
// Existent bucket with policy with Version field empty.
|
|
|
|
// writing BucketPolicy should fail.
|
|
|
|
// should result in 400 StatusBadRequest.
|
|
|
|
{
|
|
|
|
bucketName: bucketName,
|
|
|
|
bucketPolicyReader: bytes.NewReader([]byte(fmt.Sprintf(bucketPolicyTemplateWithoutVersion, bucketName, bucketName))),
|
|
|
|
|
|
|
|
policyLen: len(fmt.Sprintf(bucketPolicyTemplateWithoutVersion, bucketName, bucketName)),
|
|
|
|
accessKey: credentials.AccessKey,
|
|
|
|
secretKey: credentials.SecretKey,
|
|
|
|
expectedRespStatus: http.StatusBadRequest,
|
|
|
|
},
|
2016-07-03 04:05:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Iterating over the test cases, calling the function under test and asserting the response.
|
|
|
|
for i, testCase := range testCases {
|
|
|
|
// obtain the put bucket policy request body.
|
|
|
|
// initialize HTTP NewRecorder, this records any mutations to response writer inside the handler.
|
2016-09-30 23:32:13 +02:00
|
|
|
recV4 := httptest.NewRecorder()
|
2016-07-03 04:05:16 +02:00
|
|
|
// construct HTTP request for PUT bucket policy endpoint.
|
2016-09-30 23:32:13 +02:00
|
|
|
reqV4, err := newTestSignedRequestV4("PUT", getPutPolicyURL("", testCase.bucketName),
|
2018-09-21 04:22:09 +02:00
|
|
|
int64(testCase.policyLen), testCase.bucketPolicyReader, testCase.accessKey, testCase.secretKey, nil)
|
2016-07-03 04:05:16 +02:00
|
|
|
if err != nil {
|
2016-07-31 23:11:14 +02:00
|
|
|
t.Fatalf("Test %d: %s: Failed to create HTTP request for PutBucketPolicyHandler: <ERROR> %v", i+1, instanceType, err)
|
2016-07-03 04:05:16 +02:00
|
|
|
}
|
|
|
|
// Since `apiRouter` satisfies `http.Handler` it has a ServeHTTP to execute the logic ofthe handler.
|
|
|
|
// Call the ServeHTTP to execute the handler.
|
2016-09-30 23:32:13 +02:00
|
|
|
apiRouter.ServeHTTP(recV4, reqV4)
|
|
|
|
if recV4.Code != testCase.expectedRespStatus {
|
|
|
|
t.Errorf("Test %d: %s: Expected the response status to be `%d`, but instead found `%d`", i+1, instanceType, testCase.expectedRespStatus, recV4.Code)
|
|
|
|
}
|
|
|
|
// initialize HTTP NewRecorder, this records any mutations to response writer inside the handler.
|
|
|
|
recV2 := httptest.NewRecorder()
|
|
|
|
// construct HTTP request for PUT bucket policy endpoint.
|
|
|
|
reqV2, err := newTestSignedRequestV2("PUT", getPutPolicyURL("", testCase.bucketName),
|
2018-09-21 04:22:09 +02:00
|
|
|
int64(testCase.policyLen), testCase.bucketPolicyReader, testCase.accessKey, testCase.secretKey, nil)
|
2016-09-30 23:32:13 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Test %d: %s: Failed to create HTTP request for PutBucketPolicyHandler: <ERROR> %v", i+1, instanceType, err)
|
|
|
|
}
|
|
|
|
// Since `apiRouter` satisfies `http.Handler` it has a ServeHTTP to execute the logic ofthe handler.
|
|
|
|
// Call the ServeHTTP to execute the handler.
|
|
|
|
apiRouter.ServeHTTP(recV2, reqV2)
|
|
|
|
if recV2.Code != testCase.expectedRespStatus {
|
|
|
|
t.Errorf("Test %d: %s: Expected the response status to be `%d`, but instead found `%d`", i+1, instanceType, testCase.expectedRespStatus, recV2.Code)
|
2016-07-03 04:05:16 +02:00
|
|
|
}
|
|
|
|
}
|
2016-10-06 22:34:33 +02:00
|
|
|
|
2016-10-08 10:04:26 +02:00
|
|
|
// Test for Anonymous/unsigned http request.
|
|
|
|
// Bucket policy related functions doesn't support anonymous requests, setting policies shouldn't make a difference.
|
|
|
|
bucketPolicyStr := fmt.Sprintf(bucketPolicyTemplate, bucketName, bucketName)
|
|
|
|
// create unsigned HTTP request for PutBucketPolicyHandler.
|
|
|
|
anonReq, err := newTestRequest("PUT", getPutPolicyURL("", bucketName),
|
|
|
|
int64(len(bucketPolicyStr)), bytes.NewReader([]byte(bucketPolicyStr)))
|
|
|
|
|
|
|
|
if err != nil {
|
2019-04-09 20:39:42 +02:00
|
|
|
t.Fatalf("MinIO %s: Failed to create an anonymous request for bucket \"%s\": <ERROR> %v",
|
2016-10-08 10:04:26 +02:00
|
|
|
instanceType, bucketName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExecObjectLayerAPIAnonTest - Calls the HTTP API handler using the anonymous request, validates the ErrAccessDeniedResponse,
|
|
|
|
// sets the bucket policy using the policy statement generated from `getWriteOnlyObjectStatement` so that the
|
|
|
|
// unsigned request goes through and its validated again.
|
2018-04-25 00:53:30 +02:00
|
|
|
ExecObjectLayerAPIAnonTest(t, obj, "PutBucketPolicyHandler", bucketName, "", instanceType, apiRouter, anonReq, getAnonWriteOnlyBucketPolicy(bucketName))
|
2016-10-08 10:04:26 +02:00
|
|
|
|
2016-10-06 22:34:33 +02:00
|
|
|
// HTTP request for testing when `objectLayer` is set to `nil`.
|
|
|
|
// There is no need to use an existing bucket and valid input for creating the request
|
|
|
|
// since the `objectLayer==nil` check is performed before any other checks inside the handlers.
|
|
|
|
// The only aim is to generate an HTTP request in a way that the relevant/registered end point is evoked/called.
|
|
|
|
nilBucket := "dummy-bucket"
|
|
|
|
|
|
|
|
nilReq, err := newTestSignedRequestV4("PUT", getPutPolicyURL("", nilBucket),
|
2018-09-21 04:22:09 +02:00
|
|
|
0, nil, "", "", nil)
|
2016-10-06 22:34:33 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2019-04-09 20:39:42 +02:00
|
|
|
t.Errorf("MinIO %s: Failed to create HTTP request for testing the response when object Layer is set to `nil`.", instanceType)
|
2016-10-06 22:34:33 +02:00
|
|
|
}
|
|
|
|
// execute the object layer set to `nil` test.
|
|
|
|
// `ExecObjectLayerAPINilTest` manages the operation.
|
|
|
|
ExecObjectLayerAPINilTest(t, nilBucket, "", instanceType, apiRouter, nilReq)
|
2018-02-10 00:19:30 +01:00
|
|
|
|
2016-07-01 08:49:59 +02:00
|
|
|
}
|
2016-07-04 07:35:30 +02:00
|
|
|
|
|
|
|
// Wrapper for calling Get Bucket Policy HTTP handler tests for both XL multiple disks and single node setup.
|
|
|
|
func TestGetBucketPolicyHandler(t *testing.T) {
|
2016-10-06 11:02:42 +02:00
|
|
|
ExecObjectLayerAPITest(t, testGetBucketPolicyHandler, []string{"PutBucketPolicy", "GetBucketPolicy"})
|
2016-07-04 07:35:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// testGetBucketPolicyHandler - Test for end point which fetches the access policy json of the given bucket.
|
2016-10-06 11:02:42 +02:00
|
|
|
func testGetBucketPolicyHandler(obj ObjectLayer, instanceType, bucketName string, apiRouter http.Handler,
|
2017-10-31 19:54:32 +01:00
|
|
|
credentials auth.Credentials, t *testing.T) {
|
2016-07-04 07:35:30 +02:00
|
|
|
// template for constructing HTTP request body for PUT bucket policy.
|
2018-04-25 00:53:30 +02:00
|
|
|
bucketPolicyTemplate := `{"Version":"2012-10-17","Statement":[{"Action":["s3:GetBucketLocation","s3:ListBucket"],"Effect":"Allow","Principal":{"AWS":["*"]},"Resource":["arn:aws:s3:::%s"]},{"Action":["s3:GetObject"],"Effect":"Allow","Principal":{"AWS":["*"]},"Resource":["arn:aws:s3:::%s/this*"]}]}`
|
2016-07-04 07:35:30 +02:00
|
|
|
|
|
|
|
// Writing bucket policy before running test on GetBucketPolicy.
|
|
|
|
putTestPolicies := []struct {
|
|
|
|
bucketName string
|
|
|
|
accessKey string
|
|
|
|
secretKey string
|
|
|
|
// expected Response.
|
|
|
|
expectedRespStatus int
|
|
|
|
}{
|
2016-12-26 19:21:23 +01:00
|
|
|
{bucketName, credentials.AccessKey, credentials.SecretKey, http.StatusNoContent},
|
2016-07-04 07:35:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Iterating over the cases and writing the bucket policy.
|
|
|
|
// its required to write the policies first before running tests on GetBucketPolicy.
|
|
|
|
for i, testPolicy := range putTestPolicies {
|
|
|
|
// obtain the put bucket policy request body.
|
|
|
|
bucketPolicyStr := fmt.Sprintf(bucketPolicyTemplate, testPolicy.bucketName, testPolicy.bucketName)
|
|
|
|
// initialize HTTP NewRecorder, this records any mutations to response writer inside the handler.
|
2016-09-30 23:32:13 +02:00
|
|
|
recV4 := httptest.NewRecorder()
|
2016-07-04 07:35:30 +02:00
|
|
|
// construct HTTP request for PUT bucket policy endpoint.
|
2016-09-30 23:32:13 +02:00
|
|
|
reqV4, err := newTestSignedRequestV4("PUT", getPutPolicyURL("", testPolicy.bucketName),
|
2018-09-21 04:22:09 +02:00
|
|
|
int64(len(bucketPolicyStr)), bytes.NewReader([]byte(bucketPolicyStr)), testPolicy.accessKey, testPolicy.secretKey, nil)
|
2016-07-04 07:35:30 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Test %d: Failed to create HTTP request for PutBucketPolicyHandler: <ERROR> %v", i+1, err)
|
|
|
|
}
|
|
|
|
// Since `apiRouter` satisfies `http.Handler` it has a ServeHTTP to execute the logic ofthe handler.
|
|
|
|
// Call the ServeHTTP to execute the handler.
|
2016-09-30 23:32:13 +02:00
|
|
|
apiRouter.ServeHTTP(recV4, reqV4)
|
|
|
|
if recV4.Code != testPolicy.expectedRespStatus {
|
|
|
|
t.Fatalf("Case %d: Expected the response status to be `%d`, but instead found `%d`", i+1, testPolicy.expectedRespStatus, recV4.Code)
|
|
|
|
}
|
|
|
|
// initialize HTTP NewRecorder, this records any mutations to response writer inside the handler.
|
|
|
|
recV2 := httptest.NewRecorder()
|
|
|
|
// construct HTTP request for PUT bucket policy endpoint.
|
|
|
|
reqV2, err := newTestSignedRequestV2("PUT", getPutPolicyURL("", testPolicy.bucketName),
|
2018-09-21 04:22:09 +02:00
|
|
|
int64(len(bucketPolicyStr)), bytes.NewReader([]byte(bucketPolicyStr)), testPolicy.accessKey, testPolicy.secretKey, nil)
|
2016-09-30 23:32:13 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Test %d: Failed to create HTTP request for PutBucketPolicyHandler: <ERROR> %v", i+1, err)
|
|
|
|
}
|
|
|
|
// Since `apiRouter` satisfies `http.Handler` it has a ServeHTTP to execute the logic ofthe handler.
|
|
|
|
// Call the ServeHTTP to execute the handler.
|
|
|
|
apiRouter.ServeHTTP(recV2, reqV2)
|
|
|
|
if recV2.Code != testPolicy.expectedRespStatus {
|
|
|
|
t.Fatalf("Case %d: Expected the response status to be `%d`, but instead found `%d`", i+1, testPolicy.expectedRespStatus, recV2.Code)
|
2016-07-04 07:35:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test cases with inputs and expected result for GetBucketPolicyHandler.
|
|
|
|
testCases := []struct {
|
|
|
|
bucketName string
|
|
|
|
accessKey string
|
|
|
|
secretKey string
|
|
|
|
// expected output.
|
|
|
|
expectedBucketPolicy string
|
|
|
|
expectedRespStatus int
|
|
|
|
}{
|
2016-10-10 18:29:56 +02:00
|
|
|
// Test case - 1.
|
|
|
|
// Case which valid inputs, expected to return success status of 200OK.
|
|
|
|
{
|
|
|
|
bucketName: bucketName,
|
2016-12-26 19:21:23 +01:00
|
|
|
accessKey: credentials.AccessKey,
|
|
|
|
secretKey: credentials.SecretKey,
|
2016-10-10 18:29:56 +02:00
|
|
|
expectedBucketPolicy: bucketPolicyTemplate,
|
|
|
|
expectedRespStatus: http.StatusOK,
|
|
|
|
},
|
|
|
|
// Test case - 2.
|
|
|
|
// Case with non-existent bucket name.
|
|
|
|
{
|
|
|
|
bucketName: "non-existent-bucket",
|
2016-12-26 19:21:23 +01:00
|
|
|
accessKey: credentials.AccessKey,
|
|
|
|
secretKey: credentials.SecretKey,
|
2016-10-10 18:29:56 +02:00
|
|
|
expectedBucketPolicy: bucketPolicyTemplate,
|
2016-11-15 00:45:00 +01:00
|
|
|
expectedRespStatus: http.StatusNotFound,
|
2016-10-10 18:29:56 +02:00
|
|
|
},
|
|
|
|
// Test case - 3.
|
2018-04-24 05:27:33 +02:00
|
|
|
// Case with non-existent bucket name.
|
2016-10-10 18:29:56 +02:00
|
|
|
{
|
|
|
|
bucketName: ".invalid-bucket-name",
|
2016-12-26 19:21:23 +01:00
|
|
|
accessKey: credentials.AccessKey,
|
|
|
|
secretKey: credentials.SecretKey,
|
2016-10-10 18:29:56 +02:00
|
|
|
expectedBucketPolicy: "",
|
2018-04-24 05:27:33 +02:00
|
|
|
expectedRespStatus: http.StatusNotFound,
|
2016-10-10 18:29:56 +02:00
|
|
|
},
|
2016-07-04 07:35:30 +02:00
|
|
|
}
|
|
|
|
// Iterating over the cases, fetching the policy and validating the response.
|
|
|
|
for i, testCase := range testCases {
|
|
|
|
// expected bucket policy json string.
|
|
|
|
expectedBucketPolicyStr := fmt.Sprintf(testCase.expectedBucketPolicy, testCase.bucketName, testCase.bucketName)
|
|
|
|
// initialize HTTP NewRecorder, this records any mutations to response writer inside the handler.
|
2016-09-30 23:32:13 +02:00
|
|
|
recV4 := httptest.NewRecorder()
|
2016-07-04 07:35:30 +02:00
|
|
|
// construct HTTP request for PUT bucket policy endpoint.
|
2016-09-30 23:32:13 +02:00
|
|
|
reqV4, err := newTestSignedRequestV4("GET", getGetPolicyURL("", testCase.bucketName),
|
2018-09-21 04:22:09 +02:00
|
|
|
0, nil, testCase.accessKey, testCase.secretKey, nil)
|
2016-07-04 07:35:30 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Test %d: Failed to create HTTP request for GetBucketPolicyHandler: <ERROR> %v", i+1, err)
|
|
|
|
}
|
|
|
|
// Since `apiRouter` satisfies `http.Handler` it has a ServeHTTP to execute the logic of the handler.
|
|
|
|
// Call the ServeHTTP to execute the handler, GetBucketPolicyHandler handles the request.
|
2016-09-30 23:32:13 +02:00
|
|
|
apiRouter.ServeHTTP(recV4, reqV4)
|
|
|
|
// Assert the response code with the expected status.
|
|
|
|
if recV4.Code != testCase.expectedRespStatus {
|
|
|
|
t.Fatalf("Case %d: Expected the response status to be `%d`, but instead found `%d`", i+1, testCase.expectedRespStatus, recV4.Code)
|
|
|
|
}
|
|
|
|
// read the response body.
|
|
|
|
bucketPolicyReadBuf, err := ioutil.ReadAll(recV4.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Test %d: %s: Failed parsing response body: <ERROR> %v", i+1, instanceType, err)
|
|
|
|
}
|
2016-10-10 18:29:56 +02:00
|
|
|
|
|
|
|
if recV4.Code != testCase.expectedRespStatus {
|
|
|
|
// Verify whether the bucket policy fetched is same as the one inserted.
|
2018-07-24 23:17:58 +02:00
|
|
|
var expectedPolicy *policy.Policy
|
|
|
|
expectedPolicy, err = policy.ParseConfig(strings.NewReader(expectedBucketPolicyStr), testCase.bucketName)
|
2018-04-25 00:53:30 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error. %v", err)
|
|
|
|
}
|
2018-07-24 23:17:58 +02:00
|
|
|
var gotPolicy *policy.Policy
|
|
|
|
gotPolicy, err = policy.ParseConfig(bytes.NewReader(bucketPolicyReadBuf), testCase.bucketName)
|
2018-04-25 00:53:30 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error. %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(expectedPolicy, gotPolicy) {
|
2016-10-10 18:29:56 +02:00
|
|
|
t.Errorf("Test %d: %s: Bucket policy differs from expected value.", i+1, instanceType)
|
|
|
|
}
|
2016-09-30 23:32:13 +02:00
|
|
|
}
|
|
|
|
// initialize HTTP NewRecorder, this records any mutations to response writer inside the handler.
|
|
|
|
recV2 := httptest.NewRecorder()
|
|
|
|
// construct HTTP request for PUT bucket policy endpoint.
|
|
|
|
reqV2, err := newTestSignedRequestV2("GET", getGetPolicyURL("", testCase.bucketName),
|
2018-09-21 04:22:09 +02:00
|
|
|
0, nil, testCase.accessKey, testCase.secretKey, nil)
|
2016-09-30 23:32:13 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Test %d: Failed to create HTTP request for GetBucketPolicyHandler: <ERROR> %v", i+1, err)
|
|
|
|
}
|
|
|
|
// Since `apiRouter` satisfies `http.Handler` it has a ServeHTTP to execute the logic of the handler.
|
|
|
|
// Call the ServeHTTP to execute the handler, GetBucketPolicyHandler handles the request.
|
|
|
|
apiRouter.ServeHTTP(recV2, reqV2)
|
2016-07-04 07:35:30 +02:00
|
|
|
// Assert the response code with the expected status.
|
2016-09-30 23:32:13 +02:00
|
|
|
if recV2.Code != testCase.expectedRespStatus {
|
|
|
|
t.Fatalf("Case %d: Expected the response status to be `%d`, but instead found `%d`", i+1, testCase.expectedRespStatus, recV2.Code)
|
2016-07-04 07:35:30 +02:00
|
|
|
}
|
|
|
|
// read the response body.
|
2016-09-30 23:32:13 +02:00
|
|
|
bucketPolicyReadBuf, err = ioutil.ReadAll(recV2.Body)
|
2016-07-04 07:35:30 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Test %d: %s: Failed parsing response body: <ERROR> %v", i+1, instanceType, err)
|
|
|
|
}
|
2016-10-10 18:29:56 +02:00
|
|
|
if recV2.Code == http.StatusOK {
|
|
|
|
// Verify whether the bucket policy fetched is same as the one inserted.
|
2018-04-25 00:53:30 +02:00
|
|
|
expectedPolicy, err := policy.ParseConfig(strings.NewReader(expectedBucketPolicyStr), testCase.bucketName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error. %v", err)
|
|
|
|
}
|
|
|
|
gotPolicy, err := policy.ParseConfig(bytes.NewReader(bucketPolicyReadBuf), testCase.bucketName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error. %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(expectedPolicy, gotPolicy) {
|
2016-10-10 18:29:56 +02:00
|
|
|
t.Errorf("Test %d: %s: Bucket policy differs from expected value.", i+1, instanceType)
|
|
|
|
}
|
2016-07-04 07:35:30 +02:00
|
|
|
}
|
|
|
|
}
|
2016-10-06 22:34:33 +02:00
|
|
|
|
2016-10-08 10:04:26 +02:00
|
|
|
// Test for Anonymous/unsigned http request.
|
|
|
|
// Bucket policy related functions doesn't support anonymous requests, setting policies shouldn't make a difference.
|
|
|
|
// create unsigned HTTP request for PutBucketPolicyHandler.
|
|
|
|
anonReq, err := newTestRequest("GET", getPutPolicyURL("", bucketName), 0, nil)
|
|
|
|
|
|
|
|
if err != nil {
|
2019-04-09 20:39:42 +02:00
|
|
|
t.Fatalf("MinIO %s: Failed to create an anonymous request for bucket \"%s\": <ERROR> %v",
|
2016-10-08 10:04:26 +02:00
|
|
|
instanceType, bucketName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExecObjectLayerAPIAnonTest - Calls the HTTP API handler using the anonymous request, validates the ErrAccessDeniedResponse,
|
|
|
|
// sets the bucket policy using the policy statement generated from `getWriteOnlyObjectStatement` so that the
|
|
|
|
// unsigned request goes through and its validated again.
|
2018-04-25 00:53:30 +02:00
|
|
|
ExecObjectLayerAPIAnonTest(t, obj, "GetBucketPolicyHandler", bucketName, "", instanceType, apiRouter, anonReq, getAnonReadOnlyBucketPolicy(bucketName))
|
2016-10-08 10:04:26 +02:00
|
|
|
|
2016-10-06 22:34:33 +02:00
|
|
|
// HTTP request for testing when `objectLayer` is set to `nil`.
|
|
|
|
// There is no need to use an existing bucket and valid input for creating the request
|
|
|
|
// since the `objectLayer==nil` check is performed before any other checks inside the handlers.
|
|
|
|
// The only aim is to generate an HTTP request in a way that the relevant/registered end point is evoked/called.
|
|
|
|
nilBucket := "dummy-bucket"
|
|
|
|
|
|
|
|
nilReq, err := newTestSignedRequestV4("GET", getGetPolicyURL("", nilBucket),
|
2018-09-21 04:22:09 +02:00
|
|
|
0, nil, "", "", nil)
|
2016-10-06 22:34:33 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2019-04-09 20:39:42 +02:00
|
|
|
t.Errorf("MinIO %s: Failed to create HTTP request for testing the response when object Layer is set to `nil`.", instanceType)
|
2016-10-06 22:34:33 +02:00
|
|
|
}
|
|
|
|
// execute the object layer set to `nil` test.
|
|
|
|
// `ExecObjectLayerAPINilTest` manages the operation.
|
|
|
|
ExecObjectLayerAPINilTest(t, nilBucket, "", instanceType, apiRouter, nilReq)
|
2016-07-04 07:35:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wrapper for calling Delete Bucket Policy HTTP handler tests for both XL multiple disks and single node setup.
|
|
|
|
func TestDeleteBucketPolicyHandler(t *testing.T) {
|
2016-10-06 11:02:42 +02:00
|
|
|
ExecObjectLayerAPITest(t, testDeleteBucketPolicyHandler, []string{"PutBucketPolicy", "DeleteBucketPolicy"})
|
2016-07-04 07:35:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// testDeleteBucketPolicyHandler - Test for Delete bucket policy end point.
|
2016-10-06 11:02:42 +02:00
|
|
|
func testDeleteBucketPolicyHandler(obj ObjectLayer, instanceType, bucketName string, apiRouter http.Handler,
|
2017-10-31 19:54:32 +01:00
|
|
|
credentials auth.Credentials, t *testing.T) {
|
2016-09-26 23:28:35 +02:00
|
|
|
|
2016-07-04 07:35:30 +02:00
|
|
|
// template for constructing HTTP request body for PUT bucket policy.
|
|
|
|
bucketPolicyTemplate := `{
|
|
|
|
"Version": "2012-10-17",
|
|
|
|
"Statement": [
|
|
|
|
{
|
|
|
|
"Action": [
|
|
|
|
"s3:GetBucketLocation",
|
|
|
|
"s3:ListBucket"
|
|
|
|
],
|
|
|
|
"Effect": "Allow",
|
|
|
|
"Principal": {
|
|
|
|
"AWS": [
|
|
|
|
"*"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"Resource": [
|
|
|
|
"arn:aws:s3:::%s"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Action": [
|
|
|
|
"s3:GetObject"
|
|
|
|
],
|
|
|
|
"Effect": "Allow",
|
|
|
|
"Principal": {
|
|
|
|
"AWS": [
|
|
|
|
"*"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"Resource": [
|
|
|
|
"arn:aws:s3:::%s/this*"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}`
|
|
|
|
|
|
|
|
// Writing bucket policy before running test on DeleteBucketPolicy.
|
|
|
|
putTestPolicies := []struct {
|
|
|
|
bucketName string
|
|
|
|
accessKey string
|
|
|
|
secretKey string
|
|
|
|
// expected Response.
|
|
|
|
expectedRespStatus int
|
|
|
|
}{
|
2016-10-10 18:29:56 +02:00
|
|
|
{
|
|
|
|
bucketName: bucketName,
|
2016-12-26 19:21:23 +01:00
|
|
|
accessKey: credentials.AccessKey,
|
|
|
|
secretKey: credentials.SecretKey,
|
2016-10-10 18:29:56 +02:00
|
|
|
expectedRespStatus: http.StatusNoContent,
|
|
|
|
},
|
2016-07-04 07:35:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Iterating over the cases and writing the bucket policy.
|
|
|
|
// its required to write the policies first before running tests on GetBucketPolicy.
|
|
|
|
for i, testPolicy := range putTestPolicies {
|
|
|
|
// obtain the put bucket policy request body.
|
|
|
|
bucketPolicyStr := fmt.Sprintf(bucketPolicyTemplate, testPolicy.bucketName, testPolicy.bucketName)
|
|
|
|
// initialize HTTP NewRecorder, this records any mutations to response writer inside the handler.
|
2016-09-30 23:32:13 +02:00
|
|
|
recV4 := httptest.NewRecorder()
|
2016-07-04 07:35:30 +02:00
|
|
|
// construct HTTP request for PUT bucket policy endpoint.
|
2016-09-30 23:32:13 +02:00
|
|
|
reqV4, err := newTestSignedRequestV4("PUT", getPutPolicyURL("", testPolicy.bucketName),
|
2018-09-21 04:22:09 +02:00
|
|
|
int64(len(bucketPolicyStr)), bytes.NewReader([]byte(bucketPolicyStr)), testPolicy.accessKey, testPolicy.secretKey, nil)
|
2016-07-04 07:35:30 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Test %d: Failed to create HTTP request for PutBucketPolicyHandler: <ERROR> %v", i+1, err)
|
|
|
|
}
|
|
|
|
// Since `apiRouter` satisfies `http.Handler` it has a ServeHTTP to execute the logic of the handler.
|
|
|
|
// Call the ServeHTTP to execute the handler.
|
2016-09-30 23:32:13 +02:00
|
|
|
apiRouter.ServeHTTP(recV4, reqV4)
|
|
|
|
if recV4.Code != testPolicy.expectedRespStatus {
|
|
|
|
t.Fatalf("Case %d: Expected the response status to be `%d`, but instead found `%d`", i+1, testPolicy.expectedRespStatus, recV4.Code)
|
2016-07-04 07:35:30 +02:00
|
|
|
}
|
|
|
|
}
|
2016-09-30 23:32:13 +02:00
|
|
|
|
2016-07-04 07:35:30 +02:00
|
|
|
// testcases with input and expected output for DeleteBucketPolicyHandler.
|
|
|
|
testCases := []struct {
|
|
|
|
bucketName string
|
|
|
|
accessKey string
|
|
|
|
secretKey string
|
2016-10-10 18:29:56 +02:00
|
|
|
// expected response.
|
2016-07-04 07:35:30 +02:00
|
|
|
expectedRespStatus int
|
|
|
|
}{
|
2016-10-10 18:29:56 +02:00
|
|
|
// Test case - 1.
|
|
|
|
{
|
|
|
|
bucketName: bucketName,
|
2016-12-26 19:21:23 +01:00
|
|
|
accessKey: credentials.AccessKey,
|
|
|
|
secretKey: credentials.SecretKey,
|
2016-10-10 18:29:56 +02:00
|
|
|
expectedRespStatus: http.StatusNoContent,
|
|
|
|
},
|
|
|
|
// Test case - 2.
|
|
|
|
// Case with non-existent-bucket.
|
|
|
|
{
|
|
|
|
bucketName: "non-existent-bucket",
|
2016-12-26 19:21:23 +01:00
|
|
|
accessKey: credentials.AccessKey,
|
|
|
|
secretKey: credentials.SecretKey,
|
2016-11-15 00:45:00 +01:00
|
|
|
expectedRespStatus: http.StatusNotFound,
|
2016-10-10 18:29:56 +02:00
|
|
|
},
|
|
|
|
// Test case - 3.
|
2018-04-24 05:27:33 +02:00
|
|
|
// Case with non-existent-bucket.
|
2016-10-10 18:29:56 +02:00
|
|
|
{
|
|
|
|
bucketName: ".invalid-bucket-name",
|
2016-12-26 19:21:23 +01:00
|
|
|
accessKey: credentials.AccessKey,
|
|
|
|
secretKey: credentials.SecretKey,
|
2018-04-24 05:27:33 +02:00
|
|
|
expectedRespStatus: http.StatusNotFound,
|
2016-10-10 18:29:56 +02:00
|
|
|
},
|
2016-07-04 07:35:30 +02:00
|
|
|
}
|
|
|
|
// Iterating over the cases and deleting the bucket policy and then asserting response.
|
|
|
|
for i, testCase := range testCases {
|
|
|
|
// initialize HTTP NewRecorder, this records any mutations to response writer inside the handler.
|
2016-09-30 23:32:13 +02:00
|
|
|
recV4 := httptest.NewRecorder()
|
2016-07-04 07:35:30 +02:00
|
|
|
// construct HTTP request for Delete bucket policy endpoint.
|
2016-09-30 23:32:13 +02:00
|
|
|
reqV4, err := newTestSignedRequestV4("DELETE", getDeletePolicyURL("", testCase.bucketName),
|
2018-09-21 04:22:09 +02:00
|
|
|
0, nil, testCase.accessKey, testCase.secretKey, nil)
|
2016-07-04 07:35:30 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Test %d: Failed to create HTTP request for GetBucketPolicyHandler: <ERROR> %v", i+1, err)
|
|
|
|
}
|
|
|
|
// Since `apiRouter` satisfies `http.Handler` it has a ServeHTTP to execute the logic of the handler.
|
|
|
|
// Call the ServeHTTP to execute the handler, DeleteBucketPolicyHandler handles the request.
|
2016-09-30 23:32:13 +02:00
|
|
|
apiRouter.ServeHTTP(recV4, reqV4)
|
2016-07-04 07:35:30 +02:00
|
|
|
// Assert the response code with the expected status.
|
2016-09-30 23:32:13 +02:00
|
|
|
if recV4.Code != testCase.expectedRespStatus {
|
|
|
|
t.Fatalf("Case %d: Expected the response status to be `%d`, but instead found `%d`", i+1, testCase.expectedRespStatus, recV4.Code)
|
2016-07-04 07:35:30 +02:00
|
|
|
}
|
2016-09-30 23:32:13 +02:00
|
|
|
}
|
2016-07-04 07:35:30 +02:00
|
|
|
|
2016-09-30 23:32:13 +02:00
|
|
|
// Iterating over the cases and writing the bucket policy.
|
|
|
|
// its required to write the policies first before running tests on GetBucketPolicy.
|
|
|
|
for i, testPolicy := range putTestPolicies {
|
|
|
|
// obtain the put bucket policy request body.
|
|
|
|
bucketPolicyStr := fmt.Sprintf(bucketPolicyTemplate, testPolicy.bucketName, testPolicy.bucketName)
|
|
|
|
// initialize HTTP NewRecorder, this records any mutations to response writer inside the handler.
|
|
|
|
recV2 := httptest.NewRecorder()
|
|
|
|
// construct HTTP request for PUT bucket policy endpoint.
|
|
|
|
reqV2, err := newTestSignedRequestV2("PUT", getPutPolicyURL("", testPolicy.bucketName),
|
2018-09-21 04:22:09 +02:00
|
|
|
int64(len(bucketPolicyStr)), bytes.NewReader([]byte(bucketPolicyStr)), testPolicy.accessKey, testPolicy.secretKey, nil)
|
2016-09-30 23:32:13 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Test %d: Failed to create HTTP request for PutBucketPolicyHandler: <ERROR> %v", i+1, err)
|
|
|
|
}
|
|
|
|
// Since `apiRouter` satisfies `http.Handler` it has a ServeHTTP to execute the logic of the handler.
|
|
|
|
// Call the ServeHTTP to execute the handler.
|
|
|
|
apiRouter.ServeHTTP(recV2, reqV2)
|
|
|
|
if recV2.Code != testPolicy.expectedRespStatus {
|
|
|
|
t.Fatalf("Case %d: Expected the response status to be `%d`, but instead found `%d`", i+1, testPolicy.expectedRespStatus, recV2.Code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, testCase := range testCases {
|
|
|
|
// initialize HTTP NewRecorder, this records any mutations to response writer inside the handler.
|
|
|
|
recV2 := httptest.NewRecorder()
|
|
|
|
// construct HTTP request for Delete bucket policy endpoint.
|
|
|
|
reqV2, err := newTestSignedRequestV2("DELETE", getDeletePolicyURL("", testCase.bucketName),
|
2018-09-21 04:22:09 +02:00
|
|
|
0, nil, testCase.accessKey, testCase.secretKey, nil)
|
2016-09-30 23:32:13 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Test %d: Failed to create HTTP request for GetBucketPolicyHandler: <ERROR> %v", i+1, err)
|
|
|
|
}
|
|
|
|
// Since `apiRouter` satisfies `http.Handler` it has a ServeHTTP to execute the logic of the handler.
|
|
|
|
// Call the ServeHTTP to execute the handler, DeleteBucketPolicyHandler handles the request.
|
|
|
|
apiRouter.ServeHTTP(recV2, reqV2)
|
|
|
|
// Assert the response code with the expected status.
|
|
|
|
if recV2.Code != testCase.expectedRespStatus {
|
|
|
|
t.Fatalf("Case %d: Expected the response status to be `%d`, but instead found `%d`", i+1, testCase.expectedRespStatus, recV2.Code)
|
|
|
|
}
|
2016-07-04 07:35:30 +02:00
|
|
|
}
|
2016-10-08 10:04:26 +02:00
|
|
|
// Test for Anonymous/unsigned http request.
|
|
|
|
// Bucket policy related functions doesn't support anonymous requests, setting policies shouldn't make a difference.
|
|
|
|
// create unsigned HTTP request for PutBucketPolicyHandler.
|
|
|
|
anonReq, err := newTestRequest("DELETE", getPutPolicyURL("", bucketName), 0, nil)
|
|
|
|
|
|
|
|
if err != nil {
|
2019-04-09 20:39:42 +02:00
|
|
|
t.Fatalf("MinIO %s: Failed to create an anonymous request for bucket \"%s\": <ERROR> %v",
|
2016-10-08 10:04:26 +02:00
|
|
|
instanceType, bucketName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExecObjectLayerAPIAnonTest - Calls the HTTP API handler using the anonymous request, validates the ErrAccessDeniedResponse,
|
|
|
|
// sets the bucket policy using the policy statement generated from `getWriteOnlyObjectStatement` so that the
|
|
|
|
// unsigned request goes through and its validated again.
|
2018-04-25 00:53:30 +02:00
|
|
|
ExecObjectLayerAPIAnonTest(t, obj, "DeleteBucketPolicyHandler", bucketName, "", instanceType, apiRouter, anonReq, getAnonWriteOnlyBucketPolicy(bucketName))
|
2016-10-06 22:34:33 +02:00
|
|
|
|
|
|
|
// HTTP request for testing when `objectLayer` is set to `nil`.
|
|
|
|
// There is no need to use an existing bucket and valid input for creating the request
|
|
|
|
// since the `objectLayer==nil` check is performed before any other checks inside the handlers.
|
|
|
|
// The only aim is to generate an HTTP request in a way that the relevant/registered end point is evoked/called.
|
|
|
|
nilBucket := "dummy-bucket"
|
|
|
|
|
|
|
|
nilReq, err := newTestSignedRequestV4("DELETE", getDeletePolicyURL("", nilBucket),
|
2018-09-21 04:22:09 +02:00
|
|
|
0, nil, "", "", nil)
|
2016-10-06 22:34:33 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2019-04-09 20:39:42 +02:00
|
|
|
t.Errorf("MinIO %s: Failed to create HTTP request for testing the response when object Layer is set to `nil`.", instanceType)
|
2016-10-06 22:34:33 +02:00
|
|
|
}
|
|
|
|
// execute the object layer set to `nil` test.
|
|
|
|
// `ExecObjectLayerAPINilTest` manages the operation.
|
|
|
|
ExecObjectLayerAPINilTest(t, nilBucket, "", instanceType, apiRouter, nilReq)
|
2016-07-04 07:35:30 +02:00
|
|
|
}
|