2015-07-05 19:55:55 -04:00
|
|
|
/*
|
2015-07-24 20:51:40 -04:00
|
|
|
* Minio Cloud Storage, (C) 2015 Minio, Inc.
|
2015-07-05 19:55:55 -04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2015-09-19 03:52:01 -04:00
|
|
|
package main
|
2015-07-05 19:55:55 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-10-08 22:56:41 -04:00
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
2015-07-05 19:55:55 -04:00
|
|
|
"net/http"
|
2015-10-08 22:56:41 -04:00
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2015-07-05 19:55:55 -04:00
|
|
|
|
2015-08-22 21:34:00 -04:00
|
|
|
"github.com/gorilla/rpc/v2/json"
|
2015-08-03 19:17:21 -04:00
|
|
|
"github.com/minio/minio/pkg/probe"
|
2015-07-05 19:55:55 -04:00
|
|
|
)
|
|
|
|
|
2015-09-19 03:52:01 -04:00
|
|
|
// rpcOperation RPC operation
|
|
|
|
type rpcOperation struct {
|
2015-07-05 19:55:55 -04:00
|
|
|
Method string
|
|
|
|
Request interface{}
|
|
|
|
}
|
|
|
|
|
2015-09-19 03:52:01 -04:00
|
|
|
// rpcRequest rpc client request
|
|
|
|
type rpcRequest struct {
|
2015-07-05 19:55:55 -04:00
|
|
|
req *http.Request
|
|
|
|
transport http.RoundTripper
|
|
|
|
}
|
|
|
|
|
2015-09-19 03:52:01 -04:00
|
|
|
// newRPCRequest initiate a new client RPC request
|
2015-10-08 22:56:41 -04:00
|
|
|
func newRPCRequest(config *AuthConfig, url string, op rpcOperation, transport http.RoundTripper) (*rpcRequest, *probe.Error) {
|
|
|
|
t := time.Now().UTC()
|
2015-07-05 19:55:55 -04:00
|
|
|
params, err := json.EncodeClientRequest(op.Method, op.Request)
|
|
|
|
if err != nil {
|
2015-08-08 02:47:22 -04:00
|
|
|
return nil, probe.NewError(err)
|
2015-07-05 19:55:55 -04:00
|
|
|
}
|
2015-10-08 22:56:41 -04:00
|
|
|
|
|
|
|
body := bytes.NewReader(params)
|
|
|
|
req, err := http.NewRequest("POST", url, body)
|
2015-07-05 19:55:55 -04:00
|
|
|
if err != nil {
|
2015-08-08 02:47:22 -04:00
|
|
|
return nil, probe.NewError(err)
|
2015-07-05 19:55:55 -04:00
|
|
|
}
|
2015-10-08 22:56:41 -04:00
|
|
|
req.Header.Set("x-minio-date", t.Format(iso8601Format))
|
|
|
|
|
|
|
|
// save for subsequent use
|
|
|
|
hash := func() string {
|
|
|
|
sum256Bytes, _ := sum256Reader(body)
|
|
|
|
return hex.EncodeToString(sum256Bytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
hashedPayload := hash()
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
req.Header.Set("x-amz-content-sha256", hashedPayload)
|
|
|
|
|
|
|
|
var headers []string
|
|
|
|
vals := make(map[string][]string)
|
|
|
|
for k, vv := range req.Header {
|
|
|
|
if _, ok := ignoredHeaders[http.CanonicalHeaderKey(k)]; ok {
|
|
|
|
continue // ignored header
|
|
|
|
}
|
|
|
|
headers = append(headers, strings.ToLower(k))
|
|
|
|
vals[strings.ToLower(k)] = vv
|
|
|
|
}
|
|
|
|
headers = append(headers, "host")
|
|
|
|
sort.Strings(headers)
|
|
|
|
|
|
|
|
var canonicalHeaders bytes.Buffer
|
|
|
|
for _, k := range headers {
|
|
|
|
canonicalHeaders.WriteString(k)
|
|
|
|
canonicalHeaders.WriteByte(':')
|
|
|
|
switch {
|
|
|
|
case k == "host":
|
|
|
|
canonicalHeaders.WriteString(req.URL.Host)
|
|
|
|
fallthrough
|
|
|
|
default:
|
|
|
|
for idx, v := range vals[k] {
|
|
|
|
if idx > 0 {
|
|
|
|
canonicalHeaders.WriteByte(',')
|
|
|
|
}
|
|
|
|
canonicalHeaders.WriteString(v)
|
|
|
|
}
|
|
|
|
canonicalHeaders.WriteByte('\n')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
signedHeaders := strings.Join(headers, ";")
|
|
|
|
|
|
|
|
req.URL.RawQuery = strings.Replace(req.URL.Query().Encode(), "+", "%20", -1)
|
|
|
|
encodedPath := getURLEncodedName(req.URL.Path)
|
|
|
|
// convert any space strings back to "+"
|
|
|
|
encodedPath = strings.Replace(encodedPath, "+", "%20", -1)
|
|
|
|
|
|
|
|
//
|
|
|
|
// canonicalRequest =
|
|
|
|
// <HTTPMethod>\n
|
|
|
|
// <CanonicalURI>\n
|
|
|
|
// <CanonicalQueryString>\n
|
|
|
|
// <CanonicalHeaders>\n
|
|
|
|
// <SignedHeaders>\n
|
|
|
|
// <HashedPayload>
|
|
|
|
//
|
|
|
|
canonicalRequest := strings.Join([]string{
|
|
|
|
req.Method,
|
|
|
|
encodedPath,
|
|
|
|
req.URL.RawQuery,
|
|
|
|
canonicalHeaders.String(),
|
|
|
|
signedHeaders,
|
|
|
|
hashedPayload,
|
|
|
|
}, "\n")
|
|
|
|
|
|
|
|
scope := strings.Join([]string{
|
|
|
|
t.Format(yyyymmdd),
|
|
|
|
"milkyway",
|
|
|
|
"rpc",
|
|
|
|
"rpc_request",
|
|
|
|
}, "/")
|
|
|
|
|
|
|
|
stringToSign := rpcAuthHeaderPrefix + "\n" + t.Format(iso8601Format) + "\n"
|
|
|
|
stringToSign = stringToSign + scope + "\n"
|
|
|
|
stringToSign = stringToSign + hex.EncodeToString(sum256([]byte(canonicalRequest)))
|
|
|
|
|
|
|
|
fmt.Println(config)
|
|
|
|
date := sumHMAC([]byte("MINIO"+config.Users["admin"].SecretAccessKey), []byte(t.Format(yyyymmdd)))
|
|
|
|
region := sumHMAC(date, []byte("milkyway"))
|
|
|
|
service := sumHMAC(region, []byte("rpc"))
|
|
|
|
signingKey := sumHMAC(service, []byte("rpc_request"))
|
|
|
|
|
|
|
|
signature := hex.EncodeToString(sumHMAC(signingKey, []byte(stringToSign)))
|
|
|
|
|
|
|
|
// final Authorization header
|
|
|
|
parts := []string{
|
|
|
|
rpcAuthHeaderPrefix + " Credential=" + config.Users["admin"].AccessKeyID + "/" + scope,
|
|
|
|
"SignedHeaders=" + signedHeaders,
|
|
|
|
"Signature=" + signature,
|
|
|
|
}
|
|
|
|
auth := strings.Join(parts, ", ")
|
|
|
|
req.Header.Set("Authorization", auth)
|
|
|
|
|
2015-09-19 03:52:01 -04:00
|
|
|
rpcReq := &rpcRequest{}
|
2015-07-05 19:55:55 -04:00
|
|
|
rpcReq.req = req
|
|
|
|
if transport == nil {
|
|
|
|
transport = http.DefaultTransport
|
|
|
|
}
|
|
|
|
rpcReq.transport = transport
|
|
|
|
return rpcReq, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do - make a http connection
|
2015-09-19 03:52:01 -04:00
|
|
|
func (r rpcRequest) Do() (*http.Response, *probe.Error) {
|
2015-07-05 19:55:55 -04:00
|
|
|
resp, err := r.transport.RoundTrip(r.req)
|
|
|
|
if err != nil {
|
2015-08-18 22:30:17 -04:00
|
|
|
if err, ok := probe.UnwrapError(err); ok {
|
|
|
|
return nil, err.Trace()
|
2015-08-08 02:47:22 -04:00
|
|
|
}
|
|
|
|
return nil, probe.NewError(err)
|
2015-07-05 19:55:55 -04:00
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
2015-07-07 20:27:34 -04:00
|
|
|
|
|
|
|
// Get - get value of requested header
|
2015-09-19 03:52:01 -04:00
|
|
|
func (r rpcRequest) Get(key string) string {
|
2015-07-07 20:27:34 -04:00
|
|
|
return r.req.Header.Get(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set - set value of a header key
|
2015-09-19 03:52:01 -04:00
|
|
|
func (r *rpcRequest) Set(key, value string) {
|
2015-07-07 20:27:34 -04:00
|
|
|
r.req.Header.Set(key, value)
|
|
|
|
}
|