diff --git a/builder/digitalocean/builder.go b/builder/digitalocean/builder.go index 7cf07788a..409dc7748 100644 --- a/builder/digitalocean/builder.go +++ b/builder/digitalocean/builder.go @@ -80,10 +80,17 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) // Build the steps steps := []multistep.Step{ - &stepCreateSSHKey{ - Debug: b.config.PackerDebug, - DebugKeyPath: fmt.Sprintf("do_%s.pem", b.config.PackerBuildName), + &communicator.StepSSHKeyGen{ + CommConf: &b.config.Comm, + SSHTemporaryKeyPair: b.config.Comm.SSH.SSHTemporaryKeyPair, }, + multistep.If(b.config.PackerDebug && b.config.Comm.SSHPrivateKeyFile == "", + &communicator.StepDumpSSHKey{ + Path: fmt.Sprintf("do_%s.pem", b.config.PackerBuildName), + SSH: &b.config.Comm.SSH, + }, + ), + &stepCreateSSHKey{}, new(stepCreateDroplet), new(stepDropletInfo), &communicator.StepConnect{ diff --git a/builder/digitalocean/step_create_ssh_key.go b/builder/digitalocean/step_create_ssh_key.go index 76e988d2a..3eeb526bc 100644 --- a/builder/digitalocean/step_create_ssh_key.go +++ b/builder/digitalocean/step_create_ssh_key.go @@ -2,26 +2,16 @@ package digitalocean import ( "context" - "crypto/rand" - "crypto/rsa" - "crypto/x509" - "encoding/pem" "fmt" "log" - "os" - "runtime" "github.com/digitalocean/godo" "github.com/hashicorp/packer-plugin-sdk/multistep" packersdk "github.com/hashicorp/packer-plugin-sdk/packer" "github.com/hashicorp/packer-plugin-sdk/uuid" - "golang.org/x/crypto/ssh" ) type stepCreateSSHKey struct { - Debug bool - DebugKeyPath string - keyId int } @@ -30,31 +20,12 @@ func (s *stepCreateSSHKey) Run(ctx context.Context, state multistep.StateBag) mu ui := state.Get("ui").(packersdk.Ui) c := state.Get("config").(*Config) - ui.Say("Creating temporary ssh key for droplet...") - - priv, err := rsa.GenerateKey(rand.Reader, 2014) - if err != nil { - err := fmt.Errorf("error generating RSA key: %s", err) - state.Put("error", err) - ui.Error(err.Error()) - return multistep.ActionHalt + if c.Comm.SSHPublicKey == nil { + ui.Say("No public SSH key found; skipping SSH public key import...") + return multistep.ActionContinue } - // ASN.1 DER encoded form - priv_der := x509.MarshalPKCS1PrivateKey(priv) - priv_blk := pem.Block{ - Type: "RSA PRIVATE KEY", - Headers: nil, - Bytes: priv_der, - } - - // Set the private key in the config for later - c.Comm.SSHPrivateKey = pem.EncodeToMemory(&priv_blk) - - // Marshal the public key into SSH compatible format - // TODO properly handle the public key error - pub, _ := ssh.NewPublicKey(&priv.PublicKey) - pub_sshformat := string(ssh.MarshalAuthorizedKey(pub)) + ui.Say("Importing SSH public key...") // The name of the public key on DO name := fmt.Sprintf("packer-%s", uuid.TimeOrderedUUID()) @@ -62,7 +33,7 @@ func (s *stepCreateSSHKey) Run(ctx context.Context, state multistep.StateBag) mu // Create the key! key, _, err := client.Keys.Create(context.TODO(), &godo.KeyCreateRequest{ Name: name, - PublicKey: pub_sshformat, + PublicKey: string(c.Comm.SSHPublicKey), }) if err != nil { err := fmt.Errorf("Error creating temporary SSH key: %s", err) @@ -79,31 +50,6 @@ func (s *stepCreateSSHKey) Run(ctx context.Context, state multistep.StateBag) mu // Remember some state for the future state.Put("ssh_key_id", key.ID) - // If we're in debug mode, output the private key to the working directory. - if s.Debug { - ui.Message(fmt.Sprintf("Saving key for debug purposes: %s", s.DebugKeyPath)) - f, err := os.Create(s.DebugKeyPath) - if err != nil { - state.Put("error", fmt.Errorf("Error saving debug key: %s", err)) - return multistep.ActionHalt - } - defer f.Close() - - // Write the key out - if _, err := f.Write(pem.EncodeToMemory(&priv_blk)); err != nil { - state.Put("error", fmt.Errorf("Error saving debug key: %s", err)) - return multistep.ActionHalt - } - - // Chmod it so that it is SSH ready - if runtime.GOOS != "windows" { - if err := f.Chmod(0600); err != nil { - state.Put("error", fmt.Errorf("Error setting permissions of debug key: %s", err)) - return multistep.ActionHalt - } - } - } - return multistep.ActionContinue }