github.com/rothwerx/packer@v0.9.0/builder/googlecompute/step_create_ssh_key.go (about)

     1  package googlecompute
     2  
     3  import (
     4  	"crypto/rand"
     5  	"crypto/rsa"
     6  	"crypto/x509"
     7  	"encoding/pem"
     8  	"fmt"
     9  	"os"
    10  
    11  	"github.com/mitchellh/multistep"
    12  	"github.com/mitchellh/packer/packer"
    13  	"golang.org/x/crypto/ssh"
    14  )
    15  
    16  // StepCreateSSHKey represents a Packer build step that generates SSH key pairs.
    17  type StepCreateSSHKey struct {
    18  	Debug        bool
    19  	DebugKeyPath string
    20  }
    21  
    22  // Run executes the Packer build step that generates SSH key pairs.
    23  func (s *StepCreateSSHKey) Run(state multistep.StateBag) multistep.StepAction {
    24  	ui := state.Get("ui").(packer.Ui)
    25  
    26  	ui.Say("Creating temporary SSH key for instance...")
    27  	priv, err := rsa.GenerateKey(rand.Reader, 2048)
    28  	if err != nil {
    29  		err := fmt.Errorf("Error creating temporary ssh key: %s", err)
    30  		state.Put("error", err)
    31  		ui.Error(err.Error())
    32  		return multistep.ActionHalt
    33  	}
    34  
    35  	priv_blk := pem.Block{
    36  		Type:    "RSA PRIVATE KEY",
    37  		Headers: nil,
    38  		Bytes:   x509.MarshalPKCS1PrivateKey(priv),
    39  	}
    40  
    41  	pub, err := ssh.NewPublicKey(&priv.PublicKey)
    42  	if err != nil {
    43  		err := fmt.Errorf("Error creating temporary ssh key: %s", err)
    44  		state.Put("error", err)
    45  		ui.Error(err.Error())
    46  		return multistep.ActionHalt
    47  	}
    48  	state.Put("ssh_private_key", string(pem.EncodeToMemory(&priv_blk)))
    49  	state.Put("ssh_public_key", string(ssh.MarshalAuthorizedKey(pub)))
    50  
    51  	if s.Debug {
    52  		ui.Message(fmt.Sprintf("Saving key for debug purposes: %s", s.DebugKeyPath))
    53  		f, err := os.Create(s.DebugKeyPath)
    54  		if err != nil {
    55  			state.Put("error", fmt.Errorf("Error saving debug key: %s", err))
    56  			return multistep.ActionHalt
    57  		}
    58  
    59  		// Write out the key
    60  		err = pem.Encode(f, &priv_blk)
    61  		f.Close()
    62  		if err != nil {
    63  			state.Put("error", fmt.Errorf("Error saving debug key: %s", err))
    64  			return multistep.ActionHalt
    65  		}
    66  	}
    67  	return multistep.ActionContinue
    68  }
    69  
    70  // Nothing to clean up. SSH keys are associated with a single GCE instance.
    71  func (s *StepCreateSSHKey) Cleanup(state multistep.StateBag) {}