From 7e1cbc6d89962d3b908e775c24f210848d4cf8b8 Mon Sep 17 00:00:00 2001 From: Stephen Fox Date: Fri, 1 Mar 2019 10:00:29 -0500 Subject: [PATCH] Rename references to key pair name to comment. Per the OpenSSH manual, the field is for a comment. --- .../virtualbox/common/step_ssh_key_pair.go | 12 +++--- helper/ssh/key_pair.go | 43 ++++++++++--------- helper/ssh/key_pair_test.go | 40 ++++++++--------- 3 files changed, 49 insertions(+), 46 deletions(-) diff --git a/builder/virtualbox/common/step_ssh_key_pair.go b/builder/virtualbox/common/step_ssh_key_pair.go index 66577bab1..929bc9526 100644 --- a/builder/virtualbox/common/step_ssh_key_pair.go +++ b/builder/virtualbox/common/step_ssh_key_pair.go @@ -37,7 +37,7 @@ func (s *StepSshKeyPair) Run(_ context.Context, state multistep.StateBag) multis kp, err := ssh.KeyPairFromPrivateKey(ssh.FromPrivateKeyConfig{ RawPrivateKeyPemBlock: privateKeyBytes, - Name: fmt.Sprintf("packer_%s", uuid.TimeOrderedUUID()), + Comment: fmt.Sprintf("packer_%s", uuid.TimeOrderedUUID()), }) if err != nil { state.Put("error", err) @@ -45,8 +45,8 @@ func (s *StepSshKeyPair) Run(_ context.Context, state multistep.StateBag) multis } s.Comm.SSHPrivateKey = privateKeyBytes - s.Comm.SSHKeyPairName = kp.Name - s.Comm.SSHTemporaryKeyPairName = kp.Name + s.Comm.SSHKeyPairName = kp.Comment + s.Comm.SSHTemporaryKeyPairName = kp.Comment s.Comm.SSHPublicKey = kp.PublicKeyAuthorizedKeysLine return multistep.ActionContinue @@ -60,15 +60,15 @@ func (s *StepSshKeyPair) Run(_ context.Context, state multistep.StateBag) multis ui.Say("Creating ephemeral key pair for SSH communicator...") kp, err := ssh.NewKeyPair(ssh.CreateKeyPairConfig{ - Name: fmt.Sprintf("packer_%s", uuid.TimeOrderedUUID()), + Comment: fmt.Sprintf("packer_%s", uuid.TimeOrderedUUID()), }) if err != nil { state.Put("error", fmt.Errorf("Error creating temporary keypair: %s", err)) return multistep.ActionHalt } - s.Comm.SSHKeyPairName = kp.Name - s.Comm.SSHTemporaryKeyPairName = kp.Name + s.Comm.SSHKeyPairName = kp.Comment + s.Comm.SSHTemporaryKeyPairName = kp.Comment s.Comm.SSHPrivateKey = kp.PrivateKeyPemBlock s.Comm.SSHPublicKey = kp.PublicKeyAuthorizedKeysLine s.Comm.SSHClearAuthorizedKeys = true diff --git a/helper/ssh/key_pair.go b/helper/ssh/key_pair.go index f6852080d..72dd5b7f6 100644 --- a/helper/ssh/key_pair.go +++ b/helper/ssh/key_pair.go @@ -47,9 +47,10 @@ type KeyPair struct { // as a line in OpenSSH authorized_keys. PublicKeyAuthorizedKeysLine []byte - // Name is the key pair's name. This is used to identify - // the key pair in the SSH server's 'authorized_keys'. - Name string + // Comment is the key pair's comment. This is typically used + // to identify the key pair's owner in the SSH user's + // 'authorized_keys' file. + Comment string } // KeyPairFromPrivateKey returns a KeyPair loaded from an existing private key. @@ -76,7 +77,7 @@ func KeyPairFromPrivateKey(config FromPrivateKeyConfig) (KeyPair, error) { } return KeyPair{ PrivateKeyPemBlock: config.RawPrivateKeyPemBlock, - PublicKeyAuthorizedKeysLine: authorizedKeysLine(publicKey, config.Name), + PublicKeyAuthorizedKeysLine: authorizedKeysLine(publicKey, config.Comment), }, nil case *dsa.PrivateKey: publicKey, err := gossh.NewPublicKey(&pk.PublicKey) @@ -85,7 +86,7 @@ func KeyPairFromPrivateKey(config FromPrivateKeyConfig) (KeyPair, error) { } return KeyPair{ PrivateKeyPemBlock: config.RawPrivateKeyPemBlock, - PublicKeyAuthorizedKeysLine: authorizedKeysLine(publicKey, config.Name), + PublicKeyAuthorizedKeysLine: authorizedKeysLine(publicKey, config.Comment), }, nil } @@ -99,9 +100,10 @@ type FromPrivateKeyConfig struct { // should be loaded from. RawPrivateKeyPemBlock []byte - // Name is the resulting key pair's name. This is used to identify - // the key pair in the SSH server's 'authorized_keys'. - Name string + // Comment is the key pair's comment. This is typically used + // to identify the key pair's owner in the SSH user's + // 'authorized_keys' file. + Comment string } // NewKeyPair generates a new SSH key pair using the specified @@ -169,8 +171,8 @@ func newEcdsaKeyPair(config CreateKeyPairConfig) (KeyPair, error) { return KeyPair{ PrivateKeyPemBlock: privatePem, - PublicKeyAuthorizedKeysLine: authorizedKeysLine(sshPublicKey, config.Name), - Name: config.Name, + PublicKeyAuthorizedKeysLine: authorizedKeysLine(sshPublicKey, config.Comment), + Comment: config.Comment, }, nil } @@ -201,8 +203,8 @@ func newRsaKeyPair(config CreateKeyPairConfig) (KeyPair, error) { return KeyPair{ PrivateKeyPemBlock: privatePemBlock, - PublicKeyAuthorizedKeysLine: authorizedKeysLine(sshPublicKey, config.Name), - Name: config.Name, + PublicKeyAuthorizedKeysLine: authorizedKeysLine(sshPublicKey, config.Comment), + Comment: config.Comment, }, nil } @@ -216,9 +218,10 @@ type CreateKeyPairConfig struct { // 521-bit curve. Bits int - // Name is the resulting key pair's name. This is used to identify - // the key pair in the SSH server's 'authorized_keys'. - Name string + // Comment is the resulting key pair's comment. This is typically + // used to identify the key pair's owner in the SSH user's + // 'authorized_keys' file. + Comment string } // rawPemBlock encodes a pem.Block to a slice of bytes. @@ -235,19 +238,19 @@ func rawPemBlock(block *pem.Block) ([]byte, error) { // authorizedKeysLine serializes key for inclusion in an OpenSSH // authorized_keys file. The return value ends without newline so -// a key name can be appended to the end. -func authorizedKeysLine(key gossh.PublicKey, name string) []byte { +// a comment can be appended to the end. +func authorizedKeysLine(key gossh.PublicKey, comment string) []byte { marshaledPublicKey := gossh.MarshalAuthorizedKey(key) // Remove the mandatory unix new line. Awful, but the go // ssh library automatically appends a unix new line. - // We remove it so a key name can be safely appended to the + // We remove it so a key comment can be safely appended to the // end of the string. marshaledPublicKey = bytes.TrimSpace(marshaledPublicKey) - if len(strings.TrimSpace(name)) > 0 { + if len(strings.TrimSpace(comment)) > 0 { marshaledPublicKey = append(marshaledPublicKey, ' ') - marshaledPublicKey = append(marshaledPublicKey, name...) + marshaledPublicKey = append(marshaledPublicKey, comment...) } return marshaledPublicKey diff --git a/helper/ssh/key_pair_test.go b/helper/ssh/key_pair_test.go index eddb9a3cb..3c98c84e8 100644 --- a/helper/ssh/key_pair_test.go +++ b/helper/ssh/key_pair_test.go @@ -177,9 +177,9 @@ func TestNewKeyPair_ECDSA_Default(t *testing.T) { func TestNewKeyPair_ECDSA_Positive(t *testing.T) { for _, bits := range []int{521, 384, 256} { config := CreateKeyPairConfig{ - Type: Ecdsa, - Bits: bits, - Name: uuid.TimeOrderedUUID(), + Type: Ecdsa, + Bits: bits, + Comment: uuid.TimeOrderedUUID(), } kp, err := NewKeyPair(config) @@ -188,8 +188,8 @@ func TestNewKeyPair_ECDSA_Positive(t *testing.T) { } err = verifyEcdsaKeyPair(kp, expectedData{ - bits: bits, - name: config.Name, + bits: bits, + comment: config.Comment, }) if err != nil { t.Fatal(err.Error()) @@ -212,9 +212,9 @@ func TestNewKeyPair_ECDSA_Negative(t *testing.T) { func TestNewKeyPair_RSA_Positive(t *testing.T) { for _, bits := range []int{4096, 2048} { config := CreateKeyPairConfig{ - Type: Rsa, - Bits: bits, - Name: uuid.TimeOrderedUUID(), + Type: Rsa, + Bits: bits, + Comment: uuid.TimeOrderedUUID(), } kp, err := NewKeyPair(config) @@ -223,8 +223,8 @@ func TestNewKeyPair_RSA_Positive(t *testing.T) { } err = verifyRsaKeyPair(kp, expectedData{ - bits: config.Bits, - name: config.Name, + bits: config.Bits, + comment: config.Comment, }) if err != nil { t.Fatal(err.Error()) @@ -316,8 +316,8 @@ type fromPrivateExpectedData struct { } type expectedData struct { - bits int - name string + bits int + comment string } func verifyEcdsaKeyPair(kp KeyPair, e expectedData) error { @@ -341,9 +341,9 @@ func verifyEcdsaKeyPair(kp KeyPair, e expectedData) error { } expectedBytes := bytes.TrimSuffix(gossh.MarshalAuthorizedKey(publicKey), []byte("\n")) - if len(e.name) > 0 { + if len(e.comment) > 0 { expectedBytes = append(expectedBytes, ' ') - expectedBytes = append(expectedBytes, e.name...) + expectedBytes = append(expectedBytes, e.comment...) } if !bytes.Equal(expectedBytes, kp.PublicKeyAuthorizedKeysLine) { @@ -375,9 +375,9 @@ func verifyRsaKeyPair(kp KeyPair, e expectedData) error { } expectedBytes := bytes.TrimSuffix(gossh.MarshalAuthorizedKey(publicKey), []byte("\n")) - if len(e.name) > 0 { + if len(e.comment) > 0 { expectedBytes = append(expectedBytes, ' ') - expectedBytes = append(expectedBytes, e.name...) + expectedBytes = append(expectedBytes, e.comment...) } if !bytes.Equal(expectedBytes, kp.PublicKeyAuthorizedKeysLine) { @@ -405,9 +405,9 @@ func verifyDsaKeyPair(kp KeyPair, e fromPrivateExpectedData) error { } expectedBytes := bytes.TrimSuffix(gossh.MarshalAuthorizedKey(publicKey), []byte("\n")) - if len(e.d.name) > 0 { + if len(e.d.comment) > 0 { expectedBytes = append(expectedBytes, ' ') - expectedBytes = append(expectedBytes, e.d.name...) + expectedBytes = append(expectedBytes, e.d.comment...) } if !bytes.Equal(expectedBytes, kp.PublicKeyAuthorizedKeysLine) { @@ -435,9 +435,9 @@ func verifyEd25519KeyPair(kp KeyPair, e fromPrivateExpectedData) error { } expectedBytes := bytes.TrimSuffix(gossh.MarshalAuthorizedKey(publicKey), []byte("\n")) - if len(e.d.name) > 0 { + if len(e.d.comment) > 0 { expectedBytes = append(expectedBytes, ' ') - expectedBytes = append(expectedBytes, e.d.name...) + expectedBytes = append(expectedBytes, e.d.comment...) } if !bytes.Equal(expectedBytes, kp.PublicKeyAuthorizedKeysLine) {