Merge pull request #32 from harshavardhana/pr_out_now_minio_client_can_access_minio_server_through_hmac_style_authentication

This commit is contained in:
Harshavardhana
2015-02-06 01:38:03 -08:00
2 changed files with 29 additions and 12 deletions
+19 -5
View File
@@ -23,6 +23,7 @@ import (
"encoding/base64"
"fmt"
"io"
//"log"
"net/http"
"net/url"
"sort"
@@ -42,11 +43,16 @@ type Auth struct {
Hostname string
}
const standardUSRegionAWS = "s3.amazonaws.com"
const standardUSRegionAWS = "https://s3.amazonaws.com"
func (a *Auth) hostname() string {
// Prefix with https for Amazon hostnames
if a.Hostname != "" {
return a.Hostname
if strings.HasSuffix(a.Hostname, "amazonaws.com") {
return "https://" + a.Hostname
} else {
return "http://" + a.Hostname
}
}
return standardUSRegionAWS
}
@@ -57,7 +63,7 @@ func (a *Auth) SignRequest(req *http.Request) {
}
hm := hmac.New(sha1.New, []byte(a.SecretAccessKey))
ss := a.stringToSign(req)
// log.Printf("String to sign: %q (%x)", ss, ss)
//log.Printf("String to sign: %q (%x)", ss, ss)
io.WriteString(hm, ss)
authHeader := new(bytes.Buffer)
@@ -193,14 +199,22 @@ func (a *Auth) bucketFromHostname(req *http.Request) string {
if host == "" {
host = req.URL.Host
}
if host == a.hostname() {
if host == strings.TrimPrefix(a.hostname(), "http://") {
return ""
}
if hostSuffix := a.hostname(); hasDotSuffix(host, hostSuffix) {
if host == strings.TrimPrefix(a.hostname(), "https://") {
return ""
}
if hostSuffix := strings.TrimPrefix(a.hostname(), "https://"); hasDotSuffix(host, hostSuffix) {
return host[:len(host)-len(hostSuffix)-1]
}
if lastColon := strings.LastIndex(host, ":"); lastColon != -1 {
return host[:lastColon]
}
return host
}
+10 -7
View File
@@ -60,13 +60,16 @@ func (c *Client) transport() http.RoundTripper {
// bucketURL returns the URL prefix of the bucket, with trailing slash
func (c *Client) bucketURL(bucket string) string {
if IsValidBucket(bucket) && !strings.Contains(bucket, ".") {
return fmt.Sprintf("https://%s.%s/", bucket, c.hostname())
if strings.HasSuffix(c.hostname(), "amazonaws.com") {
if IsValidBucket(bucket) && !strings.Contains(bucket, ".") {
return fmt.Sprintf("https://%s.%s/", bucket, strings.TrimPrefix(c.hostname(), "https://"))
}
}
return fmt.Sprintf("https://%s/%s/", c.hostname(), bucket)
return fmt.Sprintf("%s/%s/", c.hostname(), bucket)
}
func (c *Client) keyURL(bucket, key string) string {
return c.bucketURL(bucket) + key
}
@@ -75,12 +78,12 @@ func newReq(url_ string) *http.Request {
if err != nil {
panic(fmt.Sprintf("s3 client; invalid URL: %v", err))
}
req.Header.Set("User-Agent", "minio-s3")
req.Header.Set("User-Agent", "Minio Client")
return req
}
func (c *Client) Buckets() ([]*Bucket, error) {
req := newReq("https://" + c.hostname() + "/")
req := newReq(c.hostname() + "/")
c.Auth.SignRequest(req)
res, err := c.transport().RoundTrip(req)
if err != nil {
@@ -195,7 +198,7 @@ func (c *Client) BucketLocation(bucket string) (location string, err error) {
if !strings.HasSuffix(c.hostname(), "amazonaws.com") {
return "", errors.New("BucketLocation not implemented for non-Amazon S3 hostnames")
}
url_ := fmt.Sprintf("https://s3.amazonaws.com/%s/?location", url.QueryEscape(bucket))
url_ := fmt.Sprintf("%s/%s/?location", c.hostname(), url.QueryEscape(bucket))
req := newReq(url_)
c.Auth.SignRequest(req)
res, err := c.transport().RoundTrip(req)
@@ -207,7 +210,7 @@ func (c *Client) BucketLocation(bucket string) (location string, err error) {
return "", err
}
if xres.Location == "" {
return "s3.amazonaws.com", nil
return strings.TrimPrefix(c.hostname(), "https://"), nil
}
return "s3-" + xres.Location + ".amazonaws.com", nil
}