Merge pull request #7922 from stephen-fox/honor-comment-existing-ssh-key-pair

Honor value of 'Comment' field in 'ssh.KeyPairFromPrivateKey()'.
This commit is contained in:
Megan Marsh 2019-07-29 13:22:46 -07:00 committed by GitHub
commit 96366ac814
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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{}, err
} }
return KeyPair{ return KeyPair{
Comment: config.Comment,
PrivateKeyPemBlock: config.RawPrivateKeyPemBlock, PrivateKeyPemBlock: config.RawPrivateKeyPemBlock,
PublicKeyAuthorizedKeysLine: authorizedKeysLine(publicKey, config.Comment), PublicKeyAuthorizedKeysLine: authorizedKeysLine(publicKey, config.Comment),
}, nil }, nil
@ -86,6 +87,7 @@ func KeyPairFromPrivateKey(config FromPrivateKeyConfig) (KeyPair, error) {
return KeyPair{}, err return KeyPair{}, err
} }
return KeyPair{ return KeyPair{
Comment: config.Comment,
PrivateKeyPemBlock: config.RawPrivateKeyPemBlock, PrivateKeyPemBlock: config.RawPrivateKeyPemBlock,
PublicKeyAuthorizedKeysLine: authorizedKeysLine(publicKey, config.Comment), PublicKeyAuthorizedKeysLine: authorizedKeysLine(publicKey, config.Comment),
}, nil }, nil

View File

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