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
/
userauth_test.go
378 lines (302 loc) · 12.2 KB
/
userauth_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
package main
import (
"crypto/rand"
"net/http"
"os"
"testing"
helper "github.com/NBISweden/S3-Upload-Proxy/helper"
"github.com/minio/minio-go/v6/pkg/s3signer"
"github.com/stretchr/testify/assert"
)
func TestAlwaysAuthenticator(t *testing.T) {
a := NewAlwaysAllow()
r, _ := http.NewRequest("Get", "/", nil)
_, err := a.Authenticate(r)
assert.Nil(t, err)
}
func TestUserTokenAuthenticator_NoFile(t *testing.T) {
var pubkeys map[string][]byte
a := NewValidateFromToken(pubkeys)
a.pubkeys = make(map[string][]byte)
err := a.getjwtkey("")
assert.Error(t, err)
}
func TestUserTokenAuthenticator_GetFile(t *testing.T) {
// Create temp demo rsa key pair
demoKeysPath := "temp-rsa-keys"
prKeyPath, pubKeyPath, err := helper.MakeFolder(demoKeysPath)
assert.NoError(t, err)
err = helper.CreateRSAkeys(prKeyPath, pubKeyPath)
assert.NoError(t, err)
var pubkeys map[string][]byte
jwtpubkeypath := demoKeysPath + "/public-key/"
a := NewValidateFromToken(pubkeys)
a.pubkeys = make(map[string][]byte)
err = a.getjwtkey(jwtpubkeypath)
assert.NoError(t, err)
defer os.RemoveAll(demoKeysPath)
}
func TestUserTokenAuthenticator_WrongURL(t *testing.T) {
var pubkeys map[string][]byte
a := NewValidateFromToken(pubkeys)
a.pubkeys = make(map[string][]byte)
jwtpubkeyurl := "/dummy/"
err := a.getjwtpubkey(jwtpubkeyurl)
assert.Equal(t, "jwtpubkeyurl is not a proper URL (/dummy/)", err.Error())
}
func TestUserTokenAuthenticator_BadURL(t *testing.T) {
var pubkeys map[string][]byte
a := NewValidateFromToken(pubkeys)
a.pubkeys = make(map[string][]byte)
jwtpubkeyurl := "dummy.com/jwk"
err := a.getjwtpubkey(jwtpubkeyurl)
assert.Equal(t, "parse \"dummy.com/jwk\": invalid URI for request", err.Error())
}
func TestUserTokenAuthenticator_GoodURL(t *testing.T) {
var pubkeys map[string][]byte
a := NewValidateFromToken(pubkeys)
a.pubkeys = make(map[string][]byte)
jwtpubkeyurl := "https://example.com/jwk/"
err := a.getjwtpubkey(jwtpubkeyurl)
assert.ErrorContains(t, err, "failed to fetch remote JWK")
}
func TestUserTokenAuthenticator_ValidateSignature_RSA(t *testing.T) {
// These tests should be possible to reuse with all correct authenticators somehow
// Create temp demo rsa key pair
demoKeysPath := "demo-rsa-keys"
demoPrKeyName := "/dummy.ega.nbis.se"
prKeyPath, pubKeyPath, err := helper.MakeFolder(demoKeysPath)
assert.NoError(t, err)
err = helper.CreateRSAkeys(prKeyPath, pubKeyPath)
assert.NoError(t, err)
var pubkeys map[string][]byte
jwtpubkeypath := demoKeysPath + "/public-key/"
a := NewValidateFromToken(pubkeys)
a.pubkeys = make(map[string][]byte)
_ = a.getjwtkey(jwtpubkeypath)
// Parse demo private key
prKeyParsed, err := helper.ParsePrivateRSAKey(prKeyPath, demoPrKeyName)
assert.NoError(t, err)
// Create token and set up request defaults
defaultToken, err := helper.CreateRSAToken(prKeyParsed, "RS256", "JWT", helper.DefaultTokenClaims)
assert.NoError(t, err)
r, _ := http.NewRequest("", "/", nil)
r.Host = "localhost"
r.Header.Set("X-Amz-Security-Token", defaultToken)
// Test that a user can access their own bucket
r.URL.Path = "/dummy/"
s3signer.SignV4(*r, "username", "testpass", "", "us-east-1")
token, err := a.Authenticate(r)
assert.Nil(t, err)
assert.Equal(t, token["pilot"], helper.DefaultTokenClaims["pilot"])
// Test that a valid user can't access someone elses bucket
r.URL.Path = "/notvalid/"
s3signer.SignV4(*r, "username", "testpass", "", "us-east-1")
_, otherBucket := a.Authenticate(r)
assert.Equal(t, "token supplied username dummy but URL had notvalid", otherBucket.Error())
// Create and test Elixir token with wrong username
wrongUserToken, err := helper.CreateRSAToken(prKeyParsed, "RS256", "JWT", helper.WrongUserClaims)
assert.NoError(t, err)
r, _ = http.NewRequest("", "/", nil)
r.Host = "localhost"
r.Header.Set("X-Amz-Security-Token", wrongUserToken)
r.URL.Path = "/username/"
_, wrongUsername := a.Authenticate(r)
assert.Equal(t, "token supplied username [email protected] but URL had username", wrongUsername.Error())
// Create and test expired Elixir token
expiredToken, err := helper.CreateRSAToken(prKeyParsed, "RS256", "JWT", helper.ExpiredClaims)
assert.NoError(t, err)
r, _ = http.NewRequest("", "/", nil)
r.Host = "localhost"
r.Header.Set("X-Amz-Security-Token", expiredToken)
r.URL.Path = "/dummy/"
_, err = a.Authenticate(r)
assert.Error(t, err)
// Elixir token is not valid (e.g. issued in a future time)
nonValidToken, err := helper.CreateRSAToken(prKeyParsed, "RS256", "JWT", helper.NonValidClaims)
assert.NoError(t, err)
r, _ = http.NewRequest("", "/", nil)
r.Host = "localhost"
r.Header.Set("X-Amz-Security-Token", nonValidToken)
r.URL.Path = "/username/"
_, nonvalidToken := a.Authenticate(r)
// The error output is huge so a smaller part is compared
assert.Equal(t, "signed token (RS256) not valid:", nonvalidToken.Error()[0:31])
// Elixir tokens broken
r, _ = http.NewRequest("", "/", nil)
r.Host = "localhost"
r.Header.Set("X-Amz-Security-Token", defaultToken[3:])
r.URL.Path = "/username/"
_, brokenToken := a.Authenticate(r)
assert.Equal(t, "broken token (claims are empty): map[]", brokenToken.Error()[0:38])
r, _ = http.NewRequest("", "/", nil)
r.Host = "localhost"
r.Header.Set("X-Amz-Security-Token", "random"+defaultToken)
r.URL.Path = "/username/"
_, err = a.Authenticate(r)
assert.Error(t, err)
_, brokenToken2 := a.Authenticate(r)
assert.Equal(t, "broken token (claims are empty): map[]", brokenToken2.Error()[0:38])
// Bad issuer
basIss, err := helper.CreateRSAToken(prKeyParsed, "RS256", "JWT", helper.WrongTokenAlgClaims)
assert.NoError(t, err)
r, _ = http.NewRequest("", "/", nil)
r.Host = "localhost"
r.Header.Set("X-Amz-Security-Token", basIss)
r.URL.Path = "/dummy/"
_, err = a.Authenticate(r)
assert.Contains(t, err.Error(), "failed to get issuer from token")
// Delete the keys when testing is done or failed
defer os.RemoveAll(demoKeysPath)
}
func TestUserTokenAuthenticator_ValidateSignature_EC(t *testing.T) {
// Create temp demo ec key pair
demoKeysPath := "demo-ec-keys"
demoPrKeyName := "/dummy.ega.nbis.se"
prKeyPath, pubKeyPath, err := helper.MakeFolder(demoKeysPath)
assert.NoError(t, err)
err = helper.CreateECkeys(prKeyPath, pubKeyPath)
assert.NoError(t, err)
var pubkeys map[string][]byte
jwtpubkeypath := demoKeysPath + "/public-key/"
a := NewValidateFromToken(pubkeys)
a.pubkeys = make(map[string][]byte)
_ = a.getjwtkey(jwtpubkeypath)
// Parse demo private key
prKeyParsed, err := helper.ParsePrivateECKey(prKeyPath, demoPrKeyName)
assert.NoError(t, err)
// Create token and set up request defaults
defaultToken, err := helper.CreateECToken(prKeyParsed, "ES256", "JWT", helper.DefaultTokenClaims)
assert.NoError(t, err)
r, _ := http.NewRequest("", "/", nil)
r.Host = "localhost"
r.Header.Set("X-Amz-Security-Token", defaultToken)
// Test that a user can access their own bucket
r.URL.Path = "/dummy/"
s3signer.SignV4(*r, "username", "testpass", "", "us-east-1")
_, err = a.Authenticate(r)
assert.Nil(t, err)
// Test that a valid user can't access someone elses bucket
r.URL.Path = "/notvalid/"
s3signer.SignV4(*r, "username", "testpass", "", "us-east-1")
_, otherBucket := a.Authenticate(r)
assert.Equal(t, "token supplied username dummy but URL had notvalid", otherBucket.Error())
// Create and test Elixir token with wrong username
wrongUserToken, err := helper.CreateECToken(prKeyParsed, "ES256", "JWT", helper.WrongUserClaims)
assert.NoError(t, err)
r, _ = http.NewRequest("", "/", nil)
r.Host = "localhost"
r.Header.Set("X-Amz-Security-Token", wrongUserToken)
r.URL.Path = "/username/"
_, wrongUsername := a.Authenticate(r)
assert.Equal(t, "token supplied username [email protected] but URL had username", wrongUsername.Error())
// Create and test expired Elixir token
expiredToken, err := helper.CreateECToken(prKeyParsed, "ES256", "JWT", helper.ExpiredClaims)
assert.NoError(t, err)
r, _ = http.NewRequest("", "/", nil)
r.Host = "localhost"
r.Header.Set("X-Amz-Security-Token", expiredToken)
r.URL.Path = "/dummy/"
_, err = a.Authenticate(r)
assert.Error(t, err)
// Elixir token is not valid
nonValidToken, err := helper.CreateECToken(prKeyParsed, "ES256", "JWT", helper.NonValidClaims)
assert.NoError(t, err)
r, _ = http.NewRequest("", "/", nil)
r.Host = "localhost"
r.Header.Set("X-Amz-Security-Token", nonValidToken)
r.URL.Path = "/username/"
_, nonvalidToken := a.Authenticate(r)
// The error output is huge so a smaller part is compared
assert.Equal(t, "signed token (ES256) not valid:", nonvalidToken.Error()[0:31])
// Elixir tokens broken
r, _ = http.NewRequest("", "/", nil)
r.Host = "localhost"
r.Header.Set("X-Amz-Security-Token", defaultToken[3:])
r.URL.Path = "/username/"
_, brokenToken := a.Authenticate(r)
assert.Equal(t, "broken token (claims are empty): map[]", brokenToken.Error()[0:38])
r, _ = http.NewRequest("", "/", nil)
r.Host = "localhost"
r.Header.Set("X-Amz-Security-Token", "random"+defaultToken)
r.URL.Path = "/username/"
_, brokenToken2 := a.Authenticate(r)
assert.Equal(t, "broken token (claims are empty): map[]", brokenToken2.Error()[0:38])
// Bad issuer
basIss, err := helper.CreateECToken(prKeyParsed, "ES256", "JWT", helper.WrongTokenAlgClaims)
assert.NoError(t, err)
r, _ = http.NewRequest("", "/", nil)
r.Host = "localhost"
r.Header.Set("X-Amz-Security-Token", basIss)
r.URL.Path = "/dummy/"
_, err = a.Authenticate(r)
assert.Contains(t, err.Error(), "failed to get issuer from token")
// Test LS-AAI user authentication
token, err := helper.CreateECToken(prKeyParsed, "ES256", "JWT", helper.WrongUserClaims)
assert.NoError(t, err)
r, _ = http.NewRequest("", "/c5773f41d17d27bd53b1e6794aedc32d7906e779_elixir-europe.org/foo", nil)
r.Host = "localhost"
r.Header.Set("X-Amz-Security-Token", token)
_, err = a.Authenticate(r)
assert.NoError(t, err)
r, _ = http.NewRequest("", "/dataset", nil)
r.Host = "localhost"
r.Header.Set("X-Amz-Security-Token", token)
_, err = a.Authenticate(r)
assert.Equal(t, "token supplied username [email protected] but URL had dataset", err.Error())
defer os.RemoveAll(demoKeysPath)
}
func TestWrongKeyType_RSA(t *testing.T) {
// Create temp demo ec key pair
demoKeysPath := "demo-ec-keys"
demoPrKeyName := "/dummy.ega.nbis.se"
prKeyPath, pubKeyPath, err := helper.MakeFolder(demoKeysPath)
assert.NoError(t, err)
err = helper.CreateECkeys(prKeyPath, pubKeyPath)
assert.NoError(t, err)
var pubkeys map[string][]byte
jwtpubkeypath := demoKeysPath + "/public-key/"
a := NewValidateFromToken(pubkeys)
a.pubkeys = make(map[string][]byte)
_ = a.getjwtkey(jwtpubkeypath)
// Parse demo private key
_, err = helper.ParsePrivateRSAKey(prKeyPath, demoPrKeyName)
assert.Equal(t, "x509: failed to parse private key (use ParseECPrivateKey instead for this key format)", err.Error())
defer os.RemoveAll(demoKeysPath)
}
func TestWrongKeyType_EC(t *testing.T) {
// Create temp demo ec key pair
demoKeysPath := "demo-rsa-keys"
demoPrKeyName := "/dummy.ega.nbis.se"
prKeyPath, pubKeyPath, err := helper.MakeFolder(demoKeysPath)
assert.NoError(t, err)
err = helper.CreateRSAkeys(prKeyPath, pubKeyPath)
assert.NoError(t, err)
var pubkeys map[string][]byte
jwtpubkeypath := demoKeysPath + "/public-key/"
a := NewValidateFromToken(pubkeys)
a.pubkeys = make(map[string][]byte)
_ = a.getjwtkey(jwtpubkeypath)
// Parse demo private key
_, err = helper.ParsePrivateECKey(prKeyPath, demoPrKeyName)
assert.Equal(t, "x509: failed to parse private key (use ParsePKCS1PrivateKey instead for this key format)", err.Error())
defer os.RemoveAll(demoKeysPath)
}
func TestUserTokenAuthenticator_ValidateSignature_HS(t *testing.T) {
// Create random secret
key := make([]byte, 256)
_, err := rand.Read(key)
assert.NoError(t, err)
// Create HS256 token
wrongAlgToken, err := helper.CreateHSToken(key, "HS256", "JWT", helper.DefaultTokenClaims)
assert.NoError(t, err)
testPub := make(map[string][]byte)
a := NewValidateFromToken(testPub)
a.pubkeys = make(map[string][]byte)
r, _ := http.NewRequest("", "/", nil)
r.Host = "localhost"
r.Header.Set("X-Amz-Security-Token", wrongAlgToken)
r.URL.Path = "/username/"
_, WrongAlg := a.Authenticate(r)
assert.Equal(t, "unsupported algorithm HS256", WrongAlg.Error())
}