github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/oracle/oci/step_ssh_key_pair.go (about)

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