refactor all copy pasted sshConfig with into communicator.Config.SSHConfigFunc

* still need to append the auth methods into an array for gracefullness
This commit is contained in:
Adrien Delorme 2018-08-22 17:02:23 +02:00
parent a90fccf329
commit 41f6e0334d
44 changed files with 36 additions and 783 deletions

View File

@ -156,10 +156,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
Host: SSHHost( Host: SSHHost(
client, client,
b.config.SSHPrivateIp), b.config.SSHPrivateIp),
SSHConfig: SSHConfig( SSHConfig: b.config.RunConfig.Comm.SSHConfigFunc(),
b.config.RunConfig.Comm.SSHAgentAuth,
b.config.RunConfig.Comm.SSHUsername,
b.config.RunConfig.Comm.SSHPassword),
}, },
&common.StepProvision{}, &common.StepProvision{},
&stepStopAlicloudInstance{ &stepStopAlicloudInstance{

View File

@ -1,15 +1,9 @@
package ecs package ecs
import ( import (
"fmt"
"net"
"os"
"time" "time"
packerssh "github.com/hashicorp/packer/communicator/ssh"
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
) )
var ( var (
@ -27,57 +21,3 @@ func SSHHost(e alicloudSSHHelper, private bool) func(multistep.StateBag) (string
return ipAddress, nil return ipAddress, nil
} }
} }
// SSHConfig returns a function that can be used for the SSH communicator
// config for connecting to the instance created over SSH using the private key
// or password.
func SSHConfig(useAgent bool, username, password string) func(multistep.StateBag) (*ssh.ClientConfig, error) {
return func(state multistep.StateBag) (*ssh.ClientConfig, error) {
if useAgent {
authSock := os.Getenv("SSH_AUTH_SOCK")
if authSock == "" {
return nil, fmt.Errorf("SSH_AUTH_SOCK is not set")
}
sshAgent, err := net.Dial("unix", authSock)
if err != nil {
return nil, fmt.Errorf("Cannot connect to SSH Agent socket %q: %s", authSock, err)
}
return &ssh.ClientConfig{
User: username,
Auth: []ssh.AuthMethod{
ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}, nil
}
privateKey, hasKey := state.GetOk("privateKey")
if hasKey {
signer, err := ssh.ParsePrivateKey([]byte(privateKey.(string)))
if err != nil {
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
} else {
return &ssh.ClientConfig{
User: username,
Auth: []ssh.AuthMethod{
ssh.Password(password),
ssh.KeyboardInteractive(
packerssh.PasswordKeyboardInteractive(password)),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}, nil
}
}
}

View File

@ -206,10 +206,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
Host: awscommon.SSHHost( Host: awscommon.SSHHost(
ec2conn, ec2conn,
b.config.SSHInterface), b.config.SSHInterface),
SSHConfig: awscommon.SSHConfig( SSHConfig: b.config.RunConfig.Comm.SSHConfigFunc(),
b.config.RunConfig.Comm.SSHAgentAuth,
b.config.RunConfig.Comm.SSHUsername,
b.config.RunConfig.Comm.SSHPassword),
}, },
&common.StepProvision{}, &common.StepProvision{},
&awscommon.StepStopEBSBackedInstance{ &awscommon.StepStopEBSBackedInstance{

View File

@ -223,10 +223,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
Host: awscommon.SSHHost( Host: awscommon.SSHHost(
ec2conn, ec2conn,
b.config.SSHInterface), b.config.SSHInterface),
SSHConfig: awscommon.SSHConfig( SSHConfig: b.config.RunConfig.Comm.SSHConfigFunc(),
b.config.RunConfig.Comm.SSHAgentAuth,
b.config.RunConfig.Comm.SSHUsername,
b.config.RunConfig.Comm.SSHPassword),
}, },
&common.StepProvision{}, &common.StepProvision{},
&awscommon.StepStopEBSBackedInstance{ &awscommon.StepStopEBSBackedInstance{

View File

@ -199,10 +199,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
Host: awscommon.SSHHost( Host: awscommon.SSHHost(
ec2conn, ec2conn,
b.config.SSHInterface), b.config.SSHInterface),
SSHConfig: awscommon.SSHConfig( SSHConfig: b.config.RunConfig.Comm.SSHConfigFunc(),
b.config.RunConfig.Comm.SSHAgentAuth,
b.config.RunConfig.Comm.SSHUsername,
b.config.RunConfig.Comm.SSHPassword),
}, },
&common.StepProvision{}, &common.StepProvision{},
&awscommon.StepStopEBSBackedInstance{ &awscommon.StepStopEBSBackedInstance{

View File

@ -282,10 +282,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
Host: awscommon.SSHHost( Host: awscommon.SSHHost(
ec2conn, ec2conn,
b.config.SSHInterface), b.config.SSHInterface),
SSHConfig: awscommon.SSHConfig( SSHConfig: b.config.RunConfig.Comm.SSHConfigFunc(),
b.config.RunConfig.Comm.SSHAgentAuth,
b.config.RunConfig.Comm.SSHUsername,
b.config.RunConfig.Comm.SSHPassword),
}, },
&common.StepProvision{}, &common.StepProvision{},
&StepUploadX509Cert{}, &StepUploadX509Cert{},

View File

@ -78,12 +78,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
}, },
&stepSetupNetworking{}, &stepSetupNetworking{},
&communicator.StepConnect{ &communicator.StepConnect{
Config: &b.config.Comm, Config: &b.config.Comm,
Host: commHost, Host: commHost,
SSHConfig: sshConfig( SSHConfig: b.config.Comm.SSHConfigFunc(),
b.config.Comm.SSHAgentAuth,
b.config.Comm.SSHUsername,
b.config.Comm.SSHPassword),
SSHPort: commPort, SSHPort: commPort,
WinRMPort: commPort, WinRMPort: commPort,
}, },

View File

@ -2,13 +2,8 @@ package cloudstack
import ( import (
"fmt" "fmt"
"net"
"os"
packerssh "github.com/hashicorp/packer/communicator/ssh"
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
) )
func commHost(state multistep.StateBag) (string, error) { func commHost(state multistep.StateBag) (string, error) {
@ -28,55 +23,3 @@ func commPort(state multistep.StateBag) (int, error) {
return commPort, nil return commPort, nil
} }
func sshConfig(useAgent bool, username, password string) func(state multistep.StateBag) (*ssh.ClientConfig, error) {
return func(state multistep.StateBag) (*ssh.ClientConfig, error) {
if useAgent {
authSock := os.Getenv("SSH_AUTH_SOCK")
if authSock == "" {
return nil, fmt.Errorf("SSH_AUTH_SOCK is not set")
}
sshAgent, err := net.Dial("unix", authSock)
if err != nil {
return nil, fmt.Errorf("Cannot connect to SSH Agent socket %q: %s", authSock, err)
}
return &ssh.ClientConfig{
User: username,
Auth: []ssh.AuthMethod{
ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}, nil
}
privateKey, hasKey := state.GetOk("privateKey")
if hasKey {
signer, err := ssh.ParsePrivateKey([]byte(privateKey.(string)))
if err != nil {
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
} else {
return &ssh.ClientConfig{
User: username,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Auth: []ssh.AuthMethod{
ssh.Password(password),
ssh.KeyboardInteractive(
packerssh.PasswordKeyboardInteractive(password)),
}}, nil
}
}
}

View File

@ -87,7 +87,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&communicator.StepConnect{ &communicator.StepConnect{
Config: &b.config.Comm, Config: &b.config.Comm,
Host: commHost, Host: commHost,
SSHConfig: sshConfig, SSHConfig: b.config.Comm.SSHConfigFunc(),
}, },
new(common.StepProvision), new(common.StepProvision),
new(stepShutdown), new(stepShutdown),

View File

@ -1,10 +1,6 @@
package digitalocean package digitalocean
import ( import (
"fmt"
"golang.org/x/crypto/ssh"
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
) )
@ -12,21 +8,3 @@ func commHost(state multistep.StateBag) (string, error) {
ipAddress := state.Get("droplet_ip").(string) ipAddress := state.Get("droplet_ip").(string)
return ipAddress, nil return ipAddress, nil
} }
func sshConfig(state multistep.StateBag) (*ssh.ClientConfig, error) {
config := state.Get("config").(Config)
privateKey := state.Get("privateKey").(string)
signer, err := ssh.ParsePrivateKey([]byte(privateKey))
if err != nil {
return nil, fmt.Errorf("Error setting up SSH config: %s", err)
}
return &ssh.ClientConfig{
User: config.Comm.SSHUsername,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}, nil
}

View File

@ -48,7 +48,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&communicator.StepConnect{ &communicator.StepConnect{
Config: &b.config.Comm, Config: &b.config.Comm,
Host: commHost, Host: commHost,
SSHConfig: sshConfig(&b.config.Comm), SSHConfig: b.config.Comm.SSHConfigFunc(),
CustomConnect: map[string]multistep.Step{ CustomConnect: map[string]multistep.Step{
"docker": &StepConnectDocker{}, "docker": &StepConnectDocker{},
}, },

View File

@ -1,13 +1,7 @@
package docker package docker
import ( import (
"fmt"
"io/ioutil"
"github.com/hashicorp/packer/communicator/ssh"
"github.com/hashicorp/packer/helper/communicator"
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
gossh "golang.org/x/crypto/ssh"
) )
func commHost(state multistep.StateBag) (string, error) { func commHost(state multistep.StateBag) (string, error) {
@ -15,40 +9,3 @@ func commHost(state multistep.StateBag) (string, error) {
driver := state.Get("driver").(Driver) driver := state.Get("driver").(Driver)
return driver.IPAddress(containerId) return driver.IPAddress(containerId)
} }
func sshConfig(comm *communicator.Config) func(state multistep.StateBag) (*gossh.ClientConfig, error) {
return func(state multistep.StateBag) (*gossh.ClientConfig, error) {
if comm.SSHPrivateKey != "" {
// key based auth
bytes, err := ioutil.ReadFile(comm.SSHPrivateKey)
if err != nil {
return nil, fmt.Errorf("Error setting up SSH config: %s", err)
}
privateKey := string(bytes)
signer, err := gossh.ParsePrivateKey([]byte(privateKey))
if err != nil {
return nil, fmt.Errorf("Error setting up SSH config: %s", err)
}
return &gossh.ClientConfig{
User: comm.SSHUsername,
Auth: []gossh.AuthMethod{
gossh.PublicKeys(signer),
},
HostKeyCallback: gossh.InsecureIgnoreHostKey(),
}, nil
} else {
// password based auth
return &gossh.ClientConfig{
User: comm.SSHUsername,
Auth: []gossh.AuthMethod{
gossh.Password(comm.SSHPassword),
gossh.KeyboardInteractive(
ssh.PasswordKeyboardInteractive(comm.SSHPassword)),
},
HostKeyCallback: gossh.InsecureIgnoreHostKey(),
}, nil
}
}
}

View File

@ -68,7 +68,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&communicator.StepConnect{ &communicator.StepConnect{
Config: &b.config.Comm, Config: &b.config.Comm,
Host: commHost, Host: commHost,
SSHConfig: sshConfig, SSHConfig: b.config.Comm.SSHConfigFunc(),
WinRMConfig: winrmConfig, WinRMConfig: winrmConfig,
}, },
new(common.StepProvision), new(common.StepProvision),

View File

@ -1,32 +1,10 @@
package googlecompute package googlecompute
import ( import (
"fmt"
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
"golang.org/x/crypto/ssh"
) )
func commHost(state multistep.StateBag) (string, error) { func commHost(state multistep.StateBag) (string, error) {
ipAddress := state.Get("instance_ip").(string) ipAddress := state.Get("instance_ip").(string)
return ipAddress, nil return ipAddress, nil
} }
// sshConfig returns the ssh configuration.
func sshConfig(state multistep.StateBag) (*ssh.ClientConfig, error) {
config := state.Get("config").(*Config)
privateKey := state.Get("ssh_private_key").(string)
signer, err := ssh.ParsePrivateKey([]byte(privateKey))
if err != nil {
return nil, fmt.Errorf("Error setting up SSH config: %s", err)
}
return &ssh.ClientConfig{
User: config.Comm.SSHUsername,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}, nil
}

View File

@ -1,10 +1,7 @@
package common package common
import ( import (
commonssh "github.com/hashicorp/packer/common/ssh"
"github.com/hashicorp/packer/communicator/ssh"
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
gossh "golang.org/x/crypto/ssh"
) )
func CommHost(state multistep.StateBag) (string, error) { func CommHost(state multistep.StateBag) (string, error) {
@ -23,28 +20,3 @@ func CommHost(state multistep.StateBag) (string, error) {
return ip, nil return ip, nil
} }
func SSHConfigFunc(config *SSHConfig) func(multistep.StateBag) (*gossh.ClientConfig, error) {
return func(state multistep.StateBag) (*gossh.ClientConfig, error) {
auth := []gossh.AuthMethod{
gossh.Password(config.Comm.SSHPassword),
gossh.KeyboardInteractive(
ssh.PasswordKeyboardInteractive(config.Comm.SSHPassword)),
}
if config.Comm.SSHPrivateKey != "" {
signer, err := commonssh.FileSigner(config.Comm.SSHPrivateKey)
if err != nil {
return nil, err
}
auth = append(auth, gossh.PublicKeys(signer))
}
return &gossh.ClientConfig{
User: config.Comm.SSHUsername,
Auth: auth,
HostKeyCallback: gossh.InsecureIgnoreHostKey(),
}, nil
}
}

View File

@ -444,7 +444,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&communicator.StepConnect{ &communicator.StepConnect{
Config: &b.config.SSHConfig.Comm, Config: &b.config.SSHConfig.Comm,
Host: hypervcommon.CommHost, Host: hypervcommon.CommHost,
SSHConfig: hypervcommon.SSHConfigFunc(&b.config.SSHConfig), SSHConfig: b.config.SSHConfig.Comm.SSHConfigFunc(),
}, },
// provision requires communicator to be setup // provision requires communicator to be setup

View File

@ -453,7 +453,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&communicator.StepConnect{ &communicator.StepConnect{
Config: &b.config.SSHConfig.Comm, Config: &b.config.SSHConfig.Comm,
Host: hypervcommon.CommHost, Host: hypervcommon.CommHost,
SSHConfig: hypervcommon.SSHConfigFunc(&b.config.SSHConfig), SSHConfig: b.config.SSHConfig.Comm.SSHConfigFunc(),
}, },
// provision requires communicator to be setup // provision requires communicator to be setup

View File

@ -32,13 +32,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
if b.config.CommConfig.Type != "none" { if b.config.CommConfig.Type != "none" {
steps = append(steps, steps = append(steps,
&communicator.StepConnect{ &communicator.StepConnect{
Config: &b.config.CommConfig, Config: &b.config.CommConfig,
Host: CommHost(b.config.CommConfig.Host()), Host: CommHost(b.config.CommConfig.Host()),
SSHConfig: SSHConfig( SSHConfig: b.config.CommConfig.SSHConfigFunc(),
b.config.CommConfig.SSHAgentAuth,
b.config.CommConfig.SSHUsername,
b.config.CommConfig.SSHPassword,
b.config.CommConfig.SSHPrivateKey),
}, },
) )
} }

View File

@ -45,7 +45,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&communicator.StepConnect{ &communicator.StepConnect{
Config: &b.config.Comm, Config: &b.config.Comm,
Host: commHost, Host: commHost,
SSHConfig: sshConfig, SSHConfig: b.config.Comm.SSHConfigFunc(),
}, },
&common.StepProvision{}, &common.StepProvision{},
new(stepTakeSnapshot), new(stepTakeSnapshot),

View File

@ -1,49 +1,10 @@
package oneandone package oneandone
import ( import (
"fmt"
"github.com/hashicorp/packer/communicator/ssh"
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
gossh "golang.org/x/crypto/ssh"
) )
func commHost(state multistep.StateBag) (string, error) { func commHost(state multistep.StateBag) (string, error) {
ipAddress := state.Get("server_ip").(string) ipAddress := state.Get("server_ip").(string)
return ipAddress, nil return ipAddress, nil
} }
func sshConfig(state multistep.StateBag) (*gossh.ClientConfig, error) {
config := state.Get("config").(*Config)
var privateKey string
var auth []gossh.AuthMethod
if config.Comm.SSHPassword != "" {
auth = []gossh.AuthMethod{
gossh.Password(config.Comm.SSHPassword),
gossh.KeyboardInteractive(
ssh.PasswordKeyboardInteractive(config.Comm.SSHPassword)),
}
}
if config.Comm.SSHPrivateKey != "" {
if priv, ok := state.GetOk("privateKey"); ok {
privateKey = priv.(string)
}
signer, err := gossh.ParsePrivateKey([]byte(privateKey))
if err != nil {
return nil, fmt.Errorf("Error setting up SSH config: %s", err)
}
if err != nil {
return nil, err
}
auth = append(auth, gossh.PublicKeys(signer))
}
return &gossh.ClientConfig{
User: config.Comm.SSHUsername,
Auth: auth,
HostKeyCallback: gossh.InsecureIgnoreHostKey(),
}, nil
}

View File

@ -125,10 +125,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
computeClient, computeClient,
b.config.SSHInterface, b.config.SSHInterface,
b.config.SSHIPVersion), b.config.SSHIPVersion),
SSHConfig: SSHConfig( SSHConfig: b.config.RunConfig.Comm.SSHConfigFunc(),
b.config.RunConfig.Comm.SSHAgentAuth,
b.config.RunConfig.Comm.SSHUsername,
b.config.RunConfig.Comm.SSHPassword),
}, },
&common.StepProvision{}, &common.StepProvision{},
&StepStopServer{}, &StepStopServer{},

View File

@ -4,17 +4,12 @@ import (
"errors" "errors"
"fmt" "fmt"
"log" "log"
"net"
"os"
"time" "time"
"github.com/gophercloud/gophercloud" "github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers" "github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/floatingips" "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/floatingips"
packerssh "github.com/hashicorp/packer/communicator/ssh"
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
) )
// CommHost looks up the host for the communicator. // CommHost looks up the host for the communicator.
@ -63,62 +58,6 @@ func CommHost(
} }
} }
// SSHConfig returns a function that can be used for the SSH communicator
// config for connecting to the instance created over SSH using a private key
// or a password.
func SSHConfig(useAgent bool, username, password string) func(multistep.StateBag) (*ssh.ClientConfig, error) {
return func(state multistep.StateBag) (*ssh.ClientConfig, error) {
if useAgent {
authSock := os.Getenv("SSH_AUTH_SOCK")
if authSock == "" {
return nil, fmt.Errorf("SSH_AUTH_SOCK is not set")
}
sshAgent, err := net.Dial("unix", authSock)
if err != nil {
return nil, fmt.Errorf("Cannot connect to SSH Agent socket %q: %s", authSock, err)
}
return &ssh.ClientConfig{
User: username,
Auth: []ssh.AuthMethod{
ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}, nil
}
privateKey, hasKey := state.GetOk("privateKey")
if hasKey {
signer, err := ssh.ParsePrivateKey([]byte(privateKey.(string)))
if err != nil {
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
} else {
return &ssh.ClientConfig{
User: username,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Auth: []ssh.AuthMethod{
ssh.Password(password),
ssh.KeyboardInteractive(
packerssh.PasswordKeyboardInteractive(password)),
}}, nil
}
}
}
func sshAddrFromPool(s *servers.Server, desired string, sshIPVersion string) string { func sshAddrFromPool(s *servers.Server, desired string, sshIPVersion string) string {
// Get all the addresses associated with this server. This // Get all the addresses associated with this server. This
// was taken directly from Terraform. // was taken directly from Terraform.

View File

@ -72,11 +72,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&stepSecurity{}, &stepSecurity{},
&stepCreateInstance{}, &stepCreateInstance{},
&communicator.StepConnect{ &communicator.StepConnect{
Config: &b.config.Comm, Config: &b.config.Comm,
Host: ocommon.CommHost, Host: ocommon.CommHost,
SSHConfig: ocommon.SSHConfig( SSHConfig: b.config.Comm.SSHConfigFunc(),
b.config.Comm.SSHUsername,
b.config.Comm.SSHPassword),
}, },
&common.StepProvision{}, &common.StepProvision{},
&stepSnapshot{}, &stepSnapshot{},

View File

@ -1,45 +1,10 @@
package common package common
import ( import (
"fmt"
packerssh "github.com/hashicorp/packer/communicator/ssh"
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
"golang.org/x/crypto/ssh"
) )
func CommHost(state multistep.StateBag) (string, error) { func CommHost(state multistep.StateBag) (string, error) {
ipAddress := state.Get("instance_ip").(string) ipAddress := state.Get("instance_ip").(string)
return ipAddress, nil return ipAddress, nil
} }
// SSHConfig returns a function that can be used for the SSH communicator
// config for connecting to the instance created over SSH using the private key
// 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.(string)))
if err != nil {
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
}
}

View File

@ -64,11 +64,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
BuildName: b.config.PackerBuildName, BuildName: b.config.PackerBuildName,
}, },
&communicator.StepConnect{ &communicator.StepConnect{
Config: &b.config.Comm, Config: &b.config.Comm,
Host: ocommon.CommHost, Host: ocommon.CommHost,
SSHConfig: ocommon.SSHConfig( SSHConfig: b.config.Comm.SSHConfigFunc(),
b.config.Comm.SSHUsername,
b.config.Comm.SSHPassword),
}, },
&common.StepProvision{}, &common.StepProvision{},
&stepImage{}, &stepImage{},

View File

@ -1,10 +1,7 @@
package common package common
import ( import (
commonssh "github.com/hashicorp/packer/common/ssh"
packerssh "github.com/hashicorp/packer/communicator/ssh"
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
"golang.org/x/crypto/ssh"
) )
// CommHost returns the VM's IP address which should be used to access it by SSH. // CommHost returns the VM's IP address which should be used to access it by SSH.
@ -24,29 +21,3 @@ func CommHost(state multistep.StateBag) (string, error) {
return ip, nil return ip, nil
} }
// SSHConfigFunc returns SSH credentials to access the VM by SSH.
func SSHConfigFunc(config SSHConfig) func(multistep.StateBag) (*ssh.ClientConfig, error) {
return func(state multistep.StateBag) (*ssh.ClientConfig, error) {
auth := []ssh.AuthMethod{
ssh.Password(config.Comm.SSHPassword),
ssh.KeyboardInteractive(
packerssh.PasswordKeyboardInteractive(config.Comm.SSHPassword)),
}
if config.Comm.SSHPrivateKey != "" {
signer, err := commonssh.FileSigner(config.Comm.SSHPrivateKey)
if err != nil {
return nil, err
}
auth = append(auth, ssh.PublicKeys(signer))
}
return &ssh.ClientConfig{
User: config.Comm.SSHUsername,
Auth: auth,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}, nil
}
}

View File

@ -196,7 +196,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&communicator.StepConnect{ &communicator.StepConnect{
Config: &b.config.SSHConfig.Comm, Config: &b.config.SSHConfig.Comm,
Host: parallelscommon.CommHost, Host: parallelscommon.CommHost,
SSHConfig: parallelscommon.SSHConfigFunc(b.config.SSHConfig), SSHConfig: b.config.SSHConfig.Comm.SSHConfigFunc(),
}, },
&parallelscommon.StepUploadVersion{ &parallelscommon.StepUploadVersion{
Path: b.config.PrlctlVersionFile, Path: b.config.PrlctlVersionFile,

View File

@ -85,7 +85,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&communicator.StepConnect{ &communicator.StepConnect{
Config: &b.config.SSHConfig.Comm, Config: &b.config.SSHConfig.Comm,
Host: parallelscommon.CommHost, Host: parallelscommon.CommHost,
SSHConfig: parallelscommon.SSHConfigFunc(b.config.SSHConfig), SSHConfig: b.config.SSHConfig.SSHConfigFunc(),
}, },
&parallelscommon.StepUploadVersion{ &parallelscommon.StepUploadVersion{
Path: b.config.PrlctlVersionFile, Path: b.config.PrlctlVersionFile,

View File

@ -42,7 +42,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&communicator.StepConnect{ &communicator.StepConnect{
Config: &b.config.Comm, Config: &b.config.Comm,
Host: commHost, Host: commHost,
SSHConfig: sshConfig, SSHConfig: b.config.Comm.SSHConfigFunc(),
}, },
&common.StepProvision{}, &common.StepProvision{},
new(stepTakeSnapshot), new(stepTakeSnapshot),

View File

@ -1,49 +1,10 @@
package profitbricks package profitbricks
import ( import (
"fmt"
"github.com/hashicorp/packer/communicator/ssh"
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
gossh "golang.org/x/crypto/ssh"
) )
func commHost(state multistep.StateBag) (string, error) { func commHost(state multistep.StateBag) (string, error) {
ipAddress := state.Get("server_ip").(string) ipAddress := state.Get("server_ip").(string)
return ipAddress, nil return ipAddress, nil
} }
func sshConfig(state multistep.StateBag) (*gossh.ClientConfig, error) {
config := state.Get("config").(*Config)
var privateKey string
var auth []gossh.AuthMethod
if config.Comm.SSHPassword != "" {
auth = []gossh.AuthMethod{
gossh.Password(config.Comm.SSHPassword),
gossh.KeyboardInteractive(
ssh.PasswordKeyboardInteractive(config.Comm.SSHPassword)),
}
}
if config.Comm.SSHPrivateKey != "" {
if priv, ok := state.GetOk("privateKey"); ok {
privateKey = priv.(string)
}
signer, err := gossh.ParsePrivateKey([]byte(privateKey))
if err != nil {
return nil, fmt.Errorf("Error setting up SSH config: %s", err)
}
if err != nil {
return nil, err
}
auth = append(auth, gossh.PublicKeys(signer))
}
return &gossh.ClientConfig{
User: config.Comm.SSHUsername,
Auth: auth,
HostKeyCallback: gossh.InsecureIgnoreHostKey(),
}, nil
}

View File

@ -393,7 +393,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&communicator.StepConnect{ &communicator.StepConnect{
Config: &b.config.Comm, Config: &b.config.Comm,
Host: commHost, Host: commHost,
SSHConfig: sshConfig, SSHConfig: b.config.Comm.SSHConfigFunc(),
SSHPort: commPort, SSHPort: commPort,
WinRMPort: commPort, WinRMPort: commPort,
}, },

View File

@ -1,15 +1,7 @@
package qemu package qemu
import ( import (
"fmt"
"net"
"os"
commonssh "github.com/hashicorp/packer/common/ssh"
packerssh "github.com/hashicorp/packer/communicator/ssh"
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
gossh "golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
) )
func commHost(state multistep.StateBag) (string, error) { func commHost(state multistep.StateBag) (string, error) {
@ -20,47 +12,3 @@ func commPort(state multistep.StateBag) (int, error) {
sshHostPort := state.Get("sshHostPort").(uint) sshHostPort := state.Get("sshHostPort").(uint)
return int(sshHostPort), nil return int(sshHostPort), nil
} }
func sshConfig(state multistep.StateBag) (*gossh.ClientConfig, error) {
config := state.Get("config").(*Config)
var auth []gossh.AuthMethod
if config.Comm.SSHAgentAuth {
authSock := os.Getenv("SSH_AUTH_SOCK")
if authSock == "" {
return nil, fmt.Errorf("SSH_AUTH_SOCK is not set")
}
sshAgent, err := net.Dial("unix", authSock)
if err != nil {
return nil, fmt.Errorf("Cannot connect to SSH Agent socket %q: %s", authSock, err)
}
auth = []gossh.AuthMethod{
gossh.PublicKeysCallback(agent.NewClient(sshAgent).Signers),
}
}
if config.Comm.SSHPassword != "" {
auth = append(auth,
gossh.Password(config.Comm.SSHPassword),
gossh.KeyboardInteractive(
packerssh.PasswordKeyboardInteractive(config.Comm.SSHPassword)),
)
}
if config.Comm.SSHPrivateKey != "" {
signer, err := commonssh.FileSigner(config.Comm.SSHPrivateKey)
if err != nil {
return nil, err
}
auth = append(auth, gossh.PublicKeys(signer))
}
return &gossh.ClientConfig{
User: config.Comm.SSHUsername,
Auth: auth,
HostKeyCallback: gossh.InsecureIgnoreHostKey(),
}, nil
}

View File

@ -58,7 +58,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&communicator.StepConnect{ &communicator.StepConnect{
Config: &b.config.Comm, Config: &b.config.Comm,
Host: commHost, Host: commHost,
SSHConfig: sshConfig, SSHConfig: b.config.Comm.SSHConfigFunc(),
}, },
new(common.StepProvision), new(common.StepProvision),
new(stepShutdown), new(stepShutdown),

View File

@ -1,61 +1,10 @@
package scaleway package scaleway
import ( import (
"fmt"
"net"
"os"
packerssh "github.com/hashicorp/packer/communicator/ssh"
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
) )
func commHost(state multistep.StateBag) (string, error) { func commHost(state multistep.StateBag) (string, error) {
ipAddress := state.Get("server_ip").(string) ipAddress := state.Get("server_ip").(string)
return ipAddress, nil return ipAddress, nil
} }
func sshConfig(state multistep.StateBag) (*ssh.ClientConfig, error) {
config := state.Get("config").(Config)
var auth []ssh.AuthMethod
if config.Comm.SSHAgentAuth {
authSock := os.Getenv("SSH_AUTH_SOCK")
if authSock == "" {
return nil, fmt.Errorf("SSH_AUTH_SOCK is not set")
}
sshAgent, err := net.Dial("unix", authSock)
if err != nil {
return nil, fmt.Errorf("Cannot connect to SSH Agent socket %q: %s", authSock, err)
}
auth = []ssh.AuthMethod{
ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers),
}
}
if config.Comm.SSHPassword != "" {
auth = append(auth,
ssh.Password(config.Comm.SSHPassword),
ssh.KeyboardInteractive(
packerssh.PasswordKeyboardInteractive(config.Comm.SSHPassword)),
)
}
// Use key based auth if there is a private key in the state bag
if privateKey, ok := state.GetOk("private_key"); ok {
signer, err := ssh.ParsePrivateKey([]byte(privateKey.(string)))
if err != nil {
return nil, fmt.Errorf("Error setting up SSH config: %s", err)
}
auth = append(auth, ssh.PublicKeys(signer))
}
return &ssh.ClientConfig{
User: config.Comm.SSHUsername,
Auth: auth,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}, nil
}

View File

@ -63,13 +63,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
steps := []multistep.Step{ steps := []multistep.Step{
&StepCreateSourceMachine{}, &StepCreateSourceMachine{},
&communicator.StepConnect{ &communicator.StepConnect{
Config: &config.Comm, Config: &config.Comm,
Host: commHost, Host: commHost,
SSHConfig: sshConfig( SSHConfig: b.config.Comm.SSHConfigFunc(),
b.config.Comm.SSHAgentAuth,
b.config.Comm.SSHUsername,
b.config.Comm.SSHPrivateKey,
b.config.Comm.SSHPassword),
}, },
&common.StepProvision{}, &common.StepProvision{},
&StepStopMachine{}, &StepStopMachine{},

View File

@ -1,17 +1,7 @@
package triton package triton
import ( import (
"fmt"
"io/ioutil"
"log"
"net"
"os"
packerssh "github.com/hashicorp/packer/communicator/ssh"
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
) )
func commHost(state multistep.StateBag) (string, error) { func commHost(state multistep.StateBag) (string, error) {
@ -25,68 +15,3 @@ func commHost(state multistep.StateBag) (string, error) {
return machine, nil return machine, nil
} }
// SSHConfig returns a function that can be used for the SSH communicator
// config for connecting to the instance created over SSH using the private key
// or password.
func sshConfig(useAgent bool, username, privateKeyPath, password string) func(multistep.StateBag) (*ssh.ClientConfig, error) {
return func(state multistep.StateBag) (*ssh.ClientConfig, error) {
if useAgent {
log.Println("Configuring SSH agent.")
authSock := os.Getenv("SSH_AUTH_SOCK")
if authSock == "" {
return nil, fmt.Errorf("SSH_AUTH_SOCK is not set")
}
sshAgent, err := net.Dial("unix", authSock)
if err != nil {
return nil, fmt.Errorf("Cannot connect to SSH Agent socket %q: %s", authSock, err)
}
return &ssh.ClientConfig{
User: username,
Auth: []ssh.AuthMethod{
ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}, nil
}
hasKey := privateKeyPath != ""
if hasKey {
log.Printf("Configuring SSH private key '%s'.", privateKeyPath)
privateKeyBytes, err := ioutil.ReadFile(privateKeyPath)
if err != nil {
return nil, fmt.Errorf("Unable to read SSH private key: %s", err)
}
signer, err := ssh.ParsePrivateKey(privateKeyBytes)
if err != nil {
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
} else {
log.Println("Configuring SSH keyboard interactive.")
return &ssh.ClientConfig{
User: username,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Auth: []ssh.AuthMethod{
ssh.Password(password),
ssh.KeyboardInteractive(
packerssh.PasswordKeyboardInteractive(password)),
}}, nil
}
}
}

View File

@ -1,10 +1,7 @@
package common package common
import ( import (
commonssh "github.com/hashicorp/packer/common/ssh"
"github.com/hashicorp/packer/communicator/ssh"
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
gossh "golang.org/x/crypto/ssh"
) )
func CommHost(host string) func(multistep.StateBag) (string, error) { func CommHost(host string) func(multistep.StateBag) (string, error) {
@ -17,28 +14,3 @@ func SSHPort(state multistep.StateBag) (int, error) {
sshHostPort := state.Get("sshHostPort").(int) sshHostPort := state.Get("sshHostPort").(int)
return sshHostPort, nil return sshHostPort, nil
} }
func SSHConfigFunc(config SSHConfig) func(multistep.StateBag) (*gossh.ClientConfig, error) {
return func(state multistep.StateBag) (*gossh.ClientConfig, error) {
auth := []gossh.AuthMethod{
gossh.Password(config.Comm.SSHPassword),
gossh.KeyboardInteractive(
ssh.PasswordKeyboardInteractive(config.Comm.SSHPassword)),
}
if config.Comm.SSHPrivateKey != "" {
signer, err := commonssh.FileSigner(config.Comm.SSHPrivateKey)
if err != nil {
return nil, err
}
auth = append(auth, gossh.PublicKeys(signer))
}
return &gossh.ClientConfig{
User: config.Comm.SSHUsername,
Auth: auth,
HostKeyCallback: gossh.InsecureIgnoreHostKey(),
}, nil
}
}

View File

@ -253,7 +253,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&communicator.StepConnect{ &communicator.StepConnect{
Config: &b.config.SSHConfig.Comm, Config: &b.config.SSHConfig.Comm,
Host: vboxcommon.CommHost(b.config.SSHConfig.Comm.SSHHost), Host: vboxcommon.CommHost(b.config.SSHConfig.Comm.SSHHost),
SSHConfig: vboxcommon.SSHConfigFunc(b.config.SSHConfig), SSHConfig: b.config.SSHConfig.Comm.SSHConfigFunc(),
SSHPort: vboxcommon.SSHPort, SSHPort: vboxcommon.SSHPort,
WinRMPort: vboxcommon.SSHPort, WinRMPort: vboxcommon.SSHPort,
}, },

View File

@ -114,7 +114,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&communicator.StepConnect{ &communicator.StepConnect{
Config: &b.config.SSHConfig.Comm, Config: &b.config.SSHConfig.Comm,
Host: vboxcommon.CommHost(b.config.SSHConfig.Comm.SSHHost), Host: vboxcommon.CommHost(b.config.SSHConfig.Comm.SSHHost),
SSHConfig: vboxcommon.SSHConfigFunc(b.config.SSHConfig), SSHConfig: b.config.SSHConfig.Comm.SSHConfigFunc(),
SSHPort: vboxcommon.SSHPort, SSHPort: vboxcommon.SSHPort,
WinRMPort: vboxcommon.SSHPort, WinRMPort: vboxcommon.SSHPort,
}, },

View File

@ -5,10 +5,7 @@ import (
"fmt" "fmt"
"log" "log"
commonssh "github.com/hashicorp/packer/common/ssh"
"github.com/hashicorp/packer/communicator/ssh"
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
gossh "golang.org/x/crypto/ssh"
) )
func CommHost(config *SSHConfig) func(multistep.StateBag) (string, error) { func CommHost(config *SSHConfig) func(multistep.StateBag) (string, error) {
@ -34,28 +31,3 @@ func CommHost(config *SSHConfig) func(multistep.StateBag) (string, error) {
return ipAddress, nil return ipAddress, nil
} }
} }
func SSHConfigFunc(config *SSHConfig) func(multistep.StateBag) (*gossh.ClientConfig, error) {
return func(state multistep.StateBag) (*gossh.ClientConfig, error) {
auth := []gossh.AuthMethod{
gossh.Password(config.Comm.SSHPassword),
gossh.KeyboardInteractive(
ssh.PasswordKeyboardInteractive(config.Comm.SSHPassword)),
}
if config.Comm.SSHPrivateKey != "" {
signer, err := commonssh.FileSigner(config.Comm.SSHPrivateKey)
if err != nil {
return nil, err
}
auth = append(auth, gossh.PublicKeys(signer))
}
return &gossh.ClientConfig{
User: config.Comm.SSHUsername,
Auth: auth,
HostKeyCallback: gossh.InsecureIgnoreHostKey(),
}, nil
}
}

View File

@ -343,7 +343,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&communicator.StepConnect{ &communicator.StepConnect{
Config: &b.config.SSHConfig.Comm, Config: &b.config.SSHConfig.Comm,
Host: driver.CommHost, Host: driver.CommHost,
SSHConfig: vmwcommon.SSHConfigFunc(&b.config.SSHConfig), SSHConfig: b.config.SSHConfig.Comm.SSHConfigFunc(),
}, },
&vmwcommon.StepUploadTools{ &vmwcommon.StepUploadTools{
RemoteType: b.config.RemoteType, RemoteType: b.config.RemoteType,

View File

@ -101,7 +101,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&communicator.StepConnect{ &communicator.StepConnect{
Config: &b.config.SSHConfig.Comm, Config: &b.config.SSHConfig.Comm,
Host: driver.CommHost, Host: driver.CommHost,
SSHConfig: vmwcommon.SSHConfigFunc(&b.config.SSHConfig), SSHConfig: b.config.SSHConfig.Comm.SSHConfigFunc(),
}, },
&vmwcommon.StepUploadTools{ &vmwcommon.StepUploadTools{
RemoteType: b.config.RemoteType, RemoteType: b.config.RemoteType,

View File

@ -1,44 +0,0 @@
package ssh
import (
"encoding/pem"
"fmt"
"io/ioutil"
"os"
"golang.org/x/crypto/ssh"
)
// FileSigner returns an ssh.Signer for a key file.
func FileSigner(path string) (ssh.Signer, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
keyBytes, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
// We parse the private key on our own first so that we can
// show a nicer error if the private key has a password.
block, _ := pem.Decode(keyBytes)
if block == nil {
return nil, fmt.Errorf(
"Failed to read key '%s': no key found", path)
}
if block.Headers["Proc-Type"] == "4,ENCRYPTED" {
return nil, fmt.Errorf(
"Failed to read key '%s': password protected keys are\n"+
"not supported. Please decrypt the key prior to use.", path)
}
signer, err := ssh.ParsePrivateKey(keyBytes)
if err != nil {
return nil, fmt.Errorf("Error setting up SSH config: %s", err)
}
return signer, nil
}

View File

@ -10,7 +10,6 @@ import (
"strings" "strings"
"time" "time"
commonssh "github.com/hashicorp/packer/common/ssh"
"github.com/hashicorp/packer/communicator/ssh" "github.com/hashicorp/packer/communicator/ssh"
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer"