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

Veqryn aws support #839

Merged
merged 4 commits into from
Jul 28, 2018
Merged
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
32 changes: 32 additions & 0 deletions aws/signV4.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package aws

import (
"net/http"

"github.com/smartystreets/go-aws-auth"
)

// NewV4SigningClient returns an *http.Client that will sign all requests with AWS V4 Signing.
func NewV4SigningClient(credentials awsauth.Credentials) *http.Client {
return &http.Client{
Transport: V4Transport{
HTTPClient: http.DefaultClient,
Credentials: credentials,
},
}
}

// V4Transport is a RoundTripper that will sign requests with AWS V4 Signing
type V4Transport struct {
HTTPClient *http.Client
Credentials awsauth.Credentials
}

// RoundTrip uses the underlying RoundTripper transport, but signs request first with AWS V4 Signing
func (st V4Transport) RoundTrip(req *http.Request) (*http.Response, error) {
// Instead of directly modifying the request then calling http.DefaultTransport,
// instead restart the request with the HTTPClient.Do function,
// because the HTTPClient includes safeguards around not forwarding the
// signed Authorization header to untrusted domains.
return st.HTTPClient.Do(awsauth.Sign4(req, st.Credentials))
}
27 changes: 6 additions & 21 deletions recipes/aws-connect/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,13 @@ import (
"flag"
"fmt"
"log"
"net/http"

"github.com/olivere/elastic"
"github.com/olivere/elastic/aws"
"github.com/olivere/env"
"github.com/smartystreets/go-aws-auth"

"github.com/olivere/elastic"
)

type AWSSigningTransport struct {
HTTPClient *http.Client
Credentials awsauth.Credentials
}

// RoundTrip implementation
func (a AWSSigningTransport) RoundTrip(req *http.Request) (*http.Response, error) {
return a.HTTPClient.Do(awsauth.Sign4(req, a.Credentials))
}

func main() {
var (
accessKey = flag.String("access-key", env.String("", "AWS_ACCESS_KEY"), "Access Key ID")
Expand All @@ -52,14 +41,10 @@ func main() {
log.Fatal("missing -secret-key or AWS_SECRET_KEY environment variable")
}

signingTransport := AWSSigningTransport{
Credentials: awsauth.Credentials{
AccessKeyID: *accessKey,
SecretAccessKey: *secretKey,
},
HTTPClient: http.DefaultClient,
}
signingClient := &http.Client{Transport: http.RoundTripper(signingTransport)}
signingClient := aws.NewV4SigningClient(awsauth.Credentials{
AccessKeyID: *accessKey,
SecretAccessKey: *secretKey,
})

// Create an Elasticsearch client
client, err := elastic.NewClient(
Expand Down