-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
aws/signer/v4: Add support for URL.RawPath to signer
Adds support for the URL.RawPath added in Go1.5. This allows you to hint to the signer and Go HTTP client what the escaped form of the request's URI path will be. This is needed when using the AWS v4 Signer outside of the context of the SDK on http.Requests you manage. Also adds documentation to the signer that pre-escaping of the URI path is needed, and suggestions how how to do this. aws/signer/v4 TestStandaloneSign test function is an example pre-escaping the URI path before generating a signature.
- Loading branch information
Showing
7 changed files
with
280 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// +build !go1.5 | ||
|
||
package v4_test | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/signer/v4" | ||
"github.com/aws/aws-sdk-go/awstesting/unit" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestStandaloneSign(t *testing.T) { | ||
creds := unit.Session.Config.Credentials | ||
signer := v4.NewSigner(creds, func(s *v4.Signer) { | ||
s.Debug = aws.LogDebugWithSigning | ||
s.Logger = aws.NewDefaultLogger() | ||
}) | ||
|
||
for _, c := range standaloneSignCases { | ||
host := fmt.Sprintf("%s.%s.%s.amazonaws.com", | ||
c.SubDomain, c.Region, c.Service) | ||
|
||
req, err := http.NewRequest("GET", fmt.Sprintf("https://%s", host), nil) | ||
assert.NoError(t, err) | ||
|
||
req.URL.Path = c.OrigURI | ||
req.URL.RawQuery = c.OrigQuery | ||
req.URL.Opaque = fmt.Sprintf("//%s%s", host, c.EscapedURI) | ||
opaqueURI := req.URL.Opaque | ||
|
||
_, err = signer.Sign(req, nil, c.Service, c.Region, time.Unix(0, 0)) | ||
assert.NoError(t, err) | ||
|
||
actual := req.Header.Get("Authorization") | ||
assert.Equal(t, c.ExpSig, actual) | ||
assert.Equal(t, c.OrigURI, req.URL.Path) | ||
assert.Equal(t, opaqueURI, req.URL.Opaque) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// +build go1.5 | ||
|
||
package v4_test | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/signer/v4" | ||
"github.com/aws/aws-sdk-go/awstesting/unit" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestStandaloneSign(t *testing.T) { | ||
creds := unit.Session.Config.Credentials | ||
signer := v4.NewSigner(creds, func(s *v4.Signer) { | ||
s.Debug = aws.LogDebugWithSigning | ||
s.Logger = aws.NewDefaultLogger() | ||
}) | ||
|
||
for _, c := range standaloneSignCases { | ||
host := fmt.Sprintf("https://%s.%s.%s.amazonaws.com", | ||
c.SubDomain, c.Region, c.Service) | ||
|
||
req, err := http.NewRequest("GET", host, nil) | ||
assert.NoError(t, err) | ||
|
||
req.URL.Path = c.OrigURI | ||
req.URL.RawQuery = c.OrigQuery | ||
|
||
// Set the Raw, pre-escaped path of the URL so the signer will known | ||
// what will be sent to the service when building the canonical string. | ||
req.URL.RawPath = req.URL.EscapedPath() | ||
|
||
_, err = signer.Sign(req, nil, c.Service, c.Region, time.Unix(0, 0)) | ||
assert.NoError(t, err) | ||
|
||
actual := req.Header.Get("Authorization") | ||
assert.Equal(t, c.ExpSig, actual) | ||
assert.Equal(t, c.OrigURI, req.URL.Path) | ||
assert.Equal(t, c.EscapedURI, req.URL.RawPath) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// +build go1.5 | ||
|
||
package v4 | ||
|
||
import ( | ||
"net/url" | ||
"strings" | ||
) | ||
|
||
func getURIPath(u *url.URL) string { | ||
var uri string | ||
|
||
if len(u.Opaque) > 0 { | ||
uri = "/" + strings.Join(strings.Split(u.Opaque, "/")[3:], "/") | ||
} else if len(u.RawPath) > 0 { | ||
// RawPath was added in Go 1.5. It like Opaque, but applies to just | ||
// the URI path not the full URL. | ||
uri = u.RawPath | ||
} else { | ||
uri = u.Path | ||
} | ||
|
||
if len(uri) == 0 { | ||
uri = "/" | ||
} | ||
|
||
return uri | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// +build !go1.5 | ||
|
||
package v4 | ||
|
||
import ( | ||
"net/url" | ||
"strings" | ||
) | ||
|
||
func getURIPath(u *url.URL) string { | ||
var uri string | ||
|
||
if len(u.Opaque) > 0 { | ||
uri = "/" + strings.Join(strings.Split(u.Opaque, "/")[3:], "/") | ||
} else { | ||
uri = u.Path | ||
} | ||
|
||
if len(uri) == 0 { | ||
uri = "/" | ||
} | ||
|
||
return uri | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters