Skip to content

Commit

Permalink
Fixes PR comments
Browse files Browse the repository at this point in the history
Signed-off-by: Roshan Patil <[email protected]>
  • Loading branch information
rosspatil committed Jul 5, 2022
1 parent 8e1480e commit 9e83337
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,11 @@ func main() {
}

if *awsRegion != "" {
httpClient.Transport = roundtripper.NewAWSSigningTransport(httpTransport, awsRegion, logger)
httpClient.Transport, err = roundtripper.NewAWSSigningTransport(httpTransport, *awsRegion, logger)
if err != nil {
_ = level.Error(logger).Log("msg", "failed to create AWS transport", "err", err)
os.Exit(1)
}
}

// version metric
Expand Down
16 changes: 8 additions & 8 deletions pkg/roundtripper/roundtripper.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"io"
"io/ioutil"
"net/http"
"os"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
Expand All @@ -38,29 +37,29 @@ const (
type AWSSigningTransport struct {
t http.RoundTripper
creds aws.Credentials
region *string
region string
log log.Logger
}

func NewAWSSigningTransport(transport http.RoundTripper, region *string, log log.Logger) *AWSSigningTransport {
cfg, err := config.LoadDefaultConfig(context.Background(), config.WithRegion(*region))
func NewAWSSigningTransport(transport http.RoundTripper, region string, log log.Logger) (*AWSSigningTransport, error) {
cfg, err := config.LoadDefaultConfig(context.Background(), config.WithRegion(region))
if err != nil {
_ = level.Error(log).Log("msg", "fail to load aws default config", "err", err)
os.Exit(1)
return nil, err
}

creds, err := cfg.Credentials.Retrieve(context.Background())
if err != nil {
_ = level.Error(log).Log("msg", "fail to retrive aws credentials", "err", err)
os.Exit(1)
return nil, err
}

return &AWSSigningTransport{
t: transport,
region: region,
creds: creds,
log: log,
}
}, err
}

func (a *AWSSigningTransport) RoundTrip(req *http.Request) (*http.Response, error) {
Expand All @@ -71,7 +70,7 @@ func (a *AWSSigningTransport) RoundTrip(req *http.Request) (*http.Response, erro
return nil, err
}
req.Body = newReader
err = signer.SignHTTP(context.Background(), a.creds, req, payloadHash, service, *a.region, time.Now())
err = signer.SignHTTP(context.Background(), a.creds, req, payloadHash, service, a.region, time.Now())
if err != nil {
_ = level.Error(a.log).Log("msg", "fail to sign request body", "err", err)
return nil, err
Expand All @@ -84,6 +83,7 @@ func hashPayload(r io.ReadCloser) (payloadHash string, newReader io.ReadCloser,
if r == nil {
payload = []byte("")
} else {
defer r.Close()
payload, err = ioutil.ReadAll(r)
if err != nil {
return
Expand Down

0 comments on commit 9e83337

Please sign in to comment.