2013-09-02 23:23:52 -04:00
|
|
|
package qemu
|
|
|
|
|
|
|
|
import (
|
2018-07-27 08:24:06 -04:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
commonssh "github.com/hashicorp/packer/common/ssh"
|
2018-07-27 08:24:06 -04:00
|
|
|
packerssh "github.com/hashicorp/packer/communicator/ssh"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2015-06-09 00:34:20 -04:00
|
|
|
gossh "golang.org/x/crypto/ssh"
|
2018-07-27 08:24:06 -04:00
|
|
|
"golang.org/x/crypto/ssh/agent"
|
2013-09-02 23:23:52 -04:00
|
|
|
)
|
|
|
|
|
2015-06-13 19:23:33 -04:00
|
|
|
func commHost(state multistep.StateBag) (string, error) {
|
|
|
|
return "127.0.0.1", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func commPort(state multistep.StateBag) (int, error) {
|
2013-09-02 23:23:52 -04:00
|
|
|
sshHostPort := state.Get("sshHostPort").(uint)
|
2015-06-13 19:23:33 -04:00
|
|
|
return int(sshHostPort), nil
|
2013-09-02 23:23:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func sshConfig(state multistep.StateBag) (*gossh.ClientConfig, error) {
|
2015-05-27 16:39:43 -04:00
|
|
|
config := state.Get("config").(*Config)
|
2013-09-02 23:23:52 -04:00
|
|
|
|
2018-07-27 08:24:06 -04:00
|
|
|
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)),
|
|
|
|
)
|
2013-09-02 23:23:52 -04:00
|
|
|
}
|
|
|
|
|
2015-06-13 18:47:59 -04:00
|
|
|
if config.Comm.SSHPrivateKey != "" {
|
|
|
|
signer, err := commonssh.FileSigner(config.Comm.SSHPrivateKey)
|
2013-09-02 23:23:52 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-04-10 04:48:55 -04:00
|
|
|
auth = append(auth, gossh.PublicKeys(signer))
|
2013-09-02 23:23:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return &gossh.ClientConfig{
|
2017-05-20 16:17:04 -04:00
|
|
|
User: config.Comm.SSHUsername,
|
|
|
|
Auth: auth,
|
|
|
|
HostKeyCallback: gossh.InsecureIgnoreHostKey(),
|
2013-09-02 23:23:52 -04:00
|
|
|
}, nil
|
|
|
|
}
|