| 
									
										
										
										
											2016-05-05 07:56:57 +08:00
										 |  |  | /* | 
					
						
							| 
									
										
										
										
											2016-09-10 01:18:38 +08:00
										 |  |  |  * Minio Cloud Storage, (C) 2016 Minio, Inc. | 
					
						
							| 
									
										
										
										
											2016-05-05 07:56:57 +08: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 07:23:42 +08:00
										 |  |  | package cmd | 
					
						
							| 
									
										
										
										
											2016-05-05 07:56:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2016-07-03 10:05:16 +08:00
										 |  |  | 	"bytes" | 
					
						
							| 
									
										
										
										
											2016-05-05 07:56:57 +08:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2016-10-11 00:29:56 +08:00
										 |  |  | 	"io" | 
					
						
							| 
									
										
										
										
											2016-07-04 13:35:30 +08:00
										 |  |  | 	"io/ioutil" | 
					
						
							| 
									
										
										
										
											2016-07-03 10:05:16 +08:00
										 |  |  | 	"net/http" | 
					
						
							|  |  |  | 	"net/http/httptest" | 
					
						
							| 
									
										
										
										
											2016-05-05 07:56:57 +08:00
										 |  |  | 	"testing" | 
					
						
							| 
									
										
										
										
											2016-08-20 18:16:38 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/minio/minio-go/pkg/set" | 
					
						
							| 
									
										
										
										
											2016-05-05 07:56:57 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Tests validate Bucket policy resource matcher.
 | 
					
						
							|  |  |  | func TestBucketPolicyResourceMatch(t *testing.T) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-01 14:49:59 +08:00
										 |  |  | 	// generates statement with given resource..
 | 
					
						
							| 
									
										
										
										
											2016-05-05 07:56:57 +08:00
										 |  |  | 	generateStatement := func(resource string) policyStatement { | 
					
						
							|  |  |  | 		statement := policyStatement{} | 
					
						
							| 
									
										
										
										
											2016-08-20 18:16:38 +08:00
										 |  |  | 		statement.Resources = set.CreateStringSet([]string{resource}...) | 
					
						
							| 
									
										
										
										
											2016-05-05 07:56:57 +08:00
										 |  |  | 		return statement | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// generates resource prefix.
 | 
					
						
							|  |  |  | 	generateResource := func(bucketName, objectName string) string { | 
					
						
							| 
									
										
										
										
											2017-01-11 08:43:48 +08:00
										 |  |  | 		return bucketARNPrefix + bucketName + "/" + objectName | 
					
						
							| 
									
										
										
										
											2016-05-05 07:56:57 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							|  |  |  | 		resourceToMatch       string | 
					
						
							|  |  |  | 		statement             policyStatement | 
					
						
							|  |  |  | 		expectedResourceMatch bool | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		// Test case 1-4.
 | 
					
						
							|  |  |  | 		// Policy with resource ending with bucket/* allows access to all objects inside the given bucket.
 | 
					
						
							| 
									
										
										
										
											2017-01-11 08:43:48 +08:00
										 |  |  | 		{generateResource("minio-bucket", ""), generateStatement(fmt.Sprintf("%s%s", bucketARNPrefix, "minio-bucket"+"/*")), true}, | 
					
						
							|  |  |  | 		{generateResource("minio-bucket", ""), generateStatement(fmt.Sprintf("%s%s", bucketARNPrefix, "minio-bucket"+"/*")), true}, | 
					
						
							|  |  |  | 		{generateResource("minio-bucket", ""), generateStatement(fmt.Sprintf("%s%s", bucketARNPrefix, "minio-bucket"+"/*")), true}, | 
					
						
							|  |  |  | 		{generateResource("minio-bucket", ""), generateStatement(fmt.Sprintf("%s%s", bucketARNPrefix, "minio-bucket"+"/*")), true}, | 
					
						
							| 
									
										
										
										
											2016-05-05 07:56:57 +08:00
										 |  |  | 		// Test case - 5.
 | 
					
						
							|  |  |  | 		// Policy with resource ending with bucket/oo* should not allow access to bucket/output.txt.
 | 
					
						
							| 
									
										
										
										
											2017-01-11 08:43:48 +08:00
										 |  |  | 		{generateResource("minio-bucket", "output.txt"), generateStatement(fmt.Sprintf("%s%s", bucketARNPrefix, "minio-bucket"+"/oo*")), false}, | 
					
						
							| 
									
										
										
										
											2016-05-05 07:56:57 +08:00
										 |  |  | 		// Test case - 6.
 | 
					
						
							|  |  |  | 		// Policy with resource ending with bucket/oo* should allow access to bucket/ootput.txt.
 | 
					
						
							| 
									
										
										
										
											2017-01-11 08:43:48 +08:00
										 |  |  | 		{generateResource("minio-bucket", "ootput.txt"), generateStatement(fmt.Sprintf("%s%s", bucketARNPrefix, "minio-bucket"+"/oo*")), true}, | 
					
						
							| 
									
										
										
										
											2016-05-05 07:56:57 +08:00
										 |  |  | 		// Test case - 7.
 | 
					
						
							| 
									
										
										
										
											2016-11-10 15:37:12 +08:00
										 |  |  | 		// Policy with resource ending with bucket/oo* allows access to all sub-dirs starting with "oo" inside given bucket.
 | 
					
						
							| 
									
										
										
										
											2017-01-11 08:43:48 +08:00
										 |  |  | 		{generateResource("minio-bucket", "oop-bucket/my-file"), generateStatement(fmt.Sprintf("%s%s", bucketARNPrefix, "minio-bucket"+"/oo*")), true}, | 
					
						
							| 
									
										
										
										
											2016-05-05 07:56:57 +08:00
										 |  |  | 		// Test case - 8.
 | 
					
						
							| 
									
										
										
										
											2017-01-11 08:43:48 +08:00
										 |  |  | 		{generateResource("minio-bucket", "Asia/India/1.pjg"), generateStatement(fmt.Sprintf("%s%s", bucketARNPrefix, "minio-bucket"+"/Asia/Japan/*")), false}, | 
					
						
							| 
									
										
										
										
											2016-05-05 07:56:57 +08:00
										 |  |  | 		// Test case - 9.
 | 
					
						
							| 
									
										
										
										
											2017-01-11 08:43:48 +08:00
										 |  |  | 		{generateResource("minio-bucket", "Asia/India/1.pjg"), generateStatement(fmt.Sprintf("%s%s", bucketARNPrefix, "minio-bucket"+"/Asia/Japan/*")), false}, | 
					
						
							| 
									
										
										
										
											2016-05-05 07:56:57 +08:00
										 |  |  | 		// Test case - 10.
 | 
					
						
							|  |  |  | 		// Proves that the name space is flat.
 | 
					
						
							| 
									
										
										
										
											2017-01-11 08:43:48 +08:00
										 |  |  | 		{generateResource("minio-bucket", "Africa/Bihar/India/design_info.doc/Bihar"), generateStatement(fmt.Sprintf("%s%s", bucketARNPrefix, | 
					
						
							| 
									
										
										
										
											2016-05-05 07:56:57 +08:00
										 |  |  | 			"minio-bucket"+"/*/India/*/Bihar")), true}, | 
					
						
							|  |  |  | 		// Test case - 11.
 | 
					
						
							|  |  |  | 		// Proves that the name space is flat.
 | 
					
						
							| 
									
										
										
										
											2017-01-11 08:43:48 +08:00
										 |  |  | 		{generateResource("minio-bucket", "Asia/China/India/States/Bihar/output.txt"), generateStatement(fmt.Sprintf("%s%s", bucketARNPrefix, | 
					
						
							| 
									
										
										
										
											2016-05-05 07:56:57 +08:00
										 |  |  | 			"minio-bucket"+"/*/India/*/Bihar/*")), true}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for i, testCase := range testCases { | 
					
						
							|  |  |  | 		actualResourceMatch := bucketPolicyResourceMatch(testCase.resourceToMatch, testCase.statement) | 
					
						
							|  |  |  | 		if testCase.expectedResourceMatch != actualResourceMatch { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected Resource match to be `%v`, but instead found it to be `%v`", i+1, testCase.expectedResourceMatch, actualResourceMatch) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-07-01 14:49:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // TestBucketPolicyActionMatch - Test validates whether given action on the
 | 
					
						
							|  |  |  | // bucket/object matches the allowed actions in policyStatement.
 | 
					
						
							|  |  |  | // This test preserves the allowed actions for all 3 sets of policies, that is read-write,read-only, write-only.
 | 
					
						
							|  |  |  | // The intention of the test is to catch any changes made to allowed action for on eof the above 3 major policy groups mentioned.
 | 
					
						
							|  |  |  | func TestBucketPolicyActionMatch(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-07-03 10:05:16 +08:00
										 |  |  | 	bucketName := getRandomBucketName() | 
					
						
							| 
									
										
										
										
											2016-07-01 14:49:59 +08:00
										 |  |  | 	objectPrefix := "test-object" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							|  |  |  | 		action         string | 
					
						
							|  |  |  | 		statement      policyStatement | 
					
						
							|  |  |  | 		expectedResult bool | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		// s3:GetBucketLocation is the action necessary to be present in the bucket policy to allow
 | 
					
						
							|  |  |  | 		// fetching of bucket location on an Anonymous/unsigned request.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		//r ead-write bucket policy is expected to allow GetBucketLocation operation on an anonymous request (Test case - 1).
 | 
					
						
							|  |  |  | 		{"s3:GetBucketLocation", getReadWriteBucketStatement(bucketName, objectPrefix), true}, | 
					
						
							|  |  |  | 		//	write-only bucket policy is expected to allow GetBucketLocation operation on an anonymous request (Test case - 2).
 | 
					
						
							|  |  |  | 		{"s3:GetBucketLocation", getWriteOnlyBucketStatement(bucketName, objectPrefix), true}, | 
					
						
							|  |  |  | 		//	read-only bucket policy is expected to allow GetBucketLocation operation on an anonymous request (Test case - 3).
 | 
					
						
							|  |  |  | 		{"s3:GetBucketLocation", getReadOnlyBucketStatement(bucketName, objectPrefix), true}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Any of the Object level access permissions shouldn't allow for GetBucketLocation operation  on an Anonymous/unsigned request (Test cases 4-6).
 | 
					
						
							|  |  |  | 		{"s3:GetBucketLocation", getReadWriteObjectStatement(bucketName, objectPrefix), false}, | 
					
						
							|  |  |  | 		{"s3:GetBucketLocation", getWriteOnlyObjectStatement(bucketName, objectPrefix), false}, | 
					
						
							|  |  |  | 		{"s3:GetBucketLocation", getReadOnlyObjectStatement(bucketName, objectPrefix), false}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// s3:ListBucketMultipartUploads is the action necessary to be present in the bucket policy to allow
 | 
					
						
							|  |  |  | 		// Listing of multipart uploads in a given bucket for an Anonymous/unsigned request.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		//read-write bucket policy is expected to allow  ListBucketMultipartUploads operation on an anonymous request (Test case 7).
 | 
					
						
							|  |  |  | 		{"s3:ListBucketMultipartUploads", getReadWriteBucketStatement(bucketName, objectPrefix), true}, | 
					
						
							|  |  |  | 		//	write-only bucket policy is expected to allow  ListBucketMultipartUploads operation on an anonymous request (Test case 8).
 | 
					
						
							|  |  |  | 		{"s3:ListBucketMultipartUploads", getWriteOnlyBucketStatement(bucketName, objectPrefix), true}, | 
					
						
							|  |  |  | 		// read-only bucket policy is expected to not allow ListBucketMultipartUploads operation on an anonymous request (Test case 9).
 | 
					
						
							|  |  |  | 		// the allowed actions in read-only bucket statement are  "s3:GetBucketLocation","s3:ListBucket",
 | 
					
						
							| 
									
										
										
										
											2016-08-15 17:44:48 +08:00
										 |  |  | 		// this should not allow for ListBucketMultipartUploads operations.
 | 
					
						
							| 
									
										
										
										
											2016-07-01 14:49:59 +08:00
										 |  |  | 		{"s3:ListBucketMultipartUploads", getReadOnlyBucketStatement(bucketName, objectPrefix), false}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Any of the object level policy will not allow for s3:ListBucketMultipartUploads (Test cases 10-12).
 | 
					
						
							|  |  |  | 		{"s3:ListBucketMultipartUploads", getReadWriteObjectStatement(bucketName, objectPrefix), false}, | 
					
						
							|  |  |  | 		{"s3:ListBucketMultipartUploads", getWriteOnlyObjectStatement(bucketName, objectPrefix), false}, | 
					
						
							|  |  |  | 		{"s3:ListBucketMultipartUploads", getReadOnlyObjectStatement(bucketName, objectPrefix), false}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// s3:ListBucket is the action necessary to be present in the bucket policy to allow
 | 
					
						
							|  |  |  | 		// listing of all objects inside a given bucket on an Anonymous/unsigned request.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Cases for testing ListBucket access for different Bucket level access permissions.
 | 
					
						
							|  |  |  | 		// read-only bucket policy is expected to allow ListBucket operation on an anonymous request (Test case 13).
 | 
					
						
							|  |  |  | 		{"s3:ListBucket", getReadOnlyBucketStatement(bucketName, objectPrefix), true}, | 
					
						
							|  |  |  | 		// read-write bucket policy is expected to allow ListBucket operation on an anonymous request (Test case 14).
 | 
					
						
							|  |  |  | 		{"s3:ListBucket", getReadWriteBucketStatement(bucketName, objectPrefix), true}, | 
					
						
							|  |  |  | 		// write-only  bucket policy is expected to not allow ListBucket operation on an anonymous request (Test case 15).
 | 
					
						
							|  |  |  | 		// the allowed actions in write-only  bucket statement are "s3:GetBucketLocation",	"s3:ListBucketMultipartUploads",
 | 
					
						
							| 
									
										
										
										
											2016-08-15 17:44:48 +08:00
										 |  |  | 		// this should not allow for ListBucket operations.
 | 
					
						
							| 
									
										
										
										
											2016-07-01 14:49:59 +08:00
										 |  |  | 		{"s3:ListBucket", getWriteOnlyBucketStatement(bucketName, objectPrefix), false}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Cases for testing ListBucket access for different Object level access permissions (Test cases 16-18).
 | 
					
						
							|  |  |  | 		// Any of the Object level access permissions shouldn't allow for ListBucket operation  on an Anonymous/unsigned request.
 | 
					
						
							|  |  |  | 		{"s3:ListBucket", getReadOnlyObjectStatement(bucketName, objectPrefix), false}, | 
					
						
							|  |  |  | 		{"s3:ListBucket", getReadWriteObjectStatement(bucketName, objectPrefix), false}, | 
					
						
							|  |  |  | 		{"s3:ListBucket", getWriteOnlyObjectStatement(bucketName, objectPrefix), false}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// s3:DeleteObject is the action necessary to be present in the bucket policy to allow
 | 
					
						
							|  |  |  | 		// deleting/removal of  objects inside a given bucket for an Anonymous/unsigned request.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Cases for testing DeleteObject access for different Bucket level access permissions (Test cases 19-21).
 | 
					
						
							|  |  |  | 		// Any of the Bucket level access permissions shouldn't allow for DeleteObject operation  on an Anonymous/unsigned request.
 | 
					
						
							|  |  |  | 		{"s3:DeleteObject", getReadOnlyBucketStatement(bucketName, objectPrefix), false}, | 
					
						
							|  |  |  | 		{"s3:DeleteObject", getReadWriteBucketStatement(bucketName, objectPrefix), false}, | 
					
						
							|  |  |  | 		{"s3:DeleteObject", getWriteOnlyBucketStatement(bucketName, objectPrefix), false}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Cases for testing DeleteObject access for different Object level access permissions (Test cases 22).
 | 
					
						
							|  |  |  | 		// read-only bucket policy is expected to not allow Delete Object operation on an anonymous request.
 | 
					
						
							|  |  |  | 		{"s3:DeleteObject", getReadOnlyObjectStatement(bucketName, objectPrefix), false}, | 
					
						
							|  |  |  | 		// read-write bucket policy is expected to allow Delete Bucket operation on an anonymous request (Test cases 23).
 | 
					
						
							|  |  |  | 		{"s3:DeleteObject", getReadWriteObjectStatement(bucketName, objectPrefix), true}, | 
					
						
							|  |  |  | 		// write-only  bucket policy is expected to allow Delete Object operation on an anonymous request (Test cases 24).
 | 
					
						
							|  |  |  | 		{"s3:DeleteObject", getWriteOnlyObjectStatement(bucketName, objectPrefix), true}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// s3:AbortMultipartUpload is the action necessary to be present in the bucket policy to allow
 | 
					
						
							|  |  |  | 		// cancelling or abortion of an already initiated multipart upload operation for an Anonymous/unsigned request.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Cases for testing AbortMultipartUpload access for different Bucket level access permissions (Test cases 25-27).
 | 
					
						
							|  |  |  | 		// Any of the Bucket level access permissions shouldn't allow for AbortMultipartUpload operation on an Anonymous/unsigned request.
 | 
					
						
							|  |  |  | 		{"s3:AbortMultipartUpload", getReadOnlyBucketStatement(bucketName, objectPrefix), false}, | 
					
						
							|  |  |  | 		{"s3:AbortMultipartUpload", getReadWriteBucketStatement(bucketName, objectPrefix), false}, | 
					
						
							|  |  |  | 		{"s3:AbortMultipartUpload", getWriteOnlyBucketStatement(bucketName, objectPrefix), false}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Cases for testing AbortMultipartUpload access for different Object level access permissions.
 | 
					
						
							|  |  |  | 		// read-only object policy is expected to not allow AbortMultipartUpload operation on an anonymous request (Test case 28).
 | 
					
						
							|  |  |  | 		{"s3:AbortMultipartUpload", getReadOnlyObjectStatement(bucketName, objectPrefix), false}, | 
					
						
							|  |  |  | 		// read-write object policy is expected to allow AbortMultipartUpload operation on an anonymous request (Test case 29).
 | 
					
						
							|  |  |  | 		{"s3:AbortMultipartUpload", getReadWriteObjectStatement(bucketName, objectPrefix), true}, | 
					
						
							|  |  |  | 		// write-only object policy is expected to allow AbortMultipartUpload operation on an anonymous request (Test case 30).
 | 
					
						
							|  |  |  | 		{"s3:AbortMultipartUpload", getWriteOnlyObjectStatement(bucketName, objectPrefix), true}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// s3:PutObject is the action necessary to be present in the bucket policy to allow
 | 
					
						
							|  |  |  | 		// uploading of an object for an Anonymous/unsigned request.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Cases for testing PutObject access for different Bucket level access permissions (Test cases 31-33).
 | 
					
						
							|  |  |  | 		// Any of the Bucket level access permissions shouldn't allow for PutObject operation on an Anonymous/unsigned request.
 | 
					
						
							|  |  |  | 		{"s3:PutObject", getReadOnlyBucketStatement(bucketName, objectPrefix), false}, | 
					
						
							|  |  |  | 		{"s3:PutObject", getReadWriteBucketStatement(bucketName, objectPrefix), false}, | 
					
						
							|  |  |  | 		{"s3:PutObject", getWriteOnlyBucketStatement(bucketName, objectPrefix), false}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Cases for testing PutObject access for different Object level access permissions.
 | 
					
						
							|  |  |  | 		// read-only object policy is expected to not allow PutObject operation on an anonymous request (Test case 34).
 | 
					
						
							|  |  |  | 		{"s3:PutObject", getReadOnlyObjectStatement(bucketName, objectPrefix), false}, | 
					
						
							|  |  |  | 		// read-write object policy is expected to allow PutObject operation on an anonymous request (Test case 35).
 | 
					
						
							|  |  |  | 		{"s3:PutObject", getReadWriteObjectStatement(bucketName, objectPrefix), true}, | 
					
						
							|  |  |  | 		// write-only  object policy is expected to allow PutObject operation on an anonymous request (Test case 36).
 | 
					
						
							|  |  |  | 		{"s3:PutObject", getWriteOnlyObjectStatement(bucketName, objectPrefix), true}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// s3:GetObject is the action necessary to be present in the bucket policy to allow
 | 
					
						
							|  |  |  | 		// downloading of an object for an Anonymous/unsigned request.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Cases for testing GetObject access for different Bucket level access permissions (Test cases 37-39).
 | 
					
						
							|  |  |  | 		// Any of the Bucket level access permissions shouldn't allow for GetObject operation on an Anonymous/unsigned request.
 | 
					
						
							|  |  |  | 		{"s3:GetObject", getReadOnlyBucketStatement(bucketName, objectPrefix), false}, | 
					
						
							|  |  |  | 		{"s3:GetObject", getReadWriteBucketStatement(bucketName, objectPrefix), false}, | 
					
						
							|  |  |  | 		{"s3:GetObject", getWriteOnlyBucketStatement(bucketName, objectPrefix), false}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Cases for testing GetObject access for different Object level access permissions.
 | 
					
						
							|  |  |  | 		// read-only bucket policy is expected to allow  downloading of an Object on an anonymous request (Test case 40).
 | 
					
						
							|  |  |  | 		{"s3:GetObject", getReadOnlyObjectStatement(bucketName, objectPrefix), true}, | 
					
						
							|  |  |  | 		// read-write bucket policy is expected to allow  downloading of an Object on an anonymous request (Test case 41).
 | 
					
						
							|  |  |  | 		{"s3:GetObject", getReadWriteObjectStatement(bucketName, objectPrefix), true}, | 
					
						
							|  |  |  | 		// write-only  bucket policy is expected to not allow  downloading of an Object on an anonymous request (Test case 42).
 | 
					
						
							|  |  |  | 		{"s3:GetObject", getWriteOnlyObjectStatement(bucketName, objectPrefix), false}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// s3:ListMultipartUploadParts is the action necessary to be present in the bucket policy to allow
 | 
					
						
							|  |  |  | 		// Listing of uploaded parts for an Anonymous/unsigned request.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Any of the Bucket level access permissions shouldn't allow for ListMultipartUploadParts operation on an Anonymous/unsigned request.
 | 
					
						
							|  |  |  | 		// read-only bucket policy is expected to not allow ListMultipartUploadParts operation on an anonymous request (Test cases 43-45).
 | 
					
						
							|  |  |  | 		{"s3:ListMultipartUploadParts", getReadOnlyBucketStatement(bucketName, objectPrefix), false}, | 
					
						
							|  |  |  | 		{"s3:ListMultipartUploadParts", getReadWriteBucketStatement(bucketName, objectPrefix), false}, | 
					
						
							|  |  |  | 		{"s3:ListMultipartUploadParts", getWriteOnlyBucketStatement(bucketName, objectPrefix), false}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// read-only object policy is expected to not allow ListMultipartUploadParts operation on an anonymous request (Test case 46).
 | 
					
						
							|  |  |  | 		{"s3:ListMultipartUploadParts", getReadOnlyObjectStatement(bucketName, objectPrefix), false}, | 
					
						
							|  |  |  | 		// read-write object policy is expected to allow ListMultipartUploadParts operation on an anonymous request (Test case 47).
 | 
					
						
							|  |  |  | 		{"s3:ListMultipartUploadParts", getReadWriteObjectStatement(bucketName, objectPrefix), true}, | 
					
						
							|  |  |  | 		// write-only  object policy is expected to allow ListMultipartUploadParts operation on an anonymous request (Test case 48).
 | 
					
						
							|  |  |  | 		{"s3:ListMultipartUploadParts", getWriteOnlyObjectStatement(bucketName, objectPrefix), true}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for i, testCase := range testCases { | 
					
						
							|  |  |  | 		actualResult := bucketPolicyActionMatch(testCase.action, testCase.statement) | 
					
						
							|  |  |  | 		if testCase.expectedResult != actualResult { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected the result to be `%v`, but instead found it to be `%v`", i+1, testCase.expectedResult, actualResult) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-07-03 10:05:16 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-07-01 14:49:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-03 10:05:16 +08: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 17:02:42 +08:00
										 |  |  | 	ExecObjectLayerAPITest(t, testPutBucketPolicyHandler, []string{"PutBucketPolicy"}) | 
					
						
							| 
									
										
										
										
											2016-07-03 10:05:16 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // testPutBucketPolicyHandler - Test for Bucket policy end point.
 | 
					
						
							| 
									
										
										
										
											2016-10-06 17:02:42 +08:00
										 |  |  | func testPutBucketPolicyHandler(obj ObjectLayer, instanceType, bucketName string, apiRouter http.Handler, | 
					
						
							| 
									
										
										
										
											2016-10-07 23:02:37 +08:00
										 |  |  | 	credentials credential, t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-09-27 05:28:35 +08:00
										 |  |  | 	initBucketPolicies(obj) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-15 07:45:00 +08:00
										 |  |  | 	bucketName1 := fmt.Sprintf("%s-1", bucketName) | 
					
						
							|  |  |  | 	if err := obj.MakeBucket(bucketName1); err != nil { | 
					
						
							|  |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-03 10:05:16 +08:00
										 |  |  | 	// template for constructing HTTP request body for PUT bucket policy.
 | 
					
						
							| 
									
										
										
										
											2016-08-11 11:10:48 +08: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 10:05:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// test cases with sample input and expected output.
 | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							|  |  |  | 		bucketName string | 
					
						
							| 
									
										
										
										
											2016-10-11 00:29:56 +08: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 10:05:16 +08:00
										 |  |  | 		// expected Response.
 | 
					
						
							|  |  |  | 		expectedRespStatus int | 
					
						
							|  |  |  | 	}{ | 
					
						
							| 
									
										
										
										
											2016-10-11 00:29:56 +08:00
										 |  |  | 		// Test case - 1.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			bucketName:         bucketName, | 
					
						
							|  |  |  | 			bucketPolicyReader: bytes.NewReader([]byte(fmt.Sprintf(bucketPolicyTemplate, bucketName, bucketName))), | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			policyLen:          len(fmt.Sprintf(bucketPolicyTemplate, bucketName, bucketName)), | 
					
						
							| 
									
										
										
										
											2016-12-27 02:21:23 +08:00
										 |  |  | 			accessKey:          credentials.AccessKey, | 
					
						
							|  |  |  | 			secretKey:          credentials.SecretKey, | 
					
						
							| 
									
										
										
										
											2016-10-11 00:29:56 +08: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))), | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			policyLen:          maxAccessPolicySize + 1, | 
					
						
							| 
									
										
										
										
											2016-12-27 02:21:23 +08:00
										 |  |  | 			accessKey:          credentials.AccessKey, | 
					
						
							|  |  |  | 			secretKey:          credentials.SecretKey, | 
					
						
							| 
									
										
										
										
											2016-10-11 00:29:56 +08: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-27 02:21:23 +08:00
										 |  |  | 			accessKey:          credentials.AccessKey, | 
					
						
							|  |  |  | 			secretKey:          credentials.SecretKey, | 
					
						
							| 
									
										
										
										
											2016-10-11 00:29:56 +08: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-27 02:21:23 +08:00
										 |  |  | 			accessKey:          credentials.AccessKey, | 
					
						
							|  |  |  | 			secretKey:          credentials.SecretKey, | 
					
						
							| 
									
										
										
										
											2016-10-11 00:29:56 +08: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 07:45:00 +08:00
										 |  |  | 			bucketName:         bucketName, | 
					
						
							| 
									
										
										
										
											2016-10-11 00:29:56 +08:00
										 |  |  | 			bucketPolicyReader: bytes.NewReader([]byte("dummy-policy")), | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			policyLen:          len([]byte("dummy-policy")), | 
					
						
							| 
									
										
										
										
											2016-12-27 02:21:23 +08:00
										 |  |  | 			accessKey:          credentials.AccessKey, | 
					
						
							|  |  |  | 			secretKey:          credentials.SecretKey, | 
					
						
							| 
									
										
										
										
											2016-10-11 00:29:56 +08: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 07:45:00 +08:00
										 |  |  | 			bucketName:         bucketName1, | 
					
						
							| 
									
										
										
										
											2016-10-11 00:29:56 +08:00
										 |  |  | 			bucketPolicyReader: bytes.NewReader([]byte(fmt.Sprintf(bucketPolicyTemplate, bucketName, bucketName))), | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			policyLen:          len(fmt.Sprintf(bucketPolicyTemplate, bucketName, bucketName)), | 
					
						
							| 
									
										
										
										
											2016-12-27 02:21:23 +08:00
										 |  |  | 			accessKey:          credentials.AccessKey, | 
					
						
							|  |  |  | 			secretKey:          credentials.SecretKey, | 
					
						
							| 
									
										
										
										
											2016-10-11 00:29:56 +08:00
										 |  |  | 			expectedRespStatus: http.StatusBadRequest, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		// Test case - 8.
 | 
					
						
							|  |  |  | 		// non-existent bucket is used.
 | 
					
						
							|  |  |  | 		// writing BucketPolicy should fail.
 | 
					
						
							|  |  |  | 		// should result is 500 InternalServerError.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			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-27 02:21:23 +08:00
										 |  |  | 			accessKey:          credentials.AccessKey, | 
					
						
							|  |  |  | 			secretKey:          credentials.SecretKey, | 
					
						
							| 
									
										
										
										
											2016-11-15 07:45:00 +08:00
										 |  |  | 			expectedRespStatus: http.StatusNotFound, | 
					
						
							| 
									
										
										
										
											2016-10-11 00:29:56 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		// Test case - 9.
 | 
					
						
							|  |  |  | 		// invalid bucket name is used.
 | 
					
						
							|  |  |  | 		// writing BucketPolicy should fail.
 | 
					
						
							|  |  |  | 		// should result is 400 StatusBadRequest.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			bucketName:         ".invalid-bucket", | 
					
						
							|  |  |  | 			bucketPolicyReader: bytes.NewReader([]byte(fmt.Sprintf(bucketPolicyTemplate, ".invalid-bucket", ".invalid-bucket"))), | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			policyLen:          len(fmt.Sprintf(bucketPolicyTemplate, bucketName, bucketName)), | 
					
						
							| 
									
										
										
										
											2016-12-27 02:21:23 +08:00
										 |  |  | 			accessKey:          credentials.AccessKey, | 
					
						
							|  |  |  | 			secretKey:          credentials.SecretKey, | 
					
						
							| 
									
										
										
										
											2016-10-11 00:29:56 +08:00
										 |  |  | 			expectedRespStatus: http.StatusBadRequest, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2016-07-03 10:05:16 +08: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-10-01 05:32:13 +08:00
										 |  |  | 		recV4 := httptest.NewRecorder() | 
					
						
							| 
									
										
										
										
											2016-07-03 10:05:16 +08:00
										 |  |  | 		// construct HTTP request for PUT bucket policy endpoint.
 | 
					
						
							| 
									
										
										
										
											2016-10-01 05:32:13 +08:00
										 |  |  | 		reqV4, err := newTestSignedRequestV4("PUT", getPutPolicyURL("", testCase.bucketName), | 
					
						
							| 
									
										
										
										
											2016-10-11 00:29:56 +08:00
										 |  |  | 			int64(testCase.policyLen), testCase.bucketPolicyReader, testCase.accessKey, testCase.secretKey) | 
					
						
							| 
									
										
										
										
											2016-07-03 10:05:16 +08:00
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2016-08-01 05:11:14 +08:00
										 |  |  | 			t.Fatalf("Test %d: %s: Failed to create HTTP request for PutBucketPolicyHandler: <ERROR> %v", i+1, instanceType, err) | 
					
						
							| 
									
										
										
										
											2016-07-03 10:05:16 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		// Since `apiRouter` satisfies `http.Handler` it has a ServeHTTP to execute the logic ofthe handler.
 | 
					
						
							|  |  |  | 		// Call the ServeHTTP to execute the handler.
 | 
					
						
							| 
									
										
										
										
											2016-10-01 05:32:13 +08: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), | 
					
						
							| 
									
										
										
										
											2016-10-11 00:29:56 +08:00
										 |  |  | 			int64(testCase.policyLen), testCase.bucketPolicyReader, testCase.accessKey, testCase.secretKey) | 
					
						
							| 
									
										
										
										
											2016-10-01 05:32:13 +08: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 10:05:16 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-07 04:34:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-08 16:04:26 +08: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 { | 
					
						
							|  |  |  | 		t.Fatalf("Minio %s: Failed to create an anonymous request for bucket \"%s\": <ERROR> %v", | 
					
						
							|  |  |  | 			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.
 | 
					
						
							|  |  |  | 	ExecObjectLayerAPIAnonTest(t, "PutBucketPolicyHandler", bucketName, "", instanceType, apiRouter, anonReq, getWriteOnlyObjectStatement) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-07 04:34:33 +08: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), | 
					
						
							|  |  |  | 		0, nil, "", "") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-10-08 16:04:26 +08:00
										 |  |  | 		t.Errorf("Minio %s: Failed to create HTTP request for testing the response when object Layer is set to `nil`.", instanceType) | 
					
						
							| 
									
										
										
										
											2016-10-07 04:34:33 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	// execute the object layer set to `nil` test.
 | 
					
						
							|  |  |  | 	// `ExecObjectLayerAPINilTest` manages the operation.
 | 
					
						
							|  |  |  | 	ExecObjectLayerAPINilTest(t, nilBucket, "", instanceType, apiRouter, nilReq) | 
					
						
							| 
									
										
										
										
											2016-07-01 14:49:59 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-07-04 13:35:30 +08: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 17:02:42 +08:00
										 |  |  | 	ExecObjectLayerAPITest(t, testGetBucketPolicyHandler, []string{"PutBucketPolicy", "GetBucketPolicy"}) | 
					
						
							| 
									
										
										
										
											2016-07-04 13:35:30 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // testGetBucketPolicyHandler - Test for end point which fetches the access policy json of the given bucket.
 | 
					
						
							| 
									
										
										
										
											2016-10-06 17:02:42 +08:00
										 |  |  | func testGetBucketPolicyHandler(obj ObjectLayer, instanceType, bucketName string, apiRouter http.Handler, | 
					
						
							| 
									
										
										
										
											2016-10-07 23:02:37 +08:00
										 |  |  | 	credentials credential, t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-10-06 17:02:42 +08:00
										 |  |  | 	// initialize bucket policy.
 | 
					
						
							| 
									
										
										
										
											2016-09-27 05:28:35 +08:00
										 |  |  | 	initBucketPolicies(obj) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-04 13:35:30 +08:00
										 |  |  | 	// template for constructing HTTP request body for PUT bucket policy.
 | 
					
						
							| 
									
										
										
										
											2016-08-20 18:16:38 +08:00
										 |  |  | 	bucketPolicyTemplate := `{"Version":"2012-10-17","Statement":[{"Action":["s3:GetBucketLocation","s3:ListBucket"],"Effect":"Allow","Principal":{"AWS":["*"]},"Resource":["arn:aws:s3:::%s"],"Sid":""},{"Action":["s3:GetObject"],"Effect":"Allow","Principal":{"AWS":["*"]},"Resource":["arn:aws:s3:::%s/this*"],"Sid":""}]}` | 
					
						
							| 
									
										
										
										
											2016-07-04 13:35:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Writing bucket policy before running test on GetBucketPolicy.
 | 
					
						
							|  |  |  | 	putTestPolicies := []struct { | 
					
						
							|  |  |  | 		bucketName string | 
					
						
							|  |  |  | 		accessKey  string | 
					
						
							|  |  |  | 		secretKey  string | 
					
						
							|  |  |  | 		// expected Response.
 | 
					
						
							|  |  |  | 		expectedRespStatus int | 
					
						
							|  |  |  | 	}{ | 
					
						
							| 
									
										
										
										
											2016-12-27 02:21:23 +08:00
										 |  |  | 		{bucketName, credentials.AccessKey, credentials.SecretKey, http.StatusNoContent}, | 
					
						
							| 
									
										
										
										
											2016-07-04 13:35:30 +08: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-10-01 05:32:13 +08:00
										 |  |  | 		recV4 := httptest.NewRecorder() | 
					
						
							| 
									
										
										
										
											2016-07-04 13:35:30 +08:00
										 |  |  | 		// construct HTTP request for PUT bucket policy endpoint.
 | 
					
						
							| 
									
										
										
										
											2016-10-01 05:32:13 +08:00
										 |  |  | 		reqV4, err := newTestSignedRequestV4("PUT", getPutPolicyURL("", testPolicy.bucketName), | 
					
						
							| 
									
										
										
										
											2016-07-04 13:35:30 +08:00
										 |  |  | 			int64(len(bucketPolicyStr)), bytes.NewReader([]byte(bucketPolicyStr)), testPolicy.accessKey, testPolicy.secretKey) | 
					
						
							|  |  |  | 		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-10-01 05:32:13 +08: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), | 
					
						
							|  |  |  | 			int64(len(bucketPolicyStr)), bytes.NewReader([]byte(bucketPolicyStr)), testPolicy.accessKey, testPolicy.secretKey) | 
					
						
							|  |  |  | 		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 13:35:30 +08: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-11 00:29:56 +08:00
										 |  |  | 		// Test case - 1.
 | 
					
						
							|  |  |  | 		// Case which valid inputs, expected to return success status of 200OK.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			bucketName:           bucketName, | 
					
						
							| 
									
										
										
										
											2016-12-27 02:21:23 +08:00
										 |  |  | 			accessKey:            credentials.AccessKey, | 
					
						
							|  |  |  | 			secretKey:            credentials.SecretKey, | 
					
						
							| 
									
										
										
										
											2016-10-11 00:29:56 +08:00
										 |  |  | 			expectedBucketPolicy: bucketPolicyTemplate, | 
					
						
							|  |  |  | 			expectedRespStatus:   http.StatusOK, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		// Test case - 2.
 | 
					
						
							|  |  |  | 		// Case with non-existent bucket name.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			bucketName:           "non-existent-bucket", | 
					
						
							| 
									
										
										
										
											2016-12-27 02:21:23 +08:00
										 |  |  | 			accessKey:            credentials.AccessKey, | 
					
						
							|  |  |  | 			secretKey:            credentials.SecretKey, | 
					
						
							| 
									
										
										
										
											2016-10-11 00:29:56 +08:00
										 |  |  | 			expectedBucketPolicy: bucketPolicyTemplate, | 
					
						
							| 
									
										
										
										
											2016-11-15 07:45:00 +08:00
										 |  |  | 			expectedRespStatus:   http.StatusNotFound, | 
					
						
							| 
									
										
										
										
											2016-10-11 00:29:56 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		// Test case - 3.
 | 
					
						
							|  |  |  | 		// Case with invalid bucket name.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			bucketName:           ".invalid-bucket-name", | 
					
						
							| 
									
										
										
										
											2016-12-27 02:21:23 +08:00
										 |  |  | 			accessKey:            credentials.AccessKey, | 
					
						
							|  |  |  | 			secretKey:            credentials.SecretKey, | 
					
						
							| 
									
										
										
										
											2016-10-11 00:29:56 +08:00
										 |  |  | 			expectedBucketPolicy: "", | 
					
						
							|  |  |  | 			expectedRespStatus:   http.StatusBadRequest, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2016-07-04 13:35:30 +08: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-10-01 05:32:13 +08:00
										 |  |  | 		recV4 := httptest.NewRecorder() | 
					
						
							| 
									
										
										
										
											2016-07-04 13:35:30 +08:00
										 |  |  | 		// construct HTTP request for PUT bucket policy endpoint.
 | 
					
						
							| 
									
										
										
										
											2016-10-01 05:32:13 +08:00
										 |  |  | 		reqV4, err := newTestSignedRequestV4("GET", getGetPolicyURL("", testCase.bucketName), | 
					
						
							| 
									
										
										
										
											2016-07-04 13:35:30 +08:00
										 |  |  | 			0, nil, testCase.accessKey, testCase.secretKey) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		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-10-01 05:32:13 +08: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-11 00:29:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if recV4.Code != testCase.expectedRespStatus { | 
					
						
							|  |  |  | 			// Verify whether the bucket policy fetched is same as the one inserted.
 | 
					
						
							|  |  |  | 			if expectedBucketPolicyStr != string(bucketPolicyReadBuf) { | 
					
						
							|  |  |  | 				t.Errorf("Test %d: %s: Bucket policy differs from expected value.", i+1, instanceType) | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2016-10-01 05:32:13 +08: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), | 
					
						
							|  |  |  | 			0, nil, testCase.accessKey, testCase.secretKey) | 
					
						
							|  |  |  | 		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 13:35:30 +08:00
										 |  |  | 		// Assert the response code with the expected status.
 | 
					
						
							| 
									
										
										
										
											2016-10-01 05:32:13 +08: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 13:35:30 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		// read the response body.
 | 
					
						
							| 
									
										
										
										
											2016-10-01 05:32:13 +08:00
										 |  |  | 		bucketPolicyReadBuf, err = ioutil.ReadAll(recV2.Body) | 
					
						
							| 
									
										
										
										
											2016-07-04 13:35:30 +08:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			t.Fatalf("Test %d: %s: Failed parsing response body: <ERROR> %v", i+1, instanceType, err) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-10-11 00:29:56 +08:00
										 |  |  | 		if recV2.Code == http.StatusOK { | 
					
						
							|  |  |  | 			// Verify whether the bucket policy fetched is same as the one inserted.
 | 
					
						
							|  |  |  | 			if expectedBucketPolicyStr != string(bucketPolicyReadBuf) { | 
					
						
							|  |  |  | 				t.Errorf("Test %d: %s: Bucket policy differs from expected value.", i+1, instanceType) | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2016-07-04 13:35:30 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-07 04:34:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-08 16:04:26 +08: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 { | 
					
						
							|  |  |  | 		t.Fatalf("Minio %s: Failed to create an anonymous request for bucket \"%s\": <ERROR> %v", | 
					
						
							|  |  |  | 			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.
 | 
					
						
							|  |  |  | 	ExecObjectLayerAPIAnonTest(t, "GetBucketPolicyHandler", bucketName, "", instanceType, apiRouter, anonReq, getReadOnlyObjectStatement) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-07 04:34:33 +08: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), | 
					
						
							|  |  |  | 		0, nil, "", "") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-10-08 16:04:26 +08:00
										 |  |  | 		t.Errorf("Minio %s: Failed to create HTTP request for testing the response when object Layer is set to `nil`.", instanceType) | 
					
						
							| 
									
										
										
										
											2016-10-07 04:34:33 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	// execute the object layer set to `nil` test.
 | 
					
						
							|  |  |  | 	// `ExecObjectLayerAPINilTest` manages the operation.
 | 
					
						
							|  |  |  | 	ExecObjectLayerAPINilTest(t, nilBucket, "", instanceType, apiRouter, nilReq) | 
					
						
							| 
									
										
										
										
											2016-07-04 13:35:30 +08: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 17:02:42 +08:00
										 |  |  | 	ExecObjectLayerAPITest(t, testDeleteBucketPolicyHandler, []string{"PutBucketPolicy", "DeleteBucketPolicy"}) | 
					
						
							| 
									
										
										
										
											2016-07-04 13:35:30 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // testDeleteBucketPolicyHandler - Test for Delete bucket policy end point.
 | 
					
						
							| 
									
										
										
										
											2016-10-06 17:02:42 +08:00
										 |  |  | func testDeleteBucketPolicyHandler(obj ObjectLayer, instanceType, bucketName string, apiRouter http.Handler, | 
					
						
							| 
									
										
										
										
											2016-10-07 23:02:37 +08:00
										 |  |  | 	credentials credential, t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-10-06 17:02:42 +08:00
										 |  |  | 	// initialize bucket policy.
 | 
					
						
							| 
									
										
										
										
											2016-09-27 05:28:35 +08:00
										 |  |  | 	initBucketPolicies(obj) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-04 13:35:30 +08: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-11 00:29:56 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			bucketName:         bucketName, | 
					
						
							| 
									
										
										
										
											2016-12-27 02:21:23 +08:00
										 |  |  | 			accessKey:          credentials.AccessKey, | 
					
						
							|  |  |  | 			secretKey:          credentials.SecretKey, | 
					
						
							| 
									
										
										
										
											2016-10-11 00:29:56 +08:00
										 |  |  | 			expectedRespStatus: http.StatusNoContent, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2016-07-04 13:35:30 +08: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-10-01 05:32:13 +08:00
										 |  |  | 		recV4 := httptest.NewRecorder() | 
					
						
							| 
									
										
										
										
											2016-07-04 13:35:30 +08:00
										 |  |  | 		// construct HTTP request for PUT bucket policy endpoint.
 | 
					
						
							| 
									
										
										
										
											2016-10-01 05:32:13 +08:00
										 |  |  | 		reqV4, err := newTestSignedRequestV4("PUT", getPutPolicyURL("", testPolicy.bucketName), | 
					
						
							| 
									
										
										
										
											2016-07-04 13:35:30 +08:00
										 |  |  | 			int64(len(bucketPolicyStr)), bytes.NewReader([]byte(bucketPolicyStr)), testPolicy.accessKey, testPolicy.secretKey) | 
					
						
							|  |  |  | 		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-10-01 05:32:13 +08: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 13:35:30 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-01 05:32:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-04 13:35:30 +08:00
										 |  |  | 	// testcases with input and expected output for DeleteBucketPolicyHandler.
 | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							|  |  |  | 		bucketName string | 
					
						
							|  |  |  | 		accessKey  string | 
					
						
							|  |  |  | 		secretKey  string | 
					
						
							| 
									
										
										
										
											2016-10-11 00:29:56 +08:00
										 |  |  | 		// expected response.
 | 
					
						
							| 
									
										
										
										
											2016-07-04 13:35:30 +08:00
										 |  |  | 		expectedRespStatus int | 
					
						
							|  |  |  | 	}{ | 
					
						
							| 
									
										
										
										
											2016-10-11 00:29:56 +08:00
										 |  |  | 		// Test case - 1.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			bucketName:         bucketName, | 
					
						
							| 
									
										
										
										
											2016-12-27 02:21:23 +08:00
										 |  |  | 			accessKey:          credentials.AccessKey, | 
					
						
							|  |  |  | 			secretKey:          credentials.SecretKey, | 
					
						
							| 
									
										
										
										
											2016-10-11 00:29:56 +08:00
										 |  |  | 			expectedRespStatus: http.StatusNoContent, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		// Test case - 2.
 | 
					
						
							|  |  |  | 		// Case with non-existent-bucket.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			bucketName:         "non-existent-bucket", | 
					
						
							| 
									
										
										
										
											2016-12-27 02:21:23 +08:00
										 |  |  | 			accessKey:          credentials.AccessKey, | 
					
						
							|  |  |  | 			secretKey:          credentials.SecretKey, | 
					
						
							| 
									
										
										
										
											2016-11-15 07:45:00 +08:00
										 |  |  | 			expectedRespStatus: http.StatusNotFound, | 
					
						
							| 
									
										
										
										
											2016-10-11 00:29:56 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		// Test case - 3.
 | 
					
						
							|  |  |  | 		// Case with invalid bucket name.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			bucketName:         ".invalid-bucket-name", | 
					
						
							| 
									
										
										
										
											2016-12-27 02:21:23 +08:00
										 |  |  | 			accessKey:          credentials.AccessKey, | 
					
						
							|  |  |  | 			secretKey:          credentials.SecretKey, | 
					
						
							| 
									
										
										
										
											2016-10-11 00:29:56 +08:00
										 |  |  | 			expectedRespStatus: http.StatusBadRequest, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2016-07-04 13:35:30 +08: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-10-01 05:32:13 +08:00
										 |  |  | 		recV4 := httptest.NewRecorder() | 
					
						
							| 
									
										
										
										
											2016-07-04 13:35:30 +08:00
										 |  |  | 		// construct HTTP request for Delete bucket policy endpoint.
 | 
					
						
							| 
									
										
										
										
											2016-10-01 05:32:13 +08:00
										 |  |  | 		reqV4, err := newTestSignedRequestV4("DELETE", getDeletePolicyURL("", testCase.bucketName), | 
					
						
							| 
									
										
										
										
											2016-07-04 13:35:30 +08:00
										 |  |  | 			0, nil, testCase.accessKey, testCase.secretKey) | 
					
						
							|  |  |  | 		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-10-01 05:32:13 +08:00
										 |  |  | 		apiRouter.ServeHTTP(recV4, reqV4) | 
					
						
							| 
									
										
										
										
											2016-07-04 13:35:30 +08:00
										 |  |  | 		// Assert the response code with the expected status.
 | 
					
						
							| 
									
										
										
										
											2016-10-01 05:32:13 +08: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 13:35:30 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-10-01 05:32:13 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-07-04 13:35:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-01 05:32:13 +08: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), | 
					
						
							|  |  |  | 			int64(len(bucketPolicyStr)), bytes.NewReader([]byte(bucketPolicyStr)), testPolicy.accessKey, testPolicy.secretKey) | 
					
						
							|  |  |  | 		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), | 
					
						
							|  |  |  | 			0, nil, testCase.accessKey, testCase.secretKey) | 
					
						
							|  |  |  | 		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 13:35:30 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-08 16:04:26 +08: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 { | 
					
						
							|  |  |  | 		t.Fatalf("Minio %s: Failed to create an anonymous request for bucket \"%s\": <ERROR> %v", | 
					
						
							|  |  |  | 			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.
 | 
					
						
							|  |  |  | 	ExecObjectLayerAPIAnonTest(t, "DeleteBucketPolicyHandler", bucketName, "", instanceType, apiRouter, anonReq, getReadOnlyObjectStatement) | 
					
						
							| 
									
										
										
										
											2016-10-07 04:34:33 +08: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), | 
					
						
							|  |  |  | 		0, nil, "", "") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-10-08 16:04:26 +08:00
										 |  |  | 		t.Errorf("Minio %s: Failed to create HTTP request for testing the response when object Layer is set to `nil`.", instanceType) | 
					
						
							| 
									
										
										
										
											2016-10-07 04:34:33 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	// execute the object layer set to `nil` test.
 | 
					
						
							|  |  |  | 	// `ExecObjectLayerAPINilTest` manages the operation.
 | 
					
						
							|  |  |  | 	ExecObjectLayerAPINilTest(t, nilBucket, "", instanceType, apiRouter, nilReq) | 
					
						
							| 
									
										
										
										
											2016-07-04 13:35:30 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-10-06 15:23:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // TestBucketPolicyConditionMatch - Tests to validate whether bucket policy conditions match.
 | 
					
						
							|  |  |  | func TestBucketPolicyConditionMatch(t *testing.T) { | 
					
						
							|  |  |  | 	// obtain the inner map[string]set.StringSet for policyStatement.Conditions .
 | 
					
						
							|  |  |  | 	getInnerMap := func(key2, value string) map[string]set.StringSet { | 
					
						
							|  |  |  | 		innerMap := make(map[string]set.StringSet) | 
					
						
							|  |  |  | 		innerMap[key2] = set.CreateStringSet(value) | 
					
						
							|  |  |  | 		return innerMap | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// obtain policyStatement with Conditions set.
 | 
					
						
							|  |  |  | 	getStatementWithCondition := func(key1, key2, value string) policyStatement { | 
					
						
							|  |  |  | 		innerMap := getInnerMap(key2, value) | 
					
						
							|  |  |  | 		// to set policyStatment.Conditions .
 | 
					
						
							|  |  |  | 		conditions := make(map[string]map[string]set.StringSet) | 
					
						
							|  |  |  | 		conditions[key1] = innerMap | 
					
						
							|  |  |  | 		// new policy statement.
 | 
					
						
							|  |  |  | 		statement := policyStatement{} | 
					
						
							|  |  |  | 		// set the condition.
 | 
					
						
							|  |  |  | 		statement.Conditions = conditions | 
					
						
							|  |  |  | 		return statement | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							|  |  |  | 		statementCondition policyStatement | 
					
						
							|  |  |  | 		condition          map[string]set.StringSet | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		expectedMatch bool | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Test case - 1.
 | 
					
						
							|  |  |  | 		// StringEquals condition matches.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			statementCondition: getStatementWithCondition("StringEquals", "s3:prefix", "Asia/"), | 
					
						
							|  |  |  | 			condition:          getInnerMap("prefix", "Asia/"), | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			expectedMatch: true, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		// Test case - 2.
 | 
					
						
							|  |  |  | 		// StringEquals condition doesn't match.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			statementCondition: getStatementWithCondition("StringEquals", "s3:prefix", "Asia/"), | 
					
						
							|  |  |  | 			condition:          getInnerMap("prefix", "Africa/"), | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			expectedMatch: false, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		// Test case - 3.
 | 
					
						
							|  |  |  | 		// StringEquals condition matches.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			statementCondition: getStatementWithCondition("StringEquals", "s3:max-keys", "Asia/"), | 
					
						
							|  |  |  | 			condition:          getInnerMap("max-keys", "Asia/"), | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			expectedMatch: true, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		// Test case - 4.
 | 
					
						
							|  |  |  | 		// StringEquals condition doesn't match.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			statementCondition: getStatementWithCondition("StringEquals", "s3:max-keys", "Asia/"), | 
					
						
							|  |  |  | 			condition:          getInnerMap("max-keys", "Africa/"), | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			expectedMatch: false, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		// Test case - 5.
 | 
					
						
							|  |  |  | 		// StringNotEquals condition matches.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			statementCondition: getStatementWithCondition("StringNotEquals", "s3:prefix", "Asia/"), | 
					
						
							|  |  |  | 			condition:          getInnerMap("prefix", "Asia/"), | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-31 01:18:10 +08:00
										 |  |  | 			expectedMatch: false, | 
					
						
							| 
									
										
										
										
											2016-10-06 15:23:46 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		// Test case - 6.
 | 
					
						
							|  |  |  | 		// StringNotEquals condition doesn't match.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			statementCondition: getStatementWithCondition("StringNotEquals", "s3:prefix", "Asia/"), | 
					
						
							|  |  |  | 			condition:          getInnerMap("prefix", "Africa/"), | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-31 01:18:10 +08:00
										 |  |  | 			expectedMatch: true, | 
					
						
							| 
									
										
										
										
											2016-10-06 15:23:46 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		// Test case - 7.
 | 
					
						
							|  |  |  | 		// StringNotEquals condition matches.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			statementCondition: getStatementWithCondition("StringNotEquals", "s3:max-keys", "Asia/"), | 
					
						
							|  |  |  | 			condition:          getInnerMap("max-keys", "Asia/"), | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-31 01:18:10 +08:00
										 |  |  | 			expectedMatch: false, | 
					
						
							| 
									
										
										
										
											2016-10-06 15:23:46 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		// Test case - 8.
 | 
					
						
							|  |  |  | 		// StringNotEquals condition doesn't match.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			statementCondition: getStatementWithCondition("StringNotEquals", "s3:max-keys", "Asia/"), | 
					
						
							|  |  |  | 			condition:          getInnerMap("max-keys", "Africa/"), | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-31 01:18:10 +08:00
										 |  |  | 			expectedMatch: true, | 
					
						
							| 
									
										
										
										
											2016-10-06 15:23:46 +08:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2017-01-30 11:45:11 +08:00
										 |  |  | 		// Test case - 9.
 | 
					
						
							|  |  |  | 		// StringLike condition matches.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			statementCondition: getStatementWithCondition("StringLike", "aws:Referer", "http://www.example.com/"), | 
					
						
							|  |  |  | 			condition:          getInnerMap("referer", "http://www.example.com/"), | 
					
						
							|  |  |  | 			expectedMatch:      true, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		// Test case - 10.
 | 
					
						
							|  |  |  | 		// StringLike condition doesn't match.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			statementCondition: getStatementWithCondition("StringLike", "aws:Referer", "http://www.example.com/"), | 
					
						
							|  |  |  | 			condition:          getInnerMap("referer", "www.somethingelse.com"), | 
					
						
							|  |  |  | 			expectedMatch:      false, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		// Test case - 11.
 | 
					
						
							|  |  |  | 		// StringNotLike condition evaluates to false.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			statementCondition: getStatementWithCondition("StringNotLike", "aws:Referer", "http://www.example.com/"), | 
					
						
							|  |  |  | 			condition:          getInnerMap("referer", "http://www.example.com/"), | 
					
						
							|  |  |  | 			expectedMatch:      false, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		// Test case - 12.
 | 
					
						
							|  |  |  | 		// StringNotLike condition evaluates to true.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			statementCondition: getStatementWithCondition("StringNotLike", "aws:Referer", "http://www.example.com/"), | 
					
						
							|  |  |  | 			condition:          getInnerMap("referer", "http://somethingelse.com/"), | 
					
						
							|  |  |  | 			expectedMatch:      true, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2016-10-06 15:23:46 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for i, tc := range testCases { | 
					
						
							| 
									
										
										
										
											2017-01-08 03:27:01 +08:00
										 |  |  | 		t.Run(fmt.Sprintf("Case %d", i+1), func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-10-06 15:23:46 +08:00
										 |  |  | 			// call the function under test and assert the result with the expected result.
 | 
					
						
							|  |  |  | 			doesMatch := bucketPolicyConditionMatch(tc.condition, tc.statementCondition) | 
					
						
							|  |  |  | 			if tc.expectedMatch != doesMatch { | 
					
						
							| 
									
										
										
										
											2017-01-31 01:18:10 +08:00
										 |  |  | 				t.Errorf("Expected the match to be `%v`; got `%v` - %v %v.", | 
					
						
							|  |  |  | 					tc.expectedMatch, doesMatch, tc.condition, tc.statementCondition) | 
					
						
							| 
									
										
										
										
											2016-10-06 15:23:46 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |