2016-11-15 15:12:06 -05:00
|
|
|
/*
|
2017-01-18 15:24:34 -05:00
|
|
|
* Minio Cloud Storage, (C) 2016, 2017 Minio, Inc.
|
2016-11-15 15:12:06 -05:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2017-08-12 22:25:43 -04:00
|
|
|
"os"
|
2016-11-15 15:12:06 -05:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/minio/minio/pkg/disk"
|
2017-11-25 14:58:29 -05:00
|
|
|
"github.com/minio/minio/pkg/errors"
|
2016-11-15 15:12:06 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type testStorageRPCServer struct {
|
|
|
|
configDir string
|
|
|
|
token string
|
|
|
|
diskDirs []string
|
|
|
|
stServer *storageServer
|
2017-04-11 18:44:27 -04:00
|
|
|
endpoints EndpointList
|
2016-11-15 15:12:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func createTestStorageServer(t *testing.T) *testStorageRPCServer {
|
2017-01-18 15:24:34 -05:00
|
|
|
testPath, err := newTestConfig(globalMinioDefaultRegion)
|
2016-11-15 15:12:06 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable initialize config file, %s", err)
|
|
|
|
}
|
|
|
|
|
2017-11-29 16:12:47 -05:00
|
|
|
serverCred := globalServerConfig.GetCredential()
|
2016-12-27 11:28:10 -05:00
|
|
|
token, err := authenticateNode(serverCred.AccessKey, serverCred.SecretKey)
|
2016-11-15 15:12:06 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable for JWT to generate token, %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fsDirs, err := getRandomDisks(1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create FS backend, %s", err)
|
|
|
|
}
|
|
|
|
|
2017-04-11 18:44:27 -04:00
|
|
|
endpoints := mustGetNewEndpointList(fsDirs...)
|
2016-11-15 15:12:06 -05:00
|
|
|
storageDisks, err := initStorageDisks(endpoints)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to initialize storage disks, %s", err)
|
|
|
|
}
|
|
|
|
stServer := &storageServer{
|
|
|
|
storage: storageDisks[0],
|
|
|
|
path: "/disk1",
|
2017-03-18 14:28:41 -04:00
|
|
|
timestamp: UTCNow(),
|
2016-11-15 15:12:06 -05:00
|
|
|
}
|
|
|
|
return &testStorageRPCServer{
|
|
|
|
token: token,
|
|
|
|
configDir: testPath,
|
|
|
|
diskDirs: fsDirs,
|
|
|
|
endpoints: endpoints,
|
|
|
|
stServer: stServer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func errorIfInvalidToken(t *testing.T, err error) {
|
2017-11-25 14:58:29 -05:00
|
|
|
realErr := errors.Cause(err)
|
2016-11-15 15:12:06 -05:00
|
|
|
if realErr != errInvalidToken {
|
|
|
|
t.Errorf("Expected to fail with %s but failed with %s", errInvalidToken, realErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStorageRPCInvalidToken(t *testing.T) {
|
|
|
|
st := createTestStorageServer(t)
|
|
|
|
defer removeRoots(st.diskDirs)
|
2017-08-12 22:25:43 -04:00
|
|
|
defer os.RemoveAll(st.configDir)
|
2016-11-15 15:12:06 -05:00
|
|
|
|
|
|
|
storageRPC := st.stServer
|
|
|
|
|
|
|
|
// Following test cases are meant to exercise the invalid
|
|
|
|
// token code path of the storage RPC methods.
|
|
|
|
var err error
|
2016-12-23 10:12:19 -05:00
|
|
|
badAuthRPCArgs := AuthRPCArgs{AuthToken: "invalidToken"}
|
|
|
|
badGenericVolArgs := GenericVolArgs{
|
|
|
|
AuthRPCArgs: badAuthRPCArgs,
|
2016-11-15 15:12:06 -05:00
|
|
|
Vol: "myvol",
|
|
|
|
}
|
|
|
|
// 1. DiskInfoHandler
|
|
|
|
diskInfoReply := &disk.Info{}
|
2016-12-23 10:12:19 -05:00
|
|
|
err = storageRPC.DiskInfoHandler(&badAuthRPCArgs, diskInfoReply)
|
2016-11-15 15:12:06 -05:00
|
|
|
errorIfInvalidToken(t, err)
|
|
|
|
|
|
|
|
// 2. MakeVolHandler
|
2016-12-23 10:12:19 -05:00
|
|
|
makeVolArgs := &badGenericVolArgs
|
|
|
|
makeVolReply := &AuthRPCReply{}
|
2016-11-15 15:12:06 -05:00
|
|
|
err = storageRPC.MakeVolHandler(makeVolArgs, makeVolReply)
|
|
|
|
errorIfInvalidToken(t, err)
|
|
|
|
|
|
|
|
// 3. ListVolsHandler
|
|
|
|
listVolReply := &ListVolsReply{}
|
2016-12-23 10:12:19 -05:00
|
|
|
err = storageRPC.ListVolsHandler(&badAuthRPCArgs, listVolReply)
|
2016-11-15 15:12:06 -05:00
|
|
|
errorIfInvalidToken(t, err)
|
|
|
|
|
|
|
|
// 4. StatVolHandler
|
|
|
|
statVolReply := &VolInfo{}
|
2016-12-23 10:12:19 -05:00
|
|
|
statVolArgs := &badGenericVolArgs
|
2016-11-15 15:12:06 -05:00
|
|
|
err = storageRPC.StatVolHandler(statVolArgs, statVolReply)
|
|
|
|
errorIfInvalidToken(t, err)
|
|
|
|
|
|
|
|
// 5. DeleteVolHandler
|
2016-12-23 10:12:19 -05:00
|
|
|
deleteVolArgs := &badGenericVolArgs
|
|
|
|
deleteVolReply := &AuthRPCReply{}
|
2016-11-15 15:12:06 -05:00
|
|
|
err = storageRPC.DeleteVolHandler(deleteVolArgs, deleteVolReply)
|
|
|
|
errorIfInvalidToken(t, err)
|
|
|
|
|
|
|
|
// 6. StatFileHandler
|
|
|
|
statFileArgs := &StatFileArgs{
|
2016-12-23 10:12:19 -05:00
|
|
|
AuthRPCArgs: badAuthRPCArgs,
|
2016-11-15 15:12:06 -05:00
|
|
|
}
|
|
|
|
statReply := &FileInfo{}
|
|
|
|
err = storageRPC.StatFileHandler(statFileArgs, statReply)
|
|
|
|
errorIfInvalidToken(t, err)
|
|
|
|
|
|
|
|
// 7. ListDirHandler
|
|
|
|
listDirArgs := &ListDirArgs{
|
2016-12-23 10:12:19 -05:00
|
|
|
AuthRPCArgs: badAuthRPCArgs,
|
2016-11-15 15:12:06 -05:00
|
|
|
}
|
|
|
|
listDirReply := &[]string{}
|
|
|
|
err = storageRPC.ListDirHandler(listDirArgs, listDirReply)
|
|
|
|
errorIfInvalidToken(t, err)
|
|
|
|
|
|
|
|
// 8. ReadAllHandler
|
|
|
|
readFileArgs := &ReadFileArgs{
|
2016-12-23 10:12:19 -05:00
|
|
|
AuthRPCArgs: badAuthRPCArgs,
|
2016-11-15 15:12:06 -05:00
|
|
|
}
|
|
|
|
readFileReply := &[]byte{}
|
|
|
|
err = storageRPC.ReadAllHandler(readFileArgs, readFileReply)
|
|
|
|
errorIfInvalidToken(t, err)
|
|
|
|
|
|
|
|
// 9. ReadFileHandler
|
|
|
|
err = storageRPC.ReadFileHandler(readFileArgs, readFileReply)
|
|
|
|
errorIfInvalidToken(t, err)
|
|
|
|
|
|
|
|
// 10. PrepareFileHandler
|
|
|
|
prepFileArgs := &PrepareFileArgs{
|
2016-12-23 10:12:19 -05:00
|
|
|
AuthRPCArgs: badAuthRPCArgs,
|
2016-11-15 15:12:06 -05:00
|
|
|
}
|
2016-12-23 10:12:19 -05:00
|
|
|
prepFileReply := &AuthRPCReply{}
|
2016-11-15 15:12:06 -05:00
|
|
|
err = storageRPC.PrepareFileHandler(prepFileArgs, prepFileReply)
|
|
|
|
errorIfInvalidToken(t, err)
|
|
|
|
|
|
|
|
// 11. AppendFileHandler
|
|
|
|
appendArgs := &AppendFileArgs{
|
2016-12-23 10:12:19 -05:00
|
|
|
AuthRPCArgs: badAuthRPCArgs,
|
2016-11-15 15:12:06 -05:00
|
|
|
}
|
2016-12-23 10:12:19 -05:00
|
|
|
appendReply := &AuthRPCReply{}
|
2016-11-15 15:12:06 -05:00
|
|
|
err = storageRPC.AppendFileHandler(appendArgs, appendReply)
|
|
|
|
errorIfInvalidToken(t, err)
|
|
|
|
|
|
|
|
// 12. DeleteFileHandler
|
|
|
|
delFileArgs := &DeleteFileArgs{
|
2016-12-23 10:12:19 -05:00
|
|
|
AuthRPCArgs: badAuthRPCArgs,
|
2016-11-15 15:12:06 -05:00
|
|
|
}
|
2016-12-23 10:12:19 -05:00
|
|
|
delFileRely := &AuthRPCReply{}
|
2016-11-15 15:12:06 -05:00
|
|
|
err = storageRPC.DeleteFileHandler(delFileArgs, delFileRely)
|
|
|
|
errorIfInvalidToken(t, err)
|
|
|
|
|
|
|
|
// 13. RenameFileHandler
|
|
|
|
renameArgs := &RenameFileArgs{
|
2016-12-23 10:12:19 -05:00
|
|
|
AuthRPCArgs: badAuthRPCArgs,
|
2016-11-15 15:12:06 -05:00
|
|
|
}
|
2016-12-23 10:12:19 -05:00
|
|
|
renameReply := &AuthRPCReply{}
|
2016-11-15 15:12:06 -05:00
|
|
|
err = storageRPC.RenameFileHandler(renameArgs, renameReply)
|
|
|
|
errorIfInvalidToken(t, err)
|
|
|
|
}
|