2015-02-08 05:54:21 -05:00
|
|
|
/*
|
2015-03-19 17:35:50 -04:00
|
|
|
* Minimalist Object Storage, (C) 2014,2015 Minio, Inc.
|
2015-02-08 05:54:21 -05: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-03-18 20:49:33 -04:00
|
|
|
package api
|
2015-01-28 18:45:42 -05:00
|
|
|
|
2015-02-06 05:08:52 -05:00
|
|
|
import (
|
|
|
|
"bytes"
|
2015-03-30 00:41:38 -04:00
|
|
|
"crypto/hmac"
|
|
|
|
"crypto/sha1"
|
|
|
|
"encoding/base64"
|
2015-03-15 22:40:30 -04:00
|
|
|
"errors"
|
2015-02-06 05:08:52 -05:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-03-30 00:41:38 -04:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2015-02-06 05:08:52 -05:00
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2015-03-19 01:49:02 -04:00
|
|
|
"github.com/minio-io/minio/pkg/api/config"
|
2015-02-06 05:08:52 -05:00
|
|
|
)
|
|
|
|
|
2015-03-05 23:08:29 -05:00
|
|
|
// SignRequest - a given http request using HMAC style signatures
|
2015-02-06 05:08:52 -05:00
|
|
|
func SignRequest(user config.User, req *http.Request) {
|
|
|
|
if date := req.Header.Get("Date"); date == "" {
|
|
|
|
req.Header.Set("Date", time.Now().UTC().Format(http.TimeFormat))
|
|
|
|
}
|
|
|
|
hm := hmac.New(sha1.New, []byte(user.SecretKey))
|
|
|
|
ss := getStringToSign(req)
|
|
|
|
io.WriteString(hm, ss)
|
|
|
|
|
|
|
|
authHeader := new(bytes.Buffer)
|
|
|
|
fmt.Fprintf(authHeader, "AWS %s:", user.AccessKey)
|
|
|
|
encoder := base64.NewEncoder(base64.StdEncoding, authHeader)
|
|
|
|
encoder.Write(hm.Sum(nil))
|
|
|
|
encoder.Close()
|
|
|
|
req.Header.Set("Authorization", authHeader.String())
|
|
|
|
}
|
|
|
|
|
2015-03-05 23:08:29 -05:00
|
|
|
// ValidateRequest - an API request by validating its signature using HMAC signatures
|
2015-02-06 05:08:52 -05:00
|
|
|
func ValidateRequest(user config.User, req *http.Request) (bool, error) {
|
2015-02-08 05:35:36 -05:00
|
|
|
// Verify if date headers are set, if not reject the request
|
|
|
|
if req.Header.Get("x-amz-date") == "" {
|
|
|
|
if req.Header.Get("Date") == "" {
|
2015-03-15 22:40:30 -04:00
|
|
|
return false, errors.New("Date should be set")
|
2015-02-08 05:35:36 -05:00
|
|
|
}
|
2015-02-06 05:08:52 -05:00
|
|
|
}
|
|
|
|
hm := hmac.New(sha1.New, []byte(user.SecretKey))
|
|
|
|
ss := getStringToSign(req)
|
|
|
|
io.WriteString(hm, ss)
|
|
|
|
|
|
|
|
authHeader := new(bytes.Buffer)
|
|
|
|
fmt.Fprintf(authHeader, "AWS %s:", user.AccessKey)
|
|
|
|
encoder := base64.NewEncoder(base64.StdEncoding, authHeader)
|
|
|
|
encoder.Write(hm.Sum(nil))
|
|
|
|
encoder.Close()
|
|
|
|
|
2015-02-06 23:42:09 -05:00
|
|
|
// DEBUG
|
2015-02-23 05:11:27 -05:00
|
|
|
// fmt.Println("Request header sent: ", req.Header.Get("Authorization"))
|
|
|
|
// fmt.Println("Header calculated: ", authHeader.String())
|
|
|
|
// fmt.Printf("%q : %x", ss, ss)
|
2015-02-06 05:08:52 -05:00
|
|
|
if req.Header.Get("Authorization") != authHeader.String() {
|
2015-03-15 22:40:30 -04:00
|
|
|
return false, errors.New("Authorization header mismatch")
|
2015-02-06 05:08:52 -05:00
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// From the Amazon docs:
|
2015-01-30 13:58:47 -05:00
|
|
|
//
|
2015-02-06 05:08:52 -05:00
|
|
|
// StringToSign = HTTP-Verb + "\n" +
|
|
|
|
// Content-MD5 + "\n" +
|
|
|
|
// Content-Type + "\n" +
|
|
|
|
// Date + "\n" +
|
|
|
|
// CanonicalizedAmzHeaders +
|
|
|
|
// CanonicalizedResource;
|
|
|
|
func getStringToSign(req *http.Request) string {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
buf.WriteString(req.Method)
|
|
|
|
buf.WriteByte('\n')
|
|
|
|
buf.WriteString(req.Header.Get("Content-MD5"))
|
|
|
|
buf.WriteByte('\n')
|
|
|
|
buf.WriteString(req.Header.Get("Content-Type"))
|
|
|
|
buf.WriteByte('\n')
|
|
|
|
if req.Header.Get("x-amz-date") == "" {
|
|
|
|
buf.WriteString(req.Header.Get("Date"))
|
|
|
|
}
|
|
|
|
buf.WriteByte('\n')
|
|
|
|
writeCanonicalizedAmzHeaders(buf, req)
|
|
|
|
writeCanonicalizedResource(buf, req)
|
2015-03-07 05:49:36 -05:00
|
|
|
|
2015-02-06 05:08:52 -05:00
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2015-02-23 20:44:55 -05:00
|
|
|
// Lower all upper case letters
|
2015-02-06 05:08:52 -05:00
|
|
|
func hasPrefixCaseInsensitive(s, pfx string) bool {
|
|
|
|
if len(pfx) > len(s) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
shead := s[:len(pfx)]
|
|
|
|
if shead == pfx {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
shead = strings.ToLower(shead)
|
|
|
|
return shead == pfx || shead == strings.ToLower(pfx)
|
|
|
|
}
|
|
|
|
|
2015-02-23 20:44:55 -05:00
|
|
|
// Canonicalize amazon special headers, headers starting with 'x-amz-'
|
2015-02-06 05:08:52 -05:00
|
|
|
func writeCanonicalizedAmzHeaders(buf *bytes.Buffer, req *http.Request) {
|
2015-03-05 23:08:29 -05:00
|
|
|
var amzHeaders []string
|
2015-02-06 05:08:52 -05:00
|
|
|
vals := make(map[string][]string)
|
|
|
|
for k, vv := range req.Header {
|
|
|
|
if hasPrefixCaseInsensitive(k, "x-amz-") {
|
|
|
|
lk := strings.ToLower(k)
|
|
|
|
amzHeaders = append(amzHeaders, lk)
|
|
|
|
vals[lk] = vv
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(amzHeaders)
|
|
|
|
for _, k := range amzHeaders {
|
|
|
|
buf.WriteString(k)
|
|
|
|
buf.WriteByte(':')
|
|
|
|
for idx, v := range vals[k] {
|
|
|
|
if idx > 0 {
|
|
|
|
buf.WriteByte(',')
|
|
|
|
}
|
|
|
|
if strings.Contains(v, "\n") {
|
|
|
|
// TODO: "Unfold" long headers that
|
|
|
|
// span multiple lines (as allowed by
|
|
|
|
// RFC 2616, section 4.2) by replacing
|
|
|
|
// the folding white-space (including
|
|
|
|
// new-line) by a single space.
|
|
|
|
buf.WriteString(v)
|
|
|
|
} else {
|
|
|
|
buf.WriteString(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buf.WriteByte('\n')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-23 20:44:55 -05:00
|
|
|
// Resource list must be sorted:
|
2015-03-19 03:24:20 -04:00
|
|
|
var subResList = []string{
|
|
|
|
"acl",
|
|
|
|
"location",
|
|
|
|
"logging",
|
|
|
|
"notification",
|
|
|
|
"partNumber",
|
|
|
|
"policy",
|
|
|
|
"uploadId",
|
|
|
|
"uploads",
|
|
|
|
"response-content-type",
|
|
|
|
"response-content-language",
|
|
|
|
"response-content-disposition",
|
|
|
|
"response-content-encoding",
|
|
|
|
"website",
|
|
|
|
}
|
2015-02-06 05:08:52 -05:00
|
|
|
|
2015-05-01 15:17:27 -04:00
|
|
|
func url2BucketAndObject(url string) (string, string) {
|
|
|
|
var bucketName, objectName string
|
|
|
|
splits := strings.SplitN(url, "/", 3)
|
|
|
|
switch len(splits) {
|
|
|
|
case 0, 1:
|
|
|
|
bucketName = ""
|
|
|
|
objectName = ""
|
|
|
|
case 2:
|
|
|
|
bucketName = splits[1]
|
|
|
|
objectName = ""
|
|
|
|
case 3:
|
|
|
|
bucketName = splits[1]
|
|
|
|
objectName = splits[2]
|
|
|
|
}
|
|
|
|
return bucketName, objectName
|
|
|
|
}
|
|
|
|
|
2015-02-06 05:08:52 -05:00
|
|
|
// From the Amazon docs:
|
2015-01-30 13:58:47 -05:00
|
|
|
//
|
2015-02-06 05:08:52 -05:00
|
|
|
// CanonicalizedResource = [ "/" + Bucket ] +
|
|
|
|
// <HTTP-Request-URI, from the protocol name up to the query string> +
|
|
|
|
// [ sub-resource, if present. For example "?acl", "?location", "?logging", or "?torrent"];
|
|
|
|
func writeCanonicalizedResource(buf *bytes.Buffer, req *http.Request) {
|
2015-05-01 15:17:27 -04:00
|
|
|
bucket, _ := url2BucketAndObject(req.URL.Path)
|
2015-02-23 05:11:27 -05:00
|
|
|
if bucket != "" {
|
|
|
|
buf.WriteByte('/')
|
|
|
|
buf.WriteString(bucket)
|
|
|
|
}
|
2015-02-06 05:08:52 -05:00
|
|
|
buf.WriteString(req.URL.Path)
|
2015-03-19 03:24:20 -04:00
|
|
|
sort.Strings(subResList)
|
2015-02-06 05:08:52 -05:00
|
|
|
if req.URL.RawQuery != "" {
|
|
|
|
n := 0
|
|
|
|
vals, _ := url.ParseQuery(req.URL.RawQuery)
|
|
|
|
for _, subres := range subResList {
|
|
|
|
if vv, ok := vals[subres]; ok && len(vv) > 0 {
|
|
|
|
n++
|
|
|
|
if n == 1 {
|
|
|
|
buf.WriteByte('?')
|
|
|
|
} else {
|
|
|
|
buf.WriteByte('&')
|
|
|
|
}
|
|
|
|
buf.WriteString(subres)
|
|
|
|
if len(vv[0]) > 0 {
|
|
|
|
buf.WriteByte('=')
|
|
|
|
buf.WriteString(url.QueryEscape(vv[0]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|