Vendor update for github.com/minio/minio-go

This commit is contained in:
Krishna Srinivas
2015-10-08 13:20:24 -07:00
parent 428cc1eed6
commit 7c251c314b
6 changed files with 220 additions and 43 deletions
+22
View File
@@ -556,6 +556,28 @@ func (a apiCore) putObject(bucket, object, contentType string, md5SumBytes []byt
return metadata, nil
}
func (a apiCore) presignedPostPolicyRequest(p *PostPolicy) *request {
r := new(request)
r.config = a.config
return r
}
func (a apiCore) presignedPostPolicy(p *PostPolicy) map[string]string {
t := time.Now().UTC()
r := a.presignedPostPolicyRequest(p)
p.policies = append(p.policies, policy{"eq", "$x-amz-date", t.Format(iso8601DateFormat)})
p.policies = append(p.policies, policy{"eq", "$x-amz-algorithm", authHeader})
p.policies = append(p.policies, policy{"eq", "$x-amz-credential", r.config.AccessKeyID + "/" + getScope(a.config.Region, t)})
policyBase64 := p.base64()
p.formData["policy"] = policyBase64
p.formData["x-amz-algorithm"] = authHeader
p.formData["x-amz-credential"] = a.config.AccessKeyID + "/" + getScope(a.config.Region, t)
p.formData["x-amz-date"] = t.Format(iso8601DateFormat)
p.formData["x-amz-signature"] = r.PostPresignSignature(policyBase64, t)
return p.formData
}
func (a apiCore) presignedGetObjectRequest(bucket, object string, expires, offset, length int64) (*request, error) {
op := &operation{
HTTPServer: a.config.Endpoint,
+15
View File
@@ -73,6 +73,7 @@ type ObjectAPI interface {
// PresignedAPI - object specific for now
type PresignedAPI interface {
PresignedGetObject(bucket, object string, expires time.Duration) (string, error)
PresignedPostPolicy(*PostPolicy) (map[string]string, error)
}
// BucketStatCh - bucket metadata over read channel
@@ -555,6 +556,20 @@ func (a api) listMultipartUploadsRecursiveInRoutine(bucket, object string, ch ch
}
}
// PresignedPostPolicy return POST form data that can be used for object upload
func (a api) PresignedPostPolicy(p *PostPolicy) (map[string]string, error) {
if p.expiration.IsZero() {
return nil, errors.New("Expiration time must be specified")
}
if _, ok := p.formData["key"]; !ok {
return nil, errors.New("object key must be specified")
}
if _, ok := p.formData["bucket"]; !ok {
return nil, errors.New("bucket name must be specified")
}
return a.presignedPostPolicy(p), nil
}
// PutObject create an object in a bucket
//
// You must have WRITE permissions on a bucket to create an object
+28
View File
@@ -19,9 +19,12 @@ package minio
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"encoding/xml"
"io"
"strings"
"time"
)
// decoder provides a unified decoding method interface
@@ -79,3 +82,28 @@ func sumHMAC(key []byte, data []byte) []byte {
hash.Write(data)
return hash.Sum(nil)
}
// getSigningKey hmac seed to calculate final signature
func getSigningKey(secret, region string, t time.Time) []byte {
date := sumHMAC([]byte("AWS4"+secret), []byte(t.Format(yyyymmdd)))
regionbytes := sumHMAC(date, []byte(region))
service := sumHMAC(regionbytes, []byte("s3"))
signingKey := sumHMAC(service, []byte("aws4_request"))
return signingKey
}
// getSignature final signature in hexadecimal form
func getSignature(signingKey []byte, stringToSign string) string {
return hex.EncodeToString(sumHMAC(signingKey, []byte(stringToSign)))
}
// getScope generate a string of a specific date, an AWS region, and a service
func getScope(region string, t time.Time) string {
scope := strings.Join([]string{
t.Format(yyyymmdd),
region,
"s3",
"aws4_request",
}, "/")
return scope
}
+137
View File
@@ -0,0 +1,137 @@
package minio
import (
"encoding/base64"
"errors"
"fmt"
"strings"
"time"
)
// expirationDateFormat date format for expiration key in json policy
const expirationDateFormat = "2006-01-02T15:04:05.999Z"
// contentLengthRange - min and max size of content
type contentLengthRange struct {
min int
max int
}
func (c contentLengthRange) marshalJSON() string {
return fmt.Sprintf("[\"content-length-range\",%d,%d]", c.min, c.max)
}
// Policy explanation: http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-HTTPPOSTConstructPolicy.html
type policy struct {
matchType string
key string
value string
}
func (p policy) marshalJSON() string {
return fmt.Sprintf("[\"%s\",\"%s\",\"%s\"]", p.matchType, p.key, p.value)
}
// PostPolicy provides strict static type conversion and validation for Amazon S3's POST policy JSON string.
type PostPolicy struct {
expiration time.Time // expiration date and time of the POST policy.
policies []policy
contentLengthRange contentLengthRange
// Post form data
formData map[string]string
}
// NewPostPolicy instantiate new post policy
func NewPostPolicy() *PostPolicy {
p := &PostPolicy{}
p.policies = make([]policy, 0)
p.formData = make(map[string]string)
return p
}
// SetExpires expiration time
func (p *PostPolicy) SetExpires(t time.Time) error {
if t.IsZero() {
return errors.New("time input invalid")
}
p.expiration = t
return nil
}
// SetKey Object name
func (p *PostPolicy) SetKey(key string) error {
if strings.TrimSpace(key) == "" || key == "" {
return errors.New("key invalid")
}
policy := policy{"eq", "$key", key}
p.policies = append(p.policies, policy)
p.formData["key"] = key
return nil
}
// SetKeyStartsWith Object name that can start with
func (p *PostPolicy) SetKeyStartsWith(keyStartsWith string) error {
if strings.TrimSpace(keyStartsWith) == "" || keyStartsWith == "" {
return errors.New("key-starts-with invalid")
}
policy := policy{"starts-with", "$key", keyStartsWith}
p.policies = append(p.policies, policy)
p.formData["key"] = keyStartsWith
return nil
}
// SetBucket bucket name
func (p *PostPolicy) SetBucket(bucket string) error {
if strings.TrimSpace(bucket) == "" || bucket == "" {
return errors.New("bucket invalid")
}
policy := policy{"eq", "$bucket", bucket}
p.policies = append(p.policies, policy)
p.formData["bucket"] = bucket
return nil
}
// SetContentType content-type
func (p *PostPolicy) SetContentType(contentType string) error {
if strings.TrimSpace(contentType) == "" || contentType == "" {
return errors.New("contentType invalid")
}
policy := policy{"eq", "$Content-Type", contentType}
p.policies = append(p.policies, policy)
p.formData["Content-Type"] = contentType
return nil
}
// marshalJSON provides Marshalled JSON
func (p PostPolicy) marshalJSON() []byte {
var expirationStr string
if p.expiration.IsZero() == false {
expirationStr = `"expiration":"` + p.expiration.Format(expirationDateFormat) + `"`
}
var policiesStr string
policies := []string{}
for _, policy := range p.policies {
policies = append(policies, policy.marshalJSON())
}
if p.contentLengthRange.min != 0 || p.contentLengthRange.max != 0 {
policies = append(policies, p.contentLengthRange.marshalJSON())
}
if len(policies) > 0 {
policiesStr = `"conditions":[` + strings.Join(policies, ",") + "]"
}
retStr := "{"
if len(expirationStr) > 0 {
retStr = retStr + expirationStr + ","
}
if len(policiesStr) > 0 {
retStr = retStr + policiesStr
}
retStr = retStr + "}"
return []byte(retStr)
}
// base64 produces base64 of PostPolicy's Marshalled json
func (p PostPolicy) base64() string {
return base64.StdEncoding.EncodeToString(p.marshalJSON())
}
+16 -36
View File
@@ -46,9 +46,9 @@ type request struct {
}
const (
authHeader = "AWS4-HMAC-SHA256"
iso8601Format = "20060102T150405Z"
yyyymmdd = "20060102"
authHeader = "AWS4-HMAC-SHA256"
iso8601DateFormat = "20060102T150405Z"
yyyymmdd = "20060102"
)
///
@@ -422,40 +422,14 @@ func (r *request) getCanonicalRequest(hashedPayload string) string {
return canonicalRequest
}
// getScope generate a string of a specific date, an AWS region, and a service
func (r *request) getScope(t time.Time) string {
scope := strings.Join([]string{
t.Format(yyyymmdd),
r.config.Region,
"s3",
"aws4_request",
}, "/")
return scope
}
// getStringToSign a string based on selected query values
func (r *request) getStringToSign(canonicalRequest string, t time.Time) string {
stringToSign := authHeader + "\n" + t.Format(iso8601Format) + "\n"
stringToSign = stringToSign + r.getScope(t) + "\n"
stringToSign := authHeader + "\n" + t.Format(iso8601DateFormat) + "\n"
stringToSign = stringToSign + getScope(r.config.Region, t) + "\n"
stringToSign = stringToSign + hex.EncodeToString(sum256([]byte(canonicalRequest)))
return stringToSign
}
// getSigningKey hmac seed to calculate final signature
func (r *request) getSigningKey(t time.Time) []byte {
secret := r.config.SecretAccessKey
date := sumHMAC([]byte("AWS4"+secret), []byte(t.Format(yyyymmdd)))
region := sumHMAC(date, []byte(r.config.Region))
service := sumHMAC(region, []byte("s3"))
signingKey := sumHMAC(service, []byte("aws4_request"))
return signingKey
}
// getSignature final signature in hexadecimal form
func (r *request) getSignature(signingKey []byte, stringToSign string) string {
return hex.EncodeToString(sumHMAC(signingKey, []byte(stringToSign)))
}
// Presign the request, in accordance with - http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-query-string-auth.html
func (r *request) PreSignV4() (string, error) {
if r.config.AccessKeyID == "" && r.config.SecretAccessKey == "" {
@@ -465,6 +439,12 @@ func (r *request) PreSignV4() (string, error) {
return r.req.URL.String(), nil
}
func (r *request) PostPresignSignature(policyBase64 string, t time.Time) string {
signingkey := getSigningKey(r.config.SecretAccessKey, r.config.Region, t)
signature := getSignature(signingkey, policyBase64)
return signature
}
// SignV4 the request before Do(), in accordance with - http://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-authenticating-requests.html
func (r *request) SignV4() {
query := r.req.URL.Query()
@@ -474,10 +454,10 @@ func (r *request) SignV4() {
t := time.Now().UTC()
// Add date if not present
if r.expires != "" {
query.Set("X-Amz-Date", t.Format(iso8601Format))
query.Set("X-Amz-Date", t.Format(iso8601DateFormat))
query.Set("X-Amz-Expires", r.expires)
} else {
r.Set("X-Amz-Date", t.Format(iso8601Format))
r.Set("X-Amz-Date", t.Format(iso8601DateFormat))
}
hashedPayload := r.getHashedPayload()
@@ -485,15 +465,15 @@ func (r *request) SignV4() {
if r.expires != "" {
query.Set("X-Amz-SignedHeaders", signedHeaders)
}
scope := r.getScope(t)
scope := getScope(r.config.Region, t)
if r.expires != "" {
query.Set("X-Amz-Credential", r.config.AccessKeyID+"/"+scope)
r.req.URL.RawQuery = query.Encode()
}
canonicalRequest := r.getCanonicalRequest(hashedPayload)
stringToSign := r.getStringToSign(canonicalRequest, t)
signingKey := r.getSigningKey(t)
signature := r.getSignature(signingKey, stringToSign)
signingKey := getSigningKey(r.config.SecretAccessKey, r.config.Region, t)
signature := getSignature(signingKey, stringToSign)
if r.expires != "" {
r.req.URL.RawQuery += "&X-Amz-Signature=" + signature
+2 -7
View File
@@ -29,8 +29,8 @@
},
{
"path": "github.com/minio/minio-go",
"revision": "4900c9c8695340456ca8409c2354d503f839a608",
"revisionTime": "2015-10-02T12:03:54-07:00"
"revision": "26948ab46dafaa60c6e14637ebfc1eb43802cb6c",
"revisionTime": "2015-10-06T11:59:55-07:00"
},
{
"path": "github.com/minio/minio-go-legacy",
@@ -62,11 +62,6 @@
"revision": "a5e2b567a4dd6cc74545b8a4f27c9d63b9e7735b",
"revisionTime": "2015-07-19T16:15:31+09:00"
},
{
"path": "github.com/tchap/go-patricia/patricia",
"revision": "a560f85131e8aafc2cc4bcb1e265ae3fc6ee5f50",
"revisionTime": "2015-06-11T09:53:39+02:00"
},
{
"path": "gopkg.in/check.v1",
"revision": "11d3bc7aa68e238947792f30573146a3231fc0f1",