forked from Kriechi/aws-s3-reverse-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_test.go
131 lines (110 loc) · 5.12 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
package main
import (
"fmt"
"net"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/stretchr/testify/assert"
)
// func TestMain(m *testing.M) {
// log.SetOutput(ioutil.Discard)
// }
func newTestHandler(t *testing.T) *Handler {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello, client")
}))
// defer ts.Close()
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 TestHandlerMissingAmzDate(t *testing.T) {
h := newTestHandler(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 := newTestHandler(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 := newTestHandler(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 := newTestHandler(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 := newTestHandler(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/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, 200, resp.Code)
assert.Contains(t, resp.Body.String(), "Hello, client")
}
func TestHandlerInvalidCredential(t *testing.T) {
h := newTestHandler(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 := newTestHandler(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 := newTestHandler(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")
}