2019-02-03 12:07:57 -05:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/hashicorp/packer/common/uuid"
|
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2019-02-04 12:21:58 -05:00
|
|
|
"github.com/hashicorp/packer/helper/ssh"
|
2019-02-03 12:07:57 -05:00
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
// StepSshKeyPair executes the business logic for setting the SSH key pair in
|
|
|
|
// the specified communicator.Config.
|
|
|
|
type StepSshKeyPair struct {
|
|
|
|
Debug bool
|
|
|
|
DebugKeyPath string
|
|
|
|
Comm *communicator.Config
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepSshKeyPair) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2019-02-03 12:07:57 -05:00
|
|
|
if s.Comm.SSHPassword != "" {
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
if s.Comm.SSHPrivateKeyFile != "" {
|
|
|
|
ui.Say("Using existing SSH private key for the communicator...")
|
|
|
|
privateKeyBytes, err := s.Comm.ReadSSHPrivateKeyFile()
|
|
|
|
if err != nil {
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2019-02-06 13:46:55 -05:00
|
|
|
|
2019-02-25 17:16:24 -05:00
|
|
|
kp, err := ssh.KeyPairFromPrivateKey(ssh.FromPrivateKeyConfig{
|
|
|
|
RawPrivateKeyPemBlock: privateKeyBytes,
|
2019-03-01 10:00:29 -05:00
|
|
|
Comment: fmt.Sprintf("packer_%s", uuid.TimeOrderedUUID()),
|
2019-02-25 17:16:24 -05:00
|
|
|
})
|
2019-02-06 13:46:55 -05:00
|
|
|
if err != nil {
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2019-02-03 12:07:57 -05:00
|
|
|
|
|
|
|
s.Comm.SSHPrivateKey = privateKeyBytes
|
2019-03-01 10:00:29 -05:00
|
|
|
s.Comm.SSHKeyPairName = kp.Comment
|
|
|
|
s.Comm.SSHTemporaryKeyPairName = kp.Comment
|
2019-02-25 17:16:24 -05:00
|
|
|
s.Comm.SSHPublicKey = kp.PublicKeyAuthorizedKeysLine
|
2019-02-03 12:07:57 -05:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.Comm.SSHAgentAuth {
|
|
|
|
ui.Say("Using local SSH Agent to authenticate connections for the communicator...")
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Creating ephemeral key pair for SSH communicator...")
|
|
|
|
|
2019-02-25 17:16:24 -05:00
|
|
|
kp, err := ssh.NewKeyPair(ssh.CreateKeyPairConfig{
|
2019-03-01 10:00:29 -05:00
|
|
|
Comment: fmt.Sprintf("packer_%s", uuid.TimeOrderedUUID()),
|
2019-02-25 17:16:24 -05:00
|
|
|
})
|
2019-02-03 12:07:57 -05:00
|
|
|
if err != nil {
|
|
|
|
state.Put("error", fmt.Errorf("Error creating temporary keypair: %s", err))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2019-03-01 10:00:29 -05:00
|
|
|
s.Comm.SSHKeyPairName = kp.Comment
|
|
|
|
s.Comm.SSHTemporaryKeyPairName = kp.Comment
|
2019-02-25 17:16:24 -05:00
|
|
|
s.Comm.SSHPrivateKey = kp.PrivateKeyPemBlock
|
|
|
|
s.Comm.SSHPublicKey = kp.PublicKeyAuthorizedKeysLine
|
2019-02-04 12:12:18 -05:00
|
|
|
s.Comm.SSHClearAuthorizedKeys = true
|
2019-02-03 12:07:57 -05:00
|
|
|
|
2019-02-25 17:16:24 -05:00
|
|
|
ui.Say("Created ephemeral SSH key pair for communicator")
|
2019-02-03 12:07:57 -05:00
|
|
|
|
|
|
|
// If we're in debug mode, output the private key to the working
|
|
|
|
// directory.
|
|
|
|
if s.Debug {
|
|
|
|
ui.Message(fmt.Sprintf("Saving communicator private key for debug purposes: %s", s.DebugKeyPath))
|
2019-02-28 17:23:33 -05:00
|
|
|
f, err := os.OpenFile(s.DebugKeyPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
2019-02-03 12:07:57 -05:00
|
|
|
if err != nil {
|
|
|
|
state.Put("error", fmt.Errorf("Error saving debug key: %s", err))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
// Write the key out
|
2019-02-25 17:16:24 -05:00
|
|
|
if _, err := f.Write(kp.PrivateKeyPemBlock); err != nil {
|
2019-02-03 12:07:57 -05:00
|
|
|
state.Put("error", fmt.Errorf("Error saving debug key: %s", err))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepSshKeyPair) Cleanup(state multistep.StateBag) {
|
|
|
|
if s.Debug {
|
|
|
|
if err := os.Remove(s.DebugKeyPath); err != nil {
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
ui.Error(fmt.Sprintf(
|
|
|
|
"Error removing debug key '%s': %s", s.DebugKeyPath, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|