2016-01-25 20:29:20 -05: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-01-23 22:44:32 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2016-02-07 06:37:54 -05:00
|
|
|
"net/url"
|
2016-02-04 01:46:45 -05:00
|
|
|
"os"
|
2016-02-07 06:37:54 -05:00
|
|
|
"path/filepath"
|
2016-02-04 01:46:45 -05:00
|
|
|
"runtime"
|
|
|
|
"strconv"
|
2016-02-01 15:47:46 -05:00
|
|
|
"strings"
|
2016-01-23 22:44:32 -05:00
|
|
|
"time"
|
|
|
|
|
2016-01-25 01:26:53 -05:00
|
|
|
jwtgo "github.com/dgrijalva/jwt-go"
|
2016-02-04 01:46:45 -05:00
|
|
|
"github.com/dustin/go-humanize"
|
2016-02-12 21:55:17 -05:00
|
|
|
"github.com/gorilla/rpc/v2/json2"
|
2016-01-27 04:52:54 -05:00
|
|
|
"github.com/minio/minio-go"
|
2016-01-26 15:08:45 -05:00
|
|
|
"github.com/minio/minio/pkg/disk"
|
2016-01-23 22:44:32 -05:00
|
|
|
)
|
|
|
|
|
2016-02-18 05:13:52 -05:00
|
|
|
// isJWTReqAuthenticated validates if any incoming request to be a
|
|
|
|
// valid JWT authenticated request.
|
|
|
|
func isJWTReqAuthenticated(req *http.Request) bool {
|
|
|
|
jwt := initJWT()
|
|
|
|
token, e := jwtgo.ParseFromRequest(req, func(token *jwtgo.Token) (interface{}, error) {
|
2016-01-27 04:52:54 -05:00
|
|
|
if _, ok := token.Method.(*jwtgo.SigningMethodHMAC); !ok {
|
2016-01-23 22:44:32 -05:00
|
|
|
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
|
|
|
|
}
|
2016-01-27 04:52:54 -05:00
|
|
|
return jwt.secretAccessKey, nil
|
2016-01-23 22:44:32 -05:00
|
|
|
})
|
2016-01-27 04:52:54 -05:00
|
|
|
if e != nil {
|
2016-01-23 22:44:32 -05:00
|
|
|
return false
|
|
|
|
}
|
2016-02-18 05:13:52 -05:00
|
|
|
return token.Valid
|
2016-01-23 22:44:32 -05:00
|
|
|
}
|
|
|
|
|
2016-02-19 03:00:32 -05:00
|
|
|
// GenericRep - reply structure for calls for which reply is success/failure
|
|
|
|
// for ex. RemoveObject MakeBucket
|
|
|
|
type GenericRep struct {
|
|
|
|
UIVersion string `json:"uiVersion"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenericArgs - empty struct
|
|
|
|
type GenericArgs struct{}
|
|
|
|
|
2016-02-11 13:56:27 -05:00
|
|
|
// GetUIVersion - get UI version
|
2016-02-18 05:13:52 -05:00
|
|
|
func (web webAPI) GetUIVersion(r *http.Request, args *GenericArgs, reply *GenericRep) error {
|
2016-02-11 13:56:27 -05:00
|
|
|
reply.UIVersion = uiVersion
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-19 03:00:32 -05:00
|
|
|
// ServerInfoRep - server info reply.
|
|
|
|
type ServerInfoRep struct {
|
|
|
|
MinioVersion string
|
|
|
|
MinioMemory string
|
|
|
|
MinioPlatform string
|
|
|
|
MinioRuntime string
|
|
|
|
UIVersion string `json:"uiVersion"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServerInfoArgs - server info args.
|
|
|
|
type ServerInfoArgs struct{}
|
|
|
|
|
2016-02-04 01:46:45 -05:00
|
|
|
// ServerInfo - get server info.
|
2016-02-18 05:13:52 -05:00
|
|
|
func (web *webAPI) ServerInfo(r *http.Request, args *ServerInfoArgs, reply *ServerInfoRep) error {
|
|
|
|
if !isJWTReqAuthenticated(r) {
|
2016-02-12 21:55:17 -05:00
|
|
|
return &json2.Error{Message: "Unauthorized request"}
|
2016-02-04 01:46:45 -05:00
|
|
|
}
|
|
|
|
host, err := os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
host = ""
|
|
|
|
}
|
|
|
|
memstats := &runtime.MemStats{}
|
|
|
|
runtime.ReadMemStats(memstats)
|
|
|
|
mem := fmt.Sprintf("Used: %s | Allocated: %s | Used-Heap: %s | Allocated-Heap: %s",
|
|
|
|
humanize.Bytes(memstats.Alloc),
|
|
|
|
humanize.Bytes(memstats.TotalAlloc),
|
|
|
|
humanize.Bytes(memstats.HeapAlloc),
|
|
|
|
humanize.Bytes(memstats.HeapSys))
|
|
|
|
platform := fmt.Sprintf("Host: %s | OS: %s | Arch: %s",
|
|
|
|
host,
|
|
|
|
runtime.GOOS,
|
|
|
|
runtime.GOARCH)
|
|
|
|
goruntime := fmt.Sprintf("Version: %s | CPUs: %s", runtime.Version(), strconv.Itoa(runtime.NumCPU()))
|
2016-02-08 15:40:22 -05:00
|
|
|
reply.MinioVersion = minioVersion
|
|
|
|
reply.MinioMemory = mem
|
|
|
|
reply.MinioPlatform = platform
|
|
|
|
reply.MinioRuntime = goruntime
|
|
|
|
reply.UIVersion = uiVersion
|
2016-02-04 01:46:45 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-19 03:00:32 -05:00
|
|
|
// DiskInfoArgs - disk info args.
|
|
|
|
type DiskInfoArgs struct{}
|
|
|
|
|
|
|
|
// DiskInfoRep - disk info reply.
|
|
|
|
type DiskInfoRep struct {
|
|
|
|
DiskInfo disk.Info `json:"diskInfo"`
|
|
|
|
UIVersion string `json:"uiVersion"`
|
|
|
|
}
|
|
|
|
|
2016-01-26 15:08:45 -05:00
|
|
|
// DiskInfo - get disk statistics.
|
2016-02-18 05:13:52 -05:00
|
|
|
func (web *webAPI) DiskInfo(r *http.Request, args *DiskInfoArgs, reply *DiskInfoRep) error {
|
|
|
|
if !isJWTReqAuthenticated(r) {
|
2016-02-12 21:55:17 -05:00
|
|
|
return &json2.Error{Message: "Unauthorized request"}
|
2016-01-26 15:08:45 -05:00
|
|
|
}
|
2016-01-27 04:52:54 -05:00
|
|
|
info, e := disk.GetInfo(web.FSPath)
|
|
|
|
if e != nil {
|
2016-02-12 21:55:17 -05:00
|
|
|
return &json2.Error{Message: e.Error()}
|
2016-01-26 15:08:45 -05:00
|
|
|
}
|
2016-02-08 15:40:22 -05:00
|
|
|
reply.DiskInfo = info
|
|
|
|
reply.UIVersion = uiVersion
|
2016-01-26 15:08:45 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-19 03:00:32 -05:00
|
|
|
// MakeBucketArgs - make bucket args.
|
|
|
|
type MakeBucketArgs struct {
|
|
|
|
BucketName string `json:"bucketName"`
|
|
|
|
}
|
|
|
|
|
2016-01-25 01:26:53 -05:00
|
|
|
// MakeBucket - make a bucket.
|
2016-02-18 05:13:52 -05:00
|
|
|
func (web *webAPI) MakeBucket(r *http.Request, args *MakeBucketArgs, reply *GenericRep) error {
|
|
|
|
if !isJWTReqAuthenticated(r) {
|
2016-02-12 21:55:17 -05:00
|
|
|
return &json2.Error{Message: "Unauthorized request"}
|
2016-01-25 01:26:53 -05:00
|
|
|
}
|
2016-02-08 15:40:22 -05:00
|
|
|
reply.UIVersion = uiVersion
|
2016-02-12 21:55:17 -05:00
|
|
|
e := web.Client.MakeBucket(args.BucketName, "", "")
|
|
|
|
if e != nil {
|
|
|
|
return &json2.Error{Message: e.Error()}
|
|
|
|
}
|
|
|
|
return nil
|
2016-01-25 01:26:53 -05:00
|
|
|
}
|
|
|
|
|
2016-02-19 03:00:32 -05:00
|
|
|
// ListBucketsArgs - list bucket args.
|
|
|
|
type ListBucketsArgs struct{}
|
|
|
|
|
|
|
|
// ListBucketsRep - list buckets response
|
|
|
|
type ListBucketsRep struct {
|
|
|
|
Buckets []BucketInfo `json:"buckets"`
|
|
|
|
UIVersion string `json:"uiVersion"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// BucketInfo container for list buckets metadata.
|
|
|
|
type BucketInfo struct {
|
|
|
|
// The name of the bucket.
|
|
|
|
Name string `json:"name"`
|
|
|
|
// Date the bucket was created.
|
|
|
|
CreationDate time.Time `json:"creationDate"`
|
|
|
|
}
|
|
|
|
|
2016-01-23 22:44:32 -05:00
|
|
|
// ListBuckets - list buckets api.
|
2016-02-18 05:13:52 -05:00
|
|
|
func (web *webAPI) ListBuckets(r *http.Request, args *ListBucketsArgs, reply *ListBucketsRep) error {
|
|
|
|
if !isJWTReqAuthenticated(r) {
|
2016-02-12 21:55:17 -05:00
|
|
|
return &json2.Error{Message: "Unauthorized request"}
|
2016-01-23 22:44:32 -05:00
|
|
|
}
|
2016-01-27 04:52:54 -05:00
|
|
|
buckets, e := web.Client.ListBuckets()
|
|
|
|
if e != nil {
|
2016-02-12 21:55:17 -05:00
|
|
|
return &json2.Error{Message: e.Error()}
|
2016-01-23 22:44:32 -05:00
|
|
|
}
|
2016-01-25 01:26:53 -05:00
|
|
|
for _, bucket := range buckets {
|
2016-02-08 15:40:22 -05:00
|
|
|
reply.Buckets = append(reply.Buckets, BucketInfo{
|
2016-01-25 01:26:53 -05:00
|
|
|
Name: bucket.Name,
|
|
|
|
CreationDate: bucket.CreationDate,
|
|
|
|
})
|
2016-01-23 22:44:32 -05:00
|
|
|
}
|
2016-02-08 15:40:22 -05:00
|
|
|
reply.UIVersion = uiVersion
|
2016-01-23 22:44:32 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-19 03:00:32 -05:00
|
|
|
// ListObjectsArgs - list object args.
|
|
|
|
type ListObjectsArgs struct {
|
|
|
|
BucketName string `json:"bucketName"`
|
|
|
|
Prefix string `json:"prefix"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListObjectsRep - list objects response.
|
|
|
|
type ListObjectsRep struct {
|
|
|
|
Objects []ObjectInfo `json:"objects"`
|
|
|
|
UIVersion string `json:"uiVersion"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ObjectInfo container for list objects metadata.
|
|
|
|
type ObjectInfo struct {
|
|
|
|
// Name of the object
|
|
|
|
Key string `json:"name"`
|
|
|
|
// Date and time the object was last modified.
|
|
|
|
LastModified time.Time `json:"lastModified"`
|
|
|
|
// Size in bytes of the object.
|
|
|
|
Size int64 `json:"size"`
|
|
|
|
// ContentType is mime type of the object.
|
|
|
|
ContentType string `json:"contentType"`
|
|
|
|
}
|
|
|
|
|
2016-01-23 22:44:32 -05:00
|
|
|
// ListObjects - list objects api.
|
2016-02-18 05:13:52 -05:00
|
|
|
func (web *webAPI) ListObjects(r *http.Request, args *ListObjectsArgs, reply *ListObjectsRep) error {
|
|
|
|
if !isJWTReqAuthenticated(r) {
|
2016-02-12 21:55:17 -05:00
|
|
|
return &json2.Error{Message: "Unauthorized request"}
|
2016-01-23 22:44:32 -05:00
|
|
|
}
|
|
|
|
doneCh := make(chan struct{})
|
|
|
|
defer close(doneCh)
|
|
|
|
|
2016-01-25 01:26:53 -05:00
|
|
|
for object := range web.Client.ListObjects(args.BucketName, args.Prefix, false, doneCh) {
|
2016-01-23 22:44:32 -05:00
|
|
|
if object.Err != nil {
|
2016-02-12 21:55:17 -05:00
|
|
|
return &json2.Error{Message: object.Err.Error()}
|
2016-01-23 22:44:32 -05:00
|
|
|
}
|
2016-02-01 15:47:46 -05:00
|
|
|
objectInfo := ObjectInfo{
|
|
|
|
Key: object.Key,
|
|
|
|
LastModified: object.LastModified,
|
|
|
|
Size: object.Size,
|
|
|
|
}
|
2016-02-01 15:19:54 -05:00
|
|
|
// TODO - This can get slower for large directories, we can
|
|
|
|
// perhaps extend the ListObjects XML to reply back
|
|
|
|
// ContentType as well.
|
2016-02-01 15:47:46 -05:00
|
|
|
if !strings.HasSuffix(object.Key, "/") && object.Size > 0 {
|
|
|
|
objectStatInfo, e := web.Client.StatObject(args.BucketName, object.Key)
|
|
|
|
if e != nil {
|
2016-02-12 21:55:17 -05:00
|
|
|
return &json2.Error{Message: e.Error()}
|
2016-02-01 15:47:46 -05:00
|
|
|
}
|
|
|
|
objectInfo.ContentType = objectStatInfo.ContentType
|
2016-02-01 15:19:54 -05:00
|
|
|
}
|
2016-02-08 15:40:22 -05:00
|
|
|
reply.Objects = append(reply.Objects, objectInfo)
|
2016-01-23 22:44:32 -05:00
|
|
|
}
|
2016-02-08 15:40:22 -05:00
|
|
|
reply.UIVersion = uiVersion
|
2016-01-23 22:44:32 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-19 03:00:32 -05:00
|
|
|
// PutObjectURLArgs - args to generate url for upload access.
|
|
|
|
type PutObjectURLArgs struct {
|
|
|
|
TargetHost string `json:"targetHost"`
|
|
|
|
BucketName string `json:"bucketName"`
|
|
|
|
ObjectName string `json:"objectName"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutObjectURLRep - reply for presigned upload url request.
|
|
|
|
type PutObjectURLRep struct {
|
|
|
|
URL string `json:"url"`
|
|
|
|
UIVersion string `json:"uiVersion"`
|
2016-01-27 04:52:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// PutObjectURL - generates url for upload access.
|
2016-02-18 05:13:52 -05:00
|
|
|
func (web *webAPI) PutObjectURL(r *http.Request, args *PutObjectURLArgs, reply *PutObjectURLRep) error {
|
|
|
|
if !isJWTReqAuthenticated(r) {
|
2016-02-12 21:55:17 -05:00
|
|
|
return &json2.Error{Message: "Unauthorized request"}
|
2016-01-27 04:52:54 -05:00
|
|
|
}
|
2016-02-19 03:00:32 -05:00
|
|
|
client, e := minio.New(args.TargetHost, web.accessKeyID, web.secretAccessKey, web.inSecure)
|
2016-01-27 04:52:54 -05:00
|
|
|
if e != nil {
|
2016-02-12 21:55:17 -05:00
|
|
|
return &json2.Error{Message: e.Error()}
|
2016-01-27 04:52:54 -05:00
|
|
|
}
|
|
|
|
signedURLStr, e := client.PresignedPutObject(args.BucketName, args.ObjectName, time.Duration(60*60)*time.Second)
|
|
|
|
if e != nil {
|
2016-02-12 21:55:17 -05:00
|
|
|
return &json2.Error{Message: e.Error()}
|
2016-01-27 04:52:54 -05:00
|
|
|
}
|
2016-02-08 15:40:22 -05:00
|
|
|
reply.URL = signedURLStr
|
|
|
|
reply.UIVersion = uiVersion
|
2016-01-27 04:52:54 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-19 03:00:32 -05:00
|
|
|
// GetObjectURLArgs - args to generate url for download access.
|
|
|
|
type GetObjectURLArgs struct {
|
|
|
|
TargetHost string `json:"targetHost"`
|
|
|
|
BucketName string `json:"bucketName"`
|
|
|
|
ObjectName string `json:"objectName"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetObjectURLRep - reply for presigned download url request.
|
|
|
|
type GetObjectURLRep struct {
|
|
|
|
URL string `json:"url"`
|
|
|
|
UIVersion string `json:"uiVersion"`
|
|
|
|
}
|
|
|
|
|
2016-01-27 04:52:54 -05:00
|
|
|
// GetObjectURL - generates url for download access.
|
2016-02-18 05:13:52 -05:00
|
|
|
func (web *webAPI) GetObjectURL(r *http.Request, args *GetObjectURLArgs, reply *GetObjectURLRep) error {
|
|
|
|
if !isJWTReqAuthenticated(r) {
|
2016-02-12 21:55:17 -05:00
|
|
|
return &json2.Error{Message: "Unauthorized request"}
|
2016-01-23 22:44:32 -05:00
|
|
|
}
|
2016-02-03 03:00:01 -05:00
|
|
|
|
|
|
|
// See if object exists.
|
|
|
|
_, e := web.Client.StatObject(args.BucketName, args.ObjectName)
|
|
|
|
if e != nil {
|
2016-02-12 21:55:17 -05:00
|
|
|
return &json2.Error{Message: e.Error()}
|
2016-02-03 03:00:01 -05:00
|
|
|
}
|
|
|
|
|
2016-02-19 03:00:32 -05:00
|
|
|
client, e := minio.New(args.TargetHost, web.accessKeyID, web.secretAccessKey, web.inSecure)
|
2016-01-27 04:52:54 -05:00
|
|
|
if e != nil {
|
2016-02-12 21:55:17 -05:00
|
|
|
return &json2.Error{Message: e.Error()}
|
2016-01-27 04:52:54 -05:00
|
|
|
}
|
2016-02-19 03:00:32 -05:00
|
|
|
|
2016-02-07 06:37:54 -05:00
|
|
|
reqParams := make(url.Values)
|
|
|
|
// Set content disposition for browser to download the file.
|
|
|
|
reqParams.Set("response-content-disposition", fmt.Sprintf(`attachment; filename="%s"`, filepath.Base(args.ObjectName)))
|
|
|
|
signedURLStr, e := client.PresignedGetObject(args.BucketName, args.ObjectName, time.Duration(60*60)*time.Second, reqParams)
|
2016-01-27 04:52:54 -05:00
|
|
|
if e != nil {
|
2016-02-12 21:55:17 -05:00
|
|
|
return &json2.Error{Message: e.Error()}
|
2016-01-23 22:44:32 -05:00
|
|
|
}
|
2016-02-08 15:40:22 -05:00
|
|
|
reply.URL = signedURLStr
|
|
|
|
reply.UIVersion = uiVersion
|
2016-01-23 22:44:32 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-19 03:00:32 -05:00
|
|
|
// RemoveObjectArgs - args to remove an object
|
|
|
|
type RemoveObjectArgs struct {
|
|
|
|
TargetHost string `json:"targetHost"`
|
|
|
|
BucketName string `json:"bucketName"`
|
|
|
|
ObjectName string `json:"objectName"`
|
|
|
|
}
|
|
|
|
|
2016-02-05 09:16:36 -05:00
|
|
|
// RemoveObject - removes an object.
|
2016-02-18 05:13:52 -05:00
|
|
|
func (web *webAPI) RemoveObject(r *http.Request, args *RemoveObjectArgs, reply *GenericRep) error {
|
|
|
|
if !isJWTReqAuthenticated(r) {
|
2016-02-12 21:55:17 -05:00
|
|
|
return &json2.Error{Message: "Unauthorized request"}
|
2016-02-05 09:16:36 -05:00
|
|
|
}
|
2016-02-08 15:40:22 -05:00
|
|
|
reply.UIVersion = uiVersion
|
2016-02-12 21:55:17 -05:00
|
|
|
e := web.Client.RemoveObject(args.BucketName, args.ObjectName)
|
|
|
|
if e != nil {
|
|
|
|
return &json2.Error{Message: e.Error()}
|
|
|
|
}
|
|
|
|
return nil
|
2016-02-05 09:16:36 -05:00
|
|
|
}
|
|
|
|
|
2016-02-19 03:00:32 -05:00
|
|
|
// LoginArgs - login arguments.
|
|
|
|
type LoginArgs struct {
|
|
|
|
Username string `json:"username" form:"username"`
|
|
|
|
Password string `json:"password" form:"password"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoginRep - login reply.
|
|
|
|
type LoginRep struct {
|
|
|
|
Token string `json:"token"`
|
|
|
|
UIVersion string `json:"uiVersion"`
|
|
|
|
}
|
|
|
|
|
2016-01-23 22:44:32 -05:00
|
|
|
// Login - user login handler.
|
2016-02-18 05:13:52 -05:00
|
|
|
func (web *webAPI) Login(r *http.Request, args *LoginArgs, reply *LoginRep) error {
|
|
|
|
jwt := initJWT()
|
2016-01-25 20:29:20 -05:00
|
|
|
if jwt.Authenticate(args.Username, args.Password) {
|
2016-01-25 01:26:53 -05:00
|
|
|
token, err := jwt.GenerateToken(args.Username)
|
2016-01-23 22:44:32 -05:00
|
|
|
if err != nil {
|
2016-02-12 21:55:17 -05:00
|
|
|
return &json2.Error{Message: err.Cause.Error(), Data: err.String()}
|
2016-01-23 22:44:32 -05:00
|
|
|
}
|
|
|
|
reply.Token = token
|
2016-02-08 15:40:22 -05:00
|
|
|
reply.UIVersion = uiVersion
|
2016-01-23 22:44:32 -05:00
|
|
|
return nil
|
|
|
|
}
|
2016-02-12 21:55:17 -05:00
|
|
|
return &json2.Error{Message: "Invalid credentials"}
|
2016-01-23 22:44:32 -05:00
|
|
|
}
|