-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
140 additions
and
10 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
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
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,54 @@ | ||
package http | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"time" | ||
|
||
awsAuth "github.com/ONSdigital/dp-elasticsearch/v2/awsauth" | ||
) | ||
|
||
type AwsSignerRoundTripper struct { | ||
signer *awsAuth.Signer | ||
roundTripper http.RoundTripper | ||
} | ||
|
||
func NewAWSSignerRoundTripper(awsFilename, awsProfile, awsRegion, awsService string, customTransport http.RoundTripper) (*AwsSignerRoundTripper, error) { | ||
var roundTripper http.RoundTripper | ||
if awsRegion == "" || awsService == "" { | ||
return nil, fmt.Errorf("aws region and service should be valid options") | ||
} | ||
awsSigner, err := awsAuth.NewAwsSigner(awsFilename, awsProfile, awsRegion, awsService) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create aws v4 signer: %w", err) | ||
} | ||
|
||
if customTransport == nil { | ||
roundTripper = http.DefaultTransport | ||
} else { | ||
roundTripper = customTransport | ||
} | ||
|
||
return &AwsSignerRoundTripper{ | ||
signer: awsSigner, | ||
roundTripper: roundTripper, | ||
}, nil | ||
} | ||
|
||
func (srt *AwsSignerRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { | ||
var body []byte | ||
var err error | ||
if req.Body != nil { | ||
body, err = io.ReadAll(req.Body) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to read request body: %w", err) | ||
} | ||
} | ||
if err := srt.signer.Sign(req, bytes.NewReader(body), time.Now()); err != nil { | ||
return nil, fmt.Errorf("failed to sign the request: %w", err) | ||
} | ||
|
||
return srt.roundTripper.RoundTrip(req) | ||
} |
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,35 @@ | ||
package http_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ONSdigital/dp-net/http" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewAWSSignerRoundTripper(t *testing.T) { | ||
t.Parallel() | ||
|
||
awsSignerRT, err := http.NewAWSSignerRoundTripper("some_filename", "some_profile", "some_region", "some_service", nil) | ||
|
||
assert.Nil(t, err, "error should be nil") | ||
assert.NotNilf(t, awsSignerRT, "aws signer roundtripper should not return nil") | ||
} | ||
|
||
func TestNewAWSSignerRoundTripper_WhenAWSRegionIsEmpty_Returns(t *testing.T) { | ||
t.Parallel() | ||
|
||
awsSignerRT, err := http.NewAWSSignerRoundTripper("some_filename", "some_profile", "", "some_service", nil) | ||
|
||
assert.NotNil(t, err, "error should not be nil") | ||
assert.Nil(t, awsSignerRT, "aws signer roundtripper should return nil") | ||
} | ||
|
||
func TestNewAWSSignerRoundTripper_WhenAWSServiceIsEmpty_Returns(t *testing.T) { | ||
t.Parallel() | ||
|
||
awsSignerRT, err := http.NewAWSSignerRoundTripper("some_filename", "", "some_region", "", nil) | ||
|
||
assert.NotNil(t, err, "error should not be nil") | ||
assert.Nil(t, awsSignerRT, "aws signer roundtripper should return nil") | ||
} |