github.com/StackPointCloud/packer@v0.10.2-0.20180716202532-b28098e0f79b/builder/scaleway/step_create_ssh_key.go (about)

     1  package scaleway
     2  
     3  import (
     4  	"context"
     5  	"crypto/rand"
     6  	"crypto/rsa"
     7  	"crypto/x509"
     8  	"encoding/pem"
     9  	"fmt"
    10  	"io/ioutil"
    11  	"log"
    12  	"os"
    13  	"runtime"
    14  	"strings"
    15  
    16  	"github.com/hashicorp/packer/helper/multistep"
    17  	"github.com/hashicorp/packer/packer"
    18  	"golang.org/x/crypto/ssh"
    19  )
    20  
    21  type stepCreateSSHKey struct {
    22  	Debug          bool
    23  	DebugKeyPath   string
    24  	PrivateKeyFile string
    25  }
    26  
    27  func (s *stepCreateSSHKey) Run(_ context.Context, 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("private_key", string(privateKeyBytes))
    40  		state.Put("ssh_pubkey", "")
    41  
    42  		return multistep.ActionContinue
    43  	}
    44  
    45  	ui.Say("Creating temporary ssh key for server...")
    46  
    47  	priv, err := rsa.GenerateKey(rand.Reader, 2014)
    48  	if err != nil {
    49  		err := fmt.Errorf("Error creating temporary SSH key: %s", err)
    50  		state.Put("error", err)
    51  		ui.Error(err.Error())
    52  		return multistep.ActionHalt
    53  	}
    54  
    55  	// ASN.1 DER encoded form
    56  	priv_der := x509.MarshalPKCS1PrivateKey(priv)
    57  	priv_blk := pem.Block{
    58  		Type:    "RSA PRIVATE KEY",
    59  		Headers: nil,
    60  		Bytes:   priv_der,
    61  	}
    62  
    63  	// Set the private key in the statebag for later
    64  	state.Put("private_key", string(pem.EncodeToMemory(&priv_blk)))
    65  
    66  	pub, _ := ssh.NewPublicKey(&priv.PublicKey)
    67  	pub_sshformat := string(ssh.MarshalAuthorizedKey(pub))
    68  	pub_sshformat = strings.Replace(pub_sshformat, " ", "_", -1)
    69  
    70  	log.Printf("temporary ssh key created")
    71  
    72  	// Remember some state for the future
    73  	state.Put("ssh_pubkey", string(pub_sshformat))
    74  
    75  	// If we're in debug mode, output the private key to the working directory.
    76  	if s.Debug {
    77  		ui.Message(fmt.Sprintf("Saving key for debug purposes: %s", s.DebugKeyPath))
    78  		f, err := os.Create(s.DebugKeyPath)
    79  		if err != nil {
    80  			state.Put("error", fmt.Errorf("Error saving debug key: %s", err))
    81  			return multistep.ActionHalt
    82  		}
    83  		defer f.Close()
    84  
    85  		// Write the key out
    86  		if _, err := f.Write(pem.EncodeToMemory(&priv_blk)); err != nil {
    87  			state.Put("error", fmt.Errorf("Error saving debug key: %s", err))
    88  			return multistep.ActionHalt
    89  		}
    90  
    91  		// Chmod it so that it is SSH ready
    92  		if runtime.GOOS != "windows" {
    93  			if err := f.Chmod(0600); err != nil {
    94  				state.Put("error", fmt.Errorf("Error setting permissions of debug key: %s", err))
    95  				return multistep.ActionHalt
    96  			}
    97  		}
    98  	}
    99  
   100  	return multistep.ActionContinue
   101  }
   102  
   103  func (s *stepCreateSSHKey) Cleanup(state multistep.StateBag) {
   104  	// SSH key is passed via tag. Nothing to do here.
   105  	return
   106  }