2013-07-15 01:17:09 -04:00
|
|
|
package virtualbox
|
|
|
|
|
|
|
|
import (
|
|
|
|
gossh "code.google.com/p/go.crypto/ssh"
|
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/packer/communicator/ssh"
|
2013-08-28 01:54:56 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2013-07-15 01:17:09 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func sshAddress(state map[string]interface{}) (string, error) {
|
|
|
|
sshHostPort := state["sshHostPort"].(uint)
|
|
|
|
return fmt.Sprintf("127.0.0.1:%d", sshHostPort), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func sshConfig(state map[string]interface{}) (*gossh.ClientConfig, error) {
|
|
|
|
config := state["config"].(*config)
|
|
|
|
|
2013-08-28 01:54:56 -04:00
|
|
|
auth := []gossh.ClientAuth{
|
|
|
|
gossh.ClientAuthPassword(ssh.Password(config.SSHPassword)),
|
|
|
|
gossh.ClientAuthKeyboardInteractive(
|
|
|
|
ssh.PasswordKeyboardInteractive(config.SSHPassword)),
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.SSHKeyPath != "" {
|
|
|
|
keyring, err := sshKeyToKeyring(config.SSHKeyPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
auth = append(auth, gossh.ClientAuthKeyring(keyring))
|
|
|
|
}
|
|
|
|
|
2013-07-15 01:17:09 -04:00
|
|
|
return &gossh.ClientConfig{
|
|
|
|
User: config.SSHUser,
|
2013-08-28 01:54:56 -04:00
|
|
|
Auth: auth,
|
2013-07-15 01:17:09 -04:00
|
|
|
}, nil
|
|
|
|
}
|
2013-08-28 01:54:56 -04:00
|
|
|
|
|
|
|
func sshKeyToKeyring(path string) (gossh.ClientKeyring, 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
|
|
|
|
}
|
|
|
|
|
|
|
|
keyring := new(ssh.SimpleKeychain)
|
|
|
|
if err := keyring.AddPEMKey(string(keyBytes)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return keyring, nil
|
|
|
|
}
|