2017-09-12 11:30:39 -04:00
|
|
|
package oci
|
2017-02-13 05:35:14 -05:00
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2017-02-13 05:35:14 -05:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
|
|
|
"fmt"
|
2017-08-02 12:18:53 -04:00
|
|
|
"io/ioutil"
|
2017-02-13 05:35:14 -05:00
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-07 06:20:33 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2017-02-13 05:35:14 -05:00
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
)
|
|
|
|
|
2017-08-02 12:18:53 -04:00
|
|
|
type stepKeyPair struct {
|
|
|
|
Debug bool
|
|
|
|
DebugKeyPath string
|
|
|
|
PrivateKeyFile string
|
2017-02-13 05:35:14 -05:00
|
|
|
}
|
|
|
|
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s *stepKeyPair) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2017-02-13 05:35:14 -05:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
2017-08-02 12:18:53 -04:00
|
|
|
if s.PrivateKeyFile != "" {
|
|
|
|
privateKeyBytes, err := ioutil.ReadFile(s.PrivateKeyFile)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Error loading configured private key file: %s", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := ssh.ParsePrivateKey(privateKeyBytes)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Error parsing 'ssh_private_key_file': %s", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
state.Put("publicKey", string(ssh.MarshalAuthorizedKey(key.PublicKey())))
|
|
|
|
state.Put("privateKey", string(privateKeyBytes))
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2017-02-13 05:35:14 -05:00
|
|
|
ui.Say("Creating temporary ssh key for instance...")
|
|
|
|
|
2017-08-02 12:18:53 -04:00
|
|
|
priv, err := rsa.GenerateKey(rand.Reader, 2048)
|
2017-02-13 05:35:14 -05:00
|
|
|
if err != nil {
|
2017-08-02 06:53:14 -04:00
|
|
|
err = fmt.Errorf("Error creating temporary SSH key: %s", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
state.Put("error", err)
|
2017-02-13 05:35:14 -05:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// ASN.1 DER encoded form
|
|
|
|
privDer := x509.MarshalPKCS1PrivateKey(priv)
|
2017-08-02 12:18:53 -04:00
|
|
|
privBlk := pem.Block{Type: "RSA PRIVATE KEY", Headers: nil, Bytes: privDer}
|
2017-02-13 05:35:14 -05:00
|
|
|
|
|
|
|
// Set the private key in the statebag for later
|
|
|
|
state.Put("privateKey", string(pem.EncodeToMemory(&privBlk)))
|
|
|
|
|
|
|
|
// Marshal the public key into SSH compatible format
|
|
|
|
pub, err := ssh.NewPublicKey(&priv.PublicKey)
|
|
|
|
if err != nil {
|
2017-08-02 06:53:14 -04:00
|
|
|
err = fmt.Errorf("Error marshaling temporary SSH public key: %s", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
state.Put("error", err)
|
2017-02-13 05:35:14 -05:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
pubSSHFormat := string(ssh.MarshalAuthorizedKey(pub))
|
|
|
|
state.Put("publicKey", pubSSHFormat)
|
|
|
|
|
2017-08-02 12:18:53 -04:00
|
|
|
// If we're in debug mode, output the private key to the working
|
|
|
|
// directory.
|
2017-02-13 05:35:14 -05:00
|
|
|
if s.Debug {
|
|
|
|
ui.Message(fmt.Sprintf("Saving key for debug purposes: %s", s.DebugKeyPath))
|
|
|
|
f, err := os.Create(s.DebugKeyPath)
|
|
|
|
if err != nil {
|
2017-08-02 06:53:14 -04:00
|
|
|
err = fmt.Errorf("Error saving debug key: %s", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
state.Put("error", err)
|
2017-02-13 05:35:14 -05:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
// Write the key out
|
|
|
|
if _, err := f.Write(pem.EncodeToMemory(&privBlk)); err != nil {
|
2017-08-02 06:53:14 -04:00
|
|
|
err = fmt.Errorf("Error saving debug key: %s", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
state.Put("error", err)
|
2017-02-13 05:35:14 -05:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Chmod it so that it is SSH ready
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
if err := f.Chmod(0600); err != nil {
|
2017-08-02 06:53:14 -04:00
|
|
|
err = fmt.Errorf("Error setting permissions of debug key: %s", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
state.Put("error", err)
|
2017-02-13 05:35:14 -05:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2017-08-02 12:18:53 -04:00
|
|
|
func (s *stepKeyPair) Cleanup(state multistep.StateBag) {
|
2017-02-13 05:35:14 -05:00
|
|
|
// Nothing to do
|
|
|
|
}
|