2014-03-24 06:20:32 -04:00
|
|
|
package null
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-06-02 06:55:31 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/communicator/ssh"
|
2014-03-24 06:20:32 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
2015-06-09 00:34:20 -04:00
|
|
|
gossh "golang.org/x/crypto/ssh"
|
2017-06-02 06:55:31 -04:00
|
|
|
"golang.org/x/crypto/ssh/agent"
|
2014-03-24 06:20:32 -04:00
|
|
|
)
|
|
|
|
|
2015-06-13 19:23:33 -04:00
|
|
|
func CommHost(host string) func(multistep.StateBag) (string, error) {
|
2014-03-24 06:20:32 -04:00
|
|
|
return func(state multistep.StateBag) (string, error) {
|
2015-06-13 19:23:33 -04:00
|
|
|
return host, nil
|
2014-03-24 06:20:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SSHConfig returns a function that can be used for the SSH communicator
|
|
|
|
// config for connecting to the specified host via SSH
|
|
|
|
// private_key_file has precedence over password!
|
2017-06-02 06:55:31 -04:00
|
|
|
func SSHConfig(useAgent bool, username string, password string, privateKeyFile string) func(multistep.StateBag) (*gossh.ClientConfig, error) {
|
2014-03-24 06:20:32 -04:00
|
|
|
return func(state multistep.StateBag) (*gossh.ClientConfig, error) {
|
2017-06-02 06:55:31 -04:00
|
|
|
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 &gossh.ClientConfig{
|
|
|
|
User: username,
|
|
|
|
Auth: []gossh.AuthMethod{
|
|
|
|
gossh.PublicKeysCallback(agent.NewClient(sshAgent).Signers),
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
2014-03-24 06:20:32 -04:00
|
|
|
|
|
|
|
if privateKeyFile != "" {
|
|
|
|
// key based auth
|
|
|
|
|
|
|
|
bytes, err := ioutil.ReadFile(privateKeyFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error setting up SSH config: %s", err)
|
|
|
|
}
|
|
|
|
privateKey := string(bytes)
|
|
|
|
|
2014-04-26 14:28:02 -04:00
|
|
|
signer, err := gossh.ParsePrivateKey([]byte(privateKey))
|
|
|
|
if err != nil {
|
2014-03-24 06:20:32 -04:00
|
|
|
return nil, fmt.Errorf("Error setting up SSH config: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &gossh.ClientConfig{
|
|
|
|
User: username,
|
2014-04-26 14:28:02 -04:00
|
|
|
Auth: []gossh.AuthMethod{
|
|
|
|
gossh.PublicKeys(signer),
|
2014-03-24 06:20:32 -04:00
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
} else {
|
|
|
|
// password based auth
|
|
|
|
|
|
|
|
return &gossh.ClientConfig{
|
|
|
|
User: username,
|
2014-04-26 14:28:02 -04:00
|
|
|
Auth: []gossh.AuthMethod{
|
|
|
|
gossh.Password(password),
|
|
|
|
gossh.KeyboardInteractive(
|
|
|
|
ssh.PasswordKeyboardInteractive(password)),
|
2014-03-24 06:20:32 -04:00
|
|
|
},
|
2017-05-20 16:17:04 -04:00
|
|
|
HostKeyCallback: gossh.InsecureIgnoreHostKey(),
|
2014-03-24 06:20:32 -04:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|