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"
|
2016-01-27 04:52:54 -05:00
|
|
|
"net"
|
2016-01-23 22:44:32 -05:00
|
|
|
"net/http"
|
2016-02-04 01:46:45 -05:00
|
|
|
"os"
|
|
|
|
"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-01-27 04:52:54 -05:00
|
|
|
"github.com/minio/minio-go"
|
|
|
|
"github.com/minio/minio-xl/pkg/probe"
|
2016-01-26 15:08:45 -05:00
|
|
|
"github.com/minio/minio/pkg/disk"
|
2016-01-23 22:44:32 -05:00
|
|
|
)
|
|
|
|
|
2016-01-25 20:29:20 -05:00
|
|
|
// isAuthenticated validates if any incoming request to be a valid JWT
|
|
|
|
// authenticated request.
|
2016-01-23 22:44:32 -05:00
|
|
|
func isAuthenticated(req *http.Request) bool {
|
2016-01-25 01:26:53 -05:00
|
|
|
jwt := InitJWT()
|
2016-01-27 04:52:54 -05:00
|
|
|
tokenRequest, e := jwtgo.ParseFromRequest(req, func(token *jwtgo.Token) (interface{}, error) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
return tokenRequest.Valid
|
|
|
|
}
|
|
|
|
|
2016-02-04 01:46:45 -05:00
|
|
|
// ServerInfo - get server info.
|
|
|
|
func (web *WebAPI) ServerInfo(r *http.Request, args *ServerInfoArgs, reply *ServerInfo) error {
|
|
|
|
if !isAuthenticated(r) {
|
|
|
|
return errUnAuthorizedRequest
|
|
|
|
}
|
|
|
|
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()))
|
|
|
|
serverInfo := ServerInfo{}
|
|
|
|
serverInfo.MinioVersion = minioVersion
|
|
|
|
serverInfo.MinioMemory = mem
|
|
|
|
serverInfo.MinioPlatform = platform
|
|
|
|
serverInfo.MinioRuntime = goruntime
|
|
|
|
*reply = serverInfo
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-26 15:08:45 -05:00
|
|
|
// DiskInfo - get disk statistics.
|
|
|
|
func (web *WebAPI) DiskInfo(r *http.Request, args *DiskInfoArgs, reply *disk.Info) error {
|
|
|
|
if !isAuthenticated(r) {
|
|
|
|
return errUnAuthorizedRequest
|
|
|
|
}
|
2016-01-27 04:52:54 -05:00
|
|
|
info, e := disk.GetInfo(web.FSPath)
|
|
|
|
if e != nil {
|
|
|
|
return e
|
2016-01-26 15:08:45 -05:00
|
|
|
}
|
|
|
|
*reply = info
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-25 01:26:53 -05:00
|
|
|
// MakeBucket - make a bucket.
|
|
|
|
func (web *WebAPI) MakeBucket(r *http.Request, args *MakeBucketArgs, reply *string) error {
|
|
|
|
if !isAuthenticated(r) {
|
|
|
|
return errUnAuthorizedRequest
|
|
|
|
}
|
|
|
|
return web.Client.MakeBucket(args.BucketName, "", "")
|
|
|
|
}
|
|
|
|
|
2016-01-23 22:44:32 -05:00
|
|
|
// ListBuckets - list buckets api.
|
2016-01-25 01:26:53 -05:00
|
|
|
func (web *WebAPI) ListBuckets(r *http.Request, args *ListBucketsArgs, reply *[]BucketInfo) error {
|
2016-01-23 22:44:32 -05:00
|
|
|
if !isAuthenticated(r) {
|
|
|
|
return errUnAuthorizedRequest
|
|
|
|
}
|
2016-01-27 04:52:54 -05:00
|
|
|
buckets, e := web.Client.ListBuckets()
|
|
|
|
if e != nil {
|
|
|
|
return e
|
2016-01-23 22:44:32 -05:00
|
|
|
}
|
2016-01-25 01:26:53 -05:00
|
|
|
for _, bucket := range buckets {
|
|
|
|
*reply = append(*reply, BucketInfo{
|
|
|
|
Name: bucket.Name,
|
|
|
|
CreationDate: bucket.CreationDate,
|
|
|
|
})
|
2016-01-23 22:44:32 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListObjects - list objects api.
|
2016-01-25 01:26:53 -05:00
|
|
|
func (web *WebAPI) ListObjects(r *http.Request, args *ListObjectsArgs, reply *[]ObjectInfo) error {
|
2016-01-23 22:44:32 -05:00
|
|
|
if !isAuthenticated(r) {
|
|
|
|
return errUnAuthorizedRequest
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
return object.Err
|
|
|
|
}
|
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 {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
objectInfo.ContentType = objectStatInfo.ContentType
|
2016-02-01 15:19:54 -05:00
|
|
|
}
|
2016-02-01 15:47:46 -05:00
|
|
|
*reply = append(*reply, objectInfo)
|
2016-01-23 22:44:32 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-27 04:52:54 -05:00
|
|
|
func getTargetHost(apiAddress, targetHost string) (string, *probe.Error) {
|
|
|
|
if targetHost != "" {
|
|
|
|
_, port, e := net.SplitHostPort(apiAddress)
|
|
|
|
if e != nil {
|
|
|
|
return "", probe.NewError(e)
|
|
|
|
}
|
|
|
|
host, _, e := net.SplitHostPort(targetHost)
|
|
|
|
if e != nil {
|
|
|
|
return "", probe.NewError(e)
|
|
|
|
}
|
|
|
|
targetHost = net.JoinHostPort(host, port)
|
|
|
|
}
|
|
|
|
return targetHost, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutObjectURL - generates url for upload access.
|
|
|
|
func (web *WebAPI) PutObjectURL(r *http.Request, args *PutObjectURLArgs, reply *string) error {
|
|
|
|
if !isAuthenticated(r) {
|
|
|
|
return errUnAuthorizedRequest
|
|
|
|
}
|
|
|
|
targetHost, err := getTargetHost(web.apiAddress, args.TargetHost)
|
|
|
|
if err != nil {
|
|
|
|
return probe.WrapError(err)
|
|
|
|
}
|
|
|
|
client, e := minio.NewV4(targetHost, web.accessKeyID, web.secretAccessKey, web.inSecure)
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
signedURLStr, e := client.PresignedPutObject(args.BucketName, args.ObjectName, time.Duration(60*60)*time.Second)
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
*reply = signedURLStr
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetObjectURL - generates url for download access.
|
2016-01-23 22:44:32 -05:00
|
|
|
func (web *WebAPI) GetObjectURL(r *http.Request, args *GetObjectURLArgs, reply *string) error {
|
|
|
|
if !isAuthenticated(r) {
|
|
|
|
return errUnAuthorizedRequest
|
|
|
|
}
|
2016-02-03 03:00:01 -05:00
|
|
|
|
|
|
|
// See if object exists.
|
|
|
|
_, e := web.Client.StatObject(args.BucketName, args.ObjectName)
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2016-01-27 04:52:54 -05:00
|
|
|
targetHost, err := getTargetHost(web.apiAddress, args.TargetHost)
|
2016-01-23 22:44:32 -05:00
|
|
|
if err != nil {
|
2016-01-27 04:52:54 -05:00
|
|
|
return probe.WrapError(err)
|
|
|
|
}
|
|
|
|
client, e := minio.NewV4(targetHost, web.accessKeyID, web.secretAccessKey, web.inSecure)
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
signedURLStr, e := client.PresignedGetObject(args.BucketName, args.ObjectName, time.Duration(60*60)*time.Second)
|
|
|
|
if e != nil {
|
|
|
|
return e
|
2016-01-23 22:44:32 -05:00
|
|
|
}
|
2016-01-27 04:52:54 -05:00
|
|
|
*reply = signedURLStr
|
2016-01-23 22:44:32 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Login - user login handler.
|
|
|
|
func (web *WebAPI) Login(r *http.Request, args *LoginArgs, reply *AuthToken) error {
|
2016-01-25 01:26:53 -05:00
|
|
|
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-01-27 04:52:54 -05:00
|
|
|
return probe.WrapError(err.Trace())
|
2016-01-23 22:44:32 -05:00
|
|
|
}
|
|
|
|
reply.Token = token
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errUnAuthorizedRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
// RefreshToken - refresh token handler.
|
|
|
|
func (web *WebAPI) RefreshToken(r *http.Request, args *LoginArgs, reply *AuthToken) error {
|
|
|
|
if isAuthenticated(r) {
|
2016-01-25 01:26:53 -05:00
|
|
|
jwt := InitJWT()
|
|
|
|
token, err := jwt.GenerateToken(args.Username)
|
2016-01-23 22:44:32 -05:00
|
|
|
if err != nil {
|
2016-01-27 04:52:54 -05:00
|
|
|
return probe.WrapError(err.Trace())
|
2016-01-23 22:44:32 -05:00
|
|
|
}
|
|
|
|
reply.Token = token
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errUnAuthorizedRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
// Logout - user logout.
|
|
|
|
func (web *WebAPI) Logout(r *http.Request, arg *string, reply *string) error {
|
|
|
|
if isAuthenticated(r) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errUnAuthorizedRequest
|
|
|
|
}
|