2013-12-08 17:37:36 -05:00
|
|
|
package googlecompute
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
|
|
|
"fmt"
|
2016-11-02 16:24:48 -04:00
|
|
|
"io/ioutil"
|
2015-07-30 22:19:48 -04:00
|
|
|
"os"
|
|
|
|
|
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-12-08 17:37:36 -05:00
|
|
|
)
|
|
|
|
|
2013-12-12 23:09:02 -05:00
|
|
|
// StepCreateSSHKey represents a Packer build step that generates SSH key pairs.
|
2014-01-31 07:33:23 -05:00
|
|
|
type StepCreateSSHKey struct {
|
2016-11-02 16:24:48 -04:00
|
|
|
Debug bool
|
|
|
|
DebugKeyPath string
|
|
|
|
PrivateKeyFile string
|
2014-01-31 07:33:23 -05:00
|
|
|
}
|
2013-12-08 17:37:36 -05:00
|
|
|
|
|
|
|
// Run executes the Packer build step that generates SSH key pairs.
|
2016-05-24 20:13:36 -04:00
|
|
|
// The key pairs are added to the multistep state as "ssh_private_key" and
|
|
|
|
// "ssh_public_key".
|
2013-12-12 23:09:02 -05:00
|
|
|
func (s *StepCreateSSHKey) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
2016-11-02 16:24:48 -04:00
|
|
|
if s.PrivateKeyFile != "" {
|
|
|
|
ui.Say("Using existing SSH private key")
|
|
|
|
privateKeyBytes, err := ioutil.ReadFile(s.PrivateKeyFile)
|
|
|
|
if err != nil {
|
|
|
|
state.Put("error", fmt.Errorf(
|
|
|
|
"Error loading configured private key file: %s", err))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
state.Put("ssh_private_key", string(privateKeyBytes))
|
2016-11-03 12:49:09 -04:00
|
|
|
state.Put("ssh_public_key", "")
|
2016-11-02 16:24:48 -04:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-12-12 23:09:02 -05:00
|
|
|
ui.Say("Creating temporary SSH key for instance...")
|
|
|
|
priv, err := rsa.GenerateKey(rand.Reader, 2048)
|
2013-12-08 17:37:36 -05:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error creating temporary ssh key: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2013-12-12 23:09:02 -05:00
|
|
|
|
2013-12-08 17:37:36 -05:00
|
|
|
priv_blk := pem.Block{
|
|
|
|
Type: "RSA PRIVATE KEY",
|
|
|
|
Headers: nil,
|
2013-12-12 23:09:02 -05:00
|
|
|
Bytes: x509.MarshalPKCS1PrivateKey(priv),
|
2013-12-08 17:37:36 -05:00
|
|
|
}
|
2013-12-12 23:09:02 -05:00
|
|
|
|
2013-12-08 17:37:36 -05:00
|
|
|
pub, err := ssh.NewPublicKey(&priv.PublicKey)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error creating temporary ssh key: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2014-01-31 11:55:58 -05:00
|
|
|
state.Put("ssh_private_key", string(pem.EncodeToMemory(&priv_blk)))
|
|
|
|
state.Put("ssh_public_key", string(ssh.MarshalAuthorizedKey(pub)))
|
2013-12-12 23:09:02 -05:00
|
|
|
|
2014-01-31 11:55:58 -05:00
|
|
|
if s.Debug {
|
|
|
|
ui.Message(fmt.Sprintf("Saving key for debug purposes: %s", s.DebugKeyPath))
|
2014-02-21 18:10:09 -05:00
|
|
|
f, err := os.Create(s.DebugKeyPath)
|
2014-01-31 11:55:58 -05:00
|
|
|
if err != nil {
|
|
|
|
state.Put("error", fmt.Errorf("Error saving debug key: %s", err))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2014-02-21 18:10:09 -05:00
|
|
|
|
2014-01-31 11:55:58 -05:00
|
|
|
// Write out the key
|
2014-02-21 18:10:09 -05:00
|
|
|
err = pem.Encode(f, &priv_blk)
|
2014-01-31 11:55:58 -05:00
|
|
|
f.Close()
|
2014-02-21 18:10:09 -05:00
|
|
|
if err != nil {
|
|
|
|
state.Put("error", fmt.Errorf("Error saving debug key: %s", err))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2014-01-31 11:55:58 -05:00
|
|
|
}
|
2013-12-08 17:37:36 -05:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing to clean up. SSH keys are associated with a single GCE instance.
|
2013-12-12 23:09:02 -05:00
|
|
|
func (s *StepCreateSSHKey) Cleanup(state multistep.StateBag) {}
|