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