2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2016-09-30 17:32:13 -04:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/hmac"
|
|
|
|
"crypto/sha1"
|
2018-01-02 01:30:02 -05:00
|
|
|
"crypto/subtle"
|
2016-09-30 17:32:13 -04:00
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2016-12-15 17:56:18 -05:00
|
|
|
"net/url"
|
2016-09-30 17:32:13 -04:00
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2018-10-09 17:00:01 -04:00
|
|
|
|
2021-06-01 17:59:40 -04:00
|
|
|
xhttp "github.com/minio/minio/internal/http"
|
2019-07-03 01:34:32 -04:00
|
|
|
|
2021-06-01 17:59:40 -04:00
|
|
|
"github.com/minio/minio/internal/auth"
|
2016-09-30 17:32:13 -04:00
|
|
|
)
|
|
|
|
|
2016-10-05 12:18:53 -04:00
|
|
|
// Whitelist resource list that will be used in query string for signature-V2 calculation.
|
2020-05-21 21:51:23 -04:00
|
|
|
//
|
|
|
|
// This list should be kept alphabetically sorted, do not hastily edit.
|
2016-10-05 12:18:53 -04:00
|
|
|
var resourceList = []string{
|
|
|
|
"acl",
|
2020-05-21 21:51:23 -04:00
|
|
|
"cors",
|
2016-10-05 12:18:53 -04:00
|
|
|
"delete",
|
2020-05-21 21:51:23 -04:00
|
|
|
"encryption",
|
|
|
|
"legal-hold",
|
2016-10-05 12:18:53 -04:00
|
|
|
"lifecycle",
|
|
|
|
"location",
|
|
|
|
"logging",
|
|
|
|
"notification",
|
|
|
|
"partNumber",
|
|
|
|
"policy",
|
|
|
|
"requestPayment",
|
2016-12-11 17:32:25 -05:00
|
|
|
"response-cache-control",
|
|
|
|
"response-content-disposition",
|
|
|
|
"response-content-encoding",
|
|
|
|
"response-content-language",
|
|
|
|
"response-content-type",
|
|
|
|
"response-expires",
|
2020-05-21 21:51:23 -04:00
|
|
|
"retention",
|
|
|
|
"select",
|
|
|
|
"select-type",
|
|
|
|
"tagging",
|
2016-10-05 12:18:53 -04:00
|
|
|
"torrent",
|
|
|
|
"uploadId",
|
|
|
|
"uploads",
|
|
|
|
"versionId",
|
|
|
|
"versioning",
|
|
|
|
"versions",
|
|
|
|
"website",
|
|
|
|
}
|
|
|
|
|
2020-05-21 21:51:23 -04:00
|
|
|
// Signature and API related constants.
|
|
|
|
const (
|
|
|
|
signV2Algorithm = "AWS"
|
|
|
|
)
|
|
|
|
|
|
|
|
// AWS S3 Signature V2 calculation rule is give here:
|
|
|
|
// http://docs.aws.amazon.com/AmazonS3/latest/dev/RESTAuthentication.html#RESTAuthenticationStringToSign
|
2021-03-03 11:47:08 -05:00
|
|
|
func doesPolicySignatureV2Match(formValues http.Header) (auth.Credentials, APIErrorCode) {
|
2019-07-03 01:34:32 -04:00
|
|
|
accessKey := formValues.Get(xhttp.AmzAccessKeyID)
|
2021-08-12 21:07:08 -04:00
|
|
|
|
|
|
|
r := &http.Request{Header: formValues}
|
|
|
|
cred, _, s3Err := checkKeyValid(r, accessKey)
|
2018-11-07 09:40:03 -05:00
|
|
|
if s3Err != ErrNone {
|
2021-03-03 11:47:08 -05:00
|
|
|
return cred, s3Err
|
2016-10-22 11:57:12 -04:00
|
|
|
}
|
2017-03-13 17:41:13 -04:00
|
|
|
policy := formValues.Get("Policy")
|
2019-07-03 01:34:32 -04:00
|
|
|
signature := formValues.Get(xhttp.AmzSignatureV2)
|
2018-01-02 01:30:02 -05:00
|
|
|
if !compareSignatureV2(signature, calculateSignatureV2(policy, cred.SecretKey)) {
|
2021-03-03 11:47:08 -05:00
|
|
|
return cred, ErrSignatureDoesNotMatch
|
2016-10-22 11:57:12 -04:00
|
|
|
}
|
2021-03-03 11:47:08 -05:00
|
|
|
return cred, ErrNone
|
2016-10-22 11:57:12 -04:00
|
|
|
}
|
2016-09-30 17:32:13 -04:00
|
|
|
|
2017-09-24 17:20:12 -04:00
|
|
|
// Escape encodedQuery string into unescaped list of query params, returns error
|
|
|
|
// if any while unescaping the values.
|
|
|
|
func unescapeQueries(encodedQuery string) (unescapedQueries []string, err error) {
|
|
|
|
for _, query := range strings.Split(encodedQuery, "&") {
|
|
|
|
var unescapedQuery string
|
|
|
|
unescapedQuery, err = url.QueryUnescape(query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
unescapedQueries = append(unescapedQueries, unescapedQuery)
|
|
|
|
}
|
|
|
|
return unescapedQueries, nil
|
|
|
|
}
|
|
|
|
|
2016-09-30 17:32:13 -04:00
|
|
|
// doesPresignV2SignatureMatch - Verify query headers with presigned signature
|
|
|
|
// - http://docs.aws.amazon.com/AmazonS3/latest/dev/RESTAuthentication.html#RESTAuthenticationQueryStringAuth
|
|
|
|
// returns ErrNone if matches. S3 errors otherwise.
|
|
|
|
func doesPresignV2SignatureMatch(r *http.Request) APIErrorCode {
|
2017-01-23 20:01:44 -05:00
|
|
|
// r.RequestURI will have raw encoded URI as sent by the client.
|
2017-04-11 18:44:27 -04:00
|
|
|
tokens := strings.SplitN(r.RequestURI, "?", 2)
|
|
|
|
encodedResource := tokens[0]
|
|
|
|
encodedQuery := ""
|
|
|
|
if len(tokens) == 2 {
|
|
|
|
encodedQuery = tokens[1]
|
|
|
|
}
|
2017-01-23 11:54:32 -05:00
|
|
|
|
2017-09-24 17:20:12 -04:00
|
|
|
var (
|
|
|
|
filteredQueries []string
|
|
|
|
gotSignature string
|
|
|
|
expires string
|
|
|
|
accessKey string
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
var unescapedQueries []string
|
|
|
|
unescapedQueries, err = unescapeQueries(encodedQuery)
|
|
|
|
if err != nil {
|
|
|
|
return ErrInvalidQueryParams
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract the necessary values from presigned query, construct a list of new filtered queries.
|
|
|
|
for _, query := range unescapedQueries {
|
|
|
|
keyval := strings.SplitN(query, "=", 2)
|
2018-01-02 01:30:02 -05:00
|
|
|
if len(keyval) != 2 {
|
|
|
|
return ErrInvalidQueryParams
|
|
|
|
}
|
2016-10-05 12:18:53 -04:00
|
|
|
switch keyval[0] {
|
2019-07-03 01:34:32 -04:00
|
|
|
case xhttp.AmzAccessKeyID:
|
2017-09-24 17:20:12 -04:00
|
|
|
accessKey = keyval[1]
|
2019-07-03 01:34:32 -04:00
|
|
|
case xhttp.AmzSignatureV2:
|
2017-09-24 17:20:12 -04:00
|
|
|
gotSignature = keyval[1]
|
2019-07-03 01:34:32 -04:00
|
|
|
case xhttp.Expires:
|
2017-09-24 17:20:12 -04:00
|
|
|
expires = keyval[1]
|
2016-10-05 12:18:53 -04:00
|
|
|
default:
|
2017-09-24 17:20:12 -04:00
|
|
|
filteredQueries = append(filteredQueries, query)
|
2016-10-05 12:18:53 -04:00
|
|
|
}
|
2016-12-15 17:56:18 -05:00
|
|
|
}
|
2016-09-30 17:32:13 -04:00
|
|
|
|
2017-09-24 17:20:12 -04:00
|
|
|
// Invalid values returns error.
|
|
|
|
if accessKey == "" || gotSignature == "" || expires == "" {
|
2016-09-30 17:32:13 -04:00
|
|
|
return ErrInvalidQueryParams
|
|
|
|
}
|
|
|
|
|
2021-08-12 21:07:08 -04:00
|
|
|
cred, _, s3Err := checkKeyValid(r, accessKey)
|
2018-11-07 09:40:03 -05:00
|
|
|
if s3Err != ErrNone {
|
|
|
|
return s3Err
|
2016-09-30 17:32:13 -04:00
|
|
|
}
|
|
|
|
|
2016-10-05 12:18:53 -04:00
|
|
|
// Make sure the request has not expired.
|
|
|
|
expiresInt, err := strconv.ParseInt(expires, 10, 64)
|
2016-09-30 17:32:13 -04:00
|
|
|
if err != nil {
|
|
|
|
return ErrMalformedExpires
|
|
|
|
}
|
|
|
|
|
2016-12-15 17:56:18 -05:00
|
|
|
// Check if the presigned URL has expired.
|
2017-03-18 14:28:41 -04:00
|
|
|
if expiresInt < UTCNow().Unix() {
|
2016-09-30 17:32:13 -04:00
|
|
|
return ErrExpiredPresignRequest
|
|
|
|
}
|
|
|
|
|
2019-02-22 22:18:01 -05:00
|
|
|
encodedResource, err = getResource(encodedResource, r.Host, globalDomainNames)
|
2017-11-14 19:56:24 -05:00
|
|
|
if err != nil {
|
|
|
|
return ErrInvalidRequest
|
|
|
|
}
|
|
|
|
|
2018-10-09 17:00:01 -04:00
|
|
|
expectedSignature := preSignatureV2(cred, r.Method, encodedResource, strings.Join(filteredQueries, "&"), r.Header, expires)
|
2018-01-02 01:30:02 -05:00
|
|
|
if !compareSignatureV2(gotSignature, expectedSignature) {
|
2016-09-30 17:32:13 -04:00
|
|
|
return ErrSignatureDoesNotMatch
|
|
|
|
}
|
|
|
|
|
|
|
|
return ErrNone
|
|
|
|
}
|
|
|
|
|
2018-11-07 09:40:03 -05:00
|
|
|
func getReqAccessKeyV2(r *http.Request) (auth.Credentials, bool, APIErrorCode) {
|
2021-08-08 01:43:01 -04:00
|
|
|
if accessKey := r.Form.Get(xhttp.AmzAccessKeyID); accessKey != "" {
|
2021-08-12 21:07:08 -04:00
|
|
|
return checkKeyValid(r, accessKey)
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// below is V2 Signed Auth header format, splitting on `space` (after the `AWS` string).
|
|
|
|
// Authorization = "AWS" + " " + AWSAccessKeyId + ":" + Signature
|
2019-07-03 01:34:32 -04:00
|
|
|
authFields := strings.Split(r.Header.Get(xhttp.Authorization), " ")
|
2018-10-09 17:00:01 -04:00
|
|
|
if len(authFields) != 2 {
|
2018-11-07 09:40:03 -05:00
|
|
|
return auth.Credentials{}, false, ErrMissingFields
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Then will be splitting on ":", this will seprate `AWSAccessKeyId` and `Signature` string.
|
|
|
|
keySignFields := strings.Split(strings.TrimSpace(authFields[1]), ":")
|
|
|
|
if len(keySignFields) != 2 {
|
2018-11-07 09:40:03 -05:00
|
|
|
return auth.Credentials{}, false, ErrMissingFields
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
|
2021-08-12 21:07:08 -04:00
|
|
|
return checkKeyValid(r, keySignFields[0])
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
|
2016-09-30 17:32:13 -04:00
|
|
|
// Authorization = "AWS" + " " + AWSAccessKeyId + ":" + Signature;
|
2016-12-26 13:21:23 -05:00
|
|
|
// Signature = Base64( HMAC-SHA1( YourSecretKey, UTF-8-Encoding-Of( StringToSign ) ) );
|
2016-09-30 17:32:13 -04:00
|
|
|
//
|
|
|
|
// StringToSign = HTTP-Verb + "\n" +
|
|
|
|
// Content-Md5 + "\n" +
|
|
|
|
// Content-Type + "\n" +
|
|
|
|
// Date + "\n" +
|
|
|
|
// CanonicalizedProtocolHeaders +
|
|
|
|
// CanonicalizedResource;
|
|
|
|
//
|
2019-08-06 15:08:58 -04:00
|
|
|
// CanonicalizedResource = [ SlashSeparator + Bucket ] +
|
2016-09-30 17:32:13 -04:00
|
|
|
// <HTTP-Request-URI, from the protocol name up to the query string> +
|
|
|
|
// [ subresource, if present. For example "?acl", "?location", "?logging", or "?torrent"];
|
|
|
|
//
|
|
|
|
// CanonicalizedProtocolHeaders = <described below>
|
|
|
|
|
|
|
|
// doesSignV2Match - Verify authorization header with calculated header in accordance with
|
|
|
|
// - http://docs.aws.amazon.com/AmazonS3/latest/dev/auth-request-sig-v2.html
|
|
|
|
// returns true if matches, false otherwise. if error is not nil then it is always false
|
2016-10-13 12:25:56 -04:00
|
|
|
|
2018-10-09 17:00:01 -04:00
|
|
|
func validateV2AuthHeader(r *http.Request) (auth.Credentials, APIErrorCode) {
|
|
|
|
var cred auth.Credentials
|
2019-07-03 01:34:32 -04:00
|
|
|
v2Auth := r.Header.Get(xhttp.Authorization)
|
2016-10-13 12:25:56 -04:00
|
|
|
if v2Auth == "" {
|
2018-10-09 17:00:01 -04:00
|
|
|
return cred, ErrAuthHeaderEmpty
|
2016-09-30 17:32:13 -04:00
|
|
|
}
|
2018-10-09 17:00:01 -04:00
|
|
|
|
2016-10-13 12:25:56 -04:00
|
|
|
// Verify if the header algorithm is supported or not.
|
|
|
|
if !strings.HasPrefix(v2Auth, signV2Algorithm) {
|
2018-10-09 17:00:01 -04:00
|
|
|
return cred, ErrSignatureVersionNotSupported
|
2016-10-13 12:25:56 -04:00
|
|
|
}
|
|
|
|
|
2018-11-07 09:40:03 -05:00
|
|
|
cred, _, apiErr := getReqAccessKeyV2(r)
|
2018-10-09 17:00:01 -04:00
|
|
|
if apiErr != ErrNone {
|
|
|
|
return cred, apiErr
|
2016-10-13 12:25:56 -04:00
|
|
|
}
|
|
|
|
|
2018-10-09 17:00:01 -04:00
|
|
|
return cred, ErrNone
|
2016-10-13 12:25:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func doesSignV2Match(r *http.Request) APIErrorCode {
|
2019-07-03 01:34:32 -04:00
|
|
|
v2Auth := r.Header.Get(xhttp.Authorization)
|
2018-10-09 17:00:01 -04:00
|
|
|
cred, apiError := validateV2AuthHeader(r)
|
|
|
|
if apiError != ErrNone {
|
2016-10-13 12:25:56 -04:00
|
|
|
return apiError
|
|
|
|
}
|
2016-09-30 17:32:13 -04:00
|
|
|
|
2017-01-23 20:01:44 -05:00
|
|
|
// r.RequestURI will have raw encoded URI as sent by the client.
|
2017-04-11 18:44:27 -04:00
|
|
|
tokens := strings.SplitN(r.RequestURI, "?", 2)
|
|
|
|
encodedResource := tokens[0]
|
|
|
|
encodedQuery := ""
|
|
|
|
if len(tokens) == 2 {
|
|
|
|
encodedQuery = tokens[1]
|
|
|
|
}
|
2016-11-14 13:23:21 -05:00
|
|
|
|
2017-09-24 17:20:12 -04:00
|
|
|
unescapedQueries, err := unescapeQueries(encodedQuery)
|
|
|
|
if err != nil {
|
|
|
|
return ErrInvalidQueryParams
|
|
|
|
}
|
|
|
|
|
2019-02-22 22:18:01 -05:00
|
|
|
encodedResource, err = getResource(encodedResource, r.Host, globalDomainNames)
|
2017-11-14 19:56:24 -05:00
|
|
|
if err != nil {
|
|
|
|
return ErrInvalidRequest
|
|
|
|
}
|
|
|
|
|
2018-10-09 17:00:01 -04:00
|
|
|
prefix := fmt.Sprintf("%s %s:", signV2Algorithm, cred.AccessKey)
|
2018-01-02 01:30:02 -05:00
|
|
|
if !strings.HasPrefix(v2Auth, prefix) {
|
|
|
|
return ErrSignatureDoesNotMatch
|
|
|
|
}
|
|
|
|
v2Auth = v2Auth[len(prefix):]
|
2018-10-09 17:00:01 -04:00
|
|
|
expectedAuth := signatureV2(cred, r.Method, encodedResource, strings.Join(unescapedQueries, "&"), r.Header)
|
2018-01-02 01:30:02 -05:00
|
|
|
if !compareSignatureV2(v2Auth, expectedAuth) {
|
2016-09-30 17:32:13 -04:00
|
|
|
return ErrSignatureDoesNotMatch
|
|
|
|
}
|
|
|
|
return ErrNone
|
|
|
|
}
|
|
|
|
|
2016-10-22 11:57:12 -04:00
|
|
|
func calculateSignatureV2(stringToSign string, secret string) string {
|
|
|
|
hm := hmac.New(sha1.New, []byte(secret))
|
|
|
|
hm.Write([]byte(stringToSign))
|
|
|
|
return base64.StdEncoding.EncodeToString(hm.Sum(nil))
|
|
|
|
}
|
|
|
|
|
2016-10-05 12:18:53 -04:00
|
|
|
// Return signature-v2 for the presigned request.
|
2018-10-09 17:00:01 -04:00
|
|
|
func preSignatureV2(cred auth.Credentials, method string, encodedResource string, encodedQuery string, headers http.Header, expires string) string {
|
2017-09-24 17:20:12 -04:00
|
|
|
stringToSign := getStringToSignV2(method, encodedResource, encodedQuery, headers, expires)
|
2016-12-26 13:21:23 -05:00
|
|
|
return calculateSignatureV2(stringToSign, cred.SecretKey)
|
2016-09-30 17:32:13 -04:00
|
|
|
}
|
|
|
|
|
2018-01-02 01:30:02 -05:00
|
|
|
// Return the signature v2 of a given request.
|
2018-10-09 17:00:01 -04:00
|
|
|
func signatureV2(cred auth.Credentials, method string, encodedResource string, encodedQuery string, headers http.Header) string {
|
2017-09-24 17:20:12 -04:00
|
|
|
stringToSign := getStringToSignV2(method, encodedResource, encodedQuery, headers, "")
|
2016-12-26 13:21:23 -05:00
|
|
|
signature := calculateSignatureV2(stringToSign, cred.SecretKey)
|
2018-01-02 01:30:02 -05:00
|
|
|
return signature
|
|
|
|
}
|
|
|
|
|
|
|
|
// compareSignatureV2 returns true if and only if both signatures
|
|
|
|
// are equal. The signatures are expected to be base64 encoded strings
|
|
|
|
// according to the AWS S3 signature V2 spec.
|
|
|
|
func compareSignatureV2(sig1, sig2 string) bool {
|
|
|
|
// Decode signature string to binary byte-sequence representation is required
|
|
|
|
// as Base64 encoding of a value is not unique:
|
|
|
|
// For example "aGVsbG8=" and "aGVsbG8=\r" will result in the same byte slice.
|
|
|
|
signature1, err := base64.StdEncoding.DecodeString(sig1)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
signature2, err := base64.StdEncoding.DecodeString(sig2)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return subtle.ConstantTimeCompare(signature1, signature2) == 1
|
2016-09-30 17:32:13 -04:00
|
|
|
}
|
|
|
|
|
2016-10-05 12:18:53 -04:00
|
|
|
// Return canonical headers.
|
|
|
|
func canonicalizedAmzHeadersV2(headers http.Header) string {
|
|
|
|
var keys []string
|
2020-09-10 14:37:22 -04:00
|
|
|
keyval := make(map[string]string, len(headers))
|
2016-10-05 12:18:53 -04:00
|
|
|
for key := range headers {
|
|
|
|
lkey := strings.ToLower(key)
|
|
|
|
if !strings.HasPrefix(lkey, "x-amz-") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
keys = append(keys, lkey)
|
|
|
|
keyval[lkey] = strings.Join(headers[key], ",")
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
var canonicalHeaders []string
|
|
|
|
for _, key := range keys {
|
|
|
|
canonicalHeaders = append(canonicalHeaders, key+":"+keyval[key])
|
|
|
|
}
|
|
|
|
return strings.Join(canonicalHeaders, "\n")
|
2016-09-30 17:32:13 -04:00
|
|
|
}
|
|
|
|
|
2016-10-05 12:18:53 -04:00
|
|
|
// Return canonical resource string.
|
2017-09-26 14:00:07 -04:00
|
|
|
func canonicalizedResourceV2(encodedResource, encodedQuery string) string {
|
2016-10-05 12:18:53 -04:00
|
|
|
queries := strings.Split(encodedQuery, "&")
|
|
|
|
keyval := make(map[string]string)
|
|
|
|
for _, query := range queries {
|
|
|
|
key := query
|
|
|
|
val := ""
|
|
|
|
index := strings.Index(query, "=")
|
|
|
|
if index != -1 {
|
|
|
|
key = query[:index]
|
|
|
|
val = query[index+1:]
|
2016-09-30 17:32:13 -04:00
|
|
|
}
|
2016-10-05 12:18:53 -04:00
|
|
|
keyval[key] = val
|
2016-09-30 17:32:13 -04:00
|
|
|
}
|
2017-09-24 17:20:12 -04:00
|
|
|
|
2016-10-05 12:18:53 -04:00
|
|
|
var canonicalQueries []string
|
|
|
|
for _, key := range resourceList {
|
|
|
|
val, ok := keyval[key]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if val == "" {
|
|
|
|
canonicalQueries = append(canonicalQueries, key)
|
|
|
|
continue
|
2016-09-30 17:32:13 -04:00
|
|
|
}
|
2017-09-24 17:20:12 -04:00
|
|
|
canonicalQueries = append(canonicalQueries, key+"="+val)
|
2016-09-30 17:32:13 -04:00
|
|
|
}
|
2017-09-24 17:20:12 -04:00
|
|
|
|
|
|
|
// The queries will be already sorted as resourceList is sorted, if canonicalQueries
|
|
|
|
// is empty strings.Join returns empty.
|
2017-09-26 14:00:07 -04:00
|
|
|
canonicalQuery := strings.Join(canonicalQueries, "&")
|
|
|
|
if canonicalQuery != "" {
|
|
|
|
return encodedResource + "?" + canonicalQuery
|
|
|
|
}
|
|
|
|
return encodedResource
|
2016-09-30 17:32:13 -04:00
|
|
|
}
|
|
|
|
|
2017-09-24 17:20:12 -04:00
|
|
|
// Return string to sign under two different conditions.
|
|
|
|
// - if expires string is set then string to sign includes date instead of the Date header.
|
|
|
|
// - if expires string is empty then string to sign includes date header instead.
|
|
|
|
func getStringToSignV2(method string, encodedResource, encodedQuery string, headers http.Header, expires string) string {
|
2016-10-05 12:18:53 -04:00
|
|
|
canonicalHeaders := canonicalizedAmzHeadersV2(headers)
|
|
|
|
if len(canonicalHeaders) > 0 {
|
|
|
|
canonicalHeaders += "\n"
|
|
|
|
}
|
|
|
|
|
2017-09-24 17:20:12 -04:00
|
|
|
date := expires // Date is set to expires date for presign operations.
|
|
|
|
if date == "" {
|
|
|
|
// If expires date is empty then request header Date is used.
|
2019-07-03 01:34:32 -04:00
|
|
|
date = headers.Get(xhttp.Date)
|
2017-09-24 17:20:12 -04:00
|
|
|
}
|
|
|
|
|
2016-10-05 12:18:53 -04:00
|
|
|
// From the Amazon docs:
|
|
|
|
//
|
|
|
|
// StringToSign = HTTP-Verb + "\n" +
|
|
|
|
// Content-Md5 + "\n" +
|
|
|
|
// Content-Type + "\n" +
|
2017-09-24 17:20:12 -04:00
|
|
|
// Date/Expires + "\n" +
|
2016-10-05 12:18:53 -04:00
|
|
|
// CanonicalizedProtocolHeaders +
|
|
|
|
// CanonicalizedResource;
|
|
|
|
stringToSign := strings.Join([]string{
|
|
|
|
method,
|
2019-07-03 01:34:32 -04:00
|
|
|
headers.Get(xhttp.ContentMD5),
|
|
|
|
headers.Get(xhttp.ContentType),
|
2017-09-24 17:20:12 -04:00
|
|
|
date,
|
2016-10-05 12:18:53 -04:00
|
|
|
canonicalHeaders,
|
2017-09-24 17:20:12 -04:00
|
|
|
}, "\n")
|
2016-10-05 12:18:53 -04:00
|
|
|
|
2017-09-26 14:00:07 -04:00
|
|
|
return stringToSign + canonicalizedResourceV2(encodedResource, encodedQuery)
|
2016-09-30 17:32:13 -04:00
|
|
|
}
|