| 
									
										
										
										
											2016-08-01 05:11:14 +08:00
										 |  |  | /* | 
					
						
							|  |  |  |  * Minio Cloud Storage, (C) 2016 Minio, Inc. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 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 05:50:50 +08:00
										 |  |  | package cmd | 
					
						
							| 
									
										
										
										
											2016-04-09 01:37:38 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2016-08-12 10:32:04 +08:00
										 |  |  | 	"bytes" | 
					
						
							| 
									
										
										
										
											2016-08-11 12:09:31 +08:00
										 |  |  | 	"errors" | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2016-08-12 10:32:04 +08:00
										 |  |  | 	"io" | 
					
						
							| 
									
										
										
										
											2016-04-09 01:37:38 +08:00
										 |  |  | 	"net/rpc" | 
					
						
							| 
									
										
										
										
											2016-08-01 05:11:14 +08:00
										 |  |  | 	"path" | 
					
						
							|  |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2016-04-09 01:37:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-11 12:09:31 +08:00
										 |  |  | 	jwtgo "github.com/dgrijalva/jwt-go" | 
					
						
							| 
									
										
										
										
											2016-04-09 01:37:38 +08:00
										 |  |  | 	router "github.com/gorilla/mux" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Storage server implements rpc primitives to facilitate exporting a
 | 
					
						
							|  |  |  | // disk over a network.
 | 
					
						
							|  |  |  | type storageServer struct { | 
					
						
							|  |  |  | 	storage StorageAPI | 
					
						
							| 
									
										
										
										
											2016-08-01 05:11:14 +08:00
										 |  |  | 	path    string | 
					
						
							| 
									
										
										
										
											2016-04-09 01:37:38 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-11 12:09:31 +08:00
										 |  |  | // Validates if incoming token is valid.
 | 
					
						
							|  |  |  | func isRPCTokenValid(tokenStr string) bool { | 
					
						
							|  |  |  | 	jwt, err := newJWT(defaultWebTokenExpiry) // Expiry set to 24Hrs.
 | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		errorIf(err, "Unable to initialize JWT") | 
					
						
							|  |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	token, err := jwtgo.Parse(tokenStr, func(token *jwtgo.Token) (interface{}, error) { | 
					
						
							|  |  |  | 		if _, ok := token.Method.(*jwtgo.SigningMethodHMAC); !ok { | 
					
						
							|  |  |  | 			return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"]) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return []byte(jwt.SecretAccessKey), nil | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		errorIf(err, "Unable to parse JWT token string") | 
					
						
							|  |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// Return if token is valid.
 | 
					
						
							|  |  |  | 	return token.Valid | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /// Auth operations
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Login - login handler.
 | 
					
						
							|  |  |  | func (s *storageServer) LoginHandler(args *RPCLoginArgs, reply *RPCLoginReply) error { | 
					
						
							|  |  |  | 	jwt, err := newJWT(defaultTokenExpiry) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if err = jwt.Authenticate(args.Username, args.Password); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	token, err := jwt.GenerateToken(args.Username) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	reply.Token = token | 
					
						
							| 
									
										
										
										
											2016-08-19 05:50:50 +08:00
										 |  |  | 	reply.ServerVersion = Version | 
					
						
							| 
									
										
										
										
											2016-08-11 12:09:31 +08:00
										 |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-09 01:37:38 +08:00
										 |  |  | /// Volume operations handlers
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // MakeVolHandler - make vol handler is rpc wrapper for MakeVol operation.
 | 
					
						
							| 
									
										
										
										
											2016-08-11 12:09:31 +08:00
										 |  |  | func (s *storageServer) MakeVolHandler(args *GenericVolArgs, reply *GenericReply) error { | 
					
						
							|  |  |  | 	if !isRPCTokenValid(args.Token) { | 
					
						
							|  |  |  | 		return errors.New("Invalid token") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return s.storage.MakeVol(args.Vol) | 
					
						
							| 
									
										
										
										
											2016-04-09 01:37:38 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ListVolsHandler - list vols handler is rpc wrapper for ListVols operation.
 | 
					
						
							| 
									
										
										
										
											2016-08-23 02:01:21 +08:00
										 |  |  | func (s *storageServer) ListVolsHandler(args *GenericArgs, reply *ListVolsReply) error { | 
					
						
							|  |  |  | 	if !isRPCTokenValid(args.Token) { | 
					
						
							| 
									
										
										
										
											2016-08-11 12:09:31 +08:00
										 |  |  | 		return errors.New("Invalid token") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-04-09 01:37:38 +08:00
										 |  |  | 	vols, err := s.storage.ListVols() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	reply.Vols = vols | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // StatVolHandler - stat vol handler is a rpc wrapper for StatVol operation.
 | 
					
						
							| 
									
										
										
										
											2016-08-11 12:09:31 +08:00
										 |  |  | func (s *storageServer) StatVolHandler(args *GenericVolArgs, reply *VolInfo) error { | 
					
						
							|  |  |  | 	if !isRPCTokenValid(args.Token) { | 
					
						
							|  |  |  | 		return errors.New("Invalid token") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	volInfo, err := s.storage.StatVol(args.Vol) | 
					
						
							| 
									
										
										
										
											2016-04-09 01:37:38 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	*reply = volInfo | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // DeleteVolHandler - delete vol handler is a rpc wrapper for
 | 
					
						
							|  |  |  | // DeleteVol operation.
 | 
					
						
							| 
									
										
										
										
											2016-08-11 12:09:31 +08:00
										 |  |  | func (s *storageServer) DeleteVolHandler(args *GenericVolArgs, reply *GenericReply) error { | 
					
						
							|  |  |  | 	if !isRPCTokenValid(args.Token) { | 
					
						
							|  |  |  | 		return errors.New("Invalid token") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return s.storage.DeleteVol(args.Vol) | 
					
						
							| 
									
										
										
										
											2016-04-09 01:37:38 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /// File operations
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-06 03:51:56 +08:00
										 |  |  | // StatFileHandler - stat file handler is rpc wrapper to stat file.
 | 
					
						
							| 
									
										
										
										
											2016-08-11 12:09:31 +08:00
										 |  |  | func (s *storageServer) StatFileHandler(args *StatFileArgs, reply *FileInfo) error { | 
					
						
							|  |  |  | 	if !isRPCTokenValid(args.Token) { | 
					
						
							|  |  |  | 		return errors.New("Invalid token") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	fileInfo, err := s.storage.StatFile(args.Vol, args.Path) | 
					
						
							| 
									
										
										
										
											2016-04-09 01:37:38 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-05-06 03:51:56 +08:00
										 |  |  | 	*reply = fileInfo | 
					
						
							| 
									
										
										
										
											2016-04-09 01:37:38 +08:00
										 |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-06 03:51:56 +08:00
										 |  |  | // ListDirHandler - list directory handler is rpc wrapper to list dir.
 | 
					
						
							| 
									
										
										
										
											2016-08-11 12:09:31 +08:00
										 |  |  | func (s *storageServer) ListDirHandler(args *ListDirArgs, reply *[]string) error { | 
					
						
							|  |  |  | 	if !isRPCTokenValid(args.Token) { | 
					
						
							|  |  |  | 		return errors.New("Invalid token") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	entries, err := s.storage.ListDir(args.Vol, args.Path) | 
					
						
							| 
									
										
										
										
											2016-04-09 01:37:38 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-05-06 03:51:56 +08:00
										 |  |  | 	*reply = entries | 
					
						
							| 
									
										
										
										
											2016-04-09 01:37:38 +08:00
										 |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-26 05:51:06 +08:00
										 |  |  | // ReadAllHandler - read all handler is rpc wrapper to read all storage API.
 | 
					
						
							| 
									
										
										
										
											2016-08-11 12:09:31 +08:00
										 |  |  | func (s *storageServer) ReadAllHandler(args *ReadFileArgs, reply *[]byte) error { | 
					
						
							|  |  |  | 	if !isRPCTokenValid(args.Token) { | 
					
						
							|  |  |  | 		return errors.New("Invalid token") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	buf, err := s.storage.ReadAll(args.Vol, args.Path) | 
					
						
							| 
									
										
										
										
											2016-06-26 05:51:06 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-08-10 14:57:16 +08:00
										 |  |  | 	*reply = buf | 
					
						
							| 
									
										
										
										
											2016-06-26 05:51:06 +08:00
										 |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-29 06:13:15 +08:00
										 |  |  | // ReadFileHandler - read file handler is rpc wrapper to read file.
 | 
					
						
							| 
									
										
										
										
											2016-08-12 10:32:04 +08:00
										 |  |  | func (s *storageServer) ReadFileHandler(args *ReadFileArgs, reply *[]byte) (err error) { | 
					
						
							|  |  |  | 	defer func() { | 
					
						
							|  |  |  | 		if r := recover(); r != nil { | 
					
						
							|  |  |  | 			// Recover any panic and return ErrCacheFull.
 | 
					
						
							|  |  |  | 			err = bytes.ErrTooLarge | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}() // Do not crash the server.
 | 
					
						
							| 
									
										
										
										
											2016-08-11 12:09:31 +08:00
										 |  |  | 	if !isRPCTokenValid(args.Token) { | 
					
						
							|  |  |  | 		return errors.New("Invalid token") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-08-12 10:32:04 +08:00
										 |  |  | 	// Allocate the requested buffer from the client.
 | 
					
						
							|  |  |  | 	*reply = make([]byte, args.Size) | 
					
						
							|  |  |  | 	var n int64 | 
					
						
							|  |  |  | 	n, err = s.storage.ReadFile(args.Vol, args.Path, args.Offset, *reply) | 
					
						
							|  |  |  | 	// Sending an error over the rpc layer, would cause unmarshalling to fail. In situations
 | 
					
						
							|  |  |  | 	// when we have short read i.e `io.ErrUnexpectedEOF` treat it as good condition and copy
 | 
					
						
							|  |  |  | 	// the buffer properly.
 | 
					
						
							|  |  |  | 	if err == io.ErrUnexpectedEOF { | 
					
						
							|  |  |  | 		// Reset to nil as good condition.
 | 
					
						
							|  |  |  | 		err = nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	*reply = (*reply)[0:n] | 
					
						
							|  |  |  | 	return err | 
					
						
							| 
									
										
										
										
											2016-05-29 06:13:15 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // AppendFileHandler - append file handler is rpc wrapper to append file.
 | 
					
						
							| 
									
										
										
										
											2016-08-11 12:09:31 +08:00
										 |  |  | func (s *storageServer) AppendFileHandler(args *AppendFileArgs, reply *GenericReply) error { | 
					
						
							|  |  |  | 	if !isRPCTokenValid(args.Token) { | 
					
						
							|  |  |  | 		return errors.New("Invalid token") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return s.storage.AppendFile(args.Vol, args.Path, args.Buffer) | 
					
						
							| 
									
										
										
										
											2016-05-29 06:13:15 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-09 01:37:38 +08:00
										 |  |  | // DeleteFileHandler - delete file handler is rpc wrapper to delete file.
 | 
					
						
							| 
									
										
										
										
											2016-08-11 12:09:31 +08:00
										 |  |  | func (s *storageServer) DeleteFileHandler(args *DeleteFileArgs, reply *GenericReply) error { | 
					
						
							|  |  |  | 	if !isRPCTokenValid(args.Token) { | 
					
						
							|  |  |  | 		return errors.New("Invalid token") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return s.storage.DeleteFile(args.Vol, args.Path) | 
					
						
							| 
									
										
										
										
											2016-04-09 01:37:38 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-02 18:12:18 +08:00
										 |  |  | // RenameFileHandler - rename file handler is rpc wrapper to rename file.
 | 
					
						
							| 
									
										
										
										
											2016-08-11 12:09:31 +08:00
										 |  |  | func (s *storageServer) RenameFileHandler(args *RenameFileArgs, reply *GenericReply) error { | 
					
						
							|  |  |  | 	if !isRPCTokenValid(args.Token) { | 
					
						
							|  |  |  | 		return errors.New("Invalid token") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return s.storage.RenameFile(args.SrcVol, args.SrcPath, args.DstVol, args.DstPath) | 
					
						
							| 
									
										
										
										
											2016-05-02 18:12:18 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-20 11:05:38 +08:00
										 |  |  | // Initialize new storage rpc.
 | 
					
						
							| 
									
										
										
										
											2016-08-01 05:11:14 +08:00
										 |  |  | func newRPCServer(serverConfig serverCmdConfig) (servers []*storageServer, err error) { | 
					
						
							| 
									
										
										
										
											2016-04-30 05:24:10 +08:00
										 |  |  | 	// Initialize posix storage API.
 | 
					
						
							| 
									
										
										
										
											2016-08-01 05:11:14 +08:00
										 |  |  | 	exports := serverConfig.disks | 
					
						
							|  |  |  | 	ignoredExports := serverConfig.ignoredDisks | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Save ignored disks in a map
 | 
					
						
							|  |  |  | 	skipDisks := make(map[string]bool) | 
					
						
							|  |  |  | 	for _, ignoredExport := range ignoredExports { | 
					
						
							|  |  |  | 		skipDisks[ignoredExport] = true | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for _, export := range exports { | 
					
						
							|  |  |  | 		if skipDisks[export] { | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		// e.g server:/mnt/disk1
 | 
					
						
							|  |  |  | 		if isLocalStorage(export) { | 
					
						
							|  |  |  | 			if idx := strings.LastIndex(export, ":"); idx != -1 { | 
					
						
							|  |  |  | 				export = export[idx+1:] | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			var storage StorageAPI | 
					
						
							|  |  |  | 			storage, err = newPosix(export) | 
					
						
							|  |  |  | 			if err != nil && err != errDiskNotFound { | 
					
						
							|  |  |  | 				return nil, err | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if idx := strings.LastIndex(export, ":"); idx != -1 { | 
					
						
							|  |  |  | 				export = export[idx+1:] | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			servers = append(servers, &storageServer{ | 
					
						
							|  |  |  | 				storage: storage, | 
					
						
							|  |  |  | 				path:    export, | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-04-09 01:37:38 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-08-01 05:11:14 +08:00
										 |  |  | 	return servers, err | 
					
						
							| 
									
										
										
										
											2016-04-20 11:05:38 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // registerStorageRPCRouter - register storage rpc router.
 | 
					
						
							| 
									
										
										
										
											2016-08-01 05:11:14 +08:00
										 |  |  | func registerStorageRPCRouters(mux *router.Router, stServers []*storageServer) { | 
					
						
							|  |  |  | 	// Create a unique route for each disk exported from this node.
 | 
					
						
							|  |  |  | 	for _, stServer := range stServers { | 
					
						
							| 
									
										
										
										
											2016-08-10 14:57:16 +08:00
										 |  |  | 		storageRPCServer := rpc.NewServer() | 
					
						
							| 
									
										
										
										
											2016-08-01 05:11:14 +08:00
										 |  |  | 		storageRPCServer.RegisterName("Storage", stServer) | 
					
						
							|  |  |  | 		// Add minio storage routes.
 | 
					
						
							|  |  |  | 		storageRouter := mux.PathPrefix(reservedBucket).Subrouter() | 
					
						
							|  |  |  | 		storageRouter.Path(path.Join("/storage", stServer.path)).Handler(storageRPCServer) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-04-09 01:37:38 +08:00
										 |  |  | } |