Honor value of 'Comment' field in 'ssh.KeyPairFromPrivateKey()'.

The 'ssh.KeyPairFromPrivateKey()' function in the 'ssh' helper
library was not honoring the value of the 'Comment' field in the
'FromPrivateKeyConfig' struct. This commit fixes the issue, and
updates unit tests to catch the issue if it happens again.
This commit is contained in:
Stephen Fox 2019-07-25 17:09:32 -04:00
parent d21f4eb888
commit 9592211bcf
2 changed files with 39 additions and 8 deletions

View File

@ -77,6 +77,7 @@ func KeyPairFromPrivateKey(config FromPrivateKeyConfig) (KeyPair, error) {
return KeyPair{}, err
}
return KeyPair{
Comment: config.Comment,
PrivateKeyPemBlock: config.RawPrivateKeyPemBlock,
PublicKeyAuthorizedKeysLine: authorizedKeysLine(publicKey, config.Comment),
}, nil
@ -86,6 +87,7 @@ func KeyPairFromPrivateKey(config FromPrivateKeyConfig) (KeyPair, error) {
return KeyPair{}, err
}
return KeyPair{
Comment: config.Comment,
PrivateKeyPemBlock: config.RawPrivateKeyPemBlock,
PublicKeyAuthorizedKeysLine: authorizedKeysLine(publicKey, config.Comment),
}, nil

View File

@ -237,49 +237,57 @@ func TestKeyPairFromPrivateKey(t *testing.T) {
pemRsa1024: {
t: Rsa,
d: expectedData{
bits: 1024,
bits: 1024,
comment: uuid.TimeOrderedUUID(),
},
},
pemRsa2048: {
t: Rsa,
d: expectedData{
bits: 2048,
bits: 2048,
comment: uuid.TimeOrderedUUID(),
},
},
pemOpenSshRsa1024: {
t: Rsa,
d: expectedData{
bits: 1024,
bits: 1024,
comment: uuid.TimeOrderedUUID(),
},
},
pemOpenSshRsa2048: {
t: Rsa,
d: expectedData{
bits: 2048,
bits: 2048,
comment: uuid.TimeOrderedUUID(),
},
},
pemDsa: {
t: Dsa,
d: expectedData{
bits: 1024,
bits: 1024,
comment: uuid.TimeOrderedUUID(),
},
},
pemEcdsa384: {
t: Ecdsa,
d: expectedData{
bits: 384,
bits: 384,
comment: uuid.TimeOrderedUUID(),
},
},
pemEcdsa521: {
t: Ecdsa,
d: expectedData{
bits: 521,
bits: 521,
comment: uuid.TimeOrderedUUID(),
},
},
pemOpenSshEd25519: {
t: Ed25519,
d: expectedData{
bits: 256,
bits: 256,
comment: uuid.TimeOrderedUUID(),
},
},
}
@ -287,6 +295,7 @@ func TestKeyPairFromPrivateKey(t *testing.T) {
for rawPrivateKey, expected := range m {
kp, err := KeyPairFromPrivateKey(FromPrivateKeyConfig{
RawPrivateKeyPemBlock: []byte(rawPrivateKey),
Comment: expected.d.comment,
})
if err != nil {
t.Fatal(err.Error())
@ -340,6 +349,11 @@ func verifyEcdsaKeyPair(kp KeyPair, e expectedData) error {
return err
}
if kp.Comment != e.comment {
return fmt.Errorf("key pair comment should be:\n'%s'\nGot:\n'%s'",
e.comment, kp.Comment)
}
expectedBytes := bytes.TrimSuffix(gossh.MarshalAuthorizedKey(publicKey), []byte("\n"))
if len(e.comment) > 0 {
expectedBytes = append(expectedBytes, ' ')
@ -374,6 +388,11 @@ func verifyRsaKeyPair(kp KeyPair, e expectedData) error {
return err
}
if kp.Comment != e.comment {
return fmt.Errorf("key pair comment should be:\n'%s'\nGot:\n'%s'",
e.comment, kp.Comment)
}
expectedBytes := bytes.TrimSuffix(gossh.MarshalAuthorizedKey(publicKey), []byte("\n"))
if len(e.comment) > 0 {
expectedBytes = append(expectedBytes, ' ')
@ -404,6 +423,11 @@ func verifyDsaKeyPair(kp KeyPair, e fromPrivateExpectedData) error {
return err
}
if kp.Comment != e.d.comment {
return fmt.Errorf("key pair comment should be:\n'%s'\nGot:\n'%s'",
e.d.comment, kp.Comment)
}
expectedBytes := bytes.TrimSuffix(gossh.MarshalAuthorizedKey(publicKey), []byte("\n"))
if len(e.d.comment) > 0 {
expectedBytes = append(expectedBytes, ' ')
@ -434,6 +458,11 @@ func verifyEd25519KeyPair(kp KeyPair, e fromPrivateExpectedData) error {
return err
}
if kp.Comment != e.d.comment {
return fmt.Errorf("key pair comment should be:\n'%s'\nGot:\n'%s'",
e.d.comment, kp.Comment)
}
expectedBytes := bytes.TrimSuffix(gossh.MarshalAuthorizedKey(publicKey), []byte("\n"))
if len(e.d.comment) > 0 {
expectedBytes = append(expectedBytes, ' ')