|
|
@ -17,10 +17,13 @@ |
|
|
|
package main |
|
|
|
package main |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
|
|
|
|
"errors" |
|
|
|
|
|
|
|
"fmt" |
|
|
|
"net/rpc" |
|
|
|
"net/rpc" |
|
|
|
"path" |
|
|
|
"path" |
|
|
|
"strings" |
|
|
|
"strings" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
jwtgo "github.com/dgrijalva/jwt-go" |
|
|
|
router "github.com/gorilla/mux" |
|
|
|
router "github.com/gorilla/mux" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
@ -31,15 +34,62 @@ type storageServer struct { |
|
|
|
path string |
|
|
|
path string |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 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 |
|
|
|
|
|
|
|
reply.ServerVersion = minioVersion |
|
|
|
|
|
|
|
return nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Volume operations handlers
|
|
|
|
/// Volume operations handlers
|
|
|
|
|
|
|
|
|
|
|
|
// MakeVolHandler - make vol handler is rpc wrapper for MakeVol operation.
|
|
|
|
// MakeVolHandler - make vol handler is rpc wrapper for MakeVol operation.
|
|
|
|
func (s *storageServer) MakeVolHandler(arg *string, reply *GenericReply) error { |
|
|
|
func (s *storageServer) MakeVolHandler(args *GenericVolArgs, reply *GenericReply) error { |
|
|
|
return s.storage.MakeVol(*arg) |
|
|
|
if !isRPCTokenValid(args.Token) { |
|
|
|
|
|
|
|
return errors.New("Invalid token") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return s.storage.MakeVol(args.Vol) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// ListVolsHandler - list vols handler is rpc wrapper for ListVols operation.
|
|
|
|
// ListVolsHandler - list vols handler is rpc wrapper for ListVols operation.
|
|
|
|
func (s *storageServer) ListVolsHandler(arg *string, reply *ListVolsReply) error { |
|
|
|
func (s *storageServer) ListVolsHandler(token *string, reply *ListVolsReply) error { |
|
|
|
|
|
|
|
if !isRPCTokenValid(*token) { |
|
|
|
|
|
|
|
return errors.New("Invalid token") |
|
|
|
|
|
|
|
} |
|
|
|
vols, err := s.storage.ListVols() |
|
|
|
vols, err := s.storage.ListVols() |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
return err |
|
|
@ -49,8 +99,11 @@ func (s *storageServer) ListVolsHandler(arg *string, reply *ListVolsReply) error |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// StatVolHandler - stat vol handler is a rpc wrapper for StatVol operation.
|
|
|
|
// StatVolHandler - stat vol handler is a rpc wrapper for StatVol operation.
|
|
|
|
func (s *storageServer) StatVolHandler(arg *string, reply *VolInfo) error { |
|
|
|
func (s *storageServer) StatVolHandler(args *GenericVolArgs, reply *VolInfo) error { |
|
|
|
volInfo, err := s.storage.StatVol(*arg) |
|
|
|
if !isRPCTokenValid(args.Token) { |
|
|
|
|
|
|
|
return errors.New("Invalid token") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
volInfo, err := s.storage.StatVol(args.Vol) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
@ -60,15 +113,21 @@ func (s *storageServer) StatVolHandler(arg *string, reply *VolInfo) error { |
|
|
|
|
|
|
|
|
|
|
|
// DeleteVolHandler - delete vol handler is a rpc wrapper for
|
|
|
|
// DeleteVolHandler - delete vol handler is a rpc wrapper for
|
|
|
|
// DeleteVol operation.
|
|
|
|
// DeleteVol operation.
|
|
|
|
func (s *storageServer) DeleteVolHandler(arg *string, reply *GenericReply) error { |
|
|
|
func (s *storageServer) DeleteVolHandler(args *GenericVolArgs, reply *GenericReply) error { |
|
|
|
return s.storage.DeleteVol(*arg) |
|
|
|
if !isRPCTokenValid(args.Token) { |
|
|
|
|
|
|
|
return errors.New("Invalid token") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return s.storage.DeleteVol(args.Vol) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// File operations
|
|
|
|
/// File operations
|
|
|
|
|
|
|
|
|
|
|
|
// StatFileHandler - stat file handler is rpc wrapper to stat file.
|
|
|
|
// StatFileHandler - stat file handler is rpc wrapper to stat file.
|
|
|
|
func (s *storageServer) StatFileHandler(arg *StatFileArgs, reply *FileInfo) error { |
|
|
|
func (s *storageServer) StatFileHandler(args *StatFileArgs, reply *FileInfo) error { |
|
|
|
fileInfo, err := s.storage.StatFile(arg.Vol, arg.Path) |
|
|
|
if !isRPCTokenValid(args.Token) { |
|
|
|
|
|
|
|
return errors.New("Invalid token") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
fileInfo, err := s.storage.StatFile(args.Vol, args.Path) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
@ -77,8 +136,11 @@ func (s *storageServer) StatFileHandler(arg *StatFileArgs, reply *FileInfo) erro |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// ListDirHandler - list directory handler is rpc wrapper to list dir.
|
|
|
|
// ListDirHandler - list directory handler is rpc wrapper to list dir.
|
|
|
|
func (s *storageServer) ListDirHandler(arg *ListDirArgs, reply *[]string) error { |
|
|
|
func (s *storageServer) ListDirHandler(args *ListDirArgs, reply *[]string) error { |
|
|
|
entries, err := s.storage.ListDir(arg.Vol, arg.Path) |
|
|
|
if !isRPCTokenValid(args.Token) { |
|
|
|
|
|
|
|
return errors.New("Invalid token") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
entries, err := s.storage.ListDir(args.Vol, args.Path) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
@ -87,8 +149,11 @@ func (s *storageServer) ListDirHandler(arg *ListDirArgs, reply *[]string) error |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// ReadAllHandler - read all handler is rpc wrapper to read all storage API.
|
|
|
|
// ReadAllHandler - read all handler is rpc wrapper to read all storage API.
|
|
|
|
func (s *storageServer) ReadAllHandler(arg *ReadFileArgs, reply *[]byte) error { |
|
|
|
func (s *storageServer) ReadAllHandler(args *ReadFileArgs, reply *[]byte) error { |
|
|
|
buf, err := s.storage.ReadAll(arg.Vol, arg.Path) |
|
|
|
if !isRPCTokenValid(args.Token) { |
|
|
|
|
|
|
|
return errors.New("Invalid token") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
buf, err := s.storage.ReadAll(args.Vol, args.Path) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
@ -97,8 +162,11 @@ func (s *storageServer) ReadAllHandler(arg *ReadFileArgs, reply *[]byte) error { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// ReadFileHandler - read file handler is rpc wrapper to read file.
|
|
|
|
// ReadFileHandler - read file handler is rpc wrapper to read file.
|
|
|
|
func (s *storageServer) ReadFileHandler(arg *ReadFileArgs, reply *int64) error { |
|
|
|
func (s *storageServer) ReadFileHandler(args *ReadFileArgs, reply *int64) error { |
|
|
|
n, err := s.storage.ReadFile(arg.Vol, arg.Path, arg.Offset, arg.Buffer) |
|
|
|
if !isRPCTokenValid(args.Token) { |
|
|
|
|
|
|
|
return errors.New("Invalid token") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
n, err := s.storage.ReadFile(args.Vol, args.Path, args.Offset, args.Buffer) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
@ -107,18 +175,27 @@ func (s *storageServer) ReadFileHandler(arg *ReadFileArgs, reply *int64) error { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// AppendFileHandler - append file handler is rpc wrapper to append file.
|
|
|
|
// AppendFileHandler - append file handler is rpc wrapper to append file.
|
|
|
|
func (s *storageServer) AppendFileHandler(arg *AppendFileArgs, reply *GenericReply) error { |
|
|
|
func (s *storageServer) AppendFileHandler(args *AppendFileArgs, reply *GenericReply) error { |
|
|
|
return s.storage.AppendFile(arg.Vol, arg.Path, arg.Buffer) |
|
|
|
if !isRPCTokenValid(args.Token) { |
|
|
|
|
|
|
|
return errors.New("Invalid token") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return s.storage.AppendFile(args.Vol, args.Path, args.Buffer) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// DeleteFileHandler - delete file handler is rpc wrapper to delete file.
|
|
|
|
// DeleteFileHandler - delete file handler is rpc wrapper to delete file.
|
|
|
|
func (s *storageServer) DeleteFileHandler(arg *DeleteFileArgs, reply *GenericReply) error { |
|
|
|
func (s *storageServer) DeleteFileHandler(args *DeleteFileArgs, reply *GenericReply) error { |
|
|
|
return s.storage.DeleteFile(arg.Vol, arg.Path) |
|
|
|
if !isRPCTokenValid(args.Token) { |
|
|
|
|
|
|
|
return errors.New("Invalid token") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return s.storage.DeleteFile(args.Vol, args.Path) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// RenameFileHandler - rename file handler is rpc wrapper to rename file.
|
|
|
|
// RenameFileHandler - rename file handler is rpc wrapper to rename file.
|
|
|
|
func (s *storageServer) RenameFileHandler(arg *RenameFileArgs, reply *GenericReply) error { |
|
|
|
func (s *storageServer) RenameFileHandler(args *RenameFileArgs, reply *GenericReply) error { |
|
|
|
return s.storage.RenameFile(arg.SrcVol, arg.SrcPath, arg.DstVol, arg.DstPath) |
|
|
|
if !isRPCTokenValid(args.Token) { |
|
|
|
|
|
|
|
return errors.New("Invalid token") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return s.storage.RenameFile(args.SrcVol, args.SrcPath, args.DstVol, args.DstPath) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Initialize new storage rpc.
|
|
|
|
// Initialize new storage rpc.
|
|
|
|