Skip to content

Commit

Permalink
Fix AWS v4 signer when body implements io.ReadSeeker
Browse files Browse the repository at this point in the history
This commit adds special handling for the case that the request
body already implements `io.ReadSeeker`. In that case, we don't
have to read the body, but simply pass it to the signer.

It also simplifies the handling of the response after executing
the request.

Close olivere#1306
  • Loading branch information
veqryn committed Apr 10, 2020
1 parent fac54be commit 80d41b5
Showing 1 changed file with 14 additions and 21 deletions.
35 changes: 14 additions & 21 deletions aws/v4/aws_v4.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package v4

import (
"bytes"
"io"
"io/ioutil"
"net/http"
"net/url"
Expand Down Expand Up @@ -53,37 +54,29 @@ func (st Transport) RoundTrip(req *http.Request) (*http.Response, error) {
// Escaping path
req.URL.RawPath = url.PathEscape(req.URL.RawPath)
}

now := time.Now().UTC()
req.Header.Set("Date", now.Format(time.RFC3339))

var err error
switch req.Body {
case nil:
_, err = st.signer.Sign(req, nil, "es", st.region, now)
default:
buf, err := ioutil.ReadAll(req.Body)
if err != nil {
return nil, err
switch body := req.Body.(type) {
case io.ReadSeeker:
_, err = st.signer.Sign(req, body, "es", st.region, now)
default:
buf, err := ioutil.ReadAll(req.Body)
if err != nil {
return nil, err
}
req.Body = ioutil.NopCloser(bytes.NewReader(buf))
_, err = st.signer.Sign(req, bytes.NewReader(buf), "es", st.region, time.Now().UTC())
}
req.Body = ioutil.NopCloser(bytes.NewReader(buf))
_, err = st.signer.Sign(req, bytes.NewReader(buf), "es", st.region, time.Now().UTC())
}
if err != nil {
return nil, err
}
resp, err := st.client.Do(req)
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
if resp.Body != nil {
buf := new(bytes.Buffer)
_, err = buf.ReadFrom(resp.Body)
if err != nil {
return nil, err
}
resp.Body = ioutil.NopCloser(bytes.NewReader(buf.Bytes()))
}
return resp, nil
return st.client.Do(req)
}

0 comments on commit 80d41b5

Please sign in to comment.