2014-12-29 06:46:54 -06:00
|
|
|
package common
|
2014-11-10 12:16:02 -06:00
|
|
|
|
|
|
|
import (
|
2014-12-08 09:34:48 -06:00
|
|
|
"bytes"
|
2021-01-03 16:08:24 -06:00
|
|
|
"encoding/pem"
|
2014-12-08 09:34:48 -06:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2021-01-03 16:08:24 -06:00
|
|
|
"io/ioutil"
|
2014-12-08 09:34:48 -06:00
|
|
|
"log"
|
|
|
|
"net"
|
2021-01-03 16:08:24 -06:00
|
|
|
"os"
|
2014-12-08 09:34:48 -06:00
|
|
|
"strings"
|
2020-12-16 01:17:27 -06:00
|
|
|
|
2021-01-03 16:08:24 -06:00
|
|
|
"github.com/hashicorp/packer/communicator/ssh"
|
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2020-12-16 01:17:27 -06:00
|
|
|
gossh "golang.org/x/crypto/ssh"
|
2014-11-10 12:16:02 -06:00
|
|
|
)
|
|
|
|
|
2014-12-29 06:46:54 -06:00
|
|
|
func SSHAddress(state multistep.StateBag) (string, error) {
|
2014-12-08 09:34:48 -06:00
|
|
|
sshIP := state.Get("ssh_address").(string)
|
2014-11-10 12:16:02 -06:00
|
|
|
sshHostPort := 22
|
|
|
|
return fmt.Sprintf("%s:%d", sshIP, sshHostPort), nil
|
|
|
|
}
|
|
|
|
|
2014-12-29 06:46:54 -06:00
|
|
|
func SSHLocalAddress(state multistep.StateBag) (string, error) {
|
2015-01-02 10:22:36 -06:00
|
|
|
sshLocalPort, ok := state.Get("local_ssh_port").(uint)
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("SSH port forwarding hasn't been set up yet")
|
|
|
|
}
|
2014-12-08 09:34:48 -06:00
|
|
|
conn_str := fmt.Sprintf("%s:%d", "127.0.0.1", sshLocalPort)
|
|
|
|
return conn_str, nil
|
2014-11-18 12:18:39 -06:00
|
|
|
}
|
|
|
|
|
2015-06-19 12:33:11 -05:00
|
|
|
func SSHPort(state multistep.StateBag) (int, error) {
|
|
|
|
sshHostPort := state.Get("local_ssh_port").(uint)
|
|
|
|
return int(sshHostPort), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func CommHost(state multistep.StateBag) (string, error) {
|
|
|
|
return "127.0.0.1", nil
|
|
|
|
}
|
2014-11-10 12:16:02 -06:00
|
|
|
|
2015-06-19 12:33:11 -05:00
|
|
|
func SSHConfigFunc(config SSHConfig) func(multistep.StateBag) (*gossh.ClientConfig, error) {
|
|
|
|
return func(state multistep.StateBag) (*gossh.ClientConfig, error) {
|
|
|
|
config := state.Get("commonconfig").(CommonConfig)
|
|
|
|
auth := []gossh.AuthMethod{
|
|
|
|
gossh.Password(config.SSHPassword),
|
|
|
|
gossh.KeyboardInteractive(
|
|
|
|
ssh.PasswordKeyboardInteractive(config.SSHPassword)),
|
2014-11-10 12:16:02 -06:00
|
|
|
}
|
|
|
|
|
2015-06-19 12:33:11 -05:00
|
|
|
if config.SSHKeyPath != "" {
|
2021-01-03 16:08:24 -06:00
|
|
|
signer, err := FileSigner(config.SSHKeyPath)
|
2015-06-19 12:33:11 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
auth = append(auth, gossh.PublicKeys(signer))
|
|
|
|
}
|
2014-11-10 12:16:02 -06:00
|
|
|
|
2015-06-19 12:33:11 -05:00
|
|
|
return &gossh.ClientConfig{
|
2020-12-16 01:17:27 -06:00
|
|
|
User: config.SSHUser,
|
|
|
|
Auth: auth,
|
|
|
|
HostKeyCallback: gossh.InsecureIgnoreHostKey(),
|
2015-06-19 12:33:11 -05:00
|
|
|
}, nil
|
|
|
|
}
|
2014-11-10 12:16:02 -06:00
|
|
|
}
|
2014-11-18 12:18:39 -06:00
|
|
|
|
2015-01-02 10:22:36 -06:00
|
|
|
func doExecuteSSHCmd(cmd, target string, config *gossh.ClientConfig) (stdout string, err error) {
|
|
|
|
client, err := gossh.Dial("tcp", target, config)
|
2014-12-08 09:34:48 -06:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2014-11-18 12:18:39 -06:00
|
|
|
|
2014-12-08 09:34:48 -06:00
|
|
|
//Create session
|
|
|
|
session, err := client.NewSession()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2014-11-18 12:18:39 -06:00
|
|
|
|
2014-12-08 09:34:48 -06:00
|
|
|
defer session.Close()
|
2014-11-18 12:18:39 -06:00
|
|
|
|
2014-12-08 09:34:48 -06:00
|
|
|
var b bytes.Buffer
|
|
|
|
session.Stdout = &b
|
|
|
|
if err := session.Run(cmd); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2014-11-18 12:18:39 -06:00
|
|
|
|
2014-12-08 09:34:48 -06:00
|
|
|
return strings.Trim(b.String(), "\n"), nil
|
2014-11-18 12:18:39 -06:00
|
|
|
}
|
|
|
|
|
2015-01-02 10:22:36 -06:00
|
|
|
func ExecuteHostSSHCmd(state multistep.StateBag, cmd string) (stdout string, err error) {
|
|
|
|
config := state.Get("commonconfig").(CommonConfig)
|
2017-02-02 02:13:23 -06:00
|
|
|
sshAddress, _ := SSHAddress(state)
|
2015-01-02 10:22:36 -06:00
|
|
|
// Setup connection config
|
|
|
|
sshConfig := &gossh.ClientConfig{
|
|
|
|
User: config.Username,
|
|
|
|
Auth: []gossh.AuthMethod{
|
|
|
|
gossh.Password(config.Password),
|
|
|
|
},
|
2020-12-16 01:17:27 -06:00
|
|
|
HostKeyCallback: gossh.InsecureIgnoreHostKey(),
|
2015-01-02 10:22:36 -06:00
|
|
|
}
|
2017-02-02 02:13:23 -06:00
|
|
|
return doExecuteSSHCmd(cmd, sshAddress, sshConfig)
|
2015-01-02 10:22:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func ExecuteGuestSSHCmd(state multistep.StateBag, cmd string) (stdout string, err error) {
|
2015-06-19 12:33:11 -05:00
|
|
|
config := state.Get("commonconfig").(CommonConfig)
|
2015-01-02 10:22:36 -06:00
|
|
|
localAddress, err := SSHLocalAddress(state)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-06-19 12:33:11 -05:00
|
|
|
sshConfig, err := SSHConfigFunc(config.SSHConfig)(state)
|
2015-01-02 10:22:36 -06:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return doExecuteSSHCmd(cmd, localAddress, sshConfig)
|
|
|
|
}
|
|
|
|
|
2014-12-08 09:41:28 -06:00
|
|
|
func forward(local_conn net.Conn, config *gossh.ClientConfig, server, remote_dest string, remote_port uint) error {
|
2014-12-10 06:31:02 -06:00
|
|
|
defer local_conn.Close()
|
|
|
|
|
2014-12-08 09:34:48 -06:00
|
|
|
ssh_client_conn, err := gossh.Dial("tcp", server+":22", config)
|
|
|
|
if err != nil {
|
2014-12-08 09:41:28 -06:00
|
|
|
log.Printf("local ssh.Dial error: %s", err)
|
|
|
|
return err
|
2014-12-08 09:34:48 -06:00
|
|
|
}
|
2014-12-10 06:31:02 -06:00
|
|
|
defer ssh_client_conn.Close()
|
2014-11-18 12:18:39 -06:00
|
|
|
|
2014-12-08 09:34:48 -06:00
|
|
|
remote_loc := fmt.Sprintf("%s:%d", remote_dest, remote_port)
|
|
|
|
ssh_conn, err := ssh_client_conn.Dial("tcp", remote_loc)
|
|
|
|
if err != nil {
|
2014-12-08 09:41:28 -06:00
|
|
|
log.Printf("ssh.Dial error: %s", err)
|
|
|
|
return err
|
2014-12-08 09:34:48 -06:00
|
|
|
}
|
2014-12-10 06:31:02 -06:00
|
|
|
defer ssh_conn.Close()
|
2014-11-18 12:18:39 -06:00
|
|
|
|
2014-12-09 11:44:21 -06:00
|
|
|
txDone := make(chan struct{})
|
|
|
|
rxDone := make(chan struct{})
|
|
|
|
|
2014-12-08 09:34:48 -06:00
|
|
|
go func() {
|
|
|
|
_, err = io.Copy(ssh_conn, local_conn)
|
|
|
|
if err != nil {
|
2014-12-08 09:41:28 -06:00
|
|
|
log.Printf("io.copy failed: %v", err)
|
2014-12-08 09:34:48 -06:00
|
|
|
}
|
2014-12-09 11:44:21 -06:00
|
|
|
close(txDone)
|
2014-12-08 09:34:48 -06:00
|
|
|
}()
|
2014-11-18 12:18:39 -06:00
|
|
|
|
2014-12-08 09:34:48 -06:00
|
|
|
go func() {
|
|
|
|
_, err = io.Copy(local_conn, ssh_conn)
|
|
|
|
if err != nil {
|
2014-12-08 09:41:28 -06:00
|
|
|
log.Printf("io.copy failed: %v", err)
|
2014-12-08 09:34:48 -06:00
|
|
|
}
|
2014-12-09 11:44:21 -06:00
|
|
|
close(rxDone)
|
|
|
|
}()
|
|
|
|
|
2014-12-10 06:31:02 -06:00
|
|
|
<-txDone
|
|
|
|
<-rxDone
|
2014-12-08 09:41:28 -06:00
|
|
|
|
|
|
|
return nil
|
2014-12-08 09:34:48 -06:00
|
|
|
}
|
2014-11-18 12:18:39 -06:00
|
|
|
|
2021-01-14 00:15:33 -06:00
|
|
|
func ssh_port_forward(local_listener net.Listener, remote_port int, remote_dest, host, username, password string) error {
|
2014-11-18 12:18:39 -06:00
|
|
|
|
2014-12-08 09:34:48 -06:00
|
|
|
config := &gossh.ClientConfig{
|
|
|
|
User: username,
|
|
|
|
Auth: []gossh.AuthMethod{
|
|
|
|
gossh.Password(password),
|
|
|
|
},
|
2020-12-16 01:17:27 -06:00
|
|
|
HostKeyCallback: gossh.InsecureIgnoreHostKey(),
|
2014-12-08 09:34:48 -06:00
|
|
|
}
|
2014-11-18 12:18:39 -06:00
|
|
|
|
2014-12-08 09:34:48 -06:00
|
|
|
for {
|
|
|
|
local_connection, err := local_listener.Accept()
|
2014-11-18 12:18:39 -06:00
|
|
|
|
2014-12-08 09:34:48 -06:00
|
|
|
if err != nil {
|
2014-12-08 09:41:28 -06:00
|
|
|
log.Printf("Local accept failed: %s", err)
|
2014-12-08 09:34:48 -06:00
|
|
|
return err
|
|
|
|
}
|
2014-11-18 12:18:39 -06:00
|
|
|
|
2014-12-08 09:34:48 -06:00
|
|
|
// Forward to a remote port
|
2021-01-14 00:15:33 -06:00
|
|
|
go forward(local_connection, config, host, remote_dest, uint(remote_port))
|
2014-12-08 09:34:48 -06:00
|
|
|
}
|
2014-11-18 12:18:39 -06:00
|
|
|
|
2014-12-08 09:34:48 -06:00
|
|
|
return nil
|
2014-11-18 12:18:39 -06:00
|
|
|
}
|
2021-01-03 16:08:24 -06:00
|
|
|
|
|
|
|
// FileSigner returns an gossh.Signer for a key file.
|
|
|
|
func FileSigner(path string) (gossh.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 := gossh.ParsePrivateKey(keyBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error setting up SSH config: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return signer, nil
|
|
|
|
}
|