Add ssh_private_key_file support to BMCS builder
This commit is contained in:
parent
e8e13dde60
commit
e8e0e8c948
|
@ -56,16 +56,19 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
|
||||||
|
|
||||||
// Build the steps
|
// Build the steps
|
||||||
steps := []multistep.Step{
|
steps := []multistep.Step{
|
||||||
&stepCreateSSHKey{
|
&stepKeyPair{
|
||||||
Debug: b.config.PackerDebug,
|
Debug: b.config.PackerDebug,
|
||||||
DebugKeyPath: fmt.Sprintf("bmc_%s.pem", b.config.PackerBuildName),
|
DebugKeyPath: fmt.Sprintf("bmcs_%s.pem", b.config.PackerBuildName),
|
||||||
|
PrivateKeyFile: b.config.Comm.SSHPrivateKey,
|
||||||
},
|
},
|
||||||
&stepCreateInstance{},
|
&stepCreateInstance{},
|
||||||
&stepInstanceInfo{},
|
&stepInstanceInfo{},
|
||||||
&communicator.StepConnect{
|
&communicator.StepConnect{
|
||||||
Config: &b.config.Comm,
|
Config: &b.config.Comm,
|
||||||
Host: commHost,
|
Host: commHost,
|
||||||
SSHConfig: sshConfig,
|
SSHConfig: SSHConfig(
|
||||||
|
b.config.Comm.SSHUsername,
|
||||||
|
b.config.Comm.SSHPassword),
|
||||||
},
|
},
|
||||||
&common.StepProvision{},
|
&common.StepProvision{},
|
||||||
&stepImage{},
|
&stepImage{},
|
||||||
|
|
|
@ -19,22 +19,33 @@ func commHost(state multistep.StateBag) (string, error) {
|
||||||
return ipAddress, nil
|
return ipAddress, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func sshConfig(state multistep.StateBag) (*ssh.ClientConfig, error) {
|
// SSHConfig returns a function that can be used for the SSH communicator
|
||||||
c := state.Get("config").(*Config)
|
// config for connecting to the instance created over SSH using the private key
|
||||||
privateKey := state.Get("privateKey").(string)
|
// or password.
|
||||||
|
func SSHConfig(username, password string) func(state multistep.StateBag) (*ssh.ClientConfig, error) {
|
||||||
|
return func(state multistep.StateBag) (*ssh.ClientConfig, error) {
|
||||||
|
privateKey, hasKey := state.GetOk("privateKey")
|
||||||
|
if hasKey {
|
||||||
|
|
||||||
signer, err := ssh.ParsePrivateKey([]byte(privateKey))
|
signer, err := ssh.ParsePrivateKey([]byte(privateKey.(string)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("Error setting up SSH config: %s", err)
|
return nil, fmt.Errorf("Error setting up SSH config: %s", err)
|
||||||
|
}
|
||||||
|
return &ssh.ClientConfig{
|
||||||
|
User: username,
|
||||||
|
Auth: []ssh.AuthMethod{ssh.PublicKeys(signer)},
|
||||||
|
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
|
||||||
|
}, nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return &ssh.ClientConfig{
|
||||||
|
User: username,
|
||||||
|
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
|
||||||
|
Auth: []ssh.AuthMethod{
|
||||||
|
ssh.Password(password),
|
||||||
|
ssh.KeyboardInteractive(packerssh.PasswordKeyboardInteractive(password)),
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return &ssh.ClientConfig{
|
|
||||||
User: c.Comm.SSHUsername,
|
|
||||||
Auth: []ssh.AuthMethod{
|
|
||||||
ssh.PublicKeys(signer),
|
|
||||||
ssh.Password(c.Comm.SSHPassword),
|
|
||||||
ssh.KeyboardInteractive(
|
|
||||||
packerssh.PasswordKeyboardInteractive(c.Comm.SSHPassword)),
|
|
||||||
},
|
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,7 @@ import (
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"encoding/pem"
|
"encoding/pem"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
|
||||||
|
@ -20,17 +21,41 @@ import (
|
||||||
"golang.org/x/crypto/ssh"
|
"golang.org/x/crypto/ssh"
|
||||||
)
|
)
|
||||||
|
|
||||||
type stepCreateSSHKey struct {
|
type stepKeyPair struct {
|
||||||
Debug bool
|
Debug bool
|
||||||
DebugKeyPath string
|
DebugKeyPath string
|
||||||
|
PrivateKeyFile string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *stepCreateSSHKey) Run(state multistep.StateBag) multistep.StepAction {
|
func (s *stepKeyPair) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
ui := state.Get("ui").(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
ui.Say("Creating temporary ssh key for instance...")
|
ui.Say("Creating temporary ssh key for instance...")
|
||||||
|
|
||||||
priv, err := rsa.GenerateKey(rand.Reader, 2014)
|
priv, err := rsa.GenerateKey(rand.Reader, 2048)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = fmt.Errorf("Error creating temporary SSH key: %s", err)
|
err = fmt.Errorf("Error creating temporary SSH key: %s", err)
|
||||||
ui.Error(err.Error())
|
ui.Error(err.Error())
|
||||||
|
@ -40,11 +65,7 @@ func (s *stepCreateSSHKey) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
|
|
||||||
// ASN.1 DER encoded form
|
// ASN.1 DER encoded form
|
||||||
privDer := x509.MarshalPKCS1PrivateKey(priv)
|
privDer := x509.MarshalPKCS1PrivateKey(priv)
|
||||||
privBlk := pem.Block{
|
privBlk := pem.Block{Type: "RSA PRIVATE KEY", Headers: nil, Bytes: privDer}
|
||||||
Type: "RSA PRIVATE KEY",
|
|
||||||
Headers: nil,
|
|
||||||
Bytes: privDer,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set the private key in the statebag for later
|
// Set the private key in the statebag for later
|
||||||
state.Put("privateKey", string(pem.EncodeToMemory(&privBlk)))
|
state.Put("privateKey", string(pem.EncodeToMemory(&privBlk)))
|
||||||
|
@ -61,7 +82,8 @@ func (s *stepCreateSSHKey) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
pubSSHFormat := string(ssh.MarshalAuthorizedKey(pub))
|
pubSSHFormat := string(ssh.MarshalAuthorizedKey(pub))
|
||||||
state.Put("publicKey", pubSSHFormat)
|
state.Put("publicKey", pubSSHFormat)
|
||||||
|
|
||||||
// If we're in debug mode, output the private key to the working directory.
|
// If we're in debug mode, output the private key to the working
|
||||||
|
// directory.
|
||||||
if s.Debug {
|
if s.Debug {
|
||||||
ui.Message(fmt.Sprintf("Saving key for debug purposes: %s", s.DebugKeyPath))
|
ui.Message(fmt.Sprintf("Saving key for debug purposes: %s", s.DebugKeyPath))
|
||||||
f, err := os.Create(s.DebugKeyPath)
|
f, err := os.Create(s.DebugKeyPath)
|
||||||
|
@ -95,6 +117,6 @@ func (s *stepCreateSSHKey) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
return multistep.ActionContinue
|
return multistep.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *stepCreateSSHKey) Cleanup(state multistep.StateBag) {
|
func (s *stepKeyPair) Cleanup(state multistep.StateBag) {
|
||||||
// Nothing to do
|
// Nothing to do
|
||||||
}
|
}
|
|
@ -67,8 +67,8 @@ builder.
|
||||||
- `compartment_ocid` (string) - The OCID of the
|
- `compartment_ocid` (string) - The OCID of the
|
||||||
[compartment](https://docs.us-phoenix-1.oraclecloud.com/Content/GSG/Tasks/choosingcompartments.htm)
|
[compartment](https://docs.us-phoenix-1.oraclecloud.com/Content/GSG/Tasks/choosingcompartments.htm)
|
||||||
|
|
||||||
- `fingerprint` (string) - Fingerprint for the key pair being used. Overrides
|
- `fingerprint` (string) - Fingerprint for the BMCS API signing key.
|
||||||
value provided by the
|
Overrides value provided by the
|
||||||
[BMCS config file](https://docs.us-phoenix-1.oraclecloud.com/Content/API/Concepts/sdkconfig.htm)
|
[BMCS config file](https://docs.us-phoenix-1.oraclecloud.com/Content/API/Concepts/sdkconfig.htm)
|
||||||
if present.
|
if present.
|
||||||
|
|
||||||
|
@ -104,13 +104,13 @@ builder.
|
||||||
|
|
||||||
- `image_name` (string) - The name to assign to the resulting custom image.
|
- `image_name` (string) - The name to assign to the resulting custom image.
|
||||||
|
|
||||||
- `key_file` (string) - Full path and filename of the private key. Overrides
|
- `key_file` (string) - Full path and filename of the BMCS API signing key.
|
||||||
value provided by the
|
Overrides value provided by the
|
||||||
[BMCS config file](https://docs.us-phoenix-1.oraclecloud.com/Content/API/Concepts/sdkconfig.htm)
|
[BMCS config file](https://docs.us-phoenix-1.oraclecloud.com/Content/API/Concepts/sdkconfig.htm)
|
||||||
if present.
|
if present.
|
||||||
|
|
||||||
- `pass_phrase` (string) - Pass phrase used to decrypt the private key used
|
- `pass_phrase` (string) - Pass phrase used to decrypt the BMCS API signing
|
||||||
to sign requests to the BMCS API. Overrides value provided by the
|
key. Overrides value provided by the
|
||||||
[BMCS config file](https://docs.us-phoenix-1.oraclecloud.com/Content/API/Concepts/sdkconfig.htm)
|
[BMCS config file](https://docs.us-phoenix-1.oraclecloud.com/Content/API/Concepts/sdkconfig.htm)
|
||||||
if present.
|
if present.
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue