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-08-18 19:23:42 -04:00
|
|
|
package cmd
|
2016-01-23 22:44:32 -05:00
|
|
|
|
|
|
|
import (
|
2016-08-30 13:04:50 -04:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2016-07-31 17:11:14 -04:00
|
|
|
"errors"
|
2016-01-23 22:44:32 -05:00
|
|
|
"fmt"
|
2016-08-30 13:04:50 -04:00
|
|
|
"io/ioutil"
|
2016-01-23 22:44:32 -05:00
|
|
|
"net/http"
|
2016-02-04 01:46:45 -05:00
|
|
|
"os"
|
2016-02-22 01:38:38 -05:00
|
|
|
"path"
|
2016-02-04 01:46:45 -05:00
|
|
|
"runtime"
|
|
|
|
"strconv"
|
2016-09-23 04:24:49 -04: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-07-14 17:59:20 -04:00
|
|
|
jwtreq "github.com/dgrijalva/jwt-go/request"
|
2016-02-04 01:46:45 -05:00
|
|
|
"github.com/dustin/go-humanize"
|
2016-03-31 09:57:29 -04:00
|
|
|
"github.com/gorilla/mux"
|
2016-02-12 21:55:17 -05:00
|
|
|
"github.com/gorilla/rpc/v2/json2"
|
2016-08-30 13:04:50 -04:00
|
|
|
"github.com/minio/minio-go/pkg/policy"
|
2016-02-23 16:05:47 -05:00
|
|
|
"github.com/minio/miniobrowser"
|
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 {
|
2016-10-05 13:18:55 -04:00
|
|
|
jwt, err := newJWT(defaultJWTExpiry)
|
2016-07-12 00:57:40 -04:00
|
|
|
if err != nil {
|
|
|
|
errorIf(err, "unable to initialize a new JWT")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-07-14 17:59:20 -04:00
|
|
|
var reqCallback jwtgo.Keyfunc
|
|
|
|
reqCallback = 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"])
|
|
|
|
}
|
config/main: Re-write config files - add to new config v3
- New config format.
```
{
"version": "3",
"address": ":9000",
"backend": {
"type": "fs",
"disk": "/path"
},
"credential": {
"accessKey": "WLGDGYAQYIGI833EV05A",
"secretKey": "BYvgJM101sHngl2uzjXS/OBF/aMxAN06JrJ3qJlF"
},
"region": "us-east-1",
"logger": {
"file": {
"enable": false,
"fileName": "",
"level": "error"
},
"syslog": {
"enable": false,
"address": "",
"level": "debug"
},
"console": {
"enable": true,
"level": "fatal"
}
}
}
```
New command lines in lieu of supporting XL.
Minio initialize filesystem backend.
~~~
$ minio init fs <path>
~~~
Minio initialize XL backend.
~~~
$ minio init xl <url1>...<url16>
~~~
For 'fs' backend it starts the server.
~~~
$ minio server
~~~
For 'xl' backend it waits for servers to join.
~~~
$ minio server
... [PROGRESS BAR] of servers connecting
~~~
Now on other servers execute 'join' and they connect.
~~~
....
minio join <url1> -- from <url2> && minio server
minio join <url1> -- from <url3> && minio server
...
...
minio join <url1> -- from <url16> && minio server
~~~
2016-02-12 18:27:10 -05:00
|
|
|
return []byte(jwt.SecretAccessKey), nil
|
2016-07-14 17:59:20 -04:00
|
|
|
}
|
|
|
|
token, err := jwtreq.ParseFromRequest(req, jwtreq.AuthorizationHeaderExtractor, reqCallback)
|
|
|
|
if err != nil {
|
|
|
|
errorIf(err, "token parsing failed")
|
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-04-08 02:44:08 -04:00
|
|
|
// WebGenericArgs - empty struct for calls that don't accept arguments
|
2016-03-21 14:15:08 -04:00
|
|
|
// for ex. ServerInfo, GenerateAuth
|
2016-04-08 02:44:08 -04:00
|
|
|
type WebGenericArgs struct{}
|
2016-03-21 14:15:08 -04:00
|
|
|
|
2016-04-08 02:44:08 -04:00
|
|
|
// WebGenericRep - reply structure for calls for which reply is success/failure
|
2016-02-19 03:00:32 -05:00
|
|
|
// for ex. RemoveObject MakeBucket
|
2016-04-08 02:44:08 -04:00
|
|
|
type WebGenericRep struct {
|
2016-02-19 03:00:32 -05:00
|
|
|
UIVersion string `json:"uiVersion"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServerInfoRep - server info reply.
|
|
|
|
type ServerInfoRep struct {
|
|
|
|
MinioVersion string
|
|
|
|
MinioMemory string
|
|
|
|
MinioPlatform string
|
|
|
|
MinioRuntime string
|
2016-08-03 16:47:03 -04:00
|
|
|
MinioEnvVars []string
|
2016-02-19 03:00:32 -05:00
|
|
|
UIVersion string `json:"uiVersion"`
|
|
|
|
}
|
|
|
|
|
2016-02-04 01:46:45 -05:00
|
|
|
// ServerInfo - get server info.
|
2016-04-12 15:45:15 -04:00
|
|
|
func (web *webAPIHandlers) ServerInfo(r *http.Request, args *WebGenericArgs, reply *ServerInfoRep) error {
|
2016-02-18 05:13:52 -05:00
|
|
|
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-08-03 16:47:03 -04:00
|
|
|
|
|
|
|
reply.MinioEnvVars = os.Environ()
|
2016-08-18 19:23:42 -04:00
|
|
|
reply.MinioVersion = Version
|
2016-02-08 15:40:22 -05:00
|
|
|
reply.MinioMemory = mem
|
|
|
|
reply.MinioPlatform = platform
|
|
|
|
reply.MinioRuntime = goruntime
|
2016-02-23 16:05:47 -05:00
|
|
|
reply.UIVersion = miniobrowser.UIVersion
|
2016-02-04 01:46:45 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-26 17:13:10 -04:00
|
|
|
// StorageInfoRep - contains storage usage statistics.
|
|
|
|
type StorageInfoRep struct {
|
|
|
|
StorageInfo StorageInfo `json:"storageInfo"`
|
|
|
|
UIVersion string `json:"uiVersion"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// StorageInfo - web call to gather storage usage statistics.
|
|
|
|
func (web *webAPIHandlers) StorageInfo(r *http.Request, args *GenericArgs, reply *StorageInfoRep) error {
|
|
|
|
if !isJWTReqAuthenticated(r) {
|
|
|
|
return &json2.Error{Message: "Unauthorized request"}
|
|
|
|
}
|
2016-07-31 17:11:14 -04:00
|
|
|
objectAPI := web.ObjectAPI()
|
|
|
|
if objectAPI == nil {
|
2016-09-26 17:28:35 -04:00
|
|
|
return &json2.Error{Message: "Server not initialized"}
|
2016-07-31 17:11:14 -04:00
|
|
|
}
|
|
|
|
reply.StorageInfo = objectAPI.StorageInfo()
|
2016-10-23 15:32:35 -04:00
|
|
|
reply.UIVersion = miniobrowser.UIVersion
|
2016-05-26 17:13:10 -04: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-04-12 15:45:15 -04:00
|
|
|
func (web *webAPIHandlers) MakeBucket(r *http.Request, args *MakeBucketArgs, reply *WebGenericRep) error {
|
2016-02-18 05:13:52 -05:00
|
|
|
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-07-31 17:11:14 -04:00
|
|
|
objectAPI := web.ObjectAPI()
|
|
|
|
if objectAPI == nil {
|
2016-09-26 17:28:35 -04:00
|
|
|
return &json2.Error{Message: "Server not initialized"}
|
2016-07-31 17:11:14 -04:00
|
|
|
}
|
|
|
|
if err := objectAPI.MakeBucket(args.BucketName); err != nil {
|
2016-04-29 17:24:10 -04:00
|
|
|
return &json2.Error{Message: err.Error()}
|
2016-02-12 21:55:17 -05:00
|
|
|
}
|
2016-10-23 15:32:35 -04:00
|
|
|
reply.UIVersion = miniobrowser.UIVersion
|
2016-02-12 21:55:17 -05:00
|
|
|
return nil
|
2016-01-25 01:26:53 -05:00
|
|
|
}
|
|
|
|
|
2016-02-19 03:00:32 -05:00
|
|
|
// ListBucketsRep - list buckets response
|
|
|
|
type ListBucketsRep struct {
|
objectAPI: Fix object API interface, remove unnecessary structs.
ObjectAPI changes.
```
ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsInfo, *probe.Error)
ListMultipartUploads(bucket, objectPrefix, keyMarker, uploadIDMarker, delimiter string, maxUploads int) (ListMultipartsInfo, *probe.Error)
ListObjectParts(bucket, object, uploadID string, partNumberMarker, maxParts int) (ListPartsInfo, *probe.Error)
CompleteMultipartUpload(bucket string, object string, uploadID string, parts []completePart) (ObjectInfo, *probe.Error)
```
2016-04-03 04:34:20 -04:00
|
|
|
Buckets []WebBucketInfo `json:"buckets"`
|
|
|
|
UIVersion string `json:"uiVersion"`
|
2016-02-19 03:00:32 -05:00
|
|
|
}
|
|
|
|
|
objectAPI: Fix object API interface, remove unnecessary structs.
ObjectAPI changes.
```
ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsInfo, *probe.Error)
ListMultipartUploads(bucket, objectPrefix, keyMarker, uploadIDMarker, delimiter string, maxUploads int) (ListMultipartsInfo, *probe.Error)
ListObjectParts(bucket, object, uploadID string, partNumberMarker, maxParts int) (ListPartsInfo, *probe.Error)
CompleteMultipartUpload(bucket string, object string, uploadID string, parts []completePart) (ObjectInfo, *probe.Error)
```
2016-04-03 04:34:20 -04:00
|
|
|
// WebBucketInfo container for list buckets metadata.
|
|
|
|
type WebBucketInfo struct {
|
2016-02-19 03:00:32 -05:00
|
|
|
// 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-04-12 15:45:15 -04:00
|
|
|
func (web *webAPIHandlers) ListBuckets(r *http.Request, args *WebGenericArgs, reply *ListBucketsRep) error {
|
2016-02-18 05:13:52 -05:00
|
|
|
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-07-31 17:11:14 -04:00
|
|
|
objectAPI := web.ObjectAPI()
|
|
|
|
if objectAPI == nil {
|
2016-09-26 17:28:35 -04:00
|
|
|
return &json2.Error{Message: "Server not initialized"}
|
2016-07-31 17:11:14 -04:00
|
|
|
}
|
|
|
|
buckets, err := objectAPI.ListBuckets()
|
2016-04-29 17:24:10 -04:00
|
|
|
if err != nil {
|
|
|
|
return &json2.Error{Message: err.Error()}
|
2016-01-23 22:44:32 -05:00
|
|
|
}
|
2016-01-25 01:26:53 -05:00
|
|
|
for _, bucket := range buckets {
|
2016-02-20 00:45:37 -05:00
|
|
|
// List all buckets which are not private.
|
2016-03-27 15:37:21 -04:00
|
|
|
if bucket.Name != path.Base(reservedBucket) {
|
objectAPI: Fix object API interface, remove unnecessary structs.
ObjectAPI changes.
```
ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsInfo, *probe.Error)
ListMultipartUploads(bucket, objectPrefix, keyMarker, uploadIDMarker, delimiter string, maxUploads int) (ListMultipartsInfo, *probe.Error)
ListObjectParts(bucket, object, uploadID string, partNumberMarker, maxParts int) (ListPartsInfo, *probe.Error)
CompleteMultipartUpload(bucket string, object string, uploadID string, parts []completePart) (ObjectInfo, *probe.Error)
```
2016-04-03 04:34:20 -04:00
|
|
|
reply.Buckets = append(reply.Buckets, WebBucketInfo{
|
2016-02-20 00:45:37 -05:00
|
|
|
Name: bucket.Name,
|
2016-03-31 09:57:29 -04:00
|
|
|
CreationDate: bucket.Created,
|
2016-02-20 00:45:37 -05:00
|
|
|
})
|
|
|
|
}
|
2016-01-23 22:44:32 -05:00
|
|
|
}
|
2016-02-23 16:05:47 -05:00
|
|
|
reply.UIVersion = miniobrowser.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 {
|
objectAPI: Fix object API interface, remove unnecessary structs.
ObjectAPI changes.
```
ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsInfo, *probe.Error)
ListMultipartUploads(bucket, objectPrefix, keyMarker, uploadIDMarker, delimiter string, maxUploads int) (ListMultipartsInfo, *probe.Error)
ListObjectParts(bucket, object, uploadID string, partNumberMarker, maxParts int) (ListPartsInfo, *probe.Error)
CompleteMultipartUpload(bucket string, object string, uploadID string, parts []completePart) (ObjectInfo, *probe.Error)
```
2016-04-03 04:34:20 -04:00
|
|
|
Objects []WebObjectInfo `json:"objects"`
|
|
|
|
UIVersion string `json:"uiVersion"`
|
2016-02-19 03:00:32 -05:00
|
|
|
}
|
|
|
|
|
objectAPI: Fix object API interface, remove unnecessary structs.
ObjectAPI changes.
```
ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsInfo, *probe.Error)
ListMultipartUploads(bucket, objectPrefix, keyMarker, uploadIDMarker, delimiter string, maxUploads int) (ListMultipartsInfo, *probe.Error)
ListObjectParts(bucket, object, uploadID string, partNumberMarker, maxParts int) (ListPartsInfo, *probe.Error)
CompleteMultipartUpload(bucket string, object string, uploadID string, parts []completePart) (ObjectInfo, *probe.Error)
```
2016-04-03 04:34:20 -04:00
|
|
|
// WebObjectInfo container for list objects metadata.
|
|
|
|
type WebObjectInfo struct {
|
2016-02-19 03:00:32 -05:00
|
|
|
// 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-04-12 15:45:15 -04:00
|
|
|
func (web *webAPIHandlers) ListObjects(r *http.Request, args *ListObjectsArgs, reply *ListObjectsRep) error {
|
2016-03-31 09:57:29 -04:00
|
|
|
marker := ""
|
2016-02-18 05:13:52 -05:00
|
|
|
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-03-31 09:57:29 -04:00
|
|
|
for {
|
2016-07-31 17:11:14 -04:00
|
|
|
objectAPI := web.ObjectAPI()
|
|
|
|
if objectAPI == nil {
|
2016-09-26 17:28:35 -04:00
|
|
|
return &json2.Error{Message: "Server not initialized"}
|
2016-07-31 17:11:14 -04:00
|
|
|
}
|
|
|
|
lo, err := objectAPI.ListObjects(args.BucketName, args.Prefix, marker, "/", 1000)
|
2016-03-31 09:57:29 -04:00
|
|
|
if err != nil {
|
2016-04-29 17:24:10 -04:00
|
|
|
return &json2.Error{Message: err.Error()}
|
2016-01-23 22:44:32 -05:00
|
|
|
}
|
2016-03-31 09:57:29 -04:00
|
|
|
marker = lo.NextMarker
|
|
|
|
for _, obj := range lo.Objects {
|
objectAPI: Fix object API interface, remove unnecessary structs.
ObjectAPI changes.
```
ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsInfo, *probe.Error)
ListMultipartUploads(bucket, objectPrefix, keyMarker, uploadIDMarker, delimiter string, maxUploads int) (ListMultipartsInfo, *probe.Error)
ListObjectParts(bucket, object, uploadID string, partNumberMarker, maxParts int) (ListPartsInfo, *probe.Error)
CompleteMultipartUpload(bucket string, object string, uploadID string, parts []completePart) (ObjectInfo, *probe.Error)
```
2016-04-03 04:34:20 -04:00
|
|
|
reply.Objects = append(reply.Objects, WebObjectInfo{
|
2016-03-31 09:57:29 -04:00
|
|
|
Key: obj.Name,
|
2016-04-08 13:37:38 -04:00
|
|
|
LastModified: obj.ModTime,
|
2016-03-31 09:57:29 -04:00
|
|
|
Size: obj.Size,
|
|
|
|
})
|
2016-02-01 15:47:46 -05:00
|
|
|
}
|
2016-03-31 09:57:29 -04:00
|
|
|
for _, prefix := range lo.Prefixes {
|
objectAPI: Fix object API interface, remove unnecessary structs.
ObjectAPI changes.
```
ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsInfo, *probe.Error)
ListMultipartUploads(bucket, objectPrefix, keyMarker, uploadIDMarker, delimiter string, maxUploads int) (ListMultipartsInfo, *probe.Error)
ListObjectParts(bucket, object, uploadID string, partNumberMarker, maxParts int) (ListPartsInfo, *probe.Error)
CompleteMultipartUpload(bucket string, object string, uploadID string, parts []completePart) (ObjectInfo, *probe.Error)
```
2016-04-03 04:34:20 -04:00
|
|
|
reply.Objects = append(reply.Objects, WebObjectInfo{
|
2016-03-31 09:57:29 -04:00
|
|
|
Key: prefix,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if !lo.IsTruncated {
|
|
|
|
break
|
2016-02-01 15:19:54 -05:00
|
|
|
}
|
2016-01-23 22:44:32 -05:00
|
|
|
}
|
2016-02-23 16:05:47 -05:00
|
|
|
reply.UIVersion = miniobrowser.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-04-12 15:45:15 -04:00
|
|
|
func (web *webAPIHandlers) RemoveObject(r *http.Request, args *RemoveObjectArgs, reply *WebGenericRep) error {
|
2016-02-18 05:13:52 -05:00
|
|
|
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-23 16:05:47 -05:00
|
|
|
reply.UIVersion = miniobrowser.UIVersion
|
2016-07-31 17:11:14 -04:00
|
|
|
objectAPI := web.ObjectAPI()
|
|
|
|
if objectAPI == nil {
|
2016-09-26 17:28:35 -04:00
|
|
|
return &json2.Error{Message: "Server not initialized"}
|
2016-07-31 17:11:14 -04:00
|
|
|
}
|
|
|
|
if err := objectAPI.DeleteObject(args.BucketName, args.ObjectName); err != nil {
|
2016-04-29 17:24:10 -04:00
|
|
|
return &json2.Error{Message: err.Error()}
|
2016-02-12 21:55:17 -05:00
|
|
|
}
|
2016-10-23 15:32:35 -04:00
|
|
|
reply.UIVersion = miniobrowser.UIVersion
|
2016-02-12 21:55:17 -05:00
|
|
|
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-04-12 15:45:15 -04:00
|
|
|
func (web *webAPIHandlers) Login(r *http.Request, args *LoginArgs, reply *LoginRep) error {
|
2016-10-05 13:18:55 -04:00
|
|
|
jwt, err := newJWT(defaultJWTExpiry)
|
2016-07-12 00:57:40 -04:00
|
|
|
if err != nil {
|
|
|
|
return &json2.Error{Message: err.Error()}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = jwt.Authenticate(args.Username, args.Password); err != nil {
|
|
|
|
return &json2.Error{Message: err.Error()}
|
|
|
|
}
|
|
|
|
|
|
|
|
token, err := jwt.GenerateToken(args.Username)
|
|
|
|
if err != nil {
|
|
|
|
return &json2.Error{Message: err.Error()}
|
2016-01-23 22:44:32 -05:00
|
|
|
}
|
2016-07-12 00:57:40 -04:00
|
|
|
reply.Token = token
|
|
|
|
reply.UIVersion = miniobrowser.UIVersion
|
|
|
|
return nil
|
2016-01-23 22:44:32 -05:00
|
|
|
}
|
2016-03-21 14:15:08 -04:00
|
|
|
|
|
|
|
// GenerateAuthReply - reply for GenerateAuth
|
|
|
|
type GenerateAuthReply struct {
|
|
|
|
AccessKey string `json:"accessKey"`
|
|
|
|
SecretKey string `json:"secretKey"`
|
|
|
|
UIVersion string `json:"uiVersion"`
|
|
|
|
}
|
|
|
|
|
2016-04-12 15:45:15 -04:00
|
|
|
func (web webAPIHandlers) GenerateAuth(r *http.Request, args *WebGenericArgs, reply *GenerateAuthReply) error {
|
2016-03-21 14:15:08 -04:00
|
|
|
if !isJWTReqAuthenticated(r) {
|
|
|
|
return &json2.Error{Message: "Unauthorized request"}
|
|
|
|
}
|
|
|
|
cred := mustGenAccessKeys()
|
|
|
|
reply.AccessKey = cred.AccessKeyID
|
|
|
|
reply.SecretKey = cred.SecretAccessKey
|
|
|
|
reply.UIVersion = miniobrowser.UIVersion
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetAuthArgs - argument for SetAuth
|
|
|
|
type SetAuthArgs struct {
|
|
|
|
AccessKey string `json:"accessKey"`
|
|
|
|
SecretKey string `json:"secretKey"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetAuthReply - reply for SetAuth
|
|
|
|
type SetAuthReply struct {
|
2016-10-17 23:18:08 -04:00
|
|
|
Token string `json:"token"`
|
|
|
|
UIVersion string `json:"uiVersion"`
|
|
|
|
PeerErrMsgs map[string]string `json:"peerErrMsgs"`
|
2016-03-21 14:15:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetAuth - Set accessKey and secretKey credentials.
|
2016-04-12 15:45:15 -04:00
|
|
|
func (web *webAPIHandlers) SetAuth(r *http.Request, args *SetAuthArgs, reply *SetAuthReply) error {
|
2016-03-21 14:15:08 -04:00
|
|
|
if !isJWTReqAuthenticated(r) {
|
|
|
|
return &json2.Error{Message: "Unauthorized request"}
|
|
|
|
}
|
2016-03-30 05:47:20 -04:00
|
|
|
if !isValidAccessKey.MatchString(args.AccessKey) {
|
|
|
|
return &json2.Error{Message: "Invalid Access Key"}
|
2016-03-21 14:15:08 -04:00
|
|
|
}
|
2016-03-30 05:47:20 -04:00
|
|
|
if !isValidSecretKey.MatchString(args.SecretKey) {
|
|
|
|
return &json2.Error{Message: "Invalid Secret Key"}
|
2016-03-21 14:15:08 -04:00
|
|
|
}
|
2016-10-17 23:18:08 -04:00
|
|
|
|
2016-03-21 14:15:08 -04:00
|
|
|
cred := credential{args.AccessKey, args.SecretKey}
|
2016-10-17 23:18:08 -04:00
|
|
|
unexpErrsMsg := "ALERT: Unexpected error(s) happened - please check the server logs."
|
|
|
|
gaveUpMsg := func(errMsg error, moreErrors bool) *json2.Error {
|
|
|
|
msg := fmt.Sprintf(
|
|
|
|
"ALERT: We gave up due to: '%s', but there were more errors. Please check the server logs.",
|
|
|
|
errMsg.Error(),
|
|
|
|
)
|
|
|
|
if moreErrors {
|
|
|
|
return &json2.Error{Message: msg}
|
|
|
|
}
|
|
|
|
return &json2.Error{Message: errMsg.Error()}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify all other Minio peers to update credentials
|
|
|
|
errsMap := updateCredsOnPeers(cred)
|
|
|
|
|
|
|
|
// Update local credentials
|
2016-03-21 14:15:08 -04:00
|
|
|
serverConfig.SetCredential(cred)
|
|
|
|
if err := serverConfig.Save(); err != nil {
|
2016-10-17 23:18:08 -04:00
|
|
|
errsMap[globalMinioAddr] = err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Log all the peer related error messages, and populate the
|
|
|
|
// PeerErrMsgs map.
|
|
|
|
reply.PeerErrMsgs = make(map[string]string)
|
|
|
|
for svr, errVal := range errsMap {
|
|
|
|
tErr := fmt.Errorf("Unable to change credentials on %s: %v", svr, errVal)
|
|
|
|
errorIf(tErr, "Credentials change could not be propagated successfully!")
|
|
|
|
reply.PeerErrMsgs[svr] = errVal.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we were unable to update locally, we return an error to
|
|
|
|
// the user/browser.
|
|
|
|
if errsMap[globalMinioAddr] != nil {
|
|
|
|
// Since the error message may be very long to display
|
|
|
|
// on the browser, we tell the user to check the
|
|
|
|
// server logs.
|
|
|
|
return &json2.Error{Message: unexpErrsMsg}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Did we have peer errors?
|
|
|
|
var moreErrors bool
|
|
|
|
if len(errsMap) > 0 {
|
|
|
|
moreErrors = true
|
2016-03-21 14:15:08 -04:00
|
|
|
}
|
|
|
|
|
2016-10-17 23:18:08 -04:00
|
|
|
// If we were able to update locally, we try to generate a new
|
|
|
|
// token and complete the request.
|
2016-10-05 13:18:55 -04:00
|
|
|
jwt, err := newJWT(defaultJWTExpiry) // JWT Expiry set to 24Hrs.
|
2016-07-12 00:57:40 -04:00
|
|
|
if err != nil {
|
2016-10-17 23:18:08 -04:00
|
|
|
return gaveUpMsg(err, moreErrors)
|
2016-07-12 00:57:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = jwt.Authenticate(args.AccessKey, args.SecretKey); err != nil {
|
2016-10-17 23:18:08 -04:00
|
|
|
return gaveUpMsg(err, moreErrors)
|
2016-03-21 14:15:08 -04:00
|
|
|
}
|
|
|
|
token, err := jwt.GenerateToken(args.AccessKey)
|
|
|
|
if err != nil {
|
2016-10-17 23:18:08 -04:00
|
|
|
return gaveUpMsg(err, moreErrors)
|
2016-03-21 14:15:08 -04:00
|
|
|
}
|
2016-10-17 23:18:08 -04:00
|
|
|
|
2016-03-21 14:15:08 -04:00
|
|
|
reply.Token = token
|
|
|
|
reply.UIVersion = miniobrowser.UIVersion
|
|
|
|
return nil
|
|
|
|
}
|
2016-03-31 09:57:29 -04:00
|
|
|
|
|
|
|
// GetAuthReply - Reply current credentials.
|
|
|
|
type GetAuthReply struct {
|
|
|
|
AccessKey string `json:"accessKey"`
|
|
|
|
SecretKey string `json:"secretKey"`
|
|
|
|
UIVersion string `json:"uiVersion"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAuth - return accessKey and secretKey credentials.
|
2016-04-12 15:45:15 -04:00
|
|
|
func (web *webAPIHandlers) GetAuth(r *http.Request, args *WebGenericArgs, reply *GetAuthReply) error {
|
2016-03-31 09:57:29 -04:00
|
|
|
if !isJWTReqAuthenticated(r) {
|
|
|
|
return &json2.Error{Message: "Unauthorized request"}
|
|
|
|
}
|
|
|
|
creds := serverConfig.GetCredential()
|
|
|
|
reply.AccessKey = creds.AccessKeyID
|
|
|
|
reply.SecretKey = creds.SecretAccessKey
|
|
|
|
reply.UIVersion = miniobrowser.UIVersion
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Upload - file upload handler.
|
2016-04-12 15:45:15 -04:00
|
|
|
func (web *webAPIHandlers) Upload(w http.ResponseWriter, r *http.Request) {
|
2016-03-31 09:57:29 -04:00
|
|
|
if !isJWTReqAuthenticated(r) {
|
|
|
|
writeWebErrorResponse(w, errInvalidToken)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
bucket := vars["bucket"]
|
|
|
|
object := vars["object"]
|
2016-07-28 00:11:15 -04:00
|
|
|
|
|
|
|
// Extract incoming metadata if any.
|
|
|
|
metadata := extractMetadataFromHeader(r.Header)
|
|
|
|
|
2016-07-31 17:11:14 -04:00
|
|
|
objectAPI := web.ObjectAPI()
|
|
|
|
if objectAPI == nil {
|
2016-09-26 17:28:35 -04:00
|
|
|
writeWebErrorResponse(w, errors.New("Server not initialized"))
|
2016-07-31 17:11:14 -04:00
|
|
|
return
|
|
|
|
}
|
2016-10-02 18:51:49 -04:00
|
|
|
sha256sum := ""
|
|
|
|
if _, err := objectAPI.PutObject(bucket, object, -1, r.Body, metadata, sha256sum); err != nil {
|
2016-04-29 17:24:10 -04:00
|
|
|
writeWebErrorResponse(w, err)
|
2016-07-28 00:11:15 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch object info for notifications.
|
2016-07-31 17:11:14 -04:00
|
|
|
objInfo, err := objectAPI.GetObjectInfo(bucket, object)
|
2016-07-28 00:11:15 -04:00
|
|
|
if err != nil {
|
|
|
|
errorIf(err, "Unable to fetch object info for \"%s\"", path.Join(bucket, object))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-09-29 01:46:19 -04:00
|
|
|
// Notify object created event.
|
|
|
|
eventNotify(eventData{
|
|
|
|
Type: ObjectCreatedPut,
|
|
|
|
Bucket: bucket,
|
|
|
|
ObjInfo: objInfo,
|
|
|
|
ReqParams: map[string]string{
|
|
|
|
"sourceIPAddress": r.RemoteAddr,
|
|
|
|
},
|
|
|
|
})
|
2016-03-31 09:57:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Download - file download handler.
|
2016-04-12 15:45:15 -04:00
|
|
|
func (web *webAPIHandlers) Download(w http.ResponseWriter, r *http.Request) {
|
2016-03-31 09:57:29 -04:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
bucket := vars["bucket"]
|
|
|
|
object := vars["object"]
|
2016-07-14 17:59:20 -04:00
|
|
|
tokenStr := r.URL.Query().Get("token")
|
2016-03-31 09:57:29 -04:00
|
|
|
|
2016-10-05 13:18:55 -04:00
|
|
|
jwt, err := newJWT(defaultJWTExpiry) // Expiry set to 24Hrs.
|
2016-07-12 00:57:40 -04:00
|
|
|
if err != nil {
|
|
|
|
errorIf(err, "error in getting new JWT")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-07-14 17:59:20 -04:00
|
|
|
token, e := jwtgo.Parse(tokenStr, func(token *jwtgo.Token) (interface{}, error) {
|
2016-03-31 09:57:29 -04:00
|
|
|
if _, ok := token.Method.(*jwtgo.SigningMethodHMAC); !ok {
|
|
|
|
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
|
|
|
|
}
|
|
|
|
return []byte(jwt.SecretAccessKey), nil
|
|
|
|
})
|
2016-07-14 17:59:20 -04:00
|
|
|
if e != nil || !token.Valid {
|
2016-03-31 09:57:29 -04:00
|
|
|
writeWebErrorResponse(w, errInvalidToken)
|
|
|
|
return
|
|
|
|
}
|
2016-04-08 02:44:08 -04:00
|
|
|
// Add content disposition.
|
2016-08-17 16:26:08 -04:00
|
|
|
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", path.Base(object)))
|
2016-03-31 09:57:29 -04:00
|
|
|
|
2016-07-31 17:11:14 -04:00
|
|
|
objectAPI := web.ObjectAPI()
|
|
|
|
if objectAPI == nil {
|
2016-09-26 17:28:35 -04:00
|
|
|
writeWebErrorResponse(w, errors.New("Server not initialized"))
|
2016-07-31 17:11:14 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
objInfo, err := objectAPI.GetObjectInfo(bucket, object)
|
fs: Break fs package to top-level and introduce ObjectAPI interface.
ObjectAPI interface brings in changes needed for XL ObjectAPI layer.
The new interface for any ObjectAPI layer is as below
```
// ObjectAPI interface.
type ObjectAPI interface {
// Bucket resource API.
DeleteBucket(bucket string) *probe.Error
ListBuckets() ([]BucketInfo, *probe.Error)
MakeBucket(bucket string) *probe.Error
GetBucketInfo(bucket string) (BucketInfo, *probe.Error)
// Bucket query API.
ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsResult, *probe.Error)
ListMultipartUploads(bucket string, resources BucketMultipartResourcesMetadata) (BucketMultipartResourcesMetadata, *probe.Error)
// Object resource API.
GetObject(bucket, object string, startOffset int64) (io.ReadCloser, *probe.Error)
GetObjectInfo(bucket, object string) (ObjectInfo, *probe.Error)
PutObject(bucket string, object string, size int64, data io.Reader, metadata map[string]string) (ObjectInfo, *probe.Error)
DeleteObject(bucket, object string) *probe.Error
// Object query API.
NewMultipartUpload(bucket, object string) (string, *probe.Error)
PutObjectPart(bucket, object, uploadID string, partID int, size int64, data io.Reader, md5Hex string) (string, *probe.Error)
ListObjectParts(bucket, object string, resources ObjectResourcesMetadata) (ObjectResourcesMetadata, *probe.Error)
CompleteMultipartUpload(bucket string, object string, uploadID string, parts []CompletePart) (ObjectInfo, *probe.Error)
AbortMultipartUpload(bucket, object, uploadID string) *probe.Error
}
```
2016-03-30 19:15:28 -04:00
|
|
|
if err != nil {
|
2016-04-29 17:24:10 -04:00
|
|
|
writeWebErrorResponse(w, err)
|
fs: Break fs package to top-level and introduce ObjectAPI interface.
ObjectAPI interface brings in changes needed for XL ObjectAPI layer.
The new interface for any ObjectAPI layer is as below
```
// ObjectAPI interface.
type ObjectAPI interface {
// Bucket resource API.
DeleteBucket(bucket string) *probe.Error
ListBuckets() ([]BucketInfo, *probe.Error)
MakeBucket(bucket string) *probe.Error
GetBucketInfo(bucket string) (BucketInfo, *probe.Error)
// Bucket query API.
ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsResult, *probe.Error)
ListMultipartUploads(bucket string, resources BucketMultipartResourcesMetadata) (BucketMultipartResourcesMetadata, *probe.Error)
// Object resource API.
GetObject(bucket, object string, startOffset int64) (io.ReadCloser, *probe.Error)
GetObjectInfo(bucket, object string) (ObjectInfo, *probe.Error)
PutObject(bucket string, object string, size int64, data io.Reader, metadata map[string]string) (ObjectInfo, *probe.Error)
DeleteObject(bucket, object string) *probe.Error
// Object query API.
NewMultipartUpload(bucket, object string) (string, *probe.Error)
PutObjectPart(bucket, object, uploadID string, partID int, size int64, data io.Reader, md5Hex string) (string, *probe.Error)
ListObjectParts(bucket, object string, resources ObjectResourcesMetadata) (ObjectResourcesMetadata, *probe.Error)
CompleteMultipartUpload(bucket string, object string, uploadID string, parts []CompletePart) (ObjectInfo, *probe.Error)
AbortMultipartUpload(bucket, object, uploadID string) *probe.Error
}
```
2016-03-30 19:15:28 -04:00
|
|
|
return
|
|
|
|
}
|
2016-05-28 18:13:15 -04:00
|
|
|
offset := int64(0)
|
2016-07-31 17:11:14 -04:00
|
|
|
err = objectAPI.GetObject(bucket, object, offset, objInfo.Size, w)
|
2016-05-28 18:13:15 -04:00
|
|
|
if err != nil {
|
fs: Break fs package to top-level and introduce ObjectAPI interface.
ObjectAPI interface brings in changes needed for XL ObjectAPI layer.
The new interface for any ObjectAPI layer is as below
```
// ObjectAPI interface.
type ObjectAPI interface {
// Bucket resource API.
DeleteBucket(bucket string) *probe.Error
ListBuckets() ([]BucketInfo, *probe.Error)
MakeBucket(bucket string) *probe.Error
GetBucketInfo(bucket string) (BucketInfo, *probe.Error)
// Bucket query API.
ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsResult, *probe.Error)
ListMultipartUploads(bucket string, resources BucketMultipartResourcesMetadata) (BucketMultipartResourcesMetadata, *probe.Error)
// Object resource API.
GetObject(bucket, object string, startOffset int64) (io.ReadCloser, *probe.Error)
GetObjectInfo(bucket, object string) (ObjectInfo, *probe.Error)
PutObject(bucket string, object string, size int64, data io.Reader, metadata map[string]string) (ObjectInfo, *probe.Error)
DeleteObject(bucket, object string) *probe.Error
// Object query API.
NewMultipartUpload(bucket, object string) (string, *probe.Error)
PutObjectPart(bucket, object, uploadID string, partID int, size int64, data io.Reader, md5Hex string) (string, *probe.Error)
ListObjectParts(bucket, object string, resources ObjectResourcesMetadata) (ObjectResourcesMetadata, *probe.Error)
CompleteMultipartUpload(bucket string, object string, uploadID string, parts []CompletePart) (ObjectInfo, *probe.Error)
AbortMultipartUpload(bucket, object, uploadID string) *probe.Error
}
```
2016-03-30 19:15:28 -04:00
|
|
|
/// No need to print error, response writer already written to.
|
|
|
|
return
|
2016-03-31 09:57:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// writeWebErrorResponse - set HTTP status code and write error description to the body.
|
|
|
|
func writeWebErrorResponse(w http.ResponseWriter, err error) {
|
2016-04-08 02:44:08 -04:00
|
|
|
// Handle invalid token as a special case.
|
2016-03-31 09:57:29 -04:00
|
|
|
if err == errInvalidToken {
|
|
|
|
w.WriteHeader(http.StatusForbidden)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
2016-04-08 02:44:08 -04:00
|
|
|
// Convert error type to api error code.
|
|
|
|
var apiErrCode APIErrorCode
|
2016-03-31 09:57:29 -04:00
|
|
|
switch err.(type) {
|
2016-04-19 05:42:10 -04:00
|
|
|
case StorageFull:
|
|
|
|
apiErrCode = ErrStorageFull
|
fs: Break fs package to top-level and introduce ObjectAPI interface.
ObjectAPI interface brings in changes needed for XL ObjectAPI layer.
The new interface for any ObjectAPI layer is as below
```
// ObjectAPI interface.
type ObjectAPI interface {
// Bucket resource API.
DeleteBucket(bucket string) *probe.Error
ListBuckets() ([]BucketInfo, *probe.Error)
MakeBucket(bucket string) *probe.Error
GetBucketInfo(bucket string) (BucketInfo, *probe.Error)
// Bucket query API.
ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsResult, *probe.Error)
ListMultipartUploads(bucket string, resources BucketMultipartResourcesMetadata) (BucketMultipartResourcesMetadata, *probe.Error)
// Object resource API.
GetObject(bucket, object string, startOffset int64) (io.ReadCloser, *probe.Error)
GetObjectInfo(bucket, object string) (ObjectInfo, *probe.Error)
PutObject(bucket string, object string, size int64, data io.Reader, metadata map[string]string) (ObjectInfo, *probe.Error)
DeleteObject(bucket, object string) *probe.Error
// Object query API.
NewMultipartUpload(bucket, object string) (string, *probe.Error)
PutObjectPart(bucket, object, uploadID string, partID int, size int64, data io.Reader, md5Hex string) (string, *probe.Error)
ListObjectParts(bucket, object string, resources ObjectResourcesMetadata) (ObjectResourcesMetadata, *probe.Error)
CompleteMultipartUpload(bucket string, object string, uploadID string, parts []CompletePart) (ObjectInfo, *probe.Error)
AbortMultipartUpload(bucket, object, uploadID string) *probe.Error
}
```
2016-03-30 19:15:28 -04:00
|
|
|
case BucketNotFound:
|
2016-04-08 02:44:08 -04:00
|
|
|
apiErrCode = ErrNoSuchBucket
|
fs: Break fs package to top-level and introduce ObjectAPI interface.
ObjectAPI interface brings in changes needed for XL ObjectAPI layer.
The new interface for any ObjectAPI layer is as below
```
// ObjectAPI interface.
type ObjectAPI interface {
// Bucket resource API.
DeleteBucket(bucket string) *probe.Error
ListBuckets() ([]BucketInfo, *probe.Error)
MakeBucket(bucket string) *probe.Error
GetBucketInfo(bucket string) (BucketInfo, *probe.Error)
// Bucket query API.
ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsResult, *probe.Error)
ListMultipartUploads(bucket string, resources BucketMultipartResourcesMetadata) (BucketMultipartResourcesMetadata, *probe.Error)
// Object resource API.
GetObject(bucket, object string, startOffset int64) (io.ReadCloser, *probe.Error)
GetObjectInfo(bucket, object string) (ObjectInfo, *probe.Error)
PutObject(bucket string, object string, size int64, data io.Reader, metadata map[string]string) (ObjectInfo, *probe.Error)
DeleteObject(bucket, object string) *probe.Error
// Object query API.
NewMultipartUpload(bucket, object string) (string, *probe.Error)
PutObjectPart(bucket, object, uploadID string, partID int, size int64, data io.Reader, md5Hex string) (string, *probe.Error)
ListObjectParts(bucket, object string, resources ObjectResourcesMetadata) (ObjectResourcesMetadata, *probe.Error)
CompleteMultipartUpload(bucket string, object string, uploadID string, parts []CompletePart) (ObjectInfo, *probe.Error)
AbortMultipartUpload(bucket, object, uploadID string) *probe.Error
}
```
2016-03-30 19:15:28 -04:00
|
|
|
case BucketNameInvalid:
|
2016-04-08 02:44:08 -04:00
|
|
|
apiErrCode = ErrInvalidBucketName
|
fs: Break fs package to top-level and introduce ObjectAPI interface.
ObjectAPI interface brings in changes needed for XL ObjectAPI layer.
The new interface for any ObjectAPI layer is as below
```
// ObjectAPI interface.
type ObjectAPI interface {
// Bucket resource API.
DeleteBucket(bucket string) *probe.Error
ListBuckets() ([]BucketInfo, *probe.Error)
MakeBucket(bucket string) *probe.Error
GetBucketInfo(bucket string) (BucketInfo, *probe.Error)
// Bucket query API.
ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsResult, *probe.Error)
ListMultipartUploads(bucket string, resources BucketMultipartResourcesMetadata) (BucketMultipartResourcesMetadata, *probe.Error)
// Object resource API.
GetObject(bucket, object string, startOffset int64) (io.ReadCloser, *probe.Error)
GetObjectInfo(bucket, object string) (ObjectInfo, *probe.Error)
PutObject(bucket string, object string, size int64, data io.Reader, metadata map[string]string) (ObjectInfo, *probe.Error)
DeleteObject(bucket, object string) *probe.Error
// Object query API.
NewMultipartUpload(bucket, object string) (string, *probe.Error)
PutObjectPart(bucket, object, uploadID string, partID int, size int64, data io.Reader, md5Hex string) (string, *probe.Error)
ListObjectParts(bucket, object string, resources ObjectResourcesMetadata) (ObjectResourcesMetadata, *probe.Error)
CompleteMultipartUpload(bucket string, object string, uploadID string, parts []CompletePart) (ObjectInfo, *probe.Error)
AbortMultipartUpload(bucket, object, uploadID string) *probe.Error
}
```
2016-03-30 19:15:28 -04:00
|
|
|
case BadDigest:
|
2016-04-08 02:44:08 -04:00
|
|
|
apiErrCode = ErrBadDigest
|
fs: Break fs package to top-level and introduce ObjectAPI interface.
ObjectAPI interface brings in changes needed for XL ObjectAPI layer.
The new interface for any ObjectAPI layer is as below
```
// ObjectAPI interface.
type ObjectAPI interface {
// Bucket resource API.
DeleteBucket(bucket string) *probe.Error
ListBuckets() ([]BucketInfo, *probe.Error)
MakeBucket(bucket string) *probe.Error
GetBucketInfo(bucket string) (BucketInfo, *probe.Error)
// Bucket query API.
ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsResult, *probe.Error)
ListMultipartUploads(bucket string, resources BucketMultipartResourcesMetadata) (BucketMultipartResourcesMetadata, *probe.Error)
// Object resource API.
GetObject(bucket, object string, startOffset int64) (io.ReadCloser, *probe.Error)
GetObjectInfo(bucket, object string) (ObjectInfo, *probe.Error)
PutObject(bucket string, object string, size int64, data io.Reader, metadata map[string]string) (ObjectInfo, *probe.Error)
DeleteObject(bucket, object string) *probe.Error
// Object query API.
NewMultipartUpload(bucket, object string) (string, *probe.Error)
PutObjectPart(bucket, object, uploadID string, partID int, size int64, data io.Reader, md5Hex string) (string, *probe.Error)
ListObjectParts(bucket, object string, resources ObjectResourcesMetadata) (ObjectResourcesMetadata, *probe.Error)
CompleteMultipartUpload(bucket string, object string, uploadID string, parts []CompletePart) (ObjectInfo, *probe.Error)
AbortMultipartUpload(bucket, object, uploadID string) *probe.Error
}
```
2016-03-30 19:15:28 -04:00
|
|
|
case IncompleteBody:
|
2016-04-08 02:44:08 -04:00
|
|
|
apiErrCode = ErrIncompleteBody
|
2016-05-08 15:36:16 -04:00
|
|
|
case ObjectExistsAsDirectory:
|
|
|
|
apiErrCode = ErrObjectExistsAsDirectory
|
fs: Break fs package to top-level and introduce ObjectAPI interface.
ObjectAPI interface brings in changes needed for XL ObjectAPI layer.
The new interface for any ObjectAPI layer is as below
```
// ObjectAPI interface.
type ObjectAPI interface {
// Bucket resource API.
DeleteBucket(bucket string) *probe.Error
ListBuckets() ([]BucketInfo, *probe.Error)
MakeBucket(bucket string) *probe.Error
GetBucketInfo(bucket string) (BucketInfo, *probe.Error)
// Bucket query API.
ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsResult, *probe.Error)
ListMultipartUploads(bucket string, resources BucketMultipartResourcesMetadata) (BucketMultipartResourcesMetadata, *probe.Error)
// Object resource API.
GetObject(bucket, object string, startOffset int64) (io.ReadCloser, *probe.Error)
GetObjectInfo(bucket, object string) (ObjectInfo, *probe.Error)
PutObject(bucket string, object string, size int64, data io.Reader, metadata map[string]string) (ObjectInfo, *probe.Error)
DeleteObject(bucket, object string) *probe.Error
// Object query API.
NewMultipartUpload(bucket, object string) (string, *probe.Error)
PutObjectPart(bucket, object, uploadID string, partID int, size int64, data io.Reader, md5Hex string) (string, *probe.Error)
ListObjectParts(bucket, object string, resources ObjectResourcesMetadata) (ObjectResourcesMetadata, *probe.Error)
CompleteMultipartUpload(bucket string, object string, uploadID string, parts []CompletePart) (ObjectInfo, *probe.Error)
AbortMultipartUpload(bucket, object, uploadID string) *probe.Error
}
```
2016-03-30 19:15:28 -04:00
|
|
|
case ObjectNotFound:
|
2016-04-08 02:44:08 -04:00
|
|
|
apiErrCode = ErrNoSuchKey
|
fs: Break fs package to top-level and introduce ObjectAPI interface.
ObjectAPI interface brings in changes needed for XL ObjectAPI layer.
The new interface for any ObjectAPI layer is as below
```
// ObjectAPI interface.
type ObjectAPI interface {
// Bucket resource API.
DeleteBucket(bucket string) *probe.Error
ListBuckets() ([]BucketInfo, *probe.Error)
MakeBucket(bucket string) *probe.Error
GetBucketInfo(bucket string) (BucketInfo, *probe.Error)
// Bucket query API.
ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsResult, *probe.Error)
ListMultipartUploads(bucket string, resources BucketMultipartResourcesMetadata) (BucketMultipartResourcesMetadata, *probe.Error)
// Object resource API.
GetObject(bucket, object string, startOffset int64) (io.ReadCloser, *probe.Error)
GetObjectInfo(bucket, object string) (ObjectInfo, *probe.Error)
PutObject(bucket string, object string, size int64, data io.Reader, metadata map[string]string) (ObjectInfo, *probe.Error)
DeleteObject(bucket, object string) *probe.Error
// Object query API.
NewMultipartUpload(bucket, object string) (string, *probe.Error)
PutObjectPart(bucket, object, uploadID string, partID int, size int64, data io.Reader, md5Hex string) (string, *probe.Error)
ListObjectParts(bucket, object string, resources ObjectResourcesMetadata) (ObjectResourcesMetadata, *probe.Error)
CompleteMultipartUpload(bucket string, object string, uploadID string, parts []CompletePart) (ObjectInfo, *probe.Error)
AbortMultipartUpload(bucket, object, uploadID string) *probe.Error
}
```
2016-03-30 19:15:28 -04:00
|
|
|
case ObjectNameInvalid:
|
2016-04-08 02:44:08 -04:00
|
|
|
apiErrCode = ErrNoSuchKey
|
2016-05-05 23:24:29 -04:00
|
|
|
case InsufficientWriteQuorum:
|
|
|
|
apiErrCode = ErrWriteQuorum
|
|
|
|
case InsufficientReadQuorum:
|
|
|
|
apiErrCode = ErrReadQuorum
|
2016-03-31 09:57:29 -04:00
|
|
|
default:
|
2016-04-08 02:44:08 -04:00
|
|
|
apiErrCode = ErrInternalError
|
2016-03-31 09:57:29 -04:00
|
|
|
}
|
2016-04-08 02:44:08 -04:00
|
|
|
apiErr := getAPIError(apiErrCode)
|
|
|
|
w.WriteHeader(apiErr.HTTPStatusCode)
|
|
|
|
w.Write([]byte(apiErr.Description))
|
2016-03-31 09:57:29 -04:00
|
|
|
}
|
2016-08-30 13:04:50 -04:00
|
|
|
|
|
|
|
// GetBucketPolicyArgs - get bucket policy args.
|
|
|
|
type GetBucketPolicyArgs struct {
|
|
|
|
BucketName string `json:"bucketName"`
|
|
|
|
Prefix string `json:"prefix"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBucketPolicyRep - get bucket policy reply.
|
|
|
|
type GetBucketPolicyRep struct {
|
|
|
|
UIVersion string `json:"uiVersion"`
|
|
|
|
Policy policy.BucketPolicy `json:"policy"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func readBucketAccessPolicy(objAPI ObjectLayer, bucketName string) (policy.BucketAccessPolicy, error) {
|
|
|
|
bucketPolicyReader, err := readBucketPolicyJSON(bucketName, objAPI)
|
|
|
|
if err != nil {
|
|
|
|
if _, ok := err.(BucketPolicyNotFound); ok {
|
2016-09-23 01:27:21 -04:00
|
|
|
return policy.BucketAccessPolicy{Version: "2012-10-17"}, nil
|
2016-08-30 13:04:50 -04:00
|
|
|
}
|
|
|
|
return policy.BucketAccessPolicy{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
bucketPolicyBuf, err := ioutil.ReadAll(bucketPolicyReader)
|
|
|
|
if err != nil {
|
|
|
|
return policy.BucketAccessPolicy{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
policyInfo := policy.BucketAccessPolicy{}
|
|
|
|
err = json.Unmarshal(bucketPolicyBuf, &policyInfo)
|
|
|
|
if err != nil {
|
|
|
|
return policy.BucketAccessPolicy{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return policyInfo, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBucketPolicy - get bucket policy.
|
|
|
|
func (web *webAPIHandlers) GetBucketPolicy(r *http.Request, args *GetBucketPolicyArgs, reply *GetBucketPolicyRep) error {
|
|
|
|
if !isJWTReqAuthenticated(r) {
|
|
|
|
return &json2.Error{Message: "Unauthorized request"}
|
|
|
|
}
|
|
|
|
|
2016-08-30 22:22:27 -04:00
|
|
|
objectAPI := web.ObjectAPI()
|
|
|
|
if objectAPI == nil {
|
2016-09-26 17:28:35 -04:00
|
|
|
return &json2.Error{Message: "Server not initialized"}
|
2016-08-30 22:22:27 -04:00
|
|
|
}
|
|
|
|
policyInfo, err := readBucketAccessPolicy(objectAPI, args.BucketName)
|
2016-08-30 13:04:50 -04:00
|
|
|
if err != nil {
|
|
|
|
return &json2.Error{Message: err.Error()}
|
|
|
|
}
|
|
|
|
|
|
|
|
reply.UIVersion = miniobrowser.UIVersion
|
2016-09-26 00:53:19 -04:00
|
|
|
reply.Policy = policy.GetPolicy(policyInfo.Statements, args.BucketName, args.Prefix)
|
2016-08-30 13:04:50 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-26 00:53:19 -04:00
|
|
|
// ListAllBucketPoliciesArgs - get all bucket policies.
|
|
|
|
type ListAllBucketPoliciesArgs struct {
|
2016-09-23 02:06:45 -04:00
|
|
|
BucketName string `json:"bucketName"`
|
|
|
|
}
|
|
|
|
|
2016-09-26 00:53:19 -04:00
|
|
|
// Collection of canned bucket policy at a given prefix.
|
|
|
|
type bucketAccessPolicy struct {
|
|
|
|
Prefix string `json:"prefix"`
|
|
|
|
Policy policy.BucketPolicy `json:"policy"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListAllBucketPoliciesRep - get all bucket policy reply.
|
|
|
|
type ListAllBucketPoliciesRep struct {
|
|
|
|
UIVersion string `json:"uiVersion"`
|
|
|
|
Policies []bucketAccessPolicy `json:"policies"`
|
2016-09-23 02:06:45 -04:00
|
|
|
}
|
|
|
|
|
2016-09-26 00:53:19 -04:00
|
|
|
// GetllBucketPolicy - get all bucket policy.
|
|
|
|
func (web *webAPIHandlers) ListAllBucketPolicies(r *http.Request, args *ListAllBucketPoliciesArgs, reply *ListAllBucketPoliciesRep) error {
|
2016-09-23 02:06:45 -04:00
|
|
|
if !isJWTReqAuthenticated(r) {
|
|
|
|
return &json2.Error{Message: "Unauthorized request"}
|
|
|
|
}
|
|
|
|
|
|
|
|
objectAPI := web.ObjectAPI()
|
|
|
|
if objectAPI == nil {
|
|
|
|
return &json2.Error{Message: "Server not initialized"}
|
|
|
|
}
|
|
|
|
policyInfo, err := readBucketAccessPolicy(objectAPI, args.BucketName)
|
|
|
|
if err != nil {
|
|
|
|
return &json2.Error{Message: err.Error()}
|
|
|
|
}
|
|
|
|
|
|
|
|
reply.UIVersion = miniobrowser.UIVersion
|
2016-09-26 00:53:19 -04:00
|
|
|
for prefix, policy := range policy.GetPolicies(policyInfo.Statements, args.BucketName) {
|
|
|
|
reply.Policies = append(reply.Policies, bucketAccessPolicy{
|
|
|
|
Prefix: prefix,
|
|
|
|
Policy: policy,
|
|
|
|
})
|
|
|
|
}
|
2016-09-23 02:06:45 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-30 13:04:50 -04:00
|
|
|
// SetBucketPolicyArgs - set bucket policy args.
|
|
|
|
type SetBucketPolicyArgs struct {
|
|
|
|
BucketName string `json:"bucketName"`
|
|
|
|
Prefix string `json:"prefix"`
|
|
|
|
Policy string `json:"policy"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetBucketPolicy - set bucket policy.
|
|
|
|
func (web *webAPIHandlers) SetBucketPolicy(r *http.Request, args *SetBucketPolicyArgs, reply *WebGenericRep) error {
|
|
|
|
if !isJWTReqAuthenticated(r) {
|
|
|
|
return &json2.Error{Message: "Unauthorized request"}
|
|
|
|
}
|
2016-09-22 19:35:12 -04:00
|
|
|
objectAPI := web.ObjectAPI()
|
|
|
|
if objectAPI == nil {
|
2016-09-26 17:28:35 -04:00
|
|
|
return &json2.Error{Message: "Server not initialized"}
|
2016-09-22 19:35:12 -04:00
|
|
|
}
|
2016-08-30 13:04:50 -04:00
|
|
|
|
2016-09-23 01:27:21 -04:00
|
|
|
bucketP := policy.BucketPolicy(args.Policy)
|
|
|
|
if !bucketP.IsValidBucketPolicy() {
|
2016-08-30 13:04:50 -04:00
|
|
|
return &json2.Error{Message: "Invalid policy " + args.Policy}
|
|
|
|
}
|
|
|
|
|
2016-08-30 22:22:27 -04:00
|
|
|
policyInfo, err := readBucketAccessPolicy(objectAPI, args.BucketName)
|
2016-08-30 13:04:50 -04:00
|
|
|
if err != nil {
|
|
|
|
return &json2.Error{Message: err.Error()}
|
|
|
|
}
|
2016-09-23 01:27:21 -04:00
|
|
|
policyInfo.Statements = policy.SetPolicy(policyInfo.Statements, bucketP, args.BucketName, args.Prefix)
|
2016-09-26 06:11:22 -04:00
|
|
|
if len(policyInfo.Statements) == 0 {
|
2016-10-13 12:19:04 -04:00
|
|
|
if err = persistAndNotifyBucketPolicyChange(args.BucketName, policyChange{true, nil}, objectAPI); err != nil {
|
2016-09-26 06:11:22 -04:00
|
|
|
return &json2.Error{Message: err.Error()}
|
|
|
|
}
|
2016-10-23 15:32:35 -04:00
|
|
|
reply.UIVersion = miniobrowser.UIVersion
|
2016-09-26 06:11:22 -04:00
|
|
|
return nil
|
|
|
|
}
|
2016-08-30 13:04:50 -04:00
|
|
|
data, err := json.Marshal(policyInfo)
|
|
|
|
if err != nil {
|
|
|
|
return &json2.Error{Message: err.Error()}
|
|
|
|
}
|
|
|
|
|
2016-09-23 01:27:21 -04:00
|
|
|
// Parse bucket policy.
|
|
|
|
var policy = &bucketPolicy{}
|
|
|
|
err = parseBucketPolicy(bytes.NewReader(data), policy)
|
|
|
|
if err != nil {
|
|
|
|
errorIf(err, "Unable to parse bucket policy.")
|
|
|
|
return &json2.Error{Message: err.Error()}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse check bucket policy.
|
|
|
|
if s3Error := checkBucketPolicyResources(args.BucketName, policy); s3Error != ErrNone {
|
|
|
|
return &json2.Error{Message: getAPIError(s3Error).Description}
|
|
|
|
}
|
|
|
|
|
2016-10-13 12:19:04 -04:00
|
|
|
// TODO: update policy statements according to bucket name,
|
|
|
|
// prefix and policy arguments.
|
|
|
|
if err := persistAndNotifyBucketPolicyChange(args.BucketName, policyChange{false, policy}, objectAPI); err != nil {
|
2016-08-30 13:04:50 -04:00
|
|
|
return &json2.Error{Message: err.Error()}
|
|
|
|
}
|
|
|
|
reply.UIVersion = miniobrowser.UIVersion
|
|
|
|
return nil
|
|
|
|
}
|
2016-09-23 04:24:49 -04:00
|
|
|
|
|
|
|
// PresignedGetArgs - presigned-get API args.
|
|
|
|
type PresignedGetArgs struct {
|
|
|
|
// Host header required for signed headers.
|
|
|
|
HostName string `json:"host"`
|
|
|
|
|
|
|
|
// Bucket name of the object to be presigned.
|
|
|
|
BucketName string `json:"bucket"`
|
|
|
|
|
|
|
|
// Object name to be presigned.
|
|
|
|
ObjectName string `json:"object"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// PresignedGetRep - presigned-get URL reply.
|
|
|
|
type PresignedGetRep struct {
|
2016-10-23 15:32:35 -04:00
|
|
|
UIVersion string `json:"uiVersion"`
|
2016-09-23 04:24:49 -04:00
|
|
|
// Presigned URL of the object.
|
|
|
|
URL string `json:"url"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// PresignedGET - returns presigned-Get url.
|
|
|
|
func (web *webAPIHandlers) PresignedGet(r *http.Request, args *PresignedGetArgs, reply *PresignedGetRep) error {
|
|
|
|
if !isJWTReqAuthenticated(r) {
|
|
|
|
return &json2.Error{Message: "Unauthorized request"}
|
|
|
|
}
|
|
|
|
if args.BucketName == "" || args.ObjectName == "" {
|
|
|
|
return &json2.Error{Message: "Required arguments: Host, Bucket, Object"}
|
|
|
|
}
|
2016-10-23 15:32:35 -04:00
|
|
|
reply.UIVersion = miniobrowser.UIVersion
|
2016-09-23 04:24:49 -04:00
|
|
|
reply.URL = presignedGet(args.HostName, args.BucketName, args.ObjectName)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns presigned url for GET method.
|
|
|
|
func presignedGet(host, bucket, object string) string {
|
|
|
|
cred := serverConfig.GetCredential()
|
|
|
|
region := serverConfig.GetRegion()
|
|
|
|
|
|
|
|
accessKey := cred.AccessKeyID
|
|
|
|
secretKey := cred.SecretAccessKey
|
|
|
|
|
|
|
|
date := time.Now().UTC()
|
|
|
|
dateStr := date.Format("20060102T150405Z")
|
|
|
|
credential := fmt.Sprintf("%s/%s", accessKey, getScope(date, region))
|
|
|
|
|
|
|
|
query := strings.Join([]string{
|
|
|
|
"X-Amz-Algorithm=" + signV4Algorithm,
|
|
|
|
"X-Amz-Credential=" + strings.Replace(credential, "/", "%2F", -1),
|
|
|
|
"X-Amz-Date=" + dateStr,
|
|
|
|
"X-Amz-Expires=" + "604800", // Default set to be expire in 7days.
|
|
|
|
"X-Amz-SignedHeaders=host",
|
|
|
|
}, "&")
|
|
|
|
|
|
|
|
path := "/" + path.Join(bucket, object)
|
|
|
|
|
|
|
|
// Headers are empty, since "host" is the only header required to be signed for Presigned URLs.
|
|
|
|
var extractedSignedHeaders http.Header
|
|
|
|
|
|
|
|
canonicalRequest := getCanonicalRequest(extractedSignedHeaders, unsignedPayload, query, path, "GET", host)
|
|
|
|
stringToSign := getStringToSign(canonicalRequest, date, region)
|
|
|
|
signingKey := getSigningKey(secretKey, date, region)
|
|
|
|
signature := getSignature(signingKey, stringToSign)
|
|
|
|
|
|
|
|
// Construct the final presigned URL.
|
|
|
|
return host + path + "?" + query + "&" + "X-Amz-Signature=" + signature
|
|
|
|
}
|