2013-06-13 11:58:06 -04:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
2017-04-08 15:52:57 -04:00
|
|
|
"context"
|
2013-06-15 16:42:40 -04:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
2013-06-13 11:58:06 -04:00
|
|
|
"fmt"
|
2014-08-28 14:24:31 -04:00
|
|
|
"log"
|
2015-01-10 07:52:45 -05:00
|
|
|
"os"
|
2015-06-10 21:57:56 -04:00
|
|
|
"runtime"
|
2014-08-28 14:24:31 -04:00
|
|
|
|
2015-06-10 17:02:06 -04:00
|
|
|
"github.com/digitalocean/godo"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/common/uuid"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2015-07-30 22:19:48 -04:00
|
|
|
"golang.org/x/crypto/ssh"
|
2013-06-13 11:58:06 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type stepCreateSSHKey struct {
|
2015-01-10 07:52:45 -05:00
|
|
|
Debug bool
|
|
|
|
DebugKeyPath string
|
2015-06-10 21:57:56 -04:00
|
|
|
|
2015-06-10 17:02:06 -04:00
|
|
|
keyId int
|
2013-06-13 11:58:06 -04:00
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepCreateSSHKey) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2015-06-10 17:02:06 -04:00
|
|
|
client := state.Get("client").(*godo.Client)
|
2013-08-31 15:25:08 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2018-08-27 09:58:00 -04:00
|
|
|
c := state.Get("config").(*Config)
|
2013-06-13 11:58:06 -04:00
|
|
|
|
|
|
|
ui.Say("Creating temporary ssh key for droplet...")
|
2013-06-15 16:42:40 -04:00
|
|
|
|
|
|
|
priv, err := rsa.GenerateKey(rand.Reader, 2014)
|
2019-11-08 13:35:48 -05:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("error generating RSA key: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2013-06-15 16:42:40 -04:00
|
|
|
|
|
|
|
// ASN.1 DER encoded form
|
|
|
|
priv_der := x509.MarshalPKCS1PrivateKey(priv)
|
|
|
|
priv_blk := pem.Block{
|
|
|
|
Type: "RSA PRIVATE KEY",
|
|
|
|
Headers: nil,
|
|
|
|
Bytes: priv_der,
|
|
|
|
}
|
|
|
|
|
2018-08-27 09:58:00 -04:00
|
|
|
// Set the private key in the config for later
|
|
|
|
c.Comm.SSHPrivateKey = pem.EncodeToMemory(&priv_blk)
|
2013-06-15 16:42:40 -04:00
|
|
|
|
|
|
|
// Marshal the public key into SSH compatible format
|
2013-09-20 00:30:22 -04:00
|
|
|
// TODO properly handle the public key error
|
|
|
|
pub, _ := ssh.NewPublicKey(&priv.PublicKey)
|
2013-09-15 15:21:21 -04:00
|
|
|
pub_sshformat := string(ssh.MarshalAuthorizedKey(pub))
|
2013-06-14 09:26:03 -04:00
|
|
|
|
|
|
|
// The name of the public key on DO
|
2013-10-16 22:21:14 -04:00
|
|
|
name := fmt.Sprintf("packer-%s", uuid.TimeOrderedUUID())
|
2013-06-14 09:26:03 -04:00
|
|
|
|
|
|
|
// Create the key!
|
2017-04-09 14:33:05 -04:00
|
|
|
key, _, err := client.Keys.Create(context.TODO(), &godo.KeyCreateRequest{
|
2015-06-10 17:02:06 -04:00
|
|
|
Name: name,
|
|
|
|
PublicKey: pub_sshformat,
|
|
|
|
})
|
2013-06-13 11:58:06 -04:00
|
|
|
if err != nil {
|
2013-06-20 00:00:51 -04:00
|
|
|
err := fmt.Errorf("Error creating temporary SSH key: %s", err)
|
2013-08-31 15:25:08 -04:00
|
|
|
state.Put("error", err)
|
2013-06-13 11:58:06 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// We use this to check cleanup
|
2015-06-10 17:02:06 -04:00
|
|
|
s.keyId = key.ID
|
2013-06-13 11:58:06 -04:00
|
|
|
|
|
|
|
log.Printf("temporary ssh key name: %s", name)
|
|
|
|
|
|
|
|
// Remember some state for the future
|
2015-06-10 17:02:06 -04:00
|
|
|
state.Put("ssh_key_id", key.ID)
|
2013-06-13 11:58:06 -04:00
|
|
|
|
2015-01-10 07:52:45 -05:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-13 11:58:06 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:25:08 -04:00
|
|
|
func (s *stepCreateSSHKey) Cleanup(state multistep.StateBag) {
|
2013-06-13 11:58:06 -04:00
|
|
|
// If no key name is set, then we never created it, so just return
|
|
|
|
if s.keyId == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-06-10 17:02:06 -04:00
|
|
|
client := state.Get("client").(*godo.Client)
|
2013-08-31 15:25:08 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-06-13 11:58:06 -04:00
|
|
|
|
|
|
|
ui.Say("Deleting temporary ssh key...")
|
2017-04-09 14:33:05 -04:00
|
|
|
_, err := client.Keys.DeleteByID(context.TODO(), s.keyId)
|
2013-06-13 11:58:06 -04:00
|
|
|
if err != nil {
|
2015-06-10 17:02:06 -04:00
|
|
|
log.Printf("Error cleaning up ssh key: %s", err)
|
2013-06-13 11:58:06 -04:00
|
|
|
ui.Error(fmt.Sprintf(
|
2015-06-10 17:02:06 -04:00
|
|
|
"Error cleaning up ssh key. Please delete the key manually: %s", err))
|
2013-06-13 11:58:06 -04:00
|
|
|
}
|
|
|
|
}
|