2019-03-26 08:29:15 -04:00
|
|
|
package yandex
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2019-03-26 08:29:15 -04:00
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
)
|
|
|
|
|
2020-04-26 19:19:08 -04:00
|
|
|
type StepCreateSSHKey struct {
|
2019-03-26 08:29:15 -04:00
|
|
|
Debug bool
|
|
|
|
DebugKeyPath string
|
|
|
|
}
|
|
|
|
|
2020-04-26 19:19:08 -04:00
|
|
|
func (s *StepCreateSSHKey) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2019-04-09 10:46:41 -04:00
|
|
|
config := state.Get("config").(*Config)
|
2019-03-26 08:29:15 -04:00
|
|
|
|
2019-04-09 10:46:41 -04:00
|
|
|
if config.Communicator.SSHPrivateKeyFile != "" {
|
2019-03-26 08:29:15 -04:00
|
|
|
ui.Say("Using existing SSH private key")
|
2019-04-09 10:46:41 -04:00
|
|
|
privateKeyBytes, err := config.Communicator.ReadSSHPrivateKeyFile()
|
2019-03-26 08:29:15 -04:00
|
|
|
if err != nil {
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := ssh.ParsePrivateKey(privateKeyBytes)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Error parsing 'ssh_private_key_file': %s", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2019-04-09 10:46:41 -04:00
|
|
|
config.Communicator.SSHPublicKey = ssh.MarshalAuthorizedKey(key.PublicKey())
|
|
|
|
config.Communicator.SSHPrivateKey = privateKeyBytes
|
2019-03-26 08:29:15 -04:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Creating temporary ssh key for instance...")
|
|
|
|
|
|
|
|
priv, err := rsa.GenerateKey(rand.Reader, 2048)
|
|
|
|
if err != nil {
|
|
|
|
return stepHaltWithError(state, fmt.Errorf("Error generating temporary SSH key: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
// ASN.1 DER encoded form
|
|
|
|
privDer := x509.MarshalPKCS1PrivateKey(priv)
|
|
|
|
privBlk := pem.Block{
|
|
|
|
Type: "RSA PRIVATE KEY",
|
|
|
|
Headers: nil,
|
|
|
|
Bytes: privDer,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Marshal the public key into SSH compatible format
|
2019-04-09 10:46:41 -04:00
|
|
|
pub, err := ssh.NewPublicKey(&priv.PublicKey)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Error creating public ssh key: %s", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2019-03-26 08:29:15 -04:00
|
|
|
pubSSHFormat := string(ssh.MarshalAuthorizedKey(pub))
|
|
|
|
|
2019-04-09 10:46:41 -04:00
|
|
|
hashMD5 := ssh.FingerprintLegacyMD5(pub)
|
|
|
|
hashSHA256 := ssh.FingerprintSHA256(pub)
|
2019-03-26 08:29:15 -04:00
|
|
|
|
2019-04-09 10:46:41 -04:00
|
|
|
log.Printf("[INFO] md5 hash of ssh pub key: %s", hashMD5)
|
|
|
|
log.Printf("[INFO] sha256 hash of ssh pub key: %s", hashSHA256)
|
2019-03-26 08:29:15 -04:00
|
|
|
|
|
|
|
// Remember some state for the future
|
|
|
|
state.Put("ssh_key_public", pubSSHFormat)
|
|
|
|
|
|
|
|
// Set the private key in the config for later
|
2019-04-09 10:46:41 -04:00
|
|
|
config.Communicator.SSHPrivateKey = pem.EncodeToMemory(&privBlk)
|
|
|
|
config.Communicator.SSHPublicKey = ssh.MarshalAuthorizedKey(pub)
|
2019-03-26 08:29:15 -04:00
|
|
|
|
|
|
|
// If we're in debug mode, output the private key to the working directory.
|
|
|
|
if s.Debug {
|
|
|
|
ui.Message(fmt.Sprintf("Saving key for debug purposes: %s", s.DebugKeyPath))
|
2019-04-09 10:46:41 -04:00
|
|
|
err := ioutil.WriteFile(s.DebugKeyPath, config.Communicator.SSHPrivateKey, 0600)
|
2019-03-26 08:29:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return stepHaltWithError(state, fmt.Errorf("Error saving debug key: %s", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2020-04-26 19:19:08 -04:00
|
|
|
func (s *StepCreateSSHKey) Cleanup(state multistep.StateBag) {
|
2019-03-26 08:29:15 -04:00
|
|
|
}
|