-
Notifications
You must be signed in to change notification settings - Fork 2
/
s3proxy_test.go
241 lines (187 loc) · 7.77 KB
/
s3proxy_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
package main
import (
"encoding/json"
"net/http"
"os"
"testing"
"time"
"github.com/gin-gonic/gin"
"github.com/mirakl/s3proxy/backend"
"github.com/mirakl/s3proxy/backend/backendtest"
"github.com/mirakl/s3proxy/router"
"github.com/mirakl/s3proxy/s3proxytest"
"github.com/stretchr/testify/assert"
)
var (
s3backend backend.Backend
r *gin.Engine
expiration = 15 * time.Minute
dummyBucket = "dummybucket"
dummyFile = "/dummyfolder/dummyfile"
s3proxyVersion = "9.9.9"
awsRegion = "eu-west-1"
accessKey = "123456"
secretKey = "ABCDEFGH12345"
serverAPIKey = "ABCD-123"
)
// Launch gin server with a fake backend implementation
func setup() {
var err error
s3backend, err = backendtest.NewS3FakeBackend(backend.S3BackendConfig{
Region: awsRegion,
AccessKey: accessKey,
SecretKey: secretKey,
})
if err != nil {
os.Exit(1)
}
r = router.NewGinEngine(gin.TestMode, s3proxyVersion, expiration, "", s3backend)
r.RedirectTrailingSlash = false // return 404 when a <path> is not found instead redirecting to <path> + "/"
}
func TestMain(m *testing.M) {
setup()
retCode := m.Run()
os.Exit(retCode)
}
func unmarshallJSON(t *testing.T, bytes []byte) map[string]interface{} {
var objmap map[string]interface{}
err := json.Unmarshal(bytes, &objmap)
assert.Nil(t, err)
return objmap
}
// Check ping responding with the version number
func TestPing(t *testing.T) {
w := s3proxytest.ServeHTTP(t, r, http.MethodGet, "/", "")
assert.Equal(t, http.StatusOK, w.Code)
objmap := unmarshallJSON(t, w.Body.Bytes())
assert.Equal(t, objmap["version"], s3proxyVersion)
}
// Generate a presigned url for an upload
func TestCreateUrlForUploadOK(t *testing.T) {
w := s3proxytest.ServeCreatePresignedURLForUpload(t, r, dummyBucket, dummyFile, "")
assert.Equal(t, http.StatusOK, w.Code)
objmap := unmarshallJSON(t, w.Body.Bytes())
url := objmap["url"]
assert.Contains(t, url, dummyBucket)
assert.Contains(t, url, dummyFile)
assert.Contains(t, url, awsRegion)
assert.Contains(t, url, "X-Amz-Signature")
assert.Contains(t, url, "X-Amz-Expires=900")
}
// Generate a presigned url for a download
func TestCreateUrlForDownloadOK(t *testing.T) {
w := s3proxytest.ServeCreatePresignedURLForDownload(t, r, dummyBucket, dummyFile, "")
assert.Equal(t, http.StatusOK, w.Code)
objmap := unmarshallJSON(t, w.Body.Bytes())
url := objmap["url"]
assert.Contains(t, url, dummyFile)
assert.Contains(t, url, awsRegion)
assert.Contains(t, url, "X-Amz-Signature")
assert.Contains(t, url, "X-Amz-Expires=900")
}
// Check the delete API, should always return 200 even if the object is not present
func TestDeleteOK(t *testing.T) {
for i := 0; i < 2; i++ {
w := s3proxytest.ServeDeleteObject(t, r, dummyBucket, dummyFile, "")
assert.Equal(t, http.StatusOK, w.Code)
objmap := unmarshallJSON(t, w.Body.Bytes())
assert.Contains(t, objmap["response"], "ok")
}
}
func TestBulkDeleteOK(t *testing.T) {
w := s3proxytest.ServeBulkDeleteObject(t, r, dummyBucket, []string{"/toto/file1", "/toto/file2", "/toto/file2"}, "")
assert.Equal(t, http.StatusOK, w.Code)
objmap := unmarshallJSON(t, w.Body.Bytes())
assert.Contains(t, objmap["response"], "ok")
}
func TestBulkDelete400BadRequest(t *testing.T) {
w := s3proxytest.ServeBulkDeleteObject(t, r, dummyBucket, []string{}, "")
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestCopyOK(t *testing.T) {
w := s3proxytest.ServeCopyObject(t, r, dummyBucket, dummyFile, dummyBucket, dummyFile+"2", "")
assert.Equal(t, http.StatusOK, w.Code)
objmap := unmarshallJSON(t, w.Body.Bytes())
assert.Contains(t, objmap["response"], "ok")
}
func TestCopyMissingDestinationBucket(t *testing.T) {
w := s3proxytest.ServeCopyObject(t, r, dummyBucket, dummyFile, "", dummyFile+"2", "")
assert.Equal(t, http.StatusBadRequest, w.Code)
objmap := unmarshallJSON(t, w.Body.Bytes())
assert.Contains(t, objmap["error"], "Missing destination bucket")
}
func TestCopyMissingDestinationKey(t *testing.T) {
w := s3proxytest.ServeCopyObject(t, r, dummyBucket, dummyFile, dummyBucket, "", "")
assert.Equal(t, http.StatusBadRequest, w.Code)
objmap := unmarshallJSON(t, w.Body.Bytes())
assert.Contains(t, objmap["error"], "Missing destination key")
}
func TestCopyNoSuchBucket(t *testing.T) {
w := s3proxytest.ServeCopyObject(t, r, "notfound", dummyFile, dummyBucket, dummyFile+"2", "")
assert.Equal(t, http.StatusNotFound, w.Code)
objmap := unmarshallJSON(t, w.Body.Bytes())
assert.Contains(t, objmap["error"], "No such bucket")
w = s3proxytest.ServeCopyObject(t, r, dummyBucket, dummyFile, "notfound", dummyFile+"2", "")
assert.Equal(t, http.StatusNotFound, w.Code)
objmap = unmarshallJSON(t, w.Body.Bytes())
assert.Contains(t, objmap["error"], "No such bucket")
}
func TestCopyNoSuchKey(t *testing.T) {
w := s3proxytest.ServeCopyObject(t, r, dummyBucket, "/notfound", dummyBucket, dummyFile+"2", "")
assert.Equal(t, http.StatusNotFound, w.Code)
objmap := unmarshallJSON(t, w.Body.Bytes())
assert.Contains(t, objmap["error"], "No such key")
}
// Check if we are getting a 500 when we have a panic. Should be handle by the recovery middleware
// we are using delete action which fires a fake panic when "error" is in the key
func TestRecoveryMiddleware(t *testing.T) {
w := s3proxytest.ServeDeleteObject(t, r, dummyBucket, "/error", "")
assert.Equal(t, http.StatusInternalServerError, w.Code)
}
// Some 404 checks
func Test404(t *testing.T) {
// missing elements in path
w := s3proxytest.ServeHTTP(t, r, http.MethodGet, "/api/v1/presigned/url/dummybucket", "")
assert.Equal(t, http.StatusNotFound, w.Code)
w = s3proxytest.ServeHTTP(t, r, http.MethodGet, "/api/v1/presigned/url", "")
assert.Equal(t, http.StatusNotFound, w.Code)
w = s3proxytest.ServeHTTP(t, r, http.MethodGet, "/api/v1/presigned/url/coucou", "")
assert.Equal(t, http.StatusNotFound, w.Code)
w = s3proxytest.ServeHTTP(t, r, http.MethodPost, "/api/v1/presigned/coucou", "")
assert.Equal(t, http.StatusNotFound, w.Code)
w = s3proxytest.ServeHTTP(t, r, http.MethodDelete, "/api/v1/presigned/", "")
assert.Equal(t, http.StatusNotFound, w.Code)
// check unsupported method
w = s3proxytest.ServeHTTP(t, r, http.MethodPut, "/api/v1/object/dummybucket/dummyfolder/dummyfile", "")
assert.Equal(t, http.StatusNotFound, w.Code)
}
// Check authorization with valid api key
func TestAuthorizationOK(t *testing.T) {
// create a server with api key protection
r = router.NewGinEngine(gin.ReleaseMode, s3proxyVersion, urlExpiration, serverAPIKey, s3backend)
// ping endpoint should not be protected
w := s3proxytest.ServeHTTP(t, r, http.MethodGet, "/", "")
assert.Equal(t, http.StatusOK, w.Code)
// Other endpoints should be protected
w = s3proxytest.ServeCreatePresignedURLForUpload(t, r, dummyBucket, dummyFile, serverAPIKey)
assert.Equal(t, http.StatusOK, w.Code)
w = s3proxytest.ServeCreatePresignedURLForDownload(t, r, dummyBucket, dummyFile, serverAPIKey)
assert.Equal(t, http.StatusOK, w.Code)
w = s3proxytest.ServeDeleteObject(t, r, dummyBucket, dummyFile, serverAPIKey)
assert.Equal(t, http.StatusOK, w.Code)
}
// Check authorization verification with missing api key
func TestAuthorization401(t *testing.T) {
// create a server with api key protection
r = router.NewGinEngine(gin.ReleaseMode, s3proxyVersion, urlExpiration, serverAPIKey, s3backend)
// ping endpoint should not be protected
w := s3proxytest.ServeHTTP(t, r, http.MethodGet, "/", "")
assert.Equal(t, http.StatusOK, w.Code)
// Other endpoints should be protected
w = s3proxytest.ServeCreatePresignedURLForUpload(t, r, dummyBucket, dummyFile, "")
assert.Equal(t, http.StatusUnauthorized, w.Code)
w = s3proxytest.ServeCreatePresignedURLForDownload(t, r, dummyBucket, dummyFile, "")
assert.Equal(t, http.StatusUnauthorized, w.Code)
w = s3proxytest.ServeDeleteObject(t, r, dummyBucket, dummyFile, "")
assert.Equal(t, http.StatusUnauthorized, w.Code)
}