-
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.EscapedPath to signer (#885)
Adds support for the URL.EscapedPath method 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 using the request signer outside of the SDK. Fix #866
- Loading branch information
Showing
7 changed files
with
236 additions
and
20 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,40 @@ | ||
// +build !go1.5 | ||
|
||
package v4_test | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
"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) | ||
|
||
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,40 @@ | ||
// +build go1.5 | ||
|
||
package v4_test | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
"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) | ||
|
||
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) | ||
|
||
// URL.EscapedPath() will be used by the signer to get the | ||
// escaped form of the request's URI path. | ||
req.URL.Path = c.OrigURI | ||
req.URL.RawQuery = c.OrigQuery | ||
|
||
_, 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.EscapedPath()) | ||
} | ||
} |
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,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.EscapedPath() | ||
} | ||
|
||
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