github.com/amanya/packer@v0.12.1-0.20161117214323-902ac5ab2eb6/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 "io/ioutil" 10 "os" 11 12 "github.com/mitchellh/multistep" 13 "github.com/mitchellh/packer/packer" 14 "golang.org/x/crypto/ssh" 15 ) 16 17 // StepCreateSSHKey represents a Packer build step that generates SSH key pairs. 18 type StepCreateSSHKey struct { 19 Debug bool 20 DebugKeyPath string 21 PrivateKeyFile string 22 } 23 24 // Run executes the Packer build step that generates SSH key pairs. 25 // The key pairs are added to the multistep state as "ssh_private_key" and 26 // "ssh_public_key". 27 func (s *StepCreateSSHKey) Run(state multistep.StateBag) multistep.StepAction { 28 ui := state.Get("ui").(packer.Ui) 29 30 if s.PrivateKeyFile != "" { 31 ui.Say("Using existing SSH private key") 32 privateKeyBytes, err := ioutil.ReadFile(s.PrivateKeyFile) 33 if err != nil { 34 state.Put("error", fmt.Errorf( 35 "Error loading configured private key file: %s", err)) 36 return multistep.ActionHalt 37 } 38 39 state.Put("ssh_private_key", string(privateKeyBytes)) 40 state.Put("ssh_public_key", "") 41 42 return multistep.ActionContinue 43 } 44 45 ui.Say("Creating temporary SSH key for instance...") 46 priv, err := rsa.GenerateKey(rand.Reader, 2048) 47 if err != nil { 48 err := fmt.Errorf("Error creating temporary ssh key: %s", err) 49 state.Put("error", err) 50 ui.Error(err.Error()) 51 return multistep.ActionHalt 52 } 53 54 priv_blk := pem.Block{ 55 Type: "RSA PRIVATE KEY", 56 Headers: nil, 57 Bytes: x509.MarshalPKCS1PrivateKey(priv), 58 } 59 60 pub, err := ssh.NewPublicKey(&priv.PublicKey) 61 if err != nil { 62 err := fmt.Errorf("Error creating temporary ssh key: %s", err) 63 state.Put("error", err) 64 ui.Error(err.Error()) 65 return multistep.ActionHalt 66 } 67 state.Put("ssh_private_key", string(pem.EncodeToMemory(&priv_blk))) 68 state.Put("ssh_public_key", string(ssh.MarshalAuthorizedKey(pub))) 69 70 if s.Debug { 71 ui.Message(fmt.Sprintf("Saving key for debug purposes: %s", s.DebugKeyPath)) 72 f, err := os.Create(s.DebugKeyPath) 73 if err != nil { 74 state.Put("error", fmt.Errorf("Error saving debug key: %s", err)) 75 return multistep.ActionHalt 76 } 77 78 // Write out the key 79 err = pem.Encode(f, &priv_blk) 80 f.Close() 81 if err != nil { 82 state.Put("error", fmt.Errorf("Error saving debug key: %s", err)) 83 return multistep.ActionHalt 84 } 85 } 86 return multistep.ActionContinue 87 } 88 89 // Nothing to clean up. SSH keys are associated with a single GCE instance. 90 func (s *StepCreateSSHKey) Cleanup(state multistep.StateBag) {}