This commit is contained in:
Ryan Kohler 2021-01-20 10:58:25 -08:00
parent 3aaffc9d6a
commit ff3aac6c19
2 changed files with 28 additions and 27 deletions

View File

@ -34,20 +34,20 @@ func NewRequestSigner(regionName string, awsSecurityCredentials map[string]strin
} }
const ( const (
// AWS Signature Version 4 signing algorithm identifier. // AWS Signature Version 4 signing algorithm identifier.
awsAlgorithm = "AWS4-HMAC-SHA256" awsAlgorithm = "AWS4-HMAC-SHA256"
// The termination string for the AWS credential scope value as defined in // The termination string for the AWS credential scope value as defined in
// https://docs.aws.amazon.com/general/latest/gr/sigv4-create-string-to-sign.html // https://docs.aws.amazon.com/general/latest/gr/sigv4-create-string-to-sign.html
awsRequestType = "aws4_request" awsRequestType = "aws4_request"
// The AWS authorization header name for the security session token if available. // The AWS authorization header name for the security session token if available.
awsSecurityTokenHeader = "x-amz-security-token" awsSecurityTokenHeader = "x-amz-security-token"
// The AWS authorization header name for the auto-generated date. // The AWS authorization header name for the auto-generated date.
awsDateHeader = "x-amz-date" awsDateHeader = "x-amz-date"
awsTimeFormatLong = "20060102T150405Z" awsTimeFormatLong = "20060102T150405Z"
awsTimeFormatShort = "20060102" awsTimeFormatShort = "20060102"
) )
@ -218,14 +218,14 @@ func (rs *RequestSigner) generateAuthentication(req *http.Request, timestamp tim
serviceName = splitHost[0] serviceName = splitHost[0]
} }
credentialScope := fmt.Sprintf("%s/%s/%s/%s",dateStamp, rs.RegionName, serviceName, awsRequestType) credentialScope := fmt.Sprintf("%s/%s/%s/%s", dateStamp, rs.RegionName, serviceName, awsRequestType)
requestString, err := canonicalRequest(req, canonicalHeaderColumns, canonicalHeaderData) requestString, err := canonicalRequest(req, canonicalHeaderColumns, canonicalHeaderData)
if err != nil { if err != nil {
return "", err return "", err
} }
requestHash, err := getSha256([]byte(requestString)) requestHash, err := getSha256([]byte(requestString))
if err != nil{ if err != nil {
return "", err return "", err
} }
@ -241,7 +241,7 @@ func (rs *RequestSigner) generateAuthentication(req *http.Request, timestamp tim
dateStamp, rs.RegionName, serviceName, awsRequestType, stringToSign, dateStamp, rs.RegionName, serviceName, awsRequestType, stringToSign,
} { } {
signingKey, err = getHmacSha256(signingKey, []byte(signingInput)) signingKey, err = getHmacSha256(signingKey, []byte(signingInput))
if err != nil{ if err != nil {
return "", err return "", err
} }
} }

View File

@ -14,7 +14,8 @@ import (
var defaultTime = time.Date(2011, 9, 9, 23, 36, 0, 0, time.UTC) var defaultTime = time.Date(2011, 9, 9, 23, 36, 0, 0, time.UTC)
var secondDefaultTime = time.Date(2020, 8, 11, 6, 55, 22, 0, time.UTC) var secondDefaultTime = time.Date(2020, 8, 11, 6, 55, 22, 0, time.UTC)
func setTime(testTime time.Time) (func() time.Time) {
func setTime(testTime time.Time) func() time.Time {
return func() time.Time { return func() time.Time {
return testTime return testTime
} }
@ -72,7 +73,7 @@ func TestAwsV4Signature_GetRequest(t *testing.T) {
} }
oldNow := now oldNow := now
defer func() {now = oldNow}() defer func() { now = oldNow }()
now = setTime(defaultTime) now = setTime(defaultTime)
testRequestSigner(t, defaultRequestSigner, input, output) testRequestSigner(t, defaultRequestSigner, input, output)
@ -90,7 +91,7 @@ func TestAwsV4Signature_GetRequestWithRelativePath(t *testing.T) {
} }
oldNow := now oldNow := now
defer func() {now = oldNow}() defer func() { now = oldNow }()
now = setTime(defaultTime) now = setTime(defaultTime)
testRequestSigner(t, defaultRequestSigner, input, output) testRequestSigner(t, defaultRequestSigner, input, output)
@ -108,7 +109,7 @@ func TestAwsV4Signature_GetRequestWithDotPath(t *testing.T) {
} }
oldNow := now oldNow := now
defer func() {now = oldNow}() defer func() { now = oldNow }()
now = setTime(defaultTime) now = setTime(defaultTime)
testRequestSigner(t, defaultRequestSigner, input, output) testRequestSigner(t, defaultRequestSigner, input, output)
@ -126,7 +127,7 @@ func TestAwsV4Signature_GetRequestWithPointlessDotPath(t *testing.T) {
} }
oldNow := now oldNow := now
defer func() {now = oldNow}() defer func() { now = oldNow }()
now = setTime(defaultTime) now = setTime(defaultTime)
testRequestSigner(t, defaultRequestSigner, input, output) testRequestSigner(t, defaultRequestSigner, input, output)
@ -144,7 +145,7 @@ func TestAwsV4Signature_GetRequestWithUtf8Path(t *testing.T) {
} }
oldNow := now oldNow := now
defer func() {now = oldNow}() defer func() { now = oldNow }()
now = setTime(defaultTime) now = setTime(defaultTime)
testRequestSigner(t, defaultRequestSigner, input, output) testRequestSigner(t, defaultRequestSigner, input, output)
@ -162,7 +163,7 @@ func TestAwsV4Signature_GetRequestWithDuplicateQuery(t *testing.T) {
} }
oldNow := now oldNow := now
defer func() {now = oldNow}() defer func() { now = oldNow }()
now = setTime(defaultTime) now = setTime(defaultTime)
testRequestSigner(t, defaultRequestSigner, input, output) testRequestSigner(t, defaultRequestSigner, input, output)
@ -180,7 +181,7 @@ func TestAwsV4Signature_GetRequestWithMisorderedQuery(t *testing.T) {
} }
oldNow := now oldNow := now
defer func() {now = oldNow}() defer func() { now = oldNow }()
now = setTime(defaultTime) now = setTime(defaultTime)
testRequestSigner(t, defaultRequestSigner, input, output) testRequestSigner(t, defaultRequestSigner, input, output)
@ -198,7 +199,7 @@ func TestAwsV4Signature_GetRequestWithUtf8Query(t *testing.T) {
} }
oldNow := now oldNow := now
defer func() {now = oldNow}() defer func() { now = oldNow }()
now = setTime(defaultTime) now = setTime(defaultTime)
testRequestSigner(t, defaultRequestSigner, input, output) testRequestSigner(t, defaultRequestSigner, input, output)
@ -218,7 +219,7 @@ func TestAwsV4Signature_PostRequest(t *testing.T) {
} }
oldNow := now oldNow := now
defer func() {now = oldNow}() defer func() { now = oldNow }()
now = setTime(defaultTime) now = setTime(defaultTime)
testRequestSigner(t, defaultRequestSigner, input, output) testRequestSigner(t, defaultRequestSigner, input, output)
@ -238,7 +239,7 @@ func TestAwsV4Signature_PostRequestWithCapitalizedHeaderValue(t *testing.T) {
} }
oldNow := now oldNow := now
defer func() {now = oldNow}() defer func() { now = oldNow }()
now = setTime(defaultTime) now = setTime(defaultTime)
testRequestSigner(t, defaultRequestSigner, input, output) testRequestSigner(t, defaultRequestSigner, input, output)
@ -258,7 +259,7 @@ func TestAwsV4Signature_PostRequestPhfft(t *testing.T) {
} }
oldNow := now oldNow := now
defer func() {now = oldNow}() defer func() { now = oldNow }()
now = setTime(defaultTime) now = setTime(defaultTime)
testRequestSigner(t, defaultRequestSigner, input, output) testRequestSigner(t, defaultRequestSigner, input, output)
@ -278,7 +279,7 @@ func TestAwsV4Signature_PostRequestWithBody(t *testing.T) {
} }
oldNow := now oldNow := now
defer func() {now = oldNow}() defer func() { now = oldNow }()
now = setTime(defaultTime) now = setTime(defaultTime)
testRequestSigner(t, defaultRequestSigner, input, output) testRequestSigner(t, defaultRequestSigner, input, output)
@ -296,7 +297,7 @@ func TestAwsV4Signature_PostRequestWithQueryString(t *testing.T) {
} }
oldNow := now oldNow := now
defer func() {now = oldNow}() defer func() { now = oldNow }()
now = setTime(defaultTime) now = setTime(defaultTime)
testRequestSigner(t, defaultRequestSigner, input, output) testRequestSigner(t, defaultRequestSigner, input, output)
@ -314,7 +315,7 @@ func TestAwsV4Signature_GetRequestWithSecurityToken(t *testing.T) {
} }
oldNow := now oldNow := now
defer func() {now = oldNow}() defer func() { now = oldNow }()
now = setTime(secondDefaultTime) now = setTime(secondDefaultTime)
testRequestSigner(t, requestSignerWithToken, input, output) testRequestSigner(t, requestSignerWithToken, input, output)
@ -332,7 +333,7 @@ func TestAwsV4Signature_PostRequestWithSecurityToken(t *testing.T) {
} }
oldNow := now oldNow := now
defer func() {now = oldNow}() defer func() { now = oldNow }()
now = setTime(secondDefaultTime) now = setTime(secondDefaultTime)
testRequestSigner(t, requestSignerWithToken, input, output) testRequestSigner(t, requestSignerWithToken, input, output)
@ -355,7 +356,7 @@ func TestAwsV4Signature_PostRequestWithSecurityTokenAndAdditionalHeaders(t *test
} }
oldNow := now oldNow := now
defer func() {now = oldNow}() defer func() { now = oldNow }()
now = setTime(secondDefaultTime) now = setTime(secondDefaultTime)
testRequestSigner(t, requestSignerWithToken, input, output) testRequestSigner(t, requestSignerWithToken, input, output)
@ -377,7 +378,7 @@ func TestAwsV4Signature_PostRequestWithAmzDateButNoSecurityToken(t *testing.T) {
} }
oldNow := now oldNow := now
defer func() {now = oldNow}() defer func() { now = oldNow }()
now = setTime(secondDefaultTime) now = setTime(secondDefaultTime)
testRequestSigner(t, requestSigner, input, output) testRequestSigner(t, requestSigner, input, output)