From b41278d4e7193f58c88632ab29f8874e899a9585 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Fri, 6 Feb 2015 01:36:40 -0800 Subject: [PATCH] Now minio client can access minio server through HMAC style authentication --- pkg/s3/auth.go | 24 +++++++++++++++++++----- pkg/s3/client.go | 17 ++++++++++------- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/pkg/s3/auth.go b/pkg/s3/auth.go index d8c45e24..14776048 100644 --- a/pkg/s3/auth.go +++ b/pkg/s3/auth.go @@ -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 } diff --git a/pkg/s3/client.go b/pkg/s3/client.go index dde4b9b4..741e8db9 100644 --- a/pkg/s3/client.go +++ b/pkg/s3/client.go @@ -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 }