github.com/StackPointCloud/packer@v0.10.2-0.20180716202532-b28098e0f79b/builder/oracle/common/step_ssh_key_pair.go (about)

     1  package common
     2  
     3  import (
     4  	"context"
     5  	"crypto/rand"
     6  	"crypto/rsa"
     7  	"crypto/x509"
     8  	"encoding/pem"
     9  	"fmt"
    10  	"io/ioutil"
    11  	"os"
    12  	"runtime"
    13  
    14  	"github.com/hashicorp/packer/helper/multistep"
    15  	"github.com/hashicorp/packer/packer"
    16  	"golang.org/x/crypto/ssh"
    17  )
    18  
    19  type StepKeyPair struct {
    20  	Debug          bool
    21  	DebugKeyPath   string
    22  	PrivateKeyFile string
    23  }
    24  
    25  func (s *StepKeyPair) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
    26  	ui := state.Get("ui").(packer.Ui)
    27  
    28  	if s.PrivateKeyFile != "" {
    29  		privateKeyBytes, err := ioutil.ReadFile(s.PrivateKeyFile)
    30  		if err != nil {
    31  			err = fmt.Errorf("Error loading configured private key file: %s", err)
    32  			ui.Error(err.Error())
    33  			state.Put("error", err)
    34  			return multistep.ActionHalt
    35  		}
    36  
    37  		key, err := ssh.ParsePrivateKey(privateKeyBytes)
    38  		if err != nil {
    39  			err = fmt.Errorf("Error parsing 'ssh_private_key_file': %s", err)
    40  			ui.Error(err.Error())
    41  			state.Put("error", err)
    42  			return multistep.ActionHalt
    43  		}
    44  
    45  		state.Put("publicKey", string(ssh.MarshalAuthorizedKey(key.PublicKey())))
    46  		state.Put("privateKey", string(privateKeyBytes))
    47  
    48  		return multistep.ActionContinue
    49  	}
    50  
    51  	ui.Say("Creating temporary ssh key for instance...")
    52  
    53  	priv, err := rsa.GenerateKey(rand.Reader, 2048)
    54  	if err != nil {
    55  		err = fmt.Errorf("Error creating temporary SSH key: %s", err)
    56  		ui.Error(err.Error())
    57  		state.Put("error", err)
    58  		return multistep.ActionHalt
    59  	}
    60  
    61  	// ASN.1 DER encoded form
    62  	privDer := x509.MarshalPKCS1PrivateKey(priv)
    63  	privBlk := pem.Block{Type: "RSA PRIVATE KEY", Headers: nil, Bytes: privDer}
    64  
    65  	// Set the private key in the statebag for later
    66  	state.Put("privateKey", string(pem.EncodeToMemory(&privBlk)))
    67  
    68  	// Marshal the public key into SSH compatible format
    69  	pub, err := ssh.NewPublicKey(&priv.PublicKey)
    70  	if err != nil {
    71  		err = fmt.Errorf("Error marshaling temporary SSH public key: %s", err)
    72  		ui.Error(err.Error())
    73  		state.Put("error", err)
    74  		return multistep.ActionHalt
    75  	}
    76  
    77  	pubSSHFormat := string(ssh.MarshalAuthorizedKey(pub))
    78  	state.Put("publicKey", pubSSHFormat)
    79  
    80  	// If we're in debug mode, output the private key to the working
    81  	// directory.
    82  	if s.Debug {
    83  		ui.Message(fmt.Sprintf("Saving key for debug purposes: %s", s.DebugKeyPath))
    84  		f, err := os.Create(s.DebugKeyPath)
    85  		if err != nil {
    86  			err = fmt.Errorf("Error saving debug key: %s", err)
    87  			ui.Error(err.Error())
    88  			state.Put("error", err)
    89  			return multistep.ActionHalt
    90  		}
    91  		defer f.Close()
    92  
    93  		// Write the key out
    94  		if _, err := f.Write(pem.EncodeToMemory(&privBlk)); err != nil {
    95  			err = fmt.Errorf("Error saving debug key: %s", err)
    96  			ui.Error(err.Error())
    97  			state.Put("error", err)
    98  			return multistep.ActionHalt
    99  		}
   100  
   101  		// Chmod it so that it is SSH ready
   102  		if runtime.GOOS != "windows" {
   103  			if err := f.Chmod(0600); err != nil {
   104  				err = fmt.Errorf("Error setting permissions of debug key: %s", err)
   105  				ui.Error(err.Error())
   106  				state.Put("error", err)
   107  				return multistep.ActionHalt
   108  			}
   109  		}
   110  	}
   111  
   112  	return multistep.ActionContinue
   113  }
   114  
   115  func (s *StepKeyPair) Cleanup(state multistep.StateBag) {
   116  	// Nothing to do
   117  }