Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sign handler which adds Authorization header #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions aws/signer/v4/v4util/v4util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package v4util

import (
"fmt"

"github.com/awslabs/aws-sdk-go/aws"
)

// Add a Authorization header to requests using the v4 signature format.
//
// It is required that the request has previously been signed using the query
// string. This is useful for non-production services like DynamoDB Local which
// do not properly support signing using the query string.
func SignWithHeader(req *aws.Request) {
headers := map[string]string{
"X-Amz-Algorithm": "",
"X-Amz-Credential": "",
"X-Amz-SignedHeaders": "",
"X-Amz-Signature": "",
}
query := req.HTTPRequest.URL.Query()
for header, _ := range headers {
v := query.Get(header)
if v == "" {
req.Error = fmt.Errorf("'%s' was not found in the query string", header)
return
}
headers[header] = v
}
authorization := fmt.Sprintf("%s Credential=%s, SignedHeaders=%s, Signature=%s",
headers["X-Amz-Algorithm"], headers["X-Amz-Credential"],
headers["X-Amz-SignedHeaders"], headers["X-Amz-Signature"])
req.HTTPRequest.Header.Set("Authorization", authorization)
}
65 changes: 65 additions & 0 deletions aws/signer/v4/v4util/v4util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package v4util

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/awslabs/aws-sdk-go/aws"
"github.com/awslabs/aws-sdk-go/service/dynamodb"
)

func TestSignWithHeader(t *testing.T) {
server := setupServer()
defer server.Close()

creds := aws.Creds("DUMMY_KEY", "DUMMY_SECRET", "")
config := &dynamodb.DynamoDBConfig{
Config: &aws.Config{
Credentials: creds,
Endpoint: server.URL,
},
}
db := dynamodb.New(config)
db.Handlers.Sign.PushBack(SignWithHeader)

_, err := db.ListTables(&dynamodb.ListTablesInput{})
if err != nil {
t.Fatal(err)
}
}

func TestSignWithHeaderFailed(t *testing.T) {
server := setupServer()
defer server.Close()

creds := aws.Creds("DUMMY_KEY", "DUMMY_SECRET", "")
config := &dynamodb.DynamoDBConfig{
Config: &aws.Config{
Credentials: creds,
Endpoint: server.URL,
},
}
db := dynamodb.New(config)
db.Handlers.Sign.Init() // remove the v4.Sign handler
db.Handlers.Sign.PushBack(SignWithHeader)

_, err := db.ListTables(&dynamodb.ListTablesInput{})
if err == nil {
t.Fatalf("Expected failure")
}
}

// setupServer creates test server which simply validates the presence of the
// Authorization header.
func setupServer() *httptest.Server {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
authorization := r.Header.Get("Authorization")
if authorization == "" {
w.WriteHeader(http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusOK)
}))
return server
}