2017-03-03 03:56:17 -05:00
|
|
|
package ecs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-06-01 20:14:51 -04:00
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2017-04-17 09:04:52 -04:00
|
|
|
packerssh "github.com/hashicorp/packer/communicator/ssh"
|
2017-03-03 03:56:17 -05:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"golang.org/x/crypto/ssh"
|
2017-03-04 05:06:32 -05:00
|
|
|
"golang.org/x/crypto/ssh/agent"
|
2017-05-25 06:27:54 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// modified in tests
|
|
|
|
sshHostSleepDuration = time.Second
|
2017-03-03 03:56:17 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type alicloudSSHHelper interface {
|
|
|
|
}
|
|
|
|
|
|
|
|
// SSHHost returns a function that can be given to the SSH communicator
|
|
|
|
func SSHHost(e alicloudSSHHelper, private bool) func(multistep.StateBag) (string, error) {
|
|
|
|
return func(state multistep.StateBag) (string, error) {
|
|
|
|
ipAddress := state.Get("ipaddress").(string)
|
|
|
|
return ipAddress, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-25 06:27:54 -04:00
|
|
|
// 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.
|
2017-03-03 03:56:17 -05:00
|
|
|
func SSHConfig(useAgent bool, username, password string) func(multistep.StateBag) (*ssh.ClientConfig, error) {
|
|
|
|
return func(state multistep.StateBag) (*ssh.ClientConfig, error) {
|
2017-03-04 05:06:32 -05: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 &ssh.ClientConfig{
|
|
|
|
User: username,
|
|
|
|
Auth: []ssh.AuthMethod{
|
|
|
|
ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers),
|
|
|
|
},
|
2017-06-11 03:58:45 -04:00
|
|
|
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
|
2017-03-04 05:06:32 -05:00
|
|
|
}, nil
|
|
|
|
}
|
2017-05-25 06:27:54 -04:00
|
|
|
|
2017-03-03 03:56:17 -05:00
|
|
|
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),
|
|
|
|
},
|
2017-06-11 03:58:45 -04:00
|
|
|
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
|
2017-03-03 03:56:17 -05:00
|
|
|
}, nil
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return &ssh.ClientConfig{
|
|
|
|
User: username,
|
|
|
|
Auth: []ssh.AuthMethod{
|
|
|
|
ssh.Password(password),
|
|
|
|
ssh.KeyboardInteractive(
|
|
|
|
packerssh.PasswordKeyboardInteractive(password)),
|
2017-06-11 03:58:45 -04:00
|
|
|
},
|
|
|
|
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
|
|
|
|
}, nil
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|