Skip to content

Commit b85bc30

Browse files
authored
Fixed ssh_password_login_disallowed test for Juniper (openconfig#5253)
* Fixed ssh_password_login_disallowed test for Juniper * Fixed gemini comments
1 parent b343c7a commit b85bc30

File tree

2 files changed

+64
-21
lines changed

2 files changed

+64
-21
lines changed

feature/gnsi/credentialz/tests/ssh_password_login_disallowed/ssh_password_login_disallowed_test.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const (
3939
username = "testuser"
4040
userPrincipal = "my_principal"
4141
command = "show version"
42-
maxSSHRetryTime = 30 // Unit is seconds.
42+
maxSSHRetryTime = 120 // Unit is seconds.
4343
)
4444

4545
func TestMain(m *testing.M) {
@@ -63,9 +63,14 @@ func TestCredentialz(t *testing.T) {
6363
}
6464
}(dir)
6565

66+
algo := "ed25519"
67+
if dut.Vendor() == ondatra.JUNIPER {
68+
algo = "rsa"
69+
}
70+
6671
// Create ssh keys/certificates for CA & testuser.
67-
credz.CreateSSHKeyPair(t, dir, "ca")
68-
credz.CreateSSHKeyPair(t, dir, username)
72+
credz.CreateSSHKeyPairAlgo(t, dir, "ca", algo)
73+
credz.CreateSSHKeyPairAlgo(t, dir, username, algo)
6974
credz.CreateUserCertificate(t, dir, userPrincipal)
7075

7176
// Setup user and password.
@@ -86,7 +91,7 @@ func TestCredentialz(t *testing.T) {
8691
}
8792

8893
// Verify ssh with password fails as expected.
89-
ctx, cancel := context.WithTimeout(t.Context(), 30*time.Second)
94+
ctx, cancel := context.WithTimeout(t.Context(), 120*time.Second)
9095
defer cancel()
9196
startTime := time.Now()
9297
for {
@@ -121,7 +126,7 @@ func TestCredentialz(t *testing.T) {
121126
}
122127

123128
// Verify ssh with certificate succeeds.
124-
ctx, cancel := context.WithTimeout(t.Context(), 30*time.Second)
129+
ctx, cancel := context.WithTimeout(t.Context(), 120*time.Second)
125130
defer cancel()
126131
startTime := time.Now()
127132
// var conn *ssh.Client
@@ -136,7 +141,7 @@ func TestCredentialz(t *testing.T) {
136141
if uint64(time.Since(startTime).Seconds()) > maxSSHRetryTime {
137142
t.Fatalf("Exceeded maxSSHRetryTime, dialing ssh failed, but we expected to succeed, error: %s", err)
138143
}
139-
t.Logf("Dialing ssh failed, retrying ...")
144+
t.Logf("Dialing ssh failed: %v, retrying ...", err)
140145
time.Sleep(5 * time.Second)
141146
}
142147

internal/security/credz/credz.go

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,17 @@ func RotateAuthorizedKey(t *testing.T, dut *ondatra.DUTDevice, dir, username, ve
219219
t.Fatalf("Failed reading private key contents, error: %s", err)
220220
}
221221
dataTypes := bytes.Fields(data)
222+
keyType := keyTypeFromAlgo(string(dataTypes[0]))
223+
if keyType == cpb.KeyType_KEY_TYPE_UNSPECIFIED {
224+
keyType = cpb.KeyType_KEY_TYPE_ED25519
225+
}
226+
authKey := dataTypes[1]
227+
if dut.Vendor() == ondatra.JUNIPER {
228+
authKey = bytes.Join(dataTypes[:2], []byte(" "))
229+
}
222230
keyContents = append(keyContents, &cpb.AccountCredentials_AuthorizedKey{
223-
// AuthorizedKey: data,
224-
AuthorizedKey: dataTypes[1],
225-
KeyType: cpb.KeyType_KEY_TYPE_ED25519,
231+
AuthorizedKey: authKey,
232+
KeyType: keyType,
226233
})
227234
}
228235
request := &cpb.RotateAccountCredentialsRequest{
@@ -253,9 +260,17 @@ func RotateTrustedUserCA(t *testing.T, dut *ondatra.DUTDevice, dir string) {
253260
t.Fatalf("Failed reading ca public key contents, error: %s", err)
254261
}
255262
dataTypes := bytes.Fields(data)
263+
keyType := keyTypeFromAlgo(string(dataTypes[0]))
264+
if keyType == cpb.KeyType_KEY_TYPE_UNSPECIFIED {
265+
t.Fatalf("Unrecognized key type: %s", dataTypes[0])
266+
}
267+
pubKey := dataTypes[1]
268+
if dut.Vendor() == ondatra.JUNIPER {
269+
pubKey = bytes.Join(dataTypes[:2], []byte(" "))
270+
}
256271
keyContents = append(keyContents, &cpb.PublicKey{
257-
PublicKey: dataTypes[1],
258-
KeyType: cpb.KeyType_KEY_TYPE_ED25519,
272+
PublicKey: pubKey,
273+
KeyType: keyType,
259274
})
260275
}
261276
request := &cpb.RotateHostParametersRequest{
@@ -424,23 +439,29 @@ func GetDutPublicKey(t *testing.T, dut *ondatra.DUTDevice, targetAlgo string) []
424439
return []byte(keyLine)
425440
}
426441

427-
// CreateSSHKeyPair creates ssh keypair with a filename of keyName in the specified directory.
428-
// Keypairs can be created for ca/dut/testuser as per individual credentialz test requirements.
429-
func CreateSSHKeyPair(t *testing.T, dir, keyName string) {
430-
sshCmd := exec.Command(
431-
"ssh-keygen",
432-
"-t", "ed25519",
433-
"-f", keyName,
434-
"-C", keyName,
435-
"-q", "-N", "",
436-
)
442+
// CreateSSHKeyPairAlgo creates ssh keypair with a filename of keyName in the specified directory with the specified algo.
443+
func CreateSSHKeyPairAlgo(t *testing.T, dir, keyName, algo string) {
444+
args := []string{
445+
"-t", algo,
446+
}
447+
if algo == "rsa" {
448+
args = append(args, "-b", "4096")
449+
}
450+
args = append(args, "-f", keyName, "-C", keyName, "-q", "-N", "")
451+
sshCmd := exec.Command("ssh-keygen", args...)
437452
sshCmd.Dir = dir
438453
err := sshCmd.Run()
439454
if err != nil {
440455
t.Fatalf("Failed generating %s key pair, error: %s", keyName, err)
441456
}
442457
}
443458

459+
// CreateSSHKeyPair creates ssh keypair with a filename of keyName in the specified directory.
460+
// Keypairs can be created for ca/dut/testuser as per individual credentialz test requirements.
461+
func CreateSSHKeyPair(t *testing.T, dir, keyName string) {
462+
CreateSSHKeyPairAlgo(t, dir, keyName, "ed25519")
463+
}
464+
444465
// CreateUserCertificate creates ssh user certificate in the specified directory.
445466
func CreateUserCertificate(t *testing.T, dir, userPrincipal string) {
446467
userCertCmd := exec.Command(
@@ -727,6 +748,23 @@ func GetConfiguredHostKey(t *testing.T, dut *ondatra.DUTDevice, algo string, fqd
727748
return algo + " " + matchingKey
728749
}
729750

751+
func keyTypeFromAlgo(algo string) cpb.KeyType {
752+
switch algo {
753+
case "ssh-rsa":
754+
return cpb.KeyType_KEY_TYPE_RSA_4096
755+
case "ecdsa-sha2-nistp256":
756+
return cpb.KeyType_KEY_TYPE_ECDSA_P_256
757+
case "ecdsa-sha2-nistp384":
758+
return cpb.KeyType_KEY_TYPE_ECDSA_P_384
759+
case "ecdsa-sha2-nistp521":
760+
return cpb.KeyType_KEY_TYPE_ECDSA_P_521
761+
case "ssh-ed25519":
762+
return cpb.KeyType_KEY_TYPE_ED25519
763+
default:
764+
return cpb.KeyType_KEY_TYPE_UNSPECIFIED
765+
}
766+
}
767+
730768
func sshAlgo(t *testing.T, pk *cpb.PublicKey) string {
731769
keyType := pk.KeyType
732770
switch keyType {

0 commit comments

Comments
 (0)