108 lines
2.7 KiB
Go
108 lines
2.7 KiB
Go
package scaleway
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"crypto/rand"
|
|
"crypto/rsa"
|
|
"crypto/x509"
|
|
"encoding/pem"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"log"
|
|
"os"
|
|
"runtime"
|
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
"github.com/hashicorp/packer/packer"
|
|
"golang.org/x/crypto/ssh"
|
|
)
|
|
|
|
type stepCreateSSHKey struct {
|
|
Debug bool
|
|
Comm *communicator.Config
|
|
DebugKeyPath string
|
|
}
|
|
|
|
func (s *stepCreateSSHKey) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
if s.Comm.SSHPrivateKeyFile != "" {
|
|
ui.Say("Using existing SSH private key")
|
|
privateKeyBytes, err := ioutil.ReadFile(s.Comm.SSHPrivateKeyFile)
|
|
if err != nil {
|
|
state.Put("error", fmt.Errorf(
|
|
"Error loading configured private key file: %s", err))
|
|
return multistep.ActionHalt
|
|
}
|
|
|
|
s.Comm.SSHPrivateKey = privateKeyBytes
|
|
s.Comm.SSHPublicKey = nil
|
|
|
|
return multistep.ActionContinue
|
|
}
|
|
|
|
ui.Say("Creating temporary ssh key for server...")
|
|
|
|
priv, err := rsa.GenerateKey(rand.Reader, 2014)
|
|
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,
|
|
}
|
|
|
|
// Set the private key in the config for later
|
|
s.Comm.SSHPrivateKey = pem.EncodeToMemory(&priv_blk)
|
|
|
|
pub, _ := ssh.NewPublicKey(&priv.PublicKey)
|
|
pub_sshformat := ssh.MarshalAuthorizedKey(pub)
|
|
pub_sshformat = bytes.Replace(pub_sshformat, []byte(" "), []byte("_"), -1)
|
|
|
|
log.Printf("temporary ssh key created")
|
|
|
|
// Remember some state for the future
|
|
s.Comm.SSHPublicKey = pub_sshformat
|
|
|
|
// 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
|
|
}
|