-
Notifications
You must be signed in to change notification settings - Fork 21
/
handler_test.go
251 lines (215 loc) · 8.49 KB
/
handler_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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package main
import (
"bytes"
"fmt"
"net"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"time"
"github.com/aws/aws-sdk-go/aws/credentials"
v4 "github.com/aws/aws-sdk-go/aws/signer/v4"
"github.com/stretchr/testify/assert"
)
// func TestMain(m *testing.M) {
// log.SetOutput(ioutil.Discard)
// }
func newTestProxy(t *testing.T) *Handler {
thf := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello, client")
})
return newTestProxyWithHandler(t, &thf)
}
func newTestProxyWithHandler(t *testing.T, thf *http.HandlerFunc) *Handler {
ts := httptest.NewServer(thf)
tsURL, _ := url.Parse(ts.URL)
h, err := NewAwsS3ReverseProxy(Options{
Debug: true,
AllowedSourceEndpoint: "foobar.example.com",
AllowedSourceSubnet: []string{"0.0.0.0/0"},
AwsCredentials: []string{"fooooooooooooooo,bar"},
Region: "eu-test-1",
UpstreamInsecure: true,
UpstreamEndpoint: tsURL.Host,
})
assert.Nil(t, err)
return h
}
func signRequest(r *http.Request) {
// delete headers to get clean signature
r.Header.Del("accept-encoding")
r.Header.Del("authorization")
r.Header.Set("X-Amz-Date", "20060102T150405Z")
r.URL.RawPath = r.URL.Path
// compute the expected signature with valid credentials
body := bytes.NewReader([]byte{})
signTime, _ := time.Parse("20060102T150405Z", r.Header["X-Amz-Date"][0])
signer := v4.NewSigner(credentials.NewStaticCredentialsFromCreds(credentials.Value{
AccessKeyID: "fooooooooooooooo",
SecretAccessKey: "bar",
}))
signer.Sign(r, body, "s3", "eu-test-1", signTime)
}
func verifySignature(w http.ResponseWriter, r *http.Request) {
// save copy of the received signature
receivedAuthorization := r.Header["Authorization"][0]
// delete headers to get clean signature
r.Header.Del("accept-encoding")
r.Header.Del("authorization")
// compute the expected signature with valid credentials
body := bytes.NewReader([]byte{})
signTime, _ := time.Parse("20060102T150405Z", r.Header["X-Amz-Date"][0])
signer := v4.NewSigner(credentials.NewStaticCredentialsFromCreds(credentials.Value{
AccessKeyID: "fooooooooooooooo",
SecretAccessKey: "bar",
}))
signer.Sign(r, body, "s3", "eu-test-1", signTime)
expectedAuthorization := r.Header["Authorization"][0]
// verify signature
fmt.Fprintln(w, receivedAuthorization, expectedAuthorization)
if receivedAuthorization == expectedAuthorization {
fmt.Fprintln(w, "ok")
} else {
fmt.Fprintln(w, "failed signature check")
}
}
func TestHandlerMissingAmzDate(t *testing.T) {
h := newTestProxy(t)
req := httptest.NewRequest(http.MethodGet, "http://foobar.example.com", nil)
resp := httptest.NewRecorder()
h.ServeHTTP(resp, req)
assert.Equal(t, 400, resp.Code)
assert.Contains(t, resp.Body.String(), "X-Amz-Date header missing or set multiple times")
}
func TestHandlerMissingAuthorization(t *testing.T) {
h := newTestProxy(t)
req := httptest.NewRequest(http.MethodGet, "http://foobar.example.com", nil)
req.Header.Set("X-Amz-Date", "20060102T150405Z")
resp := httptest.NewRecorder()
h.ServeHTTP(resp, req)
assert.Equal(t, 400, resp.Code)
assert.Contains(t, resp.Body.String(), "Authorization header missing or set multiple times")
}
func TestHandlerMissingCredential(t *testing.T) {
h := newTestProxy(t)
req := httptest.NewRequest(http.MethodGet, "http://foobar.example.com", nil)
req.Header.Set("X-Amz-Date", "20060102T150405Z")
req.Header.Set("Authorization", "foobar")
resp := httptest.NewRecorder()
h.ServeHTTP(resp, req)
assert.Equal(t, 400, resp.Code)
assert.Contains(t, resp.Body.String(), "invalid Authorization header: Credential not found")
}
func TestHandlerInvalidSignature(t *testing.T) {
h := newTestProxy(t)
req := httptest.NewRequest(http.MethodGet, "http://foobar.example.com", nil)
req.Header.Set("X-Amz-Date", "20060102T150405Z")
req.Header.Set("Authorization", "AWS4-HMAC-SHA256 Credential=fooooooooooooooo/20190101/eu-test-1/s3/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature=some-signature")
resp := httptest.NewRecorder()
h.ServeHTTP(resp, req)
assert.Equal(t, 400, resp.Code)
assert.Contains(t, resp.Body.String(), "invalid signature in Authorization header")
}
func TestHandlerValidSignature(t *testing.T) {
h := newTestProxy(t)
req := httptest.NewRequest(http.MethodGet, "http://foobar.example.com", nil)
signRequest(req)
resp := httptest.NewRecorder()
h.ServeHTTP(resp, req)
assert.Equal(t, 200, resp.Code)
assert.Contains(t, resp.Body.String(), "Hello, client")
}
func TestHandlerInvalidCredential(t *testing.T) {
h := newTestProxy(t)
req := httptest.NewRequest(http.MethodGet, "http://foobar.example.com", nil)
req.Header.Set("X-Amz-Date", "20060102T150405Z")
req.Header.Set("Authorization", "AWS4-HMAC-SHA256 Credential=XXXooooooooooooo/20060102/eu-test-1/s3/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature=a0d5e0c0924c1f9298c5f2a3925e202657bf1e239a1d6856235cbe0702855334") // signature computed manually for this test case
resp := httptest.NewRecorder()
h.ServeHTTP(resp, req)
assert.Equal(t, 400, resp.Code)
assert.Contains(t, resp.Body.String(), "invalid AccessKeyID in Credential")
}
func TestHandlerInvalidSourceSubnet(t *testing.T) {
h := newTestProxy(t)
_, newNet, _ := net.ParseCIDR("172.27.42.0/24")
h.AllowedSourceSubnet = []*net.IPNet{newNet}
req := httptest.NewRequest(http.MethodGet, "http://foobar.example.com", nil)
req.Header.Set("X-Amz-Date", "20060102T150405Z")
req.Header.Set("Authorization", "AWS4-HMAC-SHA256 Credential=XXXooooooooooooo/20060102/eu-test-1/s3/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature=a0d5e0c0924c1f9298c5f2a3925e202657bf1e239a1d6856235cbe0702855334") // signature computed manually for this test case
resp := httptest.NewRecorder()
h.ServeHTTP(resp, req)
assert.Equal(t, 400, resp.Code)
assert.Contains(t, resp.Body.String(), "source IP not allowed")
}
func TestHandlerInvalidAmzDate(t *testing.T) {
h := newTestProxy(t)
req := httptest.NewRequest(http.MethodGet, "http://foobar.example.com", nil)
req.Header.Set("X-Amz-Date", "foobar")
req.Header.Set("Authorization", "AWS4-HMAC-SHA256 Credential=fooooooooooooooo/20060102/eu-test-1/s3/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature=a0d5e0c0924c1f9298c5f2a3925e202657bf1e239a1d6856235cbe0702855334") // signature computed manually for this test case
resp := httptest.NewRecorder()
h.ServeHTTP(resp, req)
assert.Equal(t, 400, resp.Code)
assert.Contains(t, resp.Body.String(), "error parsing X-Amz-Date foobar")
}
func TestHandlerRawPathEncodingMatchingSignature(t *testing.T) {
thf := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
verifySignature(w, r)
})
h := newTestProxyWithHandler(t, &thf)
urls := []string{
"http://foobar.example.com/foo%3Dbar/test.txt",
"http://foobar.example.com/foo=bar/test.txt",
"http://foobar.example.com/foo%3Dbar/test.txt?marker=1000",
"http://foobar.example.com/foo=bar/test.txt?marker=1000",
}
for _, url := range urls {
req := httptest.NewRequest(http.MethodGet, url, nil)
signRequest(req)
resp := httptest.NewRecorder()
h.ServeHTTP(resp, req)
assert.Equal(t, 200, resp.Code)
assert.Contains(t, strings.TrimSpace(resp.Body.String()), "ok")
}
}
func TestHandlerWithQueryArgs(t *testing.T) {
thf := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
verifySignature(w, r)
if r.URL.Query().Get("marker") == "1000" {
fmt.Fprintln(w, "marker-ok")
} else {
fmt.Fprintln(w, "marker missing")
}
})
h := newTestProxyWithHandler(t, &thf)
urls := []string{
"http://foobar.example.com/foo%3Dbar/test.txt?marker=1000",
"http://foobar.example.com/foo=bar/test.txt?marker=1000",
}
for _, url := range urls {
req := httptest.NewRequest(http.MethodGet, url, nil)
signRequest(req)
resp := httptest.NewRecorder()
h.ServeHTTP(resp, req)
assert.Equal(t, 200, resp.Code)
assert.Contains(t, strings.TrimSpace(resp.Body.String()), "marker-ok")
}
}
func TestHandlerPassCustomHeaders(t *testing.T) {
thf := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("x-aws-s3-reverse-proxy") == "testing" {
fmt.Fprintln(w, "ok")
} else {
fmt.Fprintln(w, "header missing")
}
})
h := newTestProxyWithHandler(t, &thf)
req := httptest.NewRequest(http.MethodGet, "http://foobar.example.com", nil)
signRequest(req)
req.Header.Set("x-aws-s3-reverse-proxy", "testing")
resp := httptest.NewRecorder()
h.ServeHTTP(resp, req)
assert.Equal(t, 200, resp.Code)
assert.Contains(t, strings.TrimSpace(resp.Body.String()), "ok")
}