@@ -12,6 +12,7 @@ import (
12
12
"os"
13
13
"path/filepath"
14
14
"regexp"
15
+ "sync/atomic"
15
16
"testing"
16
17
17
18
"github.com/coder/coder/v2/codersdk"
@@ -268,11 +269,12 @@ func TestCloneRepoSSH(t *testing.T) {
268
269
// nolint:paralleltest // t.Setenv for SSH_AUTH_SOCK
269
270
func TestSetupRepoAuth (t * testing.T ) {
270
271
t .Setenv ("SSH_AUTH_SOCK" , "" )
272
+ ctx := context .Background ()
271
273
t .Run ("Empty" , func (t * testing.T ) {
272
274
opts := & envbuilder.Options {
273
275
Logger : testLog (t ),
274
276
}
275
- auth := envbuilder .SetupRepoAuth (opts )
277
+ auth := envbuilder .SetupRepoAuth (ctx , opts )
276
278
require .Nil (t , auth )
277
279
})
278
280
@@ -281,7 +283,7 @@ func TestSetupRepoAuth(t *testing.T) {
281
283
GitURL : "http://host.tld/repo" ,
282
284
Logger : testLog (t ),
283
285
}
284
- auth := envbuilder .SetupRepoAuth (opts )
286
+ auth := envbuilder .SetupRepoAuth (ctx , opts )
285
287
require .Nil (t , auth )
286
288
})
287
289
@@ -292,7 +294,7 @@ func TestSetupRepoAuth(t *testing.T) {
292
294
GitPassword : "pass" ,
293
295
Logger : testLog (t ),
294
296
}
295
- auth := envbuilder .SetupRepoAuth (opts )
297
+ auth := envbuilder .SetupRepoAuth (ctx , opts )
296
298
ba , ok := auth .(* githttp.BasicAuth )
297
299
require .True (t , ok )
298
300
require .Equal (t , opts .GitUsername , ba .Username )
@@ -306,7 +308,7 @@ func TestSetupRepoAuth(t *testing.T) {
306
308
GitPassword : "pass" ,
307
309
Logger : testLog (t ),
308
310
}
309
- auth := envbuilder .SetupRepoAuth (opts )
311
+ auth := envbuilder .SetupRepoAuth (ctx , opts )
310
312
ba , ok := auth .(* githttp.BasicAuth )
311
313
require .True (t , ok )
312
314
require .Equal (t , opts .GitUsername , ba .Username )
@@ -320,7 +322,7 @@ func TestSetupRepoAuth(t *testing.T) {
320
322
GitSSHPrivateKeyPath : kPath ,
321
323
Logger : testLog (t ),
322
324
}
323
- auth := envbuilder .SetupRepoAuth (opts )
325
+ auth := envbuilder .SetupRepoAuth (ctx , opts )
324
326
_ , ok := auth .(* gitssh.PublicKeys )
325
327
require .True (t , ok )
326
328
})
@@ -332,7 +334,7 @@ func TestSetupRepoAuth(t *testing.T) {
332
334
GitSSHPrivateKeyPath : kPath ,
333
335
Logger : testLog (t ),
334
336
}
335
- auth := envbuilder .SetupRepoAuth (opts )
337
+ auth := envbuilder .SetupRepoAuth (ctx , opts )
336
338
_ , ok := auth .(* gitssh.PublicKeys )
337
339
require .True (t , ok )
338
340
})
@@ -345,7 +347,7 @@ func TestSetupRepoAuth(t *testing.T) {
345
347
GitSSHPrivateKeyPath : kPath ,
346
348
Logger : testLog (t ),
347
349
}
348
- auth := envbuilder .SetupRepoAuth (opts )
350
+ auth := envbuilder .SetupRepoAuth (ctx , opts )
349
351
_ , ok := auth .(* gitssh.PublicKeys )
350
352
require .True (t , ok )
351
353
})
@@ -358,7 +360,7 @@ func TestSetupRepoAuth(t *testing.T) {
358
360
GitUsername : "user" ,
359
361
Logger : testLog (t ),
360
362
}
361
- auth := envbuilder .SetupRepoAuth (opts )
363
+ auth := envbuilder .SetupRepoAuth (ctx , opts )
362
364
_ , ok := auth .(* gitssh.PublicKeys )
363
365
require .True (t , ok )
364
366
})
@@ -370,7 +372,7 @@ func TestSetupRepoAuth(t *testing.T) {
370
372
GitSSHPrivateKeyPath : kPath ,
371
373
Logger : testLog (t ),
372
374
}
373
- auth := envbuilder .SetupRepoAuth (opts )
375
+ auth := envbuilder .SetupRepoAuth (ctx , opts )
374
376
pk , ok := auth .(* gitssh.PublicKeys )
375
377
require .True (t , ok )
376
378
require .NotNil (t , pk .Signer )
@@ -384,7 +386,7 @@ func TestSetupRepoAuth(t *testing.T) {
384
386
GitURL :
"ssh://[email protected] :repo/path" ,
385
387
Logger : testLog (t ),
386
388
}
387
- auth := envbuilder .SetupRepoAuth (opts )
389
+ auth := envbuilder .SetupRepoAuth (ctx , opts )
388
390
require .Nil (t , auth ) // TODO: actually test SSH_AUTH_SOCK
389
391
})
390
392
@@ -415,19 +417,51 @@ func TestSetupRepoAuth(t *testing.T) {
415
417
GitURL :
"ssh://[email protected] :repo/path" ,
416
418
Logger : testLog (t ),
417
419
}
418
- auth := envbuilder .SetupRepoAuth (opts )
420
+ auth := envbuilder .SetupRepoAuth (ctx , opts )
419
421
pk , ok := auth .(* gitssh.PublicKeys )
420
422
require .True (t , ok )
421
423
require .NotNil (t , pk .Signer )
422
424
require .Equal (t , actualSigner , pk .Signer )
423
425
})
424
426
425
- t .Run ("SSH/CoderForbidden " , func (t * testing.T ) {
427
+ t .Run ("SSH/CoderRetry " , func (t * testing.T ) {
426
428
token := uuid .NewString ()
429
+ actualSigner , err := gossh .ParsePrivateKey ([]byte (testKey ))
430
+ require .NoError (t , err )
431
+ var count atomic.Int64
432
+ // Return 401 initially, but eventually 200.
427
433
handler := func (w http.ResponseWriter , r * http.Request ) {
428
- hdr := r .Header .Get (codersdk .SessionTokenHeader )
429
- assert .Equal (t , hdr , token )
430
- w .WriteHeader (http .StatusForbidden )
434
+ c := count .Add (1 )
435
+ if c < 3 {
436
+ hdr := r .Header .Get (codersdk .SessionTokenHeader )
437
+ assert .Equal (t , hdr , token )
438
+ w .WriteHeader (http .StatusUnauthorized )
439
+ return
440
+ }
441
+ _ = json .NewEncoder (w ).Encode (& agentsdk.GitSSHKey {
442
+ PublicKey : string (actualSigner .PublicKey ().Marshal ()),
443
+ PrivateKey : string (testKey ),
444
+ })
445
+ }
446
+ srv := httptest .NewServer (http .HandlerFunc (handler ))
447
+ opts := & envbuilder.Options {
448
+ CoderAgentURL : srv .URL ,
449
+ CoderAgentToken : token ,
450
+ GitURL :
"ssh://[email protected] :repo/path" ,
451
+ Logger : testLog (t ),
452
+ }
453
+ auth := envbuilder .SetupRepoAuth (ctx , opts )
454
+ pk , ok := auth .(* gitssh.PublicKeys )
455
+ require .True (t , ok )
456
+ require .NotNil (t , pk .Signer )
457
+ require .Equal (t , actualSigner , pk .Signer )
458
+ })
459
+
460
+ t .Run ("SSH/NotCoder" , func (t * testing.T ) {
461
+ token := uuid .NewString ()
462
+ handler := func (w http.ResponseWriter , r * http.Request ) {
463
+ w .WriteHeader (http .StatusTeapot )
464
+ _ , _ = w .Write ([]byte ("I'm a teapot!" ))
431
465
}
432
466
srv := httptest .NewServer (http .HandlerFunc (handler ))
433
467
opts := & envbuilder.Options {
@@ -436,7 +470,7 @@ func TestSetupRepoAuth(t *testing.T) {
436
470
GitURL :
"ssh://[email protected] :repo/path" ,
437
471
Logger : testLog (t ),
438
472
}
439
- auth := envbuilder .SetupRepoAuth (opts )
473
+ auth := envbuilder .SetupRepoAuth (ctx , opts )
440
474
require .Nil (t , auth )
441
475
})
442
476
}
0 commit comments