github.com/angdraug/packer@v1.3.2/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/communicator"
    15  	"github.com/hashicorp/packer/helper/multistep"
    16  	"github.com/hashicorp/packer/packer"
    17  	"golang.org/x/crypto/ssh"
    18  )
    19  
    20  type StepKeyPair struct {
    21  	Debug        bool
    22  	Comm         *communicator.Config
    23  	DebugKeyPath string
    24  }
    25  
    26  func (s *StepKeyPair) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
    27  	ui := state.Get("ui").(packer.Ui)
    28  
    29  	if s.Comm.SSHPrivateKeyFile != "" {
    30  		privateKeyBytes, err := ioutil.ReadFile(s.Comm.SSHPrivateKeyFile)
    31  		if err != nil {
    32  			err = fmt.Errorf("Error loading configured private key file: %s", err)
    33  			ui.Error(err.Error())
    34  			state.Put("error", err)
    35  			return multistep.ActionHalt
    36  		}
    37  
    38  		key, err := ssh.ParsePrivateKey(privateKeyBytes)
    39  		if err != nil {
    40  			err = fmt.Errorf("Error parsing 'ssh_private_key_file': %s", err)
    41  			ui.Error(err.Error())
    42  			state.Put("error", err)
    43  			return multistep.ActionHalt
    44  		}
    45  
    46  		s.Comm.SSHPublicKey = ssh.MarshalAuthorizedKey(key.PublicKey())
    47  		s.Comm.SSHPrivateKey = privateKeyBytes
    48  
    49  		return multistep.ActionContinue
    50  	}
    51  
    52  	ui.Say("Creating temporary ssh key for instance...")
    53  
    54  	priv, err := rsa.GenerateKey(rand.Reader, 2048)
    55  	if err != nil {
    56  		err = fmt.Errorf("Error creating temporary SSH key: %s", err)
    57  		ui.Error(err.Error())
    58  		state.Put("error", err)
    59  		return multistep.ActionHalt
    60  	}
    61  
    62  	// ASN.1 DER encoded form
    63  	privDer := x509.MarshalPKCS1PrivateKey(priv)
    64  	privBlk := pem.Block{Type: "RSA PRIVATE KEY", Headers: nil, Bytes: privDer}
    65  
    66  	// Set the private key in the statebag for later
    67  	state.Put("privateKey", string(pem.EncodeToMemory(&privBlk)))
    68  
    69  	// Marshal the public key into SSH compatible format
    70  	pub, err := ssh.NewPublicKey(&priv.PublicKey)
    71  	if err != nil {
    72  		err = fmt.Errorf("Error marshaling temporary SSH public key: %s", err)
    73  		ui.Error(err.Error())
    74  		state.Put("error", err)
    75  		return multistep.ActionHalt
    76  	}
    77  
    78  	s.Comm.SSHPublicKey = ssh.MarshalAuthorizedKey(pub)
    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  }