@@ -3,7 +3,9 @@ package main_test
3
3
import (
4
4
"bufio"
5
5
"context"
6
+ "crypto/ecdsa"
6
7
"crypto/ed25519"
8
+ "crypto/elliptic"
7
9
"crypto/rand"
8
10
"crypto/rsa"
9
11
"encoding/json"
@@ -38,6 +40,14 @@ import (
38
40
var (
39
41
sshdPath = ""
40
42
gitalyConnInfo * gitalyConnectionInfo
43
+ keyTypes = []string {
44
+ "rsa-2048" ,
45
+ "rsa-4096" ,
46
+ "ed25519" ,
47
+ "ecdsa-p256" ,
48
+ "ecdsa-p384" ,
49
+ "ecdsa-p521" ,
50
+ }
41
51
)
42
52
43
53
const (
@@ -51,6 +61,26 @@ type gitalyConnectionInfo struct {
51
61
Storage string `json:"storage"`
52
62
}
53
63
64
+ func generateKey (keyType string ) (interface {}, error ) {
65
+ switch strings .ToLower (keyType ) {
66
+ case "rsa-2048" :
67
+ return rsa .GenerateKey (rand .Reader , 2048 )
68
+ case "rsa-4096" :
69
+ return rsa .GenerateKey (rand .Reader , 4096 )
70
+ case "ed25519" :
71
+ _ , priv , err := ed25519 .GenerateKey (rand .Reader )
72
+ return priv , err
73
+ case "ecdsa-p256" :
74
+ return ecdsa .GenerateKey (elliptic .P256 (), rand .Reader )
75
+ case "ecdsa-p384" :
76
+ return ecdsa .GenerateKey (elliptic .P384 (), rand .Reader )
77
+ case "ecdsa-p521" :
78
+ return ecdsa .GenerateKey (elliptic .P521 (), rand .Reader )
79
+ default :
80
+ return nil , fmt .Errorf ("unsupported key type: %s" , keyType )
81
+ }
82
+ }
83
+
54
84
func init () {
55
85
rootDir := rootDir ()
56
86
sshdPath = filepath .Join (rootDir , "bin" , "gitlab-sshd" )
@@ -214,20 +244,15 @@ sshd:
214
244
- "` + hostKeyPath + `"` )
215
245
}
216
246
217
- func buildClient (t * testing.T , addr string , hostKey ed25519.PublicKey ) * ssh.Client {
247
+ func buildClient (t * testing.T , addr string , clientKeyType string , hostKey ed25519.PublicKey ) * ssh.Client {
218
248
t .Helper ()
219
249
220
250
pubKey , err := ssh .NewPublicKey (hostKey )
221
251
require .NoError (t , err )
222
252
223
253
var clientPrivKey interface {}
224
254
225
- if os .Getenv ("FIPS_MODE" ) == "1" {
226
- clientPrivKey , err = rsa .GenerateKey (rand .Reader , 2048 )
227
- } else {
228
- _ , clientPrivKey , err = ed25519 .GenerateKey (nil )
229
- }
230
-
255
+ clientPrivKey , err = generateKey (clientKeyType )
231
256
require .NoError (t , err )
232
257
233
258
clientSigner , err := ssh .NewSignerFromKey (clientPrivKey )
@@ -328,7 +353,7 @@ func startSSHD(t *testing.T, dir string) string {
328
353
329
354
// Starts an instance of gitlab-sshd with the given arguments, returning an SSH
330
355
// client already connected to it
331
- func runSSHD (t * testing.T , apiHandler http.Handler ) * ssh.Client {
356
+ func runSSHD (t * testing.T , clientKeyType string , apiHandler http.Handler ) * ssh.Client {
332
357
t .Helper ()
333
358
334
359
// Set up a stub gitlab server
@@ -342,7 +367,7 @@ func runSSHD(t *testing.T, apiHandler http.Handler) *ssh.Client {
342
367
dir , hostKey := configureSSHD (t , apiServer .URL )
343
368
listenAddr := startSSHD (t , dir )
344
369
345
- return buildClient (t , listenAddr , hostKey )
370
+ return buildClient (t , listenAddr , clientKeyType , hostKey )
346
371
}
347
372
348
373
func TestDiscoverSuccess (t * testing.T ) {
@@ -352,7 +377,7 @@ func TestDiscoverSuccess(t *testing.T) {
352
377
fmt .Fprint (w , `{"id": 1000, "name": "Test User", "username": "test-user"}` )
353
378
},
354
379
}
355
- client := runSSHD (t , successAPI (t , handler ))
380
+ client := runSSHD (t , "ed25519" , successAPI (t , handler ))
356
381
357
382
session , err := client .NewSession ()
358
383
require .NoError (t , err )
@@ -370,7 +395,7 @@ func TestPersonalAccessTokenSuccess(t *testing.T) {
370
395
fmt .Fprint (w , `{"success": true, "token": "testtoken", "scopes": ["api"], "expires_at": "9001-01-01"}` )
371
396
},
372
397
}
373
- client := runSSHD (t , successAPI (t , handler ))
398
+ client := runSSHD (t , "ed25519" , successAPI (t , handler ))
374
399
375
400
session , err := client .NewSession ()
376
401
require .NoError (t , err )
@@ -388,7 +413,7 @@ func TestTwoFactorAuthRecoveryCodesSuccess(t *testing.T) {
388
413
fmt .Fprint (w , `{"success": true, "recovery_codes": ["code1", "code2"]}` )
389
414
},
390
415
}
391
- client := runSSHD (t , successAPI (t , handler ))
416
+ client := runSSHD (t , "ed25519" , successAPI (t , handler ))
392
417
session , stdin , stdout := newSession (t , client )
393
418
394
419
reader := bufio .NewReader (stdout )
@@ -428,7 +453,7 @@ func TwoFactorAuthVerifySuccess(t *testing.T) {
428
453
fmt .Fprint (w , `{"success": true}` )
429
454
},
430
455
}
431
- client := runSSHD (t , successAPI (t , handler ))
456
+ client := runSSHD (t , "ed25519" , successAPI (t , handler ))
432
457
session , stdin , stdout := newSession (t , client )
433
458
434
459
reader := bufio .NewReader (stdout )
@@ -455,7 +480,7 @@ func TestGitLfsAuthenticateSuccess(t *testing.T) {
455
480
fmt .Fprint (w , `{"username": "test-user", "lfs_token": "testlfstoken", "repo_path": "foo", "expires_in": 7200}` )
456
481
},
457
482
}
458
- client := runSSHD (t , successAPI (t , handler ))
483
+ client := runSSHD (t , "ed25519" , successAPI (t , handler ))
459
484
460
485
session , err := client .NewSession ()
461
486
require .NoError (t , err )
@@ -471,27 +496,29 @@ func TestGitLfsAuthenticateSuccess(t *testing.T) {
471
496
func TestGitReceivePackSuccess (t * testing.T ) {
472
497
ensureGitalyRepository (t )
473
498
474
- client := runSSHD (t , successAPI (t ))
475
- session , stdin , stdout := newSession (t , client )
476
-
477
- err := session .Start (fmt .Sprintf ("git-receive-pack %s" , testRepo ))
478
- require .NoError (t , err )
499
+ for _ , keyType := range keyTypes {
500
+ t .Run (keyType , func (t * testing.T ) {
501
+ client := runSSHD (t , keyType , successAPI (t ))
502
+ session , stdin , stdout := newSession (t , client )
479
503
480
- // Gracefully close connection
481
- _ , err = fmt .Fprintln (stdin , "0000" )
482
- require .NoError (t , err )
483
- stdin .Close ()
504
+ err := session .Start (fmt .Sprintf ("git-receive-pack %s" , testRepo ))
505
+ require .NoError (t , err )
484
506
485
- output , err := io .ReadAll (stdout )
486
- require .NoError (t , err )
507
+ // Gracefully close connection
508
+ _ , err = fmt .Fprintln (stdin , "0000" )
509
+ require .NoError (t , err )
510
+ stdin .Close ()
487
511
488
- outputLines := strings .Split (string (output ), "\n " )
512
+ output , err := io .ReadAll (stdout )
513
+ require .NoError (t , err )
489
514
490
- for i := 0 ; i < (len (outputLines ) - 1 ); i ++ {
491
- require .Regexp (t , "^[0-9a-f]{44} refs/(heads|tags)/[^ ]+" , outputLines [i ])
515
+ outputLines := strings .Split (string (output ), "\n " )
516
+ for i := 0 ; i < (len (outputLines ) - 1 ); i ++ {
517
+ require .Regexp (t , "^[0-9a-f]{44} refs/(heads|tags)/[^ ]+" , outputLines [i ])
518
+ }
519
+ require .Equal (t , "0000" , outputLines [len (outputLines )- 1 ])
520
+ })
492
521
}
493
-
494
- require .Equal (t , "0000" , outputLines [len (outputLines )- 1 ])
495
522
}
496
523
497
524
func TestGeoGitReceivePackSuccess (t * testing.T ) {
@@ -508,7 +535,7 @@ func TestGeoGitReceivePackSuccess(t *testing.T) {
508
535
assert .NoError (t , err )
509
536
},
510
537
}
511
- client := runSSHD (t , successAPI (t , handler ))
538
+ client := runSSHD (t , "ed25519" , successAPI (t , handler ))
512
539
session , stdin , stdout := newSession (t , client )
513
540
514
541
err := session .Start (fmt .Sprintf ("git-receive-pack %s" , testRepo ))
@@ -534,7 +561,7 @@ func TestGeoGitReceivePackSuccess(t *testing.T) {
534
561
func TestGitUploadPackSuccess (t * testing.T ) {
535
562
ensureGitalyRepository (t )
536
563
537
- client := runSSHD (t , successAPI (t ))
564
+ client := runSSHD (t , "ed25519" , successAPI (t ))
538
565
defer client .Close ()
539
566
540
567
numberOfSessions := 3
@@ -571,7 +598,7 @@ func TestGitUploadPackSuccess(t *testing.T) {
571
598
func TestGitUploadArchiveSuccess (t * testing.T ) {
572
599
ensureGitalyRepository (t )
573
600
574
- client := runSSHD (t , successAPI (t ))
601
+ client := runSSHD (t , "ed25519" , successAPI (t ))
575
602
session , stdin , stdout := newSession (t , client )
576
603
reader := bufio .NewReader (stdout )
577
604
0 commit comments