2016-08-16 01:13:03 +02:00
|
|
|
/*
|
2017-01-18 21:24:34 +01:00
|
|
|
* Minio Cloud Storage, (C) 2016, 2017 Minio, Inc.
|
2016-08-16 01:13:03 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-08-19 01:23:42 +02:00
|
|
|
package cmd
|
2016-08-16 01:13:03 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
2017-01-11 22:26:42 +01:00
|
|
|
"fmt"
|
2016-08-16 01:13:03 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2016-09-23 08:06:45 +02:00
|
|
|
"reflect"
|
2016-08-16 01:13:03 +02:00
|
|
|
"strconv"
|
2016-09-23 01:35:12 +02:00
|
|
|
"strings"
|
2016-08-16 01:13:03 +02:00
|
|
|
"testing"
|
2016-08-30 19:04:50 +02:00
|
|
|
|
2016-11-23 03:18:22 +01:00
|
|
|
humanize "github.com/dustin/go-humanize"
|
2016-08-30 19:04:50 +02:00
|
|
|
"github.com/minio/minio-go/pkg/policy"
|
2016-10-13 18:19:04 +02:00
|
|
|
"github.com/minio/minio-go/pkg/set"
|
2016-08-16 01:13:03 +02:00
|
|
|
)
|
|
|
|
|
2016-11-02 22:45:11 +01:00
|
|
|
// Tests private function writeWebErrorResponse.
|
|
|
|
func TestWriteWebErrorResponse(t *testing.T) {
|
|
|
|
var buffer bytes.Buffer
|
|
|
|
testCases := []struct {
|
|
|
|
webErr error
|
|
|
|
apiErrCode APIErrorCode
|
|
|
|
}{
|
|
|
|
// List of various errors and their corresponding API errors.
|
|
|
|
{
|
|
|
|
webErr: StorageFull{},
|
|
|
|
apiErrCode: ErrStorageFull,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
webErr: BucketNotFound{},
|
|
|
|
apiErrCode: ErrNoSuchBucket,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
webErr: BucketNameInvalid{},
|
|
|
|
apiErrCode: ErrInvalidBucketName,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
webErr: BadDigest{},
|
|
|
|
apiErrCode: ErrBadDigest,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
webErr: IncompleteBody{},
|
|
|
|
apiErrCode: ErrIncompleteBody,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
webErr: ObjectExistsAsDirectory{},
|
|
|
|
apiErrCode: ErrObjectExistsAsDirectory,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
webErr: ObjectNotFound{},
|
|
|
|
apiErrCode: ErrNoSuchKey,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
webErr: ObjectNameInvalid{},
|
|
|
|
apiErrCode: ErrNoSuchKey,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
webErr: InsufficientWriteQuorum{},
|
|
|
|
apiErrCode: ErrWriteQuorum,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
webErr: InsufficientReadQuorum{},
|
|
|
|
apiErrCode: ErrReadQuorum,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate all the test cases.
|
|
|
|
for i, testCase := range testCases {
|
|
|
|
writeWebErrorResponse(newFlushWriter(&buffer), testCase.webErr)
|
|
|
|
desc := getAPIError(testCase.apiErrCode).Description
|
|
|
|
recvDesc := buffer.Bytes()
|
|
|
|
// Check if the written desc is same as the one expected.
|
|
|
|
if !bytes.Equal(recvDesc, []byte(desc)) {
|
|
|
|
t.Errorf("Test %d: Unexpected response, expecting %s, got %s", i+1, desc, string(buffer.Bytes()))
|
|
|
|
}
|
|
|
|
buffer.Reset()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-16 01:13:03 +02:00
|
|
|
// Authenticate and get JWT token - will be called before every webrpc handler invocation
|
|
|
|
func getWebRPCToken(apiRouter http.Handler, accessKey, secretKey string) (token string, err error) {
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
request := LoginArgs{Username: accessKey, Password: secretKey}
|
|
|
|
reply := &LoginRep{}
|
2016-12-23 16:12:19 +01:00
|
|
|
req, err := newTestWebRPCRequest("Web"+loginMethodName, "", request)
|
2016-08-16 01:13:03 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
apiRouter.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
|
|
return "", errors.New("Auth failed")
|
|
|
|
}
|
|
|
|
err = getTestWebRPCResponse(rec, &reply)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if reply.Token == "" {
|
|
|
|
return "", errors.New("Auth failed")
|
|
|
|
}
|
|
|
|
return reply.Token, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrapper for calling Login Web Handler
|
|
|
|
func TestWebHandlerLogin(t *testing.T) {
|
|
|
|
ExecObjectLayerTest(t, testLoginWebHandler)
|
|
|
|
}
|
|
|
|
|
|
|
|
// testLoginWebHandler - Test login web handler
|
|
|
|
func testLoginWebHandler(obj ObjectLayer, instanceType string, t TestErrHandler) {
|
|
|
|
// Register the API end points with XL/FS object layer.
|
|
|
|
apiRouter := initTestWebRPCEndPoint(obj)
|
|
|
|
// initialize the server and obtain the credentials and root.
|
|
|
|
// credentials are necessary to sign the HTTP request.
|
2017-01-18 21:24:34 +01:00
|
|
|
rootPath, err := newTestConfig(globalMinioDefaultRegion)
|
2016-08-16 01:13:03 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Init Test config failed")
|
|
|
|
}
|
2016-11-10 08:37:12 +01:00
|
|
|
// remove the root directory after the test ends.
|
2016-08-16 01:13:03 +02:00
|
|
|
defer removeAll(rootPath)
|
|
|
|
|
|
|
|
credentials := serverConfig.GetCredential()
|
|
|
|
|
|
|
|
// test cases with sample input and expected output.
|
|
|
|
testCases := []struct {
|
|
|
|
username string
|
|
|
|
password string
|
|
|
|
success bool
|
|
|
|
}{
|
|
|
|
{"", "", false},
|
|
|
|
{"azerty", "foo", false},
|
|
|
|
{"", "foo", false},
|
|
|
|
{"azerty", "", false},
|
|
|
|
{"azerty", "foo", false},
|
2017-01-03 10:33:00 +01:00
|
|
|
{"azerty", "azerty123", false},
|
2016-12-26 19:21:23 +01:00
|
|
|
{credentials.AccessKey, credentials.SecretKey, true},
|
2016-08-16 01:13:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Iterating over the test cases, calling the function under test and asserting the response.
|
|
|
|
for i, testCase := range testCases {
|
|
|
|
_, err := getWebRPCToken(apiRouter, testCase.username, testCase.password)
|
|
|
|
if err != nil && testCase.success {
|
|
|
|
t.Fatalf("Test %d: Expected to succeed but it failed, %v", i+1, err)
|
|
|
|
}
|
|
|
|
if err == nil && !testCase.success {
|
|
|
|
t.Fatalf("Test %d: Expected to fail but it didn't, %v", i+1, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrapper for calling StorageInfo Web Handler
|
|
|
|
func TestWebHandlerStorageInfo(t *testing.T) {
|
|
|
|
ExecObjectLayerTest(t, testStorageInfoWebHandler)
|
|
|
|
}
|
|
|
|
|
|
|
|
// testStorageInfoWebHandler - Test StorageInfo web handler
|
|
|
|
func testStorageInfoWebHandler(obj ObjectLayer, instanceType string, t TestErrHandler) {
|
|
|
|
// get random bucket name.
|
|
|
|
// Register the API end points with XL/FS object layer.
|
|
|
|
apiRouter := initTestWebRPCEndPoint(obj)
|
|
|
|
// initialize the server and obtain the credentials and root.
|
|
|
|
// credentials are necessary to sign the HTTP request.
|
2017-01-18 21:24:34 +01:00
|
|
|
rootPath, err := newTestConfig(globalMinioDefaultRegion)
|
2016-08-16 01:13:03 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Init Test config failed")
|
|
|
|
}
|
2016-11-10 08:37:12 +01:00
|
|
|
// remove the root directory after the test ends.
|
2016-08-16 01:13:03 +02:00
|
|
|
defer removeAll(rootPath)
|
|
|
|
|
|
|
|
credentials := serverConfig.GetCredential()
|
|
|
|
|
2016-12-26 19:21:23 +01:00
|
|
|
authorization, err := getWebRPCToken(apiRouter, credentials.AccessKey, credentials.SecretKey)
|
2016-08-16 01:13:03 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Cannot authenticate")
|
|
|
|
}
|
|
|
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
2016-12-23 16:12:19 +01:00
|
|
|
storageInfoRequest := AuthRPCArgs{}
|
2016-08-16 01:13:03 +02:00
|
|
|
storageInfoReply := &StorageInfoRep{}
|
|
|
|
req, err := newTestWebRPCRequest("Web.StorageInfo", authorization, storageInfoRequest)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create HTTP request: <ERROR> %v", err)
|
|
|
|
}
|
|
|
|
apiRouter.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
|
|
t.Fatalf("Expected the response status to be 200, but instead found `%d`", rec.Code)
|
|
|
|
}
|
|
|
|
err = getTestWebRPCResponse(rec, &storageInfoReply)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed %v", err)
|
|
|
|
}
|
|
|
|
if storageInfoReply.StorageInfo.Total <= 0 {
|
|
|
|
t.Fatalf("Got a zero or negative total free space disk")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrapper for calling ServerInfo Web Handler
|
|
|
|
func TestWebHandlerServerInfo(t *testing.T) {
|
|
|
|
ExecObjectLayerTest(t, testServerInfoWebHandler)
|
|
|
|
}
|
|
|
|
|
|
|
|
// testServerInfoWebHandler - Test ServerInfo web handler
|
|
|
|
func testServerInfoWebHandler(obj ObjectLayer, instanceType string, t TestErrHandler) {
|
|
|
|
// Register the API end points with XL/FS object layer.
|
|
|
|
apiRouter := initTestWebRPCEndPoint(obj)
|
|
|
|
// initialize the server and obtain the credentials and root.
|
|
|
|
// credentials are necessary to sign the HTTP request.
|
2017-01-18 21:24:34 +01:00
|
|
|
rootPath, err := newTestConfig(globalMinioDefaultRegion)
|
2016-08-16 01:13:03 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Init Test config failed")
|
|
|
|
}
|
2016-11-10 08:37:12 +01:00
|
|
|
// remove the root directory after the test ends.
|
2016-08-16 01:13:03 +02:00
|
|
|
defer removeAll(rootPath)
|
|
|
|
|
|
|
|
credentials := serverConfig.GetCredential()
|
|
|
|
|
2016-12-26 19:21:23 +01:00
|
|
|
authorization, err := getWebRPCToken(apiRouter, credentials.AccessKey, credentials.SecretKey)
|
2016-08-16 01:13:03 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Cannot authenticate")
|
|
|
|
}
|
|
|
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
2016-12-23 16:12:19 +01:00
|
|
|
serverInfoRequest := AuthRPCArgs{}
|
2016-08-16 01:13:03 +02:00
|
|
|
serverInfoReply := &ServerInfoRep{}
|
|
|
|
req, err := newTestWebRPCRequest("Web.ServerInfo", authorization, serverInfoRequest)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create HTTP request: <ERROR> %v", err)
|
|
|
|
}
|
|
|
|
apiRouter.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
|
|
t.Fatalf("Expected the response status to be 200, but instead found `%d`", rec.Code)
|
|
|
|
}
|
|
|
|
err = getTestWebRPCResponse(rec, &serverInfoReply)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed, %v", err)
|
|
|
|
}
|
2016-08-19 01:23:42 +02:00
|
|
|
if serverInfoReply.MinioVersion != Version {
|
2016-08-16 01:13:03 +02:00
|
|
|
t.Fatalf("Cannot get minio version from server info handler")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrapper for calling MakeBucket Web Handler
|
|
|
|
func TestWebHandlerMakeBucket(t *testing.T) {
|
|
|
|
ExecObjectLayerTest(t, testMakeBucketWebHandler)
|
|
|
|
}
|
|
|
|
|
|
|
|
// testMakeBucketWebHandler - Test MakeBucket web handler
|
|
|
|
func testMakeBucketWebHandler(obj ObjectLayer, instanceType string, t TestErrHandler) {
|
|
|
|
// Register the API end points with XL/FS object layer.
|
|
|
|
apiRouter := initTestWebRPCEndPoint(obj)
|
|
|
|
// initialize the server and obtain the credentials and root.
|
|
|
|
// credentials are necessary to sign the HTTP request.
|
2017-01-18 21:24:34 +01:00
|
|
|
rootPath, err := newTestConfig(globalMinioDefaultRegion)
|
2016-08-16 01:13:03 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Init Test config failed")
|
|
|
|
}
|
2016-11-10 08:37:12 +01:00
|
|
|
// remove the root directory after the test ends.
|
2016-08-16 01:13:03 +02:00
|
|
|
defer removeAll(rootPath)
|
|
|
|
|
|
|
|
credentials := serverConfig.GetCredential()
|
|
|
|
|
2016-12-26 19:21:23 +01:00
|
|
|
authorization, err := getWebRPCToken(apiRouter, credentials.AccessKey, credentials.SecretKey)
|
2016-08-16 01:13:03 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Cannot authenticate")
|
|
|
|
}
|
|
|
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
|
|
|
bucketName := getRandomBucketName()
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
bucketName string
|
|
|
|
success bool
|
|
|
|
}{
|
|
|
|
{"", false},
|
|
|
|
{".", false},
|
|
|
|
{"ab", false},
|
|
|
|
{bucketName, true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, testCase := range testCases {
|
|
|
|
makeBucketRequest := MakeBucketArgs{BucketName: testCase.bucketName}
|
|
|
|
makeBucketReply := &WebGenericRep{}
|
|
|
|
req, err := newTestWebRPCRequest("Web.MakeBucket", authorization, makeBucketRequest)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Test %d: Failed to create HTTP request: <ERROR> %v", i+1, err)
|
|
|
|
}
|
|
|
|
apiRouter.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
|
|
t.Fatalf("Test %d: Expected the response status to be 200, but instead found `%d`", i+1, rec.Code)
|
|
|
|
}
|
|
|
|
err = getTestWebRPCResponse(rec, &makeBucketReply)
|
|
|
|
if testCase.success && err != nil {
|
|
|
|
t.Fatalf("Test %d: Should succeed but it didn't, %v", i+1, err)
|
|
|
|
}
|
|
|
|
if !testCase.success && err == nil {
|
|
|
|
t.Fatalf("Test %d: Should fail but it didn't (%s)", i+1, testCase.bucketName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrapper for calling ListBuckets Web Handler
|
|
|
|
func TestWebHandlerListBuckets(t *testing.T) {
|
|
|
|
ExecObjectLayerTest(t, testListBucketsWebHandler)
|
|
|
|
}
|
|
|
|
|
|
|
|
// testListBucketsHandler - Test ListBuckets web handler
|
|
|
|
func testListBucketsWebHandler(obj ObjectLayer, instanceType string, t TestErrHandler) {
|
|
|
|
// Register the API end points with XL/FS object layer.
|
|
|
|
apiRouter := initTestWebRPCEndPoint(obj)
|
|
|
|
// initialize the server and obtain the credentials and root.
|
|
|
|
// credentials are necessary to sign the HTTP request.
|
2017-01-18 21:24:34 +01:00
|
|
|
rootPath, err := newTestConfig(globalMinioDefaultRegion)
|
2016-08-16 01:13:03 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Init Test config failed")
|
|
|
|
}
|
2016-11-10 08:37:12 +01:00
|
|
|
// remove the root directory after the test ends.
|
2016-08-16 01:13:03 +02:00
|
|
|
defer removeAll(rootPath)
|
|
|
|
|
|
|
|
credentials := serverConfig.GetCredential()
|
|
|
|
|
2016-12-26 19:21:23 +01:00
|
|
|
authorization, err := getWebRPCToken(apiRouter, credentials.AccessKey, credentials.SecretKey)
|
2016-08-16 01:13:03 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Cannot authenticate")
|
|
|
|
}
|
|
|
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
|
|
|
bucketName := getRandomBucketName()
|
|
|
|
// Create bucket.
|
|
|
|
err = obj.MakeBucket(bucketName)
|
|
|
|
if err != nil {
|
|
|
|
// failed to create newbucket, abort.
|
|
|
|
t.Fatalf("%s : %s", instanceType, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
listBucketsRequest := WebGenericArgs{}
|
|
|
|
listBucketsReply := &ListBucketsRep{}
|
|
|
|
req, err := newTestWebRPCRequest("Web.ListBuckets", authorization, listBucketsRequest)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create HTTP request: <ERROR> %v", err)
|
|
|
|
}
|
|
|
|
apiRouter.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
|
|
t.Fatalf("Expected the response status to be 200, but instead found `%d`", rec.Code)
|
|
|
|
}
|
|
|
|
err = getTestWebRPCResponse(rec, &listBucketsReply)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed, %v", err)
|
|
|
|
}
|
|
|
|
if len(listBucketsReply.Buckets) == 0 {
|
|
|
|
t.Fatalf("Cannot find the bucket already created by MakeBucket")
|
|
|
|
}
|
|
|
|
if listBucketsReply.Buckets[0].Name != bucketName {
|
|
|
|
t.Fatalf("Found another bucket other than already created by MakeBucket")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrapper for calling ListObjects Web Handler
|
|
|
|
func TestWebHandlerListObjects(t *testing.T) {
|
|
|
|
ExecObjectLayerTest(t, testListObjectsWebHandler)
|
|
|
|
}
|
|
|
|
|
|
|
|
// testListObjectsHandler - Test ListObjects web handler
|
|
|
|
func testListObjectsWebHandler(obj ObjectLayer, instanceType string, t TestErrHandler) {
|
|
|
|
// Register the API end points with XL/FS object layer.
|
|
|
|
apiRouter := initTestWebRPCEndPoint(obj)
|
|
|
|
// initialize the server and obtain the credentials and root.
|
|
|
|
// credentials are necessary to sign the HTTP request.
|
2017-01-18 21:24:34 +01:00
|
|
|
rootPath, err := newTestConfig(globalMinioDefaultRegion)
|
2016-08-16 01:13:03 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Init Test config failed")
|
|
|
|
}
|
2016-11-10 08:37:12 +01:00
|
|
|
// remove the root directory after the test ends.
|
2016-08-16 01:13:03 +02:00
|
|
|
defer removeAll(rootPath)
|
|
|
|
|
|
|
|
credentials := serverConfig.GetCredential()
|
|
|
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
2016-12-26 19:21:23 +01:00
|
|
|
authorization, err := getWebRPCToken(apiRouter, credentials.AccessKey, credentials.SecretKey)
|
2016-08-16 01:13:03 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Cannot authenticate")
|
|
|
|
}
|
|
|
|
|
|
|
|
bucketName := getRandomBucketName()
|
|
|
|
objectName := "object"
|
2016-11-23 03:18:22 +01:00
|
|
|
objectSize := 1 * humanize.KiByte
|
2016-08-16 01:13:03 +02:00
|
|
|
|
|
|
|
// Create bucket.
|
|
|
|
err = obj.MakeBucket(bucketName)
|
|
|
|
if err != nil {
|
|
|
|
// failed to create newbucket, abort.
|
|
|
|
t.Fatalf("%s : %s", instanceType, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
data := bytes.Repeat([]byte("a"), objectSize)
|
|
|
|
|
2016-10-03 00:51:49 +02:00
|
|
|
_, err = obj.PutObject(bucketName, objectName, int64(len(data)), bytes.NewReader(data), map[string]string{"md5Sum": "c9a34cfc85d982698c6ac89f76071abd"}, "")
|
2016-08-16 01:13:03 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Was not able to upload an object, %v", err)
|
|
|
|
}
|
|
|
|
|
2017-01-11 22:26:42 +01:00
|
|
|
test := func(token string) (error, *ListObjectsRep) {
|
|
|
|
listObjectsRequest := ListObjectsArgs{BucketName: bucketName, Prefix: ""}
|
|
|
|
listObjectsReply := &ListObjectsRep{}
|
|
|
|
var req *http.Request
|
|
|
|
req, err = newTestWebRPCRequest("Web.ListObjects", token, listObjectsRequest)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create HTTP request: <ERROR> %v", err)
|
|
|
|
}
|
|
|
|
apiRouter.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
|
|
return fmt.Errorf("Expected the response status to be 200, but instead found `%d`", rec.Code), listObjectsReply
|
|
|
|
}
|
|
|
|
err = getTestWebRPCResponse(rec, &listObjectsReply)
|
|
|
|
if err != nil {
|
|
|
|
return err, listObjectsReply
|
|
|
|
}
|
|
|
|
return nil, listObjectsReply
|
2016-08-16 01:13:03 +02:00
|
|
|
}
|
2017-01-11 22:26:42 +01:00
|
|
|
verifyReply := func(reply *ListObjectsRep) {
|
|
|
|
if len(reply.Objects) == 0 {
|
|
|
|
t.Fatalf("Cannot find the object")
|
|
|
|
}
|
|
|
|
if reply.Objects[0].Key != objectName {
|
|
|
|
t.Fatalf("Found another object other than already created by PutObject")
|
|
|
|
}
|
|
|
|
if reply.Objects[0].Size != int64(objectSize) {
|
|
|
|
t.Fatalf("Found a object with the same name but with a different size")
|
|
|
|
}
|
2016-08-16 01:13:03 +02:00
|
|
|
}
|
2017-01-11 22:26:42 +01:00
|
|
|
|
|
|
|
// Authenticated ListObjects should succeed.
|
|
|
|
err, reply := test(authorization)
|
2016-08-16 01:13:03 +02:00
|
|
|
if err != nil {
|
2017-01-11 22:26:42 +01:00
|
|
|
t.Fatal(err)
|
2016-08-16 01:13:03 +02:00
|
|
|
}
|
2017-01-11 22:26:42 +01:00
|
|
|
verifyReply(reply)
|
|
|
|
|
|
|
|
// Unauthenticated ListObjects should fail.
|
2017-01-17 02:05:00 +01:00
|
|
|
err, _ = test("")
|
2017-01-11 22:26:42 +01:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected error `%s`", err)
|
2016-08-16 01:13:03 +02:00
|
|
|
}
|
2017-01-11 22:26:42 +01:00
|
|
|
|
|
|
|
policy := bucketPolicy{
|
|
|
|
Version: "1.0",
|
|
|
|
Statements: []policyStatement{getReadOnlyObjectStatement(bucketName, "")},
|
2016-08-16 01:13:03 +02:00
|
|
|
}
|
|
|
|
|
2017-01-11 22:26:42 +01:00
|
|
|
globalBucketPolicies.SetBucketPolicy(bucketName, policyChange{false, &policy})
|
|
|
|
|
|
|
|
// Unauthenticated ListObjects with READ bucket policy should succeed.
|
|
|
|
err, reply = test("")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
verifyReply(reply)
|
2016-08-16 01:13:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wrapper for calling RemoveObject Web Handler
|
|
|
|
func TestWebHandlerRemoveObject(t *testing.T) {
|
|
|
|
ExecObjectLayerTest(t, testRemoveObjectWebHandler)
|
|
|
|
}
|
|
|
|
|
|
|
|
// testRemoveObjectWebHandler - Test RemoveObject web handler
|
|
|
|
func testRemoveObjectWebHandler(obj ObjectLayer, instanceType string, t TestErrHandler) {
|
|
|
|
// Register the API end points with XL/FS object layer.
|
|
|
|
apiRouter := initTestWebRPCEndPoint(obj)
|
|
|
|
// initialize the server and obtain the credentials and root.
|
|
|
|
// credentials are necessary to sign the HTTP request.
|
2017-01-18 21:24:34 +01:00
|
|
|
rootPath, err := newTestConfig(globalMinioDefaultRegion)
|
2016-08-16 01:13:03 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Init Test config failed")
|
|
|
|
}
|
2016-11-10 08:37:12 +01:00
|
|
|
// remove the root directory after the test ends.
|
2016-08-16 01:13:03 +02:00
|
|
|
defer removeAll(rootPath)
|
|
|
|
|
|
|
|
credentials := serverConfig.GetCredential()
|
|
|
|
|
|
|
|
rec := httptest.NewRecorder()
|
2016-12-26 19:21:23 +01:00
|
|
|
authorization, err := getWebRPCToken(apiRouter, credentials.AccessKey, credentials.SecretKey)
|
2016-08-16 01:13:03 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Cannot authenticate")
|
|
|
|
}
|
|
|
|
|
|
|
|
bucketName := getRandomBucketName()
|
|
|
|
objectName := "object"
|
2016-11-23 03:18:22 +01:00
|
|
|
objectSize := 1 * humanize.KiByte
|
2016-08-16 01:13:03 +02:00
|
|
|
|
|
|
|
// Create bucket.
|
|
|
|
err = obj.MakeBucket(bucketName)
|
|
|
|
if err != nil {
|
|
|
|
// failed to create newbucket, abort.
|
|
|
|
t.Fatalf("%s : %s", instanceType, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
data := bytes.Repeat([]byte("a"), objectSize)
|
|
|
|
|
2016-11-22 20:12:38 +01:00
|
|
|
_, err = obj.PutObject(bucketName, objectName, int64(len(data)), bytes.NewReader(data),
|
|
|
|
map[string]string{"md5Sum": "c9a34cfc85d982698c6ac89f76071abd"}, "")
|
2016-08-16 01:13:03 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Was not able to upload an object, %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
removeObjectRequest := RemoveObjectArgs{BucketName: bucketName, ObjectName: objectName}
|
|
|
|
removeObjectReply := &WebGenericRep{}
|
|
|
|
req, err := newTestWebRPCRequest("Web.RemoveObject", authorization, removeObjectRequest)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create HTTP request: <ERROR> %v", err)
|
|
|
|
}
|
|
|
|
apiRouter.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
|
|
t.Fatalf("Expected the response status to be 200, but instead found `%d`", rec.Code)
|
|
|
|
}
|
|
|
|
err = getTestWebRPCResponse(rec, &removeObjectReply)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed, %v", err)
|
|
|
|
}
|
2016-11-22 20:12:38 +01:00
|
|
|
|
|
|
|
removeObjectRequest = RemoveObjectArgs{BucketName: bucketName, ObjectName: objectName}
|
|
|
|
removeObjectReply = &WebGenericRep{}
|
|
|
|
req, err = newTestWebRPCRequest("Web.RemoveObject", authorization, removeObjectRequest)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create HTTP request: <ERROR> %v", err)
|
|
|
|
}
|
|
|
|
apiRouter.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
|
|
t.Fatalf("Expected the response status to be 200, but instead found `%d`", rec.Code)
|
|
|
|
}
|
|
|
|
err = getTestWebRPCResponse(rec, &removeObjectReply)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed, %v", err)
|
|
|
|
}
|
2016-08-16 01:13:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wrapper for calling Generate Auth Handler
|
|
|
|
func TestWebHandlerGenerateAuth(t *testing.T) {
|
|
|
|
ExecObjectLayerTest(t, testGenerateAuthWebHandler)
|
|
|
|
}
|
|
|
|
|
|
|
|
// testGenerateAuthWebHandler - Test GenerateAuth web handler
|
|
|
|
func testGenerateAuthWebHandler(obj ObjectLayer, instanceType string, t TestErrHandler) {
|
|
|
|
// Register the API end points with XL/FS object layer.
|
|
|
|
apiRouter := initTestWebRPCEndPoint(obj)
|
|
|
|
// initialize the server and obtain the credentials and root.
|
|
|
|
// credentials are necessary to sign the HTTP request.
|
2017-01-18 21:24:34 +01:00
|
|
|
rootPath, err := newTestConfig(globalMinioDefaultRegion)
|
2016-08-16 01:13:03 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Init Test config failed")
|
|
|
|
}
|
2016-11-10 08:37:12 +01:00
|
|
|
// remove the root directory after the test ends.
|
2016-08-16 01:13:03 +02:00
|
|
|
defer removeAll(rootPath)
|
|
|
|
|
|
|
|
credentials := serverConfig.GetCredential()
|
|
|
|
|
|
|
|
rec := httptest.NewRecorder()
|
2016-12-26 19:21:23 +01:00
|
|
|
authorization, err := getWebRPCToken(apiRouter, credentials.AccessKey, credentials.SecretKey)
|
2016-08-16 01:13:03 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Cannot authenticate")
|
|
|
|
}
|
|
|
|
|
|
|
|
generateAuthRequest := WebGenericArgs{}
|
|
|
|
generateAuthReply := &GenerateAuthReply{}
|
|
|
|
req, err := newTestWebRPCRequest("Web.GenerateAuth", authorization, generateAuthRequest)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create HTTP request: <ERROR> %v", err)
|
|
|
|
}
|
|
|
|
apiRouter.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
|
|
t.Fatalf("Expected the response status to be 200, but instead found `%d`", rec.Code)
|
|
|
|
}
|
|
|
|
err = getTestWebRPCResponse(rec, &generateAuthReply)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed, %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if generateAuthReply.AccessKey == "" || generateAuthReply.SecretKey == "" {
|
|
|
|
t.Fatalf("Failed to generate auth keys")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrapper for calling Set Auth Handler
|
|
|
|
func TestWebHandlerSetAuth(t *testing.T) {
|
|
|
|
ExecObjectLayerTest(t, testSetAuthWebHandler)
|
|
|
|
}
|
|
|
|
|
|
|
|
// testSetAuthWebHandler - Test SetAuth web handler
|
|
|
|
func testSetAuthWebHandler(obj ObjectLayer, instanceType string, t TestErrHandler) {
|
|
|
|
// Register the API end points with XL/FS object layer.
|
|
|
|
apiRouter := initTestWebRPCEndPoint(obj)
|
|
|
|
// initialize the server and obtain the credentials and root.
|
|
|
|
// credentials are necessary to sign the HTTP request.
|
2017-01-18 21:24:34 +01:00
|
|
|
rootPath, err := newTestConfig(globalMinioDefaultRegion)
|
2016-08-16 01:13:03 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Init Test config failed")
|
|
|
|
}
|
2016-11-10 08:37:12 +01:00
|
|
|
// remove the root directory after the test ends.
|
2016-08-16 01:13:03 +02:00
|
|
|
defer removeAll(rootPath)
|
|
|
|
|
|
|
|
credentials := serverConfig.GetCredential()
|
|
|
|
|
|
|
|
rec := httptest.NewRecorder()
|
2016-12-26 19:21:23 +01:00
|
|
|
authorization, err := getWebRPCToken(apiRouter, credentials.AccessKey, credentials.SecretKey)
|
2016-08-16 01:13:03 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Cannot authenticate")
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
username string
|
|
|
|
password string
|
|
|
|
success bool
|
|
|
|
}{
|
|
|
|
{"", "", false},
|
2016-11-22 20:12:38 +01:00
|
|
|
{"1", "1", false},
|
2016-08-16 01:13:03 +02:00
|
|
|
{"azerty", "foooooooooooooo", true},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterating over the test cases, calling the function under test and asserting the response.
|
|
|
|
for i, testCase := range testCases {
|
|
|
|
setAuthRequest := SetAuthArgs{AccessKey: testCase.username, SecretKey: testCase.password}
|
|
|
|
setAuthReply := &SetAuthReply{}
|
|
|
|
req, err := newTestWebRPCRequest("Web.SetAuth", authorization, setAuthRequest)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Test %d: Failed to create HTTP request: <ERROR> %v", i+1, err)
|
|
|
|
}
|
|
|
|
apiRouter.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
|
|
t.Fatalf("Test %d: Expected the response status to be 200, but instead found `%d`", i+1, rec.Code)
|
|
|
|
}
|
|
|
|
err = getTestWebRPCResponse(rec, &setAuthReply)
|
|
|
|
if testCase.success && err != nil {
|
|
|
|
t.Fatalf("Test %d: Supposed to succeed but failed, %v", i+1, err)
|
|
|
|
}
|
|
|
|
if !testCase.success && err == nil {
|
|
|
|
t.Fatalf("Test %d: Supposed to fail but succeeded, %v", i+1, err)
|
|
|
|
}
|
|
|
|
if testCase.success && setAuthReply.Token == "" {
|
|
|
|
t.Fatalf("Test %d: Failed to set auth keys", i+1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrapper for calling Get Auth Handler
|
|
|
|
func TestWebHandlerGetAuth(t *testing.T) {
|
|
|
|
ExecObjectLayerTest(t, testGetAuthWebHandler)
|
|
|
|
}
|
|
|
|
|
|
|
|
// testGetAuthWebHandler - Test GetAuth web handler
|
|
|
|
func testGetAuthWebHandler(obj ObjectLayer, instanceType string, t TestErrHandler) {
|
|
|
|
// Register the API end points with XL/FS object layer.
|
|
|
|
apiRouter := initTestWebRPCEndPoint(obj)
|
|
|
|
// initialize the server and obtain the credentials and root.
|
|
|
|
// credentials are necessary to sign the HTTP request.
|
2017-01-18 21:24:34 +01:00
|
|
|
rootPath, err := newTestConfig(globalMinioDefaultRegion)
|
2016-08-16 01:13:03 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Init Test config failed")
|
|
|
|
}
|
2016-11-10 08:37:12 +01:00
|
|
|
// remove the root directory after the test ends.
|
2016-08-16 01:13:03 +02:00
|
|
|
defer removeAll(rootPath)
|
|
|
|
|
|
|
|
credentials := serverConfig.GetCredential()
|
|
|
|
|
|
|
|
rec := httptest.NewRecorder()
|
2016-12-26 19:21:23 +01:00
|
|
|
authorization, err := getWebRPCToken(apiRouter, credentials.AccessKey, credentials.SecretKey)
|
2016-08-16 01:13:03 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Cannot authenticate")
|
|
|
|
}
|
|
|
|
|
|
|
|
getAuthRequest := WebGenericArgs{}
|
|
|
|
getAuthReply := &GetAuthReply{}
|
|
|
|
req, err := newTestWebRPCRequest("Web.GetAuth", authorization, getAuthRequest)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create HTTP request: <ERROR> %v", err)
|
|
|
|
}
|
|
|
|
apiRouter.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
|
|
t.Fatalf("Expected the response status to be 200, but instead found `%d`", rec.Code)
|
|
|
|
}
|
|
|
|
err = getTestWebRPCResponse(rec, &getAuthReply)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed, %v", err)
|
|
|
|
}
|
2016-12-26 19:21:23 +01:00
|
|
|
if getAuthReply.AccessKey != credentials.AccessKey || getAuthReply.SecretKey != credentials.SecretKey {
|
2016-08-16 01:13:03 +02:00
|
|
|
t.Fatalf("Failed to get correct auth keys")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrapper for calling Upload Handler
|
|
|
|
func TestWebHandlerUpload(t *testing.T) {
|
|
|
|
ExecObjectLayerTest(t, testUploadWebHandler)
|
|
|
|
}
|
|
|
|
|
|
|
|
// testUploadWebHandler - Test Upload web handler
|
|
|
|
func testUploadWebHandler(obj ObjectLayer, instanceType string, t TestErrHandler) {
|
|
|
|
// Register the API end points with XL/FS object layer.
|
|
|
|
apiRouter := initTestWebRPCEndPoint(obj)
|
|
|
|
// initialize the server and obtain the credentials and root.
|
|
|
|
// credentials are necessary to sign the HTTP request.
|
2017-01-18 21:24:34 +01:00
|
|
|
rootPath, err := newTestConfig(globalMinioDefaultRegion)
|
2016-08-16 01:13:03 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Init Test config failed")
|
|
|
|
}
|
2016-11-10 08:37:12 +01:00
|
|
|
// remove the root directory after the test ends.
|
2016-08-16 01:13:03 +02:00
|
|
|
defer removeAll(rootPath)
|
|
|
|
|
|
|
|
credentials := serverConfig.GetCredential()
|
2017-01-11 22:26:42 +01:00
|
|
|
content := []byte("temporary file's content")
|
2016-08-16 01:13:03 +02:00
|
|
|
|
2016-12-26 19:21:23 +01:00
|
|
|
authorization, err := getWebRPCToken(apiRouter, credentials.AccessKey, credentials.SecretKey)
|
2016-08-16 01:13:03 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Cannot authenticate")
|
|
|
|
}
|
|
|
|
|
|
|
|
objectName := "test.file"
|
|
|
|
bucketName := getRandomBucketName()
|
2017-01-11 22:26:42 +01:00
|
|
|
|
|
|
|
test := func(token string) int {
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
var req *http.Request
|
|
|
|
req, err = http.NewRequest("PUT", "/minio/upload/"+bucketName+"/"+objectName, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot create upload request, %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("Content-Length", strconv.Itoa(len(content)))
|
|
|
|
req.Header.Set("x-amz-date", "20160814T114029Z")
|
|
|
|
req.Header.Set("Accept", "*/*")
|
|
|
|
req.Body = ioutil.NopCloser(bytes.NewReader(content))
|
|
|
|
|
|
|
|
if token != "" {
|
|
|
|
req.Header.Set("Authorization", "Bearer "+authorization)
|
|
|
|
}
|
|
|
|
apiRouter.ServeHTTP(rec, req)
|
|
|
|
return rec.Code
|
|
|
|
}
|
2016-08-16 01:13:03 +02:00
|
|
|
// Create bucket.
|
|
|
|
err = obj.MakeBucket(bucketName)
|
|
|
|
if err != nil {
|
|
|
|
// failed to create newbucket, abort.
|
|
|
|
t.Fatalf("%s : %s", instanceType, err)
|
|
|
|
}
|
|
|
|
|
2017-01-11 22:26:42 +01:00
|
|
|
// Authenticated upload should succeed.
|
|
|
|
code := test(authorization)
|
|
|
|
if code != http.StatusOK {
|
|
|
|
t.Fatalf("Expected the response status to be 200, but instead found `%d`", code)
|
2016-08-16 01:13:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var byteBuffer bytes.Buffer
|
|
|
|
err = obj.GetObject(bucketName, objectName, 0, int64(len(content)), &byteBuffer)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed, %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if bytes.Compare(byteBuffer.Bytes(), content) != 0 {
|
|
|
|
t.Fatalf("The upload file is different from the download file")
|
|
|
|
}
|
2017-01-11 22:26:42 +01:00
|
|
|
|
|
|
|
// Unauthenticated upload should fail.
|
|
|
|
code = test("")
|
|
|
|
if code != http.StatusForbidden {
|
|
|
|
t.Fatalf("Expected the response status to be 403, but instead found `%d`", code)
|
|
|
|
}
|
|
|
|
|
|
|
|
policy := bucketPolicy{
|
|
|
|
Version: "1.0",
|
|
|
|
Statements: []policyStatement{getWriteOnlyObjectStatement(bucketName, "")},
|
|
|
|
}
|
|
|
|
|
|
|
|
globalBucketPolicies.SetBucketPolicy(bucketName, policyChange{false, &policy})
|
|
|
|
|
|
|
|
// Unauthenticated upload with WRITE policy should succeed.
|
|
|
|
code = test("")
|
|
|
|
if code != http.StatusOK {
|
|
|
|
t.Fatalf("Expected the response status to be 200, but instead found `%d`", code)
|
|
|
|
}
|
2016-08-16 01:13:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wrapper for calling Upload Handler
|
|
|
|
func TestWebHandlerDownload(t *testing.T) {
|
|
|
|
ExecObjectLayerTest(t, testDownloadWebHandler)
|
|
|
|
}
|
|
|
|
|
|
|
|
// testDownloadWebHandler - Test Download web handler
|
|
|
|
func testDownloadWebHandler(obj ObjectLayer, instanceType string, t TestErrHandler) {
|
|
|
|
// Register the API end points with XL/FS object layer.
|
|
|
|
apiRouter := initTestWebRPCEndPoint(obj)
|
|
|
|
// initialize the server and obtain the credentials and root.
|
|
|
|
// credentials are necessary to sign the HTTP request.
|
2017-01-18 21:24:34 +01:00
|
|
|
rootPath, err := newTestConfig(globalMinioDefaultRegion)
|
2016-08-16 01:13:03 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Init Test config failed")
|
|
|
|
}
|
2016-11-10 08:37:12 +01:00
|
|
|
// remove the root directory after the test ends.
|
2016-08-16 01:13:03 +02:00
|
|
|
defer removeAll(rootPath)
|
|
|
|
|
|
|
|
credentials := serverConfig.GetCredential()
|
|
|
|
|
2016-12-26 19:21:23 +01:00
|
|
|
authorization, err := getWebRPCToken(apiRouter, credentials.AccessKey, credentials.SecretKey)
|
2016-08-16 01:13:03 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Cannot authenticate")
|
|
|
|
}
|
|
|
|
|
|
|
|
objectName := "test.file"
|
|
|
|
bucketName := getRandomBucketName()
|
2017-01-11 22:26:42 +01:00
|
|
|
|
|
|
|
test := func(token string) (int, []byte) {
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
path := "/minio/download/" + bucketName + "/" + objectName + "?token="
|
|
|
|
if token != "" {
|
|
|
|
path = path + token
|
|
|
|
}
|
|
|
|
var req *http.Request
|
|
|
|
req, err = http.NewRequest("GET", path, nil)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot create upload request, %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
apiRouter.ServeHTTP(rec, req)
|
|
|
|
return rec.Code, rec.Body.Bytes()
|
|
|
|
}
|
|
|
|
|
2016-08-16 01:13:03 +02:00
|
|
|
// Create bucket.
|
|
|
|
err = obj.MakeBucket(bucketName)
|
|
|
|
if err != nil {
|
|
|
|
// failed to create newbucket, abort.
|
|
|
|
t.Fatalf("%s : %s", instanceType, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
content := []byte("temporary file's content")
|
2016-10-03 00:51:49 +02:00
|
|
|
_, err = obj.PutObject(bucketName, objectName, int64(len(content)), bytes.NewReader(content), map[string]string{"md5Sum": "01ce59706106fe5e02e7f55fffda7f34"}, "")
|
2016-08-16 01:13:03 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Was not able to upload an object, %v", err)
|
|
|
|
}
|
|
|
|
|
2017-01-11 22:26:42 +01:00
|
|
|
// Authenticated download should succeed.
|
|
|
|
code, bodyContent := test(authorization)
|
2016-08-16 01:13:03 +02:00
|
|
|
|
2017-01-11 22:26:42 +01:00
|
|
|
if code != http.StatusOK {
|
|
|
|
t.Fatalf("Expected the response status to be 200, but instead found `%d`", code)
|
2016-08-16 01:13:03 +02:00
|
|
|
}
|
|
|
|
|
2017-01-11 22:26:42 +01:00
|
|
|
if bytes.Compare(bodyContent, content) != 0 {
|
|
|
|
t.Fatalf("The downloaded file is corrupted")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unauthenticated download should fail.
|
2017-01-17 02:05:00 +01:00
|
|
|
code, _ = test("")
|
2017-01-11 22:26:42 +01:00
|
|
|
if code != http.StatusForbidden {
|
|
|
|
t.Fatalf("Expected the response status to be 403, but instead found `%d`", code)
|
|
|
|
}
|
|
|
|
|
|
|
|
policy := bucketPolicy{
|
|
|
|
Version: "1.0",
|
|
|
|
Statements: []policyStatement{getReadOnlyObjectStatement(bucketName, "")},
|
|
|
|
}
|
|
|
|
|
|
|
|
globalBucketPolicies.SetBucketPolicy(bucketName, policyChange{false, &policy})
|
|
|
|
|
|
|
|
// Unauthenticated download with READ policy should succeed.
|
|
|
|
code, bodyContent = test("")
|
|
|
|
if code != http.StatusOK {
|
|
|
|
t.Fatalf("Expected the response status to be 200, but instead found `%d`", code)
|
2016-08-16 01:13:03 +02:00
|
|
|
}
|
|
|
|
|
2017-01-11 22:26:42 +01:00
|
|
|
if bytes.Compare(bodyContent, content) != 0 {
|
2016-08-16 01:13:03 +02:00
|
|
|
t.Fatalf("The downloaded file is corrupted")
|
|
|
|
}
|
|
|
|
}
|
2016-08-30 19:04:50 +02:00
|
|
|
|
2016-09-23 10:24:49 +02:00
|
|
|
// Wrapper for calling PresignedGet handler
|
|
|
|
func TestWebHandlerPresignedGetHandler(t *testing.T) {
|
|
|
|
ExecObjectLayerTest(t, testWebPresignedGetHandler)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testWebPresignedGetHandler(obj ObjectLayer, instanceType string, t TestErrHandler) {
|
|
|
|
// Register the API end points with XL/FS object layer.
|
|
|
|
apiRouter := initTestWebRPCEndPoint(obj)
|
|
|
|
// initialize the server and obtain the credentials and root.
|
|
|
|
// credentials are necessary to sign the HTTP request.
|
2017-01-18 21:24:34 +01:00
|
|
|
rootPath, err := newTestConfig(globalMinioDefaultRegion)
|
2016-09-23 10:24:49 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Init Test config failed")
|
|
|
|
}
|
2016-11-10 08:37:12 +01:00
|
|
|
// remove the root directory after the test ends.
|
2016-09-23 10:24:49 +02:00
|
|
|
defer removeAll(rootPath)
|
|
|
|
|
|
|
|
credentials := serverConfig.GetCredential()
|
|
|
|
|
2016-12-26 19:21:23 +01:00
|
|
|
authorization, err := getWebRPCToken(apiRouter, credentials.AccessKey, credentials.SecretKey)
|
2016-09-23 10:24:49 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Cannot authenticate")
|
|
|
|
}
|
|
|
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
|
|
|
bucketName := getRandomBucketName()
|
|
|
|
objectName := "object"
|
2016-11-23 03:18:22 +01:00
|
|
|
objectSize := 1 * humanize.KiByte
|
2016-09-23 10:24:49 +02:00
|
|
|
|
|
|
|
// Create bucket.
|
|
|
|
err = obj.MakeBucket(bucketName)
|
|
|
|
if err != nil {
|
|
|
|
// failed to create newbucket, abort.
|
|
|
|
t.Fatalf("%s : %s", instanceType, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
data := bytes.Repeat([]byte("a"), objectSize)
|
2016-10-03 00:51:49 +02:00
|
|
|
_, err = obj.PutObject(bucketName, objectName, int64(len(data)), bytes.NewReader(data), map[string]string{"md5Sum": "c9a34cfc85d982698c6ac89f76071abd"}, "")
|
2016-09-23 10:24:49 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Was not able to upload an object, %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
presignGetReq := PresignedGetArgs{
|
|
|
|
HostName: "",
|
|
|
|
BucketName: bucketName,
|
|
|
|
ObjectName: objectName,
|
2016-11-22 20:12:38 +01:00
|
|
|
Expiry: 1000,
|
2016-09-23 10:24:49 +02:00
|
|
|
}
|
|
|
|
presignGetRep := &PresignedGetRep{}
|
|
|
|
req, err := newTestWebRPCRequest("Web.PresignedGet", authorization, presignGetReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create HTTP request: <ERROR> %v", err)
|
|
|
|
}
|
|
|
|
apiRouter.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
|
|
t.Fatalf("Expected the response status to be 200, but instead found `%d`", rec.Code)
|
|
|
|
}
|
|
|
|
err = getTestWebRPCResponse(rec, &presignGetRep)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed, %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register the API end points with XL/FS object layer.
|
|
|
|
apiRouter = initTestAPIEndPoints(obj, []string{"GetObject"})
|
|
|
|
|
|
|
|
// Initialize a new api recorder.
|
|
|
|
arec := httptest.NewRecorder()
|
|
|
|
|
|
|
|
req, err = newTestRequest("GET", presignGetRep.URL, 0, nil)
|
|
|
|
req.Header.Del("x-amz-content-sha256")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to initialized a new request", err)
|
|
|
|
}
|
|
|
|
apiRouter.ServeHTTP(arec, req)
|
|
|
|
if arec.Code != http.StatusOK {
|
|
|
|
t.Fatalf("Expected the response status to be 200, but instead found `%d`", arec.Code)
|
|
|
|
}
|
|
|
|
savedData, err := ioutil.ReadAll(arec.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Reading body failed", err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(data, savedData) {
|
|
|
|
t.Fatal("Read data is not equal was what was expected")
|
|
|
|
}
|
2016-10-23 21:32:35 +02:00
|
|
|
|
|
|
|
// Register the API end points with XL/FS object layer.
|
|
|
|
apiRouter = initTestWebRPCEndPoint(obj)
|
|
|
|
|
|
|
|
presignGetReq = PresignedGetArgs{
|
|
|
|
HostName: "",
|
|
|
|
BucketName: "",
|
|
|
|
ObjectName: "",
|
|
|
|
}
|
|
|
|
presignGetRep = &PresignedGetRep{}
|
|
|
|
req, err = newTestWebRPCRequest("Web.PresignedGet", authorization, presignGetReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create HTTP request: <ERROR> %v", err)
|
|
|
|
}
|
|
|
|
apiRouter.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
|
|
t.Fatalf("Expected the response status to be 200, but instead found `%d`", rec.Code)
|
|
|
|
}
|
|
|
|
err = getTestWebRPCResponse(rec, &presignGetRep)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Failed, %v", err)
|
|
|
|
}
|
2016-11-22 20:12:38 +01:00
|
|
|
if err.Error() != "Bucket and Object are mandatory arguments." {
|
|
|
|
t.Fatalf("Unexpected, expected `Bucket and Object are mandatory arguments`, got %s", err)
|
2016-10-23 21:32:35 +02:00
|
|
|
}
|
2016-09-23 10:24:49 +02:00
|
|
|
}
|
|
|
|
|
2016-08-30 19:04:50 +02:00
|
|
|
// Wrapper for calling GetBucketPolicy Handler
|
|
|
|
func TestWebHandlerGetBucketPolicyHandler(t *testing.T) {
|
|
|
|
ExecObjectLayerTest(t, testWebGetBucketPolicyHandler)
|
|
|
|
}
|
|
|
|
|
|
|
|
// testWebGetBucketPolicyHandler - Test GetBucketPolicy web handler
|
|
|
|
func testWebGetBucketPolicyHandler(obj ObjectLayer, instanceType string, t TestErrHandler) {
|
|
|
|
// Register the API end points with XL/FS object layer.
|
|
|
|
apiRouter := initTestWebRPCEndPoint(obj)
|
|
|
|
// initialize the server and obtain the credentials and root.
|
|
|
|
// credentials are necessary to sign the HTTP request.
|
2017-01-18 21:24:34 +01:00
|
|
|
rootPath, err := newTestConfig(globalMinioDefaultRegion)
|
2016-08-30 19:04:50 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Init Test config failed")
|
|
|
|
}
|
2016-11-10 08:37:12 +01:00
|
|
|
// remove the root directory after the test ends.
|
2016-08-30 19:04:50 +02:00
|
|
|
defer removeAll(rootPath)
|
|
|
|
|
|
|
|
credentials := serverConfig.GetCredential()
|
|
|
|
|
2016-12-26 19:21:23 +01:00
|
|
|
authorization, err := getWebRPCToken(apiRouter, credentials.AccessKey, credentials.SecretKey)
|
2016-08-30 19:04:50 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Cannot authenticate")
|
|
|
|
}
|
|
|
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
|
|
|
bucketName := getRandomBucketName()
|
2016-09-23 01:35:12 +02:00
|
|
|
if err := obj.MakeBucket(bucketName); err != nil {
|
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
|
2016-10-13 18:19:04 +02:00
|
|
|
policyVal := bucketPolicy{
|
|
|
|
Version: "2012-10-17",
|
|
|
|
Statements: []policyStatement{
|
|
|
|
{
|
|
|
|
Actions: set.CreateStringSet("s3:GetBucketLocation", "s3:ListBucket"),
|
|
|
|
Effect: "Allow",
|
|
|
|
Principal: map[string][]string{"AWS": {"*"}},
|
2017-01-11 01:43:48 +01:00
|
|
|
Resources: set.CreateStringSet(bucketARNPrefix + bucketName),
|
2016-10-13 18:19:04 +02:00
|
|
|
Sid: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Actions: set.CreateStringSet("s3:GetObject"),
|
|
|
|
Effect: "Allow",
|
|
|
|
Principal: map[string][]string{"AWS": {"*"}},
|
2017-01-11 01:43:48 +01:00
|
|
|
Resources: set.CreateStringSet(bucketARNPrefix + bucketName + "/*"),
|
2016-10-13 18:19:04 +02:00
|
|
|
Sid: "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := writeBucketPolicy(bucketName, obj, &policyVal); err != nil {
|
2016-09-22 01:38:50 +02:00
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
2016-08-30 19:04:50 +02:00
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
bucketName string
|
|
|
|
prefix string
|
|
|
|
expectedResult policy.BucketPolicy
|
|
|
|
}{
|
2016-09-23 01:35:12 +02:00
|
|
|
{bucketName, "", policy.BucketPolicyReadOnly},
|
2016-08-30 19:04:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, testCase := range testCases {
|
|
|
|
args := &GetBucketPolicyArgs{BucketName: testCase.bucketName, Prefix: testCase.prefix}
|
|
|
|
reply := &GetBucketPolicyRep{}
|
|
|
|
req, err := newTestWebRPCRequest("Web.GetBucketPolicy", authorization, args)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Test %d: Failed to create HTTP request: <ERROR> %v", i+1, err)
|
|
|
|
}
|
|
|
|
apiRouter.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
|
|
t.Fatalf("Test %d: Expected the response status to be 200, but instead found `%d`", i+1, rec.Code)
|
|
|
|
}
|
|
|
|
if err = getTestWebRPCResponse(rec, &reply); err != nil {
|
|
|
|
t.Fatalf("Test %d: Should succeed but it didn't, %v", i+1, err)
|
|
|
|
}
|
|
|
|
if testCase.expectedResult != reply.Policy {
|
|
|
|
t.Fatalf("Test %d: expected: %v, got: %v", i+1, testCase.expectedResult, reply.Policy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-22 01:38:50 +02:00
|
|
|
|
2016-09-26 06:53:19 +02:00
|
|
|
// Wrapper for calling ListAllBucketPolicies Handler
|
|
|
|
func TestWebHandlerListAllBucketPoliciesHandler(t *testing.T) {
|
|
|
|
ExecObjectLayerTest(t, testWebListAllBucketPoliciesHandler)
|
2016-09-23 08:06:45 +02:00
|
|
|
}
|
|
|
|
|
2016-09-26 06:53:19 +02:00
|
|
|
// testWebListAllBucketPoliciesHandler - Test ListAllBucketPolicies web handler
|
|
|
|
func testWebListAllBucketPoliciesHandler(obj ObjectLayer, instanceType string, t TestErrHandler) {
|
2016-09-23 08:06:45 +02:00
|
|
|
// Register the API end points with XL/FS object layer.
|
|
|
|
apiRouter := initTestWebRPCEndPoint(obj)
|
|
|
|
// initialize the server and obtain the credentials and root.
|
|
|
|
// credentials are necessary to sign the HTTP request.
|
2017-01-18 21:24:34 +01:00
|
|
|
rootPath, err := newTestConfig(globalMinioDefaultRegion)
|
2016-09-23 08:06:45 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Init Test config failed")
|
|
|
|
}
|
2016-11-10 08:37:12 +01:00
|
|
|
// remove the root directory after the test ends.
|
2016-09-23 08:06:45 +02:00
|
|
|
defer removeAll(rootPath)
|
|
|
|
|
|
|
|
credentials := serverConfig.GetCredential()
|
|
|
|
|
2016-12-26 19:21:23 +01:00
|
|
|
authorization, err := getWebRPCToken(apiRouter, credentials.AccessKey, credentials.SecretKey)
|
2016-09-23 08:06:45 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Cannot authenticate")
|
|
|
|
}
|
|
|
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
|
|
|
bucketName := getRandomBucketName()
|
|
|
|
if err := obj.MakeBucket(bucketName); err != nil {
|
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
|
2016-10-13 18:19:04 +02:00
|
|
|
policyVal := bucketPolicy{
|
|
|
|
Version: "2012-10-17",
|
|
|
|
Statements: []policyStatement{
|
|
|
|
{
|
|
|
|
Actions: set.CreateStringSet("s3:GetBucketLocation"),
|
|
|
|
Effect: "Allow",
|
|
|
|
Principal: map[string][]string{"AWS": {"*"}},
|
2017-01-11 01:43:48 +01:00
|
|
|
Resources: set.CreateStringSet(bucketARNPrefix + bucketName),
|
2016-10-13 18:19:04 +02:00
|
|
|
Sid: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Actions: set.CreateStringSet("s3:ListBucket"),
|
|
|
|
Conditions: map[string]map[string]set.StringSet{
|
|
|
|
"StringEquals": {
|
|
|
|
"s3:prefix": set.CreateStringSet("hello"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Effect: "Allow",
|
|
|
|
Principal: map[string][]string{"AWS": {"*"}},
|
2017-01-11 01:43:48 +01:00
|
|
|
Resources: set.CreateStringSet(bucketARNPrefix + bucketName),
|
2016-10-13 18:19:04 +02:00
|
|
|
Sid: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Actions: set.CreateStringSet("s3:ListBucketMultipartUploads"),
|
|
|
|
Effect: "Allow",
|
|
|
|
Principal: map[string][]string{"AWS": {"*"}},
|
2017-01-11 01:43:48 +01:00
|
|
|
Resources: set.CreateStringSet(bucketARNPrefix + bucketName),
|
2016-10-13 18:19:04 +02:00
|
|
|
Sid: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Actions: set.CreateStringSet("s3:AbortMultipartUpload", "s3:DeleteObject",
|
|
|
|
"s3:GetObject", "s3:ListMultipartUploadParts", "s3:PutObject"),
|
|
|
|
Effect: "Allow",
|
|
|
|
Principal: map[string][]string{"AWS": {"*"}},
|
2017-01-11 01:43:48 +01:00
|
|
|
Resources: set.CreateStringSet(bucketARNPrefix + bucketName + "/hello*"),
|
2016-10-13 18:19:04 +02:00
|
|
|
Sid: "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := writeBucketPolicy(bucketName, obj, &policyVal); err != nil {
|
2016-09-23 08:06:45 +02:00
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
|
2016-09-26 06:53:19 +02:00
|
|
|
testCaseResult1 := []bucketAccessPolicy{{
|
|
|
|
Prefix: bucketName + "/hello*",
|
|
|
|
Policy: policy.BucketPolicyReadWrite,
|
|
|
|
}}
|
2016-09-23 08:06:45 +02:00
|
|
|
testCases := []struct {
|
|
|
|
bucketName string
|
2016-09-26 06:53:19 +02:00
|
|
|
expectedResult []bucketAccessPolicy
|
2016-09-23 08:06:45 +02:00
|
|
|
}{
|
|
|
|
{bucketName, testCaseResult1},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, testCase := range testCases {
|
2016-09-26 06:53:19 +02:00
|
|
|
args := &ListAllBucketPoliciesArgs{BucketName: testCase.bucketName}
|
|
|
|
reply := &ListAllBucketPoliciesRep{}
|
|
|
|
req, err := newTestWebRPCRequest("Web.ListAllBucketPolicies", authorization, args)
|
2016-09-23 08:06:45 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Test %d: Failed to create HTTP request: <ERROR> %v", i+1, err)
|
|
|
|
}
|
|
|
|
apiRouter.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
|
|
t.Fatalf("Test %d: Expected the response status to be 200, but instead found `%d`", i+1, rec.Code)
|
|
|
|
}
|
|
|
|
if err = getTestWebRPCResponse(rec, &reply); err != nil {
|
|
|
|
t.Fatalf("Test %d: Should succeed but it didn't, %v", i+1, err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(testCase.expectedResult, reply.Policies) {
|
|
|
|
t.Fatalf("Test %d: expected: %v, got: %v", i+1, testCase.expectedResult, reply.Policies)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-22 01:38:50 +02:00
|
|
|
// Wrapper for calling SetBucketPolicy Handler
|
|
|
|
func TestWebHandlerSetBucketPolicyHandler(t *testing.T) {
|
|
|
|
ExecObjectLayerTest(t, testWebSetBucketPolicyHandler)
|
|
|
|
}
|
|
|
|
|
|
|
|
// testWebSetBucketPolicyHandler - Test SetBucketPolicy web handler
|
|
|
|
func testWebSetBucketPolicyHandler(obj ObjectLayer, instanceType string, t TestErrHandler) {
|
|
|
|
// Register the API end points with XL/FS object layer.
|
|
|
|
apiRouter := initTestWebRPCEndPoint(obj)
|
|
|
|
// initialize the server and obtain the credentials and root.
|
|
|
|
// credentials are necessary to sign the HTTP request.
|
2017-01-18 21:24:34 +01:00
|
|
|
rootPath, err := newTestConfig(globalMinioDefaultRegion)
|
2016-09-22 01:38:50 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Init Test config failed")
|
|
|
|
}
|
2016-11-10 08:37:12 +01:00
|
|
|
// remove the root directory after the test ends.
|
2016-09-22 01:38:50 +02:00
|
|
|
defer removeAll(rootPath)
|
|
|
|
|
|
|
|
credentials := serverConfig.GetCredential()
|
|
|
|
|
2016-12-26 19:21:23 +01:00
|
|
|
authorization, err := getWebRPCToken(apiRouter, credentials.AccessKey, credentials.SecretKey)
|
2016-09-22 01:38:50 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Cannot authenticate")
|
|
|
|
}
|
|
|
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Create a bucket
|
|
|
|
bucketName := getRandomBucketName()
|
|
|
|
if err = obj.MakeBucket(bucketName); err != nil {
|
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
bucketName string
|
|
|
|
prefix string
|
|
|
|
policy string
|
|
|
|
pass bool
|
|
|
|
}{
|
|
|
|
// Invalid bucket name
|
|
|
|
{"", "", "readonly", false},
|
|
|
|
// Invalid policy
|
|
|
|
{bucketName, "", "foo", false},
|
|
|
|
// Valid parameters
|
|
|
|
{bucketName, "", "readwrite", true},
|
2016-09-26 12:11:22 +02:00
|
|
|
// None is valid and policy should be removed.
|
|
|
|
{bucketName, "", "none", true},
|
|
|
|
// Setting none again meants should return an error.
|
|
|
|
{bucketName, "", "none", false},
|
2016-09-22 01:38:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, testCase := range testCases {
|
|
|
|
args := &SetBucketPolicyArgs{BucketName: testCase.bucketName, Prefix: testCase.prefix, Policy: testCase.policy}
|
|
|
|
reply := &WebGenericRep{}
|
|
|
|
// Call SetBucketPolicy RPC
|
|
|
|
req, err := newTestWebRPCRequest("Web.SetBucketPolicy", authorization, args)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Test %d: Failed to create HTTP request: <ERROR> %v", i+1, err)
|
|
|
|
}
|
|
|
|
apiRouter.ServeHTTP(rec, req)
|
|
|
|
// Check if we have 200 OK
|
2016-09-26 12:11:22 +02:00
|
|
|
if testCase.pass && rec.Code != http.StatusOK {
|
2016-09-22 01:38:50 +02:00
|
|
|
t.Fatalf("Test %d: Expected the response status to be 200, but instead found `%d`", i+1, rec.Code)
|
|
|
|
}
|
|
|
|
// Parse RPC response
|
|
|
|
err = getTestWebRPCResponse(rec, &reply)
|
|
|
|
if testCase.pass && err != nil {
|
|
|
|
t.Fatalf("Test %d: Should succeed but it didn't, %v", i+1, err)
|
|
|
|
}
|
|
|
|
if !testCase.pass && err == nil {
|
|
|
|
t.Fatalf("Test %d: Should fail it didn't", i+1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-23 01:35:12 +02:00
|
|
|
|
|
|
|
// TestWebCheckAuthorization - Test Authorization for all web handlers
|
|
|
|
func TestWebCheckAuthorization(t *testing.T) {
|
|
|
|
// Prepare XL backend
|
2016-10-05 21:48:07 +02:00
|
|
|
obj, fsDirs, err := prepareXL()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Initialization of object layer failed for XL setup: %s", err)
|
2016-09-23 01:35:12 +02:00
|
|
|
}
|
|
|
|
// Executing the object layer tests for XL.
|
|
|
|
defer removeRoots(fsDirs)
|
|
|
|
|
|
|
|
// Register the API end points with XL/FS object layer.
|
|
|
|
apiRouter := initTestWebRPCEndPoint(obj)
|
|
|
|
// initialize the server and obtain the credentials and root.
|
|
|
|
// credentials are necessary to sign the HTTP request.
|
2017-01-18 21:24:34 +01:00
|
|
|
rootPath, err := newTestConfig(globalMinioDefaultRegion)
|
2016-10-05 21:48:07 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Init Test config failed", err)
|
2016-09-23 01:35:12 +02:00
|
|
|
}
|
2016-11-10 08:37:12 +01:00
|
|
|
// remove the root directory after the test ends.
|
2016-09-23 01:35:12 +02:00
|
|
|
defer removeAll(rootPath)
|
|
|
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Check if web rpc calls return unauthorized request with an incorrect token
|
2016-11-02 22:45:11 +01:00
|
|
|
webRPCs := []string{
|
|
|
|
"ServerInfo", "StorageInfo", "MakeBucket",
|
|
|
|
"ListBuckets", "ListObjects", "RemoveObject",
|
|
|
|
"GenerateAuth", "SetAuth", "GetAuth",
|
|
|
|
"GetBucketPolicy", "SetBucketPolicy", "ListAllBucketPolicies",
|
|
|
|
"PresignedGet",
|
|
|
|
}
|
2016-09-23 01:35:12 +02:00
|
|
|
for _, rpcCall := range webRPCs {
|
2016-12-23 16:12:19 +01:00
|
|
|
args := &AuthRPCArgs{}
|
2016-09-23 01:35:12 +02:00
|
|
|
reply := &WebGenericRep{}
|
2016-10-05 21:48:07 +02:00
|
|
|
req, nerr := newTestWebRPCRequest("Web."+rpcCall, "Bearer fooauthorization", args)
|
|
|
|
if nerr != nil {
|
|
|
|
t.Fatalf("Test %s: Failed to create HTTP request: <ERROR> %v", rpcCall, nerr)
|
2016-09-23 01:35:12 +02:00
|
|
|
}
|
|
|
|
apiRouter.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
|
|
t.Fatalf("Test %s: Expected the response status to be 200, but instead found `%d`", rpcCall, rec.Code)
|
|
|
|
}
|
|
|
|
err = getTestWebRPCResponse(rec, &reply)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Test %s: Should fail", rpcCall)
|
|
|
|
} else {
|
2016-11-02 22:45:11 +01:00
|
|
|
if !strings.Contains(err.Error(), errAuthentication.Error()) {
|
2016-09-23 01:35:12 +02:00
|
|
|
t.Fatalf("Test %s: should fail with Unauthorized request. Found error: %v", rpcCall, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-02 22:45:11 +01:00
|
|
|
rec = httptest.NewRecorder()
|
2016-09-23 01:35:12 +02:00
|
|
|
// Test authorization of Web.Download
|
|
|
|
req, err := http.NewRequest("GET", "/minio/download/bucket/object?token=wrongauth", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot create upload request, %v", err)
|
|
|
|
}
|
|
|
|
apiRouter.ServeHTTP(rec, req)
|
2016-11-02 22:45:11 +01:00
|
|
|
if rec.Code != http.StatusForbidden {
|
|
|
|
t.Fatalf("Expected the response status to be 403, but instead found `%d`", rec.Code)
|
2016-09-23 01:35:12 +02:00
|
|
|
}
|
|
|
|
resp := string(rec.Body.Bytes())
|
2016-11-02 22:45:11 +01:00
|
|
|
if !strings.EqualFold(resp, errAuthentication.Error()) {
|
|
|
|
t.Fatalf("Unexpected error message, expected: %s, found: `%s`", errAuthentication, resp)
|
2016-09-23 01:35:12 +02:00
|
|
|
}
|
|
|
|
|
2016-11-02 22:45:11 +01:00
|
|
|
rec = httptest.NewRecorder()
|
2016-09-23 01:35:12 +02:00
|
|
|
// Test authorization of Web.Upload
|
|
|
|
content := []byte("temporary file's content")
|
|
|
|
req, err = http.NewRequest("PUT", "/minio/upload/bucket/object", nil)
|
|
|
|
req.Header.Set("Authorization", "Bearer foo-authorization")
|
|
|
|
req.Header.Set("Content-Length", strconv.Itoa(len(content)))
|
|
|
|
req.Header.Set("x-amz-date", "20160814T114029Z")
|
|
|
|
req.Header.Set("Accept", "*/*")
|
|
|
|
req.Body = ioutil.NopCloser(bytes.NewReader(content))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot create upload request, %v", err)
|
|
|
|
}
|
|
|
|
apiRouter.ServeHTTP(rec, req)
|
2016-11-02 22:45:11 +01:00
|
|
|
if rec.Code != http.StatusForbidden {
|
|
|
|
t.Fatalf("Expected the response status to be 403, but instead found `%d`", rec.Code)
|
2016-09-23 01:35:12 +02:00
|
|
|
}
|
|
|
|
resp = string(rec.Body.Bytes())
|
2016-11-02 22:45:11 +01:00
|
|
|
if !strings.EqualFold(resp, errAuthentication.Error()) {
|
|
|
|
t.Fatalf("Unexpected error message, expected: `%s`, found: `%s`", errAuthentication, resp)
|
2016-09-23 01:35:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestWebObjectLayerNotReady - Test RPCs responses when disks are not ready
|
|
|
|
func TestWebObjectLayerNotReady(t *testing.T) {
|
2016-10-05 21:48:07 +02:00
|
|
|
// Initialize web rpc endpoint.
|
|
|
|
apiRouter := initTestWebRPCEndPoint(nil)
|
2016-09-23 01:35:12 +02:00
|
|
|
|
|
|
|
// initialize the server and obtain the credentials and root.
|
|
|
|
// credentials are necessary to sign the HTTP request.
|
2017-01-18 21:24:34 +01:00
|
|
|
rootPath, err := newTestConfig(globalMinioDefaultRegion)
|
2016-10-05 21:48:07 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Init Test config failed", err)
|
2016-09-23 01:35:12 +02:00
|
|
|
}
|
2016-11-10 08:37:12 +01:00
|
|
|
// remove the root directory after the test ends.
|
2016-09-23 01:35:12 +02:00
|
|
|
defer removeAll(rootPath)
|
|
|
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
|
|
|
credentials := serverConfig.GetCredential()
|
2016-12-26 19:21:23 +01:00
|
|
|
authorization, err := getWebRPCToken(apiRouter, credentials.AccessKey, credentials.SecretKey)
|
2016-10-05 21:48:07 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Cannot authenticate", err)
|
2016-09-23 01:35:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if web rpc calls return Server not initialized. ServerInfo, GenerateAuth,
|
|
|
|
// SetAuth and GetAuth are not concerned
|
|
|
|
webRPCs := []string{"StorageInfo", "MakeBucket", "ListBuckets", "ListObjects", "RemoveObject",
|
2016-11-02 22:45:11 +01:00
|
|
|
"GetBucketPolicy", "SetBucketPolicy", "ListAllBucketPolicies"}
|
2016-09-23 01:35:12 +02:00
|
|
|
for _, rpcCall := range webRPCs {
|
2016-12-23 16:12:19 +01:00
|
|
|
args := &AuthRPCArgs{}
|
2016-09-23 01:35:12 +02:00
|
|
|
reply := &WebGenericRep{}
|
2016-10-05 21:48:07 +02:00
|
|
|
req, nerr := newTestWebRPCRequest("Web."+rpcCall, authorization, args)
|
|
|
|
if nerr != nil {
|
|
|
|
t.Fatalf("Test %s: Failed to create HTTP request: <ERROR> %v", rpcCall, nerr)
|
2016-09-23 01:35:12 +02:00
|
|
|
}
|
|
|
|
apiRouter.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
|
|
t.Fatalf("Test %s: Expected the response status to be 200, but instead found `%d`", rpcCall, rec.Code)
|
|
|
|
}
|
|
|
|
err = getTestWebRPCResponse(rec, &reply)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Test %s: Should fail", rpcCall)
|
|
|
|
} else {
|
2016-11-02 22:45:11 +01:00
|
|
|
if !strings.EqualFold(err.Error(), errServerNotInitialized.Error()) {
|
|
|
|
t.Fatalf("Test %s: should fail with %s Found error: %v", rpcCall, errServerNotInitialized, err)
|
2016-09-23 01:35:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-02 22:45:11 +01:00
|
|
|
rec = httptest.NewRecorder()
|
2016-09-23 01:35:12 +02:00
|
|
|
// Test authorization of Web.Download
|
|
|
|
req, err := http.NewRequest("GET", "/minio/download/bucket/object?token="+authorization, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot create upload request, %v", err)
|
|
|
|
}
|
|
|
|
apiRouter.ServeHTTP(rec, req)
|
2016-11-02 22:45:11 +01:00
|
|
|
if rec.Code != http.StatusServiceUnavailable {
|
|
|
|
t.Fatalf("Expected the response status to be 503, but instead found `%d`", rec.Code)
|
2016-09-23 01:35:12 +02:00
|
|
|
}
|
|
|
|
resp := string(rec.Body.Bytes())
|
2016-11-02 22:45:11 +01:00
|
|
|
if !strings.EqualFold(resp, errServerNotInitialized.Error()) {
|
|
|
|
t.Fatalf("Unexpected error message, expected: `%s`, found: `%s`", errServerNotInitialized, resp)
|
2016-09-23 01:35:12 +02:00
|
|
|
}
|
|
|
|
|
2016-11-02 22:45:11 +01:00
|
|
|
rec = httptest.NewRecorder()
|
2016-09-23 01:35:12 +02:00
|
|
|
// Test authorization of Web.Upload
|
|
|
|
content := []byte("temporary file's content")
|
|
|
|
req, err = http.NewRequest("PUT", "/minio/upload/bucket/object", nil)
|
|
|
|
req.Header.Set("Authorization", "Bearer "+authorization)
|
|
|
|
req.Header.Set("Content-Length", strconv.Itoa(len(content)))
|
|
|
|
req.Header.Set("x-amz-date", "20160814T114029Z")
|
|
|
|
req.Header.Set("Accept", "*/*")
|
|
|
|
req.Body = ioutil.NopCloser(bytes.NewReader(content))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot create upload request, %v", err)
|
|
|
|
}
|
|
|
|
apiRouter.ServeHTTP(rec, req)
|
2016-11-02 22:45:11 +01:00
|
|
|
if rec.Code != http.StatusServiceUnavailable {
|
|
|
|
t.Fatalf("Expected the response status to be 503, but instead found `%d`", rec.Code)
|
2016-09-23 01:35:12 +02:00
|
|
|
}
|
|
|
|
resp = string(rec.Body.Bytes())
|
2016-11-02 22:45:11 +01:00
|
|
|
if !strings.EqualFold(resp, errServerNotInitialized.Error()) {
|
|
|
|
t.Fatalf("Unexpected error message, expected: `%s`, found: `%s`", errServerNotInitialized, resp)
|
2016-09-23 01:35:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestWebObjectLayerFaultyDisks - Test Web RPC responses with faulty disks
|
|
|
|
func TestWebObjectLayerFaultyDisks(t *testing.T) {
|
2017-01-18 21:24:34 +01:00
|
|
|
root, err := newTestConfig(globalMinioDefaultRegion)
|
2016-11-22 20:12:38 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer removeAll(root)
|
|
|
|
|
2016-09-23 01:35:12 +02:00
|
|
|
// Prepare XL backend
|
2016-10-05 21:48:07 +02:00
|
|
|
obj, fsDirs, err := prepareXL()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Initialization of object layer failed for XL setup: %s", err)
|
2016-09-23 01:35:12 +02:00
|
|
|
}
|
|
|
|
// Executing the object layer tests for XL.
|
|
|
|
defer removeRoots(fsDirs)
|
|
|
|
|
|
|
|
// Set faulty disks to XL backend
|
2016-11-17 01:42:23 +01:00
|
|
|
xl := obj.(*xlObjects)
|
2016-09-23 01:35:12 +02:00
|
|
|
for i, d := range xl.storageDisks {
|
2016-11-24 00:48:10 +01:00
|
|
|
xl.storageDisks[i] = newNaughtyDisk(d.(*retryStorage), nil, errFaultyDisk)
|
2016-09-23 01:35:12 +02:00
|
|
|
}
|
|
|
|
|
2016-10-05 21:48:07 +02:00
|
|
|
// Initialize web rpc endpoint.
|
|
|
|
apiRouter := initTestWebRPCEndPoint(obj)
|
2016-09-23 01:35:12 +02:00
|
|
|
|
|
|
|
// initialize the server and obtain the credentials and root.
|
|
|
|
// credentials are necessary to sign the HTTP request.
|
2017-01-18 21:24:34 +01:00
|
|
|
rootPath, err := newTestConfig(globalMinioDefaultRegion)
|
2016-10-05 21:48:07 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Init Test config failed", err)
|
2016-09-23 01:35:12 +02:00
|
|
|
}
|
2016-11-10 08:37:12 +01:00
|
|
|
// remove the root directory after the test ends.
|
2016-09-23 01:35:12 +02:00
|
|
|
defer removeAll(rootPath)
|
|
|
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
|
|
|
credentials := serverConfig.GetCredential()
|
2016-12-26 19:21:23 +01:00
|
|
|
authorization, err := getWebRPCToken(apiRouter, credentials.AccessKey, credentials.SecretKey)
|
2016-10-05 21:48:07 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Cannot authenticate", err)
|
2016-09-23 01:35:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if web rpc calls return errors with faulty disks. ServerInfo, GenerateAuth, SetAuth, GetAuth are not concerned
|
|
|
|
webRPCs := []string{"MakeBucket", "ListBuckets", "ListObjects", "RemoveObject",
|
|
|
|
"GetBucketPolicy", "SetBucketPolicy"}
|
|
|
|
|
|
|
|
for _, rpcCall := range webRPCs {
|
2016-12-23 16:12:19 +01:00
|
|
|
args := &AuthRPCArgs{}
|
2016-09-23 01:35:12 +02:00
|
|
|
reply := &WebGenericRep{}
|
2016-10-05 21:48:07 +02:00
|
|
|
req, nerr := newTestWebRPCRequest("Web."+rpcCall, authorization, args)
|
|
|
|
if nerr != nil {
|
|
|
|
t.Fatalf("Test %s: Failed to create HTTP request: <ERROR> %v", rpcCall, nerr)
|
2016-09-23 01:35:12 +02:00
|
|
|
}
|
|
|
|
apiRouter.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
|
|
t.Fatalf("Test %s: Expected the response status to be 200, but instead found `%d`", rpcCall, rec.Code)
|
|
|
|
}
|
|
|
|
err = getTestWebRPCResponse(rec, &reply)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Test %s: Should fail", rpcCall)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test Web.StorageInfo
|
2016-12-23 16:12:19 +01:00
|
|
|
storageInfoRequest := AuthRPCArgs{}
|
2016-09-23 01:35:12 +02:00
|
|
|
storageInfoReply := &StorageInfoRep{}
|
|
|
|
req, err := newTestWebRPCRequest("Web.StorageInfo", authorization, storageInfoRequest)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create HTTP request: <ERROR> %v", err)
|
|
|
|
}
|
|
|
|
apiRouter.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
|
|
t.Fatalf("Expected the response status to be 200, but instead found `%d`", rec.Code)
|
|
|
|
}
|
|
|
|
err = getTestWebRPCResponse(rec, &storageInfoReply)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed %v", err)
|
|
|
|
}
|
|
|
|
if storageInfoReply.StorageInfo.Total != -1 || storageInfoReply.StorageInfo.Free != -1 {
|
|
|
|
t.Fatalf("Should get negative values of Total and Free since disks are faulty ")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test authorization of Web.Download
|
|
|
|
req, err = http.NewRequest("GET", "/minio/download/bucket/object?token="+authorization, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot create upload request, %v", err)
|
|
|
|
}
|
|
|
|
apiRouter.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
|
|
t.Fatalf("Expected the response status to be 200, but instead found `%d`", rec.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test authorization of Web.Upload
|
|
|
|
content := []byte("temporary file's content")
|
|
|
|
req, err = http.NewRequest("PUT", "/minio/upload/bucket/object", nil)
|
|
|
|
req.Header.Set("Authorization", "Bearer "+authorization)
|
|
|
|
req.Header.Set("Content-Length", strconv.Itoa(len(content)))
|
|
|
|
req.Header.Set("x-amz-date", "20160814T114029Z")
|
|
|
|
req.Header.Set("Accept", "*/*")
|
|
|
|
req.Body = ioutil.NopCloser(bytes.NewReader(content))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot create upload request, %v", err)
|
|
|
|
}
|
|
|
|
apiRouter.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
|
|
t.Fatalf("Expected the response status to be 200, but instead found `%d`", rec.Code)
|
|
|
|
}
|
|
|
|
}
|