From cfc218bc4189755c46b10209f6f48c1967339cab Mon Sep 17 00:00:00 2001 From: rahulmadathumpalliyalil Date: Mon, 10 Jan 2022 18:05:49 +0000 Subject: [PATCH] Implement roundtripper --- go.mod | 4 ++- go.sum | 19 +++++++++++ http/client.go | 38 ++++++++++++++++------ http/custom_roundtripper.go | 54 ++++++++++++++++++++++++++++++++ http/custom_roundtripper_test.go | 35 +++++++++++++++++++++ 5 files changed, 140 insertions(+), 10 deletions(-) create mode 100644 http/custom_roundtripper.go create mode 100644 http/custom_roundtripper_test.go diff --git a/go.mod b/go.mod index 1b15df1..9500aed 100644 --- a/go.mod +++ b/go.mod @@ -4,15 +4,17 @@ go 1.13 require ( github.com/ONSdigital/dp-api-clients-go v1.41.1 + github.com/ONSdigital/dp-elasticsearch/v2 v2.3.0 // indirect github.com/ONSdigital/dp-healthcheck v1.1.0 // indirect github.com/ONSdigital/log.go v1.1.0 // indirect - github.com/ONSdigital/log.go/v2 v2.0.5 + github.com/ONSdigital/log.go/v2 v2.0.9 github.com/fatih/color v1.12.0 // indirect github.com/gorilla/mux v1.8.0 github.com/justinas/alice v1.2.0 github.com/mattn/go-isatty v0.0.13 // indirect github.com/pkg/errors v0.9.1 github.com/smartystreets/goconvey v1.6.4 + github.com/stretchr/testify v1.7.0 // indirect golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e // indirect ) diff --git a/go.sum b/go.sum index ff79211..df93e62 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ github.com/ONSdigital/dp-api-clients-go v1.28.0/go.mod h1:iyJy6uRL4B6OYOJA0XMr5U github.com/ONSdigital/dp-api-clients-go v1.34.3/go.mod h1:kX+YKuoLYLfkeLHMvQKRRydZVxO7ZEYyYiwG2xhV51E= github.com/ONSdigital/dp-api-clients-go v1.41.1 h1:xkeT6dCTFSAoBpZxgiJUiuqgcfjCX+c52CIiZo1Y2iU= github.com/ONSdigital/dp-api-clients-go v1.41.1/go.mod h1:Ga1+ANjviu21NFJI9wp5NctJIdB4TJLDGbpQFl2V8Wc= +github.com/ONSdigital/dp-elasticsearch/v2 v2.3.0 h1:RPGEjjpnJQrbEEFY/uPf2yb4ewhRlDk9AJOc7LZfWW4= +github.com/ONSdigital/dp-elasticsearch/v2 v2.3.0/go.mod h1:B+WXR1PRVgrU1/tS3UygATLVTnX+5Sp+M0w5i3LROac= github.com/ONSdigital/dp-healthcheck v1.0.5/go.mod h1:2wbVAUHMl9+4tWhUlxYUuA1dnf2+NrwzC+So5f5BMLk= github.com/ONSdigital/dp-healthcheck v1.1.0 h1:fKOf8MMe8l4EW28ljX0wNZ5oZTgx/slAs7lyEx4eq2c= github.com/ONSdigital/dp-healthcheck v1.1.0/go.mod h1:vZwyjMJiCHjp/sJ2R1ZEqzZT0rJ0+uHVGwxqdP4J5vg= @@ -22,6 +24,11 @@ github.com/ONSdigital/log.go v1.1.0/go.mod h1:0hOVuYR3bDUI30VRo48d5KHfJIoe+spuPX github.com/ONSdigital/log.go/v2 v2.0.0/go.mod h1:PR7vXrv9dZKUc7SI/0toxBbStk84snmybBnWpe+xY2o= github.com/ONSdigital/log.go/v2 v2.0.5 h1:kl2lF0vr3BQDwPTAUcarDvX4Um3uE3fjVRfLoHAarBc= github.com/ONSdigital/log.go/v2 v2.0.5/go.mod h1:PR7vXrv9dZKUc7SI/0toxBbStk84snmybBnWpe+xY2o= +github.com/ONSdigital/log.go/v2 v2.0.9/go.mod h1:VyTDkL82FtiAkaNFaT+bURBhLbP7NsIx4rkVbdpiuEg= +github.com/aws/aws-sdk-go v1.38.15 h1:usaPeqoxFUzy0FfBLZLZHya5Kv2cpURjb1jqCa7+odA= +github.com/aws/aws-sdk-go v1.38.15/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/facebookgo/freeport v0.0.0-20150612182905-d4adf43b75b9/go.mod h1:uPmAp6Sws4L7+Q/OokbWDAK1ibXYhB3PXFP1kol5hPg= github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU= github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM= @@ -38,6 +45,9 @@ github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB7 github.com/hokaccha/go-prettyjson v0.0.0-20190818114111-108c894c2c0e/go.mod h1:pFlLw2CfqZiIBOx6BuCeRLCrfxBJipTY0nIOF/VbGcI= github.com/hokaccha/go-prettyjson v0.0.0-20210113012101-fb4e108d2519 h1:nqAlWFEdqI0ClbTDrhDvE/8LeQ4pftrqKUX9w5k0j3s= github.com/hokaccha/go-prettyjson v0.0.0-20210113012101-fb4e108d2519/go.mod h1:pFlLw2CfqZiIBOx6BuCeRLCrfxBJipTY0nIOF/VbGcI= +github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= +github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= +github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/justinas/alice v1.2.0 h1:+MHSA/vccVCF4Uq37S42jwlkvI2Xzl7zTPCN5BnZNVo= @@ -54,11 +64,16 @@ github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyex github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= github.com/smartystreets/assertions v1.2.0 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N3yZFZkDFs= github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo= github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s= github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210314154223-e6e6c4f2bb5b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= @@ -94,3 +109,7 @@ golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBn golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/http/client.go b/http/client.go index c45fd7e..8c77367 100644 --- a/http/client.go +++ b/http/client.go @@ -26,6 +26,17 @@ type Client struct { HTTPClient *http.Client } +// DefaultTransport is the default implementation of Transport and is +// used by DefaultClient. +var DefaultTransport = &http.Transport{ + DialContext: (&net.Dialer{ + Timeout: 5 * time.Second, + }).DialContext, + TLSHandshakeTimeout: 5 * time.Second, + MaxIdleConns: 10, + IdleConnTimeout: 30 * time.Second, +} + // DefaultClient is a dp-net specific http client with sensible timeouts, // exponential backoff, and a contextual dialer. var DefaultClient = &Client{ @@ -33,15 +44,8 @@ var DefaultClient = &Client{ RetryTime: 20 * time.Millisecond, HTTPClient: &http.Client{ - Timeout: 10 * time.Second, - Transport: &http.Transport{ - DialContext: (&net.Dialer{ - Timeout: 5 * time.Second, - }).DialContext, - TLSHandshakeTimeout: 5 * time.Second, - MaxIdleConns: 10, - IdleConnTimeout: 30 * time.Second, - }, + Timeout: 10 * time.Second, + Transport: DefaultTransport, }, } @@ -68,6 +72,17 @@ func NewClient() Clienter { return &newClient } +// NewClientWithAwsSigner return a new client with aws signer profile. +func NewClientWithAwsSigner(awsFilename, awsProfile, awsRegion, awsService string) (Clienter, error) { + newClient := *DefaultClient + awsRoundTripper, err := NewAWSSignerRoundTripper(awsFilename, awsProfile, awsRegion, awsService, DefaultTransport) + if err != nil { + return nil, err + } + newClient.HTTPClient.Transport = awsRoundTripper + return &newClient, nil +} + // ClientWithTimeout facilitates creating a client and setting request timeout. func ClientWithTimeout(c Clienter, timeout time.Duration) Clienter { if c == nil { @@ -77,6 +92,11 @@ func ClientWithTimeout(c Clienter, timeout time.Duration) Clienter { return c } +// Clienter roundtripper calls the httpclient roundtripper. +func (c *Client) RoundTrip(req *http.Request) (*http.Response, error) { + return c.HTTPClient.Transport.RoundTrip(req) +} + // ClientWithListOfNonRetriablePaths facilitates creating a client and setting a // list of paths that should not be retried on failure. func ClientWithListOfNonRetriablePaths(c Clienter, paths []string) Clienter { diff --git a/http/custom_roundtripper.go b/http/custom_roundtripper.go new file mode 100644 index 0000000..34b3902 --- /dev/null +++ b/http/custom_roundtripper.go @@ -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) +} diff --git a/http/custom_roundtripper_test.go b/http/custom_roundtripper_test.go new file mode 100644 index 0000000..0567d17 --- /dev/null +++ b/http/custom_roundtripper_test.go @@ -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") +}