This repository has been archived by the owner on Jun 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy_test.go
401 lines (334 loc) · 12.9 KB
/
proxy_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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package main
import (
"crypto/tls"
"database/sql"
"encoding/json"
"fmt"
"net"
"net/http"
"net/http/httptest"
"net/url"
"os"
"testing"
common "github.com/neicnordic/sda-common/database"
"github.com/golang-jwt/jwt/v4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
type ProxyTests struct {
suite.Suite
S3conf S3Config
DBConf common.DBConf
fakeServer *FakeServer
messenger *MockMessenger
database *common.SDAdb
}
func TestProxyTestSuite(t *testing.T) {
suite.Run(t, new(ProxyTests))
}
func (suite *ProxyTests) SetupTest() {
// Create fake server
suite.fakeServer = startFakeServer("9024")
// Create an s3config for the fake server
suite.S3conf = S3Config{
url: "http://localhost:9024",
accessKey: "someAccess",
secretKey: "someSecret",
bucket: "buckbuck",
region: "us-east-1",
}
// Create a mock messenger
suite.messenger = NewMockMessenger()
// Create a database configuration for the fake database
suite.DBConf = common.DBConf{
Host: "localhost",
Port: 2345,
User: "lega_in",
Password: "lega_in",
Database: "lega",
CACert: "",
SslMode: "disable",
ClientCert: "",
ClientKey: "",
}
var err error
_, err = os.Stat("/.dockerenv")
if err == nil {
suite.DBConf.Host = "db"
suite.DBConf.Port = 5432
}
suite.database = &common.SDAdb{}
}
func (suite *ProxyTests) TearDownTest() {
suite.fakeServer.Close()
suite.database.Close()
}
type FakeServer struct {
ts *httptest.Server
resp string
pinged bool
}
func startFakeServer(port string) *FakeServer {
l, err := net.Listen("tcp", "127.0.0.1:"+port)
if err != nil {
panic(fmt.Errorf("can't create mock server for testing: %s", err))
}
f := FakeServer{}
foo := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
f.pinged = true
fmt.Fprintf(w, f.resp)
})
ts := httptest.NewUnstartedServer(foo)
ts.Listener.Close()
ts.Listener = l
ts.Start()
f.ts = ts
return &f
}
func (f *FakeServer) Close() {
f.ts.Close()
}
func (f *FakeServer) PingedAndRestore() bool {
ret := f.pinged
f.pinged = false
return ret
}
type MockMessenger struct {
lastEvent *Event
}
func (m *MockMessenger) IsConnClosed() bool {
return false
}
func NewMockMessenger() *MockMessenger {
return &MockMessenger{nil}
}
func (m *MockMessenger) SendMessage(uuid string, body []byte) error {
if uuid == "" || body == nil {
return fmt.Errorf("bad message")
}
return nil
}
// AlwaysAllow is an Authenticator that always authenticates
type AlwaysDeny struct{}
// Authenticate does not authenticate anyone.
func (u *AlwaysDeny) Authenticate(_ *http.Request) (jwt.MapClaims, error) {
return nil, fmt.Errorf("denied")
}
// nolint:bodyclose
func (suite *ProxyTests) TestServeHTTP_disallowed() {
// Start mock messenger that denies everything
proxy := NewProxy(suite.S3conf, &AlwaysDeny{}, suite.messenger, suite.database, new(tls.Config))
r, _ := http.NewRequest("", "", nil)
w := httptest.NewRecorder()
// Remove bucket disallowed
r.Method = "DELETE"
r.URL, _ = url.Parse("/asdf/")
proxy.ServeHTTP(w, r)
assert.Equal(suite.T(), 403, w.Result().StatusCode)
assert.Equal(suite.T(), false, suite.fakeServer.PingedAndRestore())
// Deletion of files are disallowed
r.Method = "DELETE"
r.URL, _ = url.Parse("/asdf/asdf")
proxy.ServeHTTP(w, r)
assert.Equal(suite.T(), 403, w.Result().StatusCode)
assert.Equal(suite.T(), false, suite.fakeServer.PingedAndRestore())
// Policy methods are not allowed
w = httptest.NewRecorder()
r.Method = "GET"
r.URL, _ = url.Parse("/asdf?acl=rw")
proxy.ServeHTTP(w, r)
assert.Equal(suite.T(), 403, w.Result().StatusCode)
assert.Equal(suite.T(), false, suite.fakeServer.PingedAndRestore())
// Normal get is dissallowed
w = httptest.NewRecorder()
r.Method = "GET"
r.URL, _ = url.Parse("/asdf/")
proxy.ServeHTTP(w, r)
assert.Equal(suite.T(), 403, w.Result().StatusCode)
assert.Equal(suite.T(), false, suite.fakeServer.PingedAndRestore())
// Put policy is disallowed
w = httptest.NewRecorder()
r.Method = "PUT"
r.URL, _ = url.Parse("/asdf?policy=rw")
proxy.ServeHTTP(w, r)
assert.Equal(suite.T(), 403, w.Result().StatusCode)
assert.Equal(suite.T(), false, suite.fakeServer.PingedAndRestore())
// Create bucket disallowed
w = httptest.NewRecorder()
r.Method = "PUT"
r.URL, _ = url.Parse("/asdf/")
proxy.ServeHTTP(w, r)
assert.Equal(suite.T(), 403, w.Result().StatusCode)
assert.Equal(suite.T(), false, suite.fakeServer.PingedAndRestore())
// Not authorized user get 401 response
w = httptest.NewRecorder()
r.Method = "GET"
r.URL, _ = url.Parse("/username/file")
proxy.ServeHTTP(w, r)
assert.Equal(suite.T(), 401, w.Result().StatusCode)
assert.Equal(suite.T(), false, suite.fakeServer.PingedAndRestore())
}
func (suite *ProxyTests) TestServeHTTPS3Unresponsive() {
s3conf := S3Config{
url: "http://localhost:40211",
accessKey: "someAccess",
secretKey: "someSecret",
bucket: "buckbuck",
region: "us-east-1",
}
proxy := NewProxy(s3conf, &AlwaysAllow{}, suite.messenger, suite.database, new(tls.Config))
r, _ := http.NewRequest("", "", nil)
w := httptest.NewRecorder()
// Just try to list the files
r.Method = "GET"
r.URL, _ = url.Parse("/asdf/asdf")
proxy.ServeHTTP(w, r)
assert.Equal(suite.T(), 500, w.Result().StatusCode) // nolint:bodyclose
}
// nolint:bodyclose
func (suite *ProxyTests) TestServeHTTP_allowed() {
// Start proxy that allows everything
database, err := common.NewSDAdb(suite.DBConf)
if err != nil {
suite.T().Skip("skip TestShutdown since broker not present")
}
proxy := NewProxy(suite.S3conf, NewAlwaysAllow(), suite.messenger, database, new(tls.Config))
// List files works
r, _ := http.NewRequest("GET", "/username/file", nil)
w := httptest.NewRecorder()
proxy.ServeHTTP(w, r)
assert.Equal(suite.T(), 200, w.Result().StatusCode)
assert.Equal(suite.T(), true, suite.fakeServer.PingedAndRestore())
assert.Equal(suite.T(), false, suite.fakeServer.PingedAndRestore()) // Testing the pinged interface
// Put file works
w = httptest.NewRecorder()
r.Method = "PUT"
suite.fakeServer.resp = "<ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"><Name>test</Name><Prefix>/elixirid/file.txt</Prefix><KeyCount>1</KeyCount><MaxKeys>2</MaxKeys><Delimiter></Delimiter><IsTruncated>false</IsTruncated><Contents><Key>/elixirid/file.txt</Key><LastModified>2020-03-10T13:20:15.000Z</LastModified><ETag>"0a44282bd39178db9680f24813c41aec-1"</ETag><Size>5</Size><Owner><ID></ID><DisplayName></DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents></ListBucketResult>"
proxy.ServeHTTP(w, r)
assert.Equal(suite.T(), 200, w.Result().StatusCode)
assert.Equal(suite.T(), true, suite.fakeServer.PingedAndRestore())
// Put with partnumber sends no message
w = httptest.NewRecorder()
r.Method = "PUT"
r.URL, _ = url.Parse("/username/file?partNumber=5")
proxy.ServeHTTP(w, r)
assert.Equal(suite.T(), 200, w.Result().StatusCode)
assert.Equal(suite.T(), true, suite.fakeServer.PingedAndRestore())
// Post with uploadId sends message
r.Method = "POST"
r.URL, _ = url.Parse("/username/file?uploadId=5")
w = httptest.NewRecorder()
proxy.ServeHTTP(w, r)
assert.Equal(suite.T(), 200, w.Result().StatusCode)
assert.Equal(suite.T(), true, suite.fakeServer.PingedAndRestore())
// Post without uploadId sends no message
r.Method = "POST"
r.URL, _ = url.Parse("/username/file")
w = httptest.NewRecorder()
proxy.ServeHTTP(w, r)
assert.Equal(suite.T(), 200, w.Result().StatusCode)
assert.Equal(suite.T(), true, suite.fakeServer.PingedAndRestore())
// Abort multipart works
r.Method = "DELETE"
r.URL, _ = url.Parse("/asdf/asdf?uploadId=123")
w = httptest.NewRecorder()
proxy.ServeHTTP(w, r)
assert.Equal(suite.T(), 200, w.Result().StatusCode)
assert.Equal(suite.T(), true, suite.fakeServer.PingedAndRestore())
// Going through the different extra stuff that can be in the get request
// that trigger different code paths in the code.
// Delimiter alone
r.Method = "GET"
r.URL, _ = url.Parse("/username/file?delimiter=puppe")
w = httptest.NewRecorder()
proxy.ServeHTTP(w, r)
assert.Equal(suite.T(), 200, w.Result().StatusCode)
assert.Equal(suite.T(), true, suite.fakeServer.PingedAndRestore())
// Delimiter alone together with prefix
r.Method = "GET"
r.URL, _ = url.Parse("/username/file?delimiter=puppe&prefix=asdf")
w = httptest.NewRecorder()
proxy.ServeHTTP(w, r)
assert.Equal(suite.T(), 200, w.Result().StatusCode)
assert.Equal(suite.T(), true, suite.fakeServer.PingedAndRestore())
// Location parameter
r.Method = "GET"
r.URL, _ = url.Parse("/username/file?location=fnuffe")
w = httptest.NewRecorder()
proxy.ServeHTTP(w, r)
assert.Equal(suite.T(), 200, w.Result().StatusCode)
assert.Equal(suite.T(), true, suite.fakeServer.PingedAndRestore())
// Filenames with platform incompatible characters are disallowed
// not checked in TestServeHTTP_allowed() because we look for a non 403 response
r.Method = "PUT"
r.URL, _ = url.Parse("/username/fi|le")
w = httptest.NewRecorder()
proxy.ServeHTTP(w, r)
assert.Equal(suite.T(), 406, w.Result().StatusCode)
assert.Equal(suite.T(), false, suite.fakeServer.PingedAndRestore())
}
func (suite *ProxyTests) TestMessageFormatting() {
// Set up basic request for multipart upload
r, _ := http.NewRequest("POST", "/buckbuck/user/new_file.txt", nil)
r.Host = "localhost"
r.Header.Set("content-length", "1234")
r.Header.Set("x-amz-content-sha256", "checksum")
claims := jwt.MapClaims{}
claims["sub"] = "[email protected]"
// start proxy that denies everything
proxy := NewProxy(suite.S3conf, &AlwaysDeny{}, suite.messenger, suite.database, new(tls.Config))
suite.fakeServer.resp = "<ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"><Name>test</Name><Prefix>/user/new_file.txt</Prefix><KeyCount>1</KeyCount><MaxKeys>2</MaxKeys><Delimiter></Delimiter><IsTruncated>false</IsTruncated><Contents><Key>/user/new_file.txt</Key><LastModified>2020-03-10T13:20:15.000Z</LastModified><ETag>"0a44282bd39178db9680f24813c41aec-1"</ETag><Size>1234</Size><Owner><ID></ID><DisplayName></DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents></ListBucketResult>"
msg, err := proxy.CreateMessageFromRequest(r, claims)
assert.Nil(suite.T(), err)
assert.IsType(suite.T(), Event{}, msg)
assert.Equal(suite.T(), int64(1234), msg.Filesize)
assert.Equal(suite.T(), "user/new_file.txt", msg.Filepath)
assert.Equal(suite.T(), "[email protected]", msg.Username)
c, _ := json.Marshal(msg.Checksum[0])
checksum := Checksum{}
_ = json.Unmarshal(c, &checksum)
assert.Equal(suite.T(), "sha256", checksum.Type)
assert.Equal(suite.T(), "5b233b981dc12e7ccf4c242b99c042b7842b73b956ad662e4fe0f8354151538b", checksum.Value)
// Test single shot upload
r.Method = "PUT"
msg, err = proxy.CreateMessageFromRequest(r, nil)
assert.Nil(suite.T(), err)
assert.IsType(suite.T(), Event{}, msg)
assert.Equal(suite.T(), "upload", msg.Operation)
}
func (suite *ProxyTests) TestDatabaseConnection() {
database, err := common.NewSDAdb(suite.DBConf)
if err != nil {
suite.T().Skip("skip TestShutdown since DB not present: ", err)
}
// Start proxy that allows everything
proxy := NewProxy(suite.S3conf, NewAlwaysAllow(), suite.messenger, database, new(tls.Config))
// PUT a file into the system
filename := "/username/db-test-file"
r, _ := http.NewRequest("PUT", filename, nil)
w := httptest.NewRecorder()
suite.fakeServer.resp = "<ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"><Name>test</Name><Prefix>/elixirid/db-test-file.txt</Prefix><KeyCount>1</KeyCount><MaxKeys>2</MaxKeys><Delimiter></Delimiter><IsTruncated>false</IsTruncated><Contents><Key>/elixirid/file.txt</Key><LastModified>2020-03-10T13:20:15.000Z</LastModified><ETag>"0a44282bd39178db9680f24813c41aec-1"</ETag><Size>5</Size><Owner><ID></ID><DisplayName></DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents></ListBucketResult>"
proxy.ServeHTTP(w, r)
res := w.Result()
defer res.Body.Close()
assert.Equal(suite.T(), 200, res.StatusCode)
assert.Equal(suite.T(), true, suite.fakeServer.PingedAndRestore())
// Check that the file is registered and uploaded in the database
// connect to the database
db, err := sql.Open(suite.DBConf.PgDataSource())
assert.Nil(suite.T(), err, "Failed to connect to database")
// Check that the file is in the database
var fileID string
query := "SELECT id FROM sda.files WHERE submission_file_path = $1"
err = db.QueryRow(query, filename[1:]).Scan(&fileID)
assert.Nil(suite.T(), err, "Failed to query database")
assert.NotNil(suite.T(), fileID, "File not found in database")
// Check that the "registered" status is in the database for this file
for _, status := range []string{"registered", "uploaded"} {
var exists int
query = "SELECT 1 FROM sda.file_event_log WHERE event = $1 AND file_id = $2"
err = db.QueryRow(query, status, fileID).Scan(&exists)
assert.Nil(suite.T(), err, "Failed to find '%v' event in database", status)
assert.Equal(suite.T(), exists, 1, "File '%v' event does not exist", status)
}
}