-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsdkv2roundtripper_test.go
206 lines (193 loc) · 6.5 KB
/
sdkv2roundtripper_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package awsgosignv4_test
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
v4 "github.com/aws/aws-sdk-go-v2/aws/signer/v4"
awsgosignv4 "github.com/jriquelme/awsgosigv4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// verifyRoundTripper verifies the signature in the Authorization header, returning a 200 empty response if everything
// is OK, otherwise returns 400. The expected signature is left in the field ExpectedSignature for later inspection.
type verifyRoundTripper struct {
Key string
Secret string
Service string
Region string
ExpectedSignature string
}
func (r *verifyRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
recorder := httptest.NewRecorder()
// check signature
authorization := req.Header.Get("Authorization")
if authorization == "" {
recorder.WriteHeader(http.StatusUnauthorized)
return recorder.Result(), nil
}
if !strings.HasPrefix(authorization, "AWS4-HMAC-SHA256") {
recorder.WriteHeader(http.StatusBadRequest)
_, _ = fmt.Fprint(recorder, "signature expected to start with AWS4-HMAC-SHA256")
return recorder.Result(), nil
}
// copy request and sign it again
req2 := req.Clone(context.Background())
// calculate body hash
b := []byte("")
if req2.Body != nil {
var err error
b, err = ioutil.ReadAll(req2.Body)
if err != nil {
recorder.WriteHeader(http.StatusBadRequest)
_, _ = fmt.Fprintf(recorder, "error reading body: %s", err)
return recorder.Result(), nil
}
req2.Body = ioutil.NopCloser(bytes.NewReader(b))
}
hash := sha256.Sum256(b)
hexHash := hex.EncodeToString(hash[:])
// use time of X-Amz-Date
xAmzDate := req2.Header.Get("X-Amz-Date")
signingTime, err := time.Parse("20060102T150405Z", xAmzDate)
if err != nil {
recorder.WriteHeader(http.StatusBadRequest)
_, _ = fmt.Fprintf(recorder, "error parsing X-Amz-Date=%s: %s", xAmzDate, err)
return recorder.Result(), nil
}
// sign
signer := v4.NewSigner()
err = signer.SignHTTP(context.Background(), aws.Credentials{
AccessKeyID: r.Key,
SecretAccessKey: r.Secret,
}, req2, hexHash, r.Service, r.Region, signingTime)
if err != nil {
recorder.WriteHeader(http.StatusBadRequest)
_, _ = fmt.Fprintf(recorder, "error signing request copy: %s", err)
return recorder.Result(), nil
}
// resulting signature must match Authorization header
r.ExpectedSignature = req2.Header.Get("Authorization")
if r.ExpectedSignature != authorization {
recorder.WriteHeader(http.StatusBadRequest)
_, _ = fmt.Fprintf(recorder, "invalid signature")
return recorder.Result(), nil
}
recorder.WriteHeader(http.StatusOK)
return recorder.Result(), nil
}
func TestSignV4SDKV2_RoundTripOK(t *testing.T) {
t.Parallel()
newRoundTripper := func() (*awsgosignv4.SignV4SDKV2, *verifyRoundTripper) {
verifier := &verifyRoundTripper{
Key: "jriquelme",
Secret: "notedigo",
Service: "es",
Region: "us-east-1",
}
r := &awsgosignv4.SignV4SDKV2{
RoundTripper: verifier,
Credentials: aws.Credentials{
AccessKeyID: "jriquelme",
SecretAccessKey: "notedigo",
},
Region: "us-east-1",
Service: "es",
Now: time.Now,
}
return r, verifier
}
t.Run("get", func(t *testing.T) {
t.Parallel()
request, err := http.NewRequest("GET", "http://localhost/hi", nil)
require.Nil(t, err)
request.Header.Set("X-API-Key", "MY-API-KEY")
r, verifier := newRoundTripper()
response, err := r.RoundTrip(request)
assert.Nil(t, err)
assert.NotNil(t, response)
assert.Equal(t, http.StatusOK, response.StatusCode)
assert.Equal(t, verifier.ExpectedSignature, request.Header.Get("Authorization"))
})
t.Run("getWithQueryParams", func(t *testing.T) {
t.Parallel()
request, err := http.NewRequest("GET", "http://localhost/hi?a=b&c=d", nil)
require.Nil(t, err)
request.Header.Set("X-API-Key", "MY-API-KEY")
r, verifier := newRoundTripper()
response, err := r.RoundTrip(request)
assert.Nil(t, err)
assert.NotNil(t, response)
assert.Equal(t, http.StatusOK, response.StatusCode)
assert.Equal(t, verifier.ExpectedSignature, request.Header.Get("Authorization"))
})
t.Run("post", func(t *testing.T) {
t.Parallel()
request, err := http.NewRequest("POST", "http://localhost/hi", bytes.NewReader([]byte(`{"msg":"hi"}`)))
require.Nil(t, err)
request.Header.Set("Content-Type", "application/json")
request.Header.Set("X-API-Key", "MY-API-KEY")
r, verifier := newRoundTripper()
response, err := r.RoundTrip(request)
assert.Nil(t, err)
assert.NotNil(t, response)
assert.Equal(t, http.StatusOK, response.StatusCode)
assert.Equal(t, verifier.ExpectedSignature, request.Header.Get("Authorization"))
})
}
// mutateRequestRoundTripper applies the Mutate function to the incoming request before invoking the wrapped
// RoundTripper.
type mutateRequestRoundTripper struct {
RoundTripper http.RoundTripper
Mutate func(req *http.Request)
}
func (r *mutateRequestRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
r.Mutate(req)
return r.RoundTripper.RoundTrip(req)
}
func TestSignV4SDKV2_RoundTripErr(t *testing.T) {
t.Parallel()
verifier := &verifyRoundTripper{
Key: "jriquelme",
Secret: "notedigo",
Service: "es",
Region: "us-east-1",
}
// this chain of RoundTripper (sign -> mutate request -> sign again) should generate different signatures.
r := &awsgosignv4.SignV4SDKV2{
RoundTripper: &mutateRequestRoundTripper{
RoundTripper: verifier,
Mutate: func(req *http.Request) {
// this header will be considered in the expected signature by the verifier, but wasn't included when
// SignV4SDKV2 created the signature present in Authorization
req.Header.Set("X-Oh-No", "asdf")
},
},
Credentials: aws.Credentials{
AccessKeyID: "jriquelme",
SecretAccessKey: "notedigo",
},
Region: "us-east-1",
Service: "es",
Now: time.Now,
}
request, err := http.NewRequest("POST", "http://localhost/hi", bytes.NewReader([]byte(`{"msg":"hi"}`)))
require.Nil(t, err)
request.Header.Set("Content-Type", "application/json")
request.Header.Set("X-API-Key", "MY-API-KEY")
response, err := r.RoundTrip(request)
assert.Nil(t, err)
assert.NotNil(t, response)
assert.Equal(t, http.StatusBadRequest, response.StatusCode)
assert.NotEqual(t, verifier.ExpectedSignature, request.Header.Get("Authorization"))
assert.Contains(t, verifier.ExpectedSignature, ";x-oh-no")
assert.NotContains(t, request.Header.Get("Authorization"), ";x-oh-no")
}