2017-04-06 05:19:17 -04:00
|
|
|
package scaleway
|
|
|
|
|
|
|
|
import (
|
2018-02-05 19:50:32 -05:00
|
|
|
"context"
|
2017-04-06 05:19:17 -04:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
|
|
|
|
"github.com/hashicorp/packer/packer"
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2017-04-06 05:19:17 -04:00
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stepCreateSSHKey struct {
|
2018-08-28 11:48:37 -04:00
|
|
|
Debug bool
|
|
|
|
DebugKeyPath string
|
2017-04-06 05:19:17 -04:00
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepCreateSSHKey) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2017-04-06 05:19:17 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2018-09-27 06:04:04 -04:00
|
|
|
config := state.Get("config").(*Config)
|
2017-04-06 05:19:17 -04:00
|
|
|
|
2018-09-27 06:04:04 -04:00
|
|
|
if config.Comm.SSHPrivateKeyFile != "" {
|
2017-04-19 05:10:52 -04:00
|
|
|
ui.Say("Using existing SSH private key")
|
2018-11-06 14:03:33 -05:00
|
|
|
privateKeyBytes, err := config.Comm.ReadSSHPrivateKeyFile()
|
2017-04-19 05:10:52 -04:00
|
|
|
if err != nil {
|
2018-11-06 14:03:33 -05:00
|
|
|
state.Put("error", err)
|
2017-04-19 05:10:52 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2018-09-27 06:04:04 -04:00
|
|
|
config.Comm.SSHPrivateKey = privateKeyBytes
|
|
|
|
config.Comm.SSHPublicKey = nil
|
2017-04-19 05:10:52 -04:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2017-04-06 05:19:17 -04:00
|
|
|
ui.Say("Creating temporary ssh key for server...")
|
|
|
|
|
2018-09-27 06:04:04 -04:00
|
|
|
priv, err := rsa.GenerateKey(rand.Reader, 4096)
|
2017-04-06 05:19:17 -04:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error creating temporary SSH key: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// ASN.1 DER encoded form
|
|
|
|
priv_der := x509.MarshalPKCS1PrivateKey(priv)
|
|
|
|
priv_blk := pem.Block{
|
|
|
|
Type: "RSA PRIVATE KEY",
|
|
|
|
Headers: nil,
|
|
|
|
Bytes: priv_der,
|
|
|
|
}
|
|
|
|
|
2018-09-27 06:04:04 -04:00
|
|
|
pub, err := ssh.NewPublicKey(&priv.PublicKey)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error creating temporary SSH key: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2017-04-06 05:19:17 -04:00
|
|
|
|
|
|
|
log.Printf("temporary ssh key created")
|
|
|
|
|
|
|
|
// Remember some state for the future
|
2018-09-27 06:04:04 -04:00
|
|
|
config.Comm.SSHPrivateKey = pem.EncodeToMemory(&priv_blk)
|
|
|
|
config.Comm.SSHPublicKey = ssh.MarshalAuthorizedKey(pub)
|
2017-04-06 05:19:17 -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))
|
|
|
|
f, err := os.Create(s.DebugKeyPath)
|
|
|
|
if err != nil {
|
|
|
|
state.Put("error", fmt.Errorf("Error saving debug key: %s", err))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
// Write the key out
|
|
|
|
if _, err := f.Write(pem.EncodeToMemory(&priv_blk)); err != nil {
|
|
|
|
state.Put("error", fmt.Errorf("Error saving debug key: %s", err))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Chmod it so that it is SSH ready
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
if err := f.Chmod(0600); err != nil {
|
|
|
|
state.Put("error", fmt.Errorf("Error setting permissions of debug key: %s", err))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepCreateSSHKey) Cleanup(state multistep.StateBag) {
|
|
|
|
// SSH key is passed via tag. Nothing to do here.
|
|
|
|
return
|
|
|
|
}
|