github.phpd.cn/hashicorp/packer@v1.3.2/builder/profitbricks/step_create_ssh_key.go (about) 1 package profitbricks 2 3 import ( 4 "context" 5 "crypto/x509" 6 "encoding/pem" 7 "fmt" 8 "io/ioutil" 9 10 "github.com/hashicorp/packer/helper/multistep" 11 "github.com/hashicorp/packer/packer" 12 "golang.org/x/crypto/ssh" 13 ) 14 15 type StepCreateSSHKey struct { 16 Debug bool 17 DebugKeyPath string 18 } 19 20 func (s *StepCreateSSHKey) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { 21 ui := state.Get("ui").(packer.Ui) 22 c := state.Get("config").(*Config) 23 24 if c.Comm.SSHPrivateKeyFile != "" { 25 pemBytes, err := ioutil.ReadFile(c.Comm.SSHPrivateKeyFile) 26 27 if err != nil { 28 ui.Error(err.Error()) 29 return multistep.ActionHalt 30 } 31 32 block, _ := pem.Decode(pemBytes) 33 34 priv, err := x509.ParsePKCS1PrivateKey(block.Bytes) 35 36 if err != nil { 37 38 state.Put("error", err.Error()) 39 ui.Error(err.Error()) 40 return multistep.ActionHalt 41 } 42 43 priv_blk := pem.Block{ 44 Type: "RSA PRIVATE KEY", 45 Headers: nil, 46 Bytes: x509.MarshalPKCS1PrivateKey(priv), 47 } 48 49 pub, err := ssh.NewPublicKey(&priv.PublicKey) 50 if err != nil { 51 err := fmt.Errorf("Error creating temporary ssh key: %s", err) 52 state.Put("error", err) 53 ui.Error(err.Error()) 54 return multistep.ActionHalt 55 } 56 c.Comm.SSHPrivateKey = pem.EncodeToMemory(&priv_blk) 57 c.Comm.SSHPublicKey = ssh.MarshalAuthorizedKey(pub) 58 } 59 return multistep.ActionContinue 60 } 61 62 func (s *StepCreateSSHKey) Cleanup(state multistep.StateBag) {}