2014-07-15 23:50:53 +02:00
|
|
|
package ssh
|
|
|
|
|
|
|
|
import (
|
2014-08-05 18:53:02 +02:00
|
|
|
"encoding/pem"
|
2014-07-15 23:50:53 +02:00
|
|
|
"fmt"
|
2014-07-16 01:51:20 +02:00
|
|
|
"io/ioutil"
|
2014-07-15 23:50:53 +02:00
|
|
|
"log"
|
2015-03-16 00:12:25 +01:00
|
|
|
"net"
|
|
|
|
"os"
|
2014-07-15 23:50:53 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2014-08-19 21:27:01 +02:00
|
|
|
"github.com/mitchellh/go-homedir"
|
2014-07-15 23:50:53 +02:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2015-03-16 00:12:25 +01:00
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
"golang.org/x/crypto/ssh/agent"
|
2014-07-15 23:50:53 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-04-10 20:34:46 +02:00
|
|
|
// DefaultUser is used if there is no user given
|
2014-07-15 23:50:53 +02:00
|
|
|
DefaultUser = "root"
|
|
|
|
|
|
|
|
// DefaultPort is used if there is no port given
|
|
|
|
DefaultPort = 22
|
|
|
|
|
|
|
|
// DefaultScriptPath is used as the path to copy the file to
|
|
|
|
// for remote execution if not provided otherwise.
|
2015-04-30 18:02:33 +02:00
|
|
|
DefaultScriptPath = "/tmp/terraform_%RAND%.sh"
|
2014-07-15 23:50:53 +02:00
|
|
|
|
|
|
|
// DefaultTimeout is used if there is no timeout given
|
|
|
|
DefaultTimeout = 5 * time.Minute
|
|
|
|
)
|
|
|
|
|
2015-04-10 20:34:46 +02:00
|
|
|
// connectionInfo is decoded from the ConnInfo of the resource. These are the
|
2015-04-09 21:58:00 +02:00
|
|
|
// only keys we look at. If a KeyFile is given, that is used instead
|
|
|
|
// of a password.
|
2015-04-10 20:34:46 +02:00
|
|
|
type connectionInfo struct {
|
2014-07-15 23:50:53 +02:00
|
|
|
User string
|
|
|
|
Password string
|
|
|
|
KeyFile string `mapstructure:"key_file"`
|
|
|
|
Host string
|
|
|
|
Port int
|
2015-03-16 00:12:25 +01:00
|
|
|
Agent bool
|
2014-07-15 23:50:53 +02:00
|
|
|
Timeout string
|
|
|
|
ScriptPath string `mapstructure:"script_path"`
|
|
|
|
TimeoutVal time.Duration `mapstructure:"-"`
|
2015-06-22 18:34:02 +02:00
|
|
|
|
|
|
|
BastionUser string `mapstructure:"bastion_user"`
|
|
|
|
BastionPassword string `mapstructure:"bastion_password"`
|
|
|
|
BastionKeyFile string `mapstructure:"bastion_key_file"`
|
|
|
|
BastionHost string `mapstructure:"bastion_host"`
|
|
|
|
BastionPort int `mapstructure:"bastion_port"`
|
2014-07-15 23:50:53 +02:00
|
|
|
}
|
|
|
|
|
2015-04-10 20:34:46 +02:00
|
|
|
// parseConnectionInfo is used to convert the ConnInfo of the InstanceState into
|
2015-04-09 21:58:00 +02:00
|
|
|
// a ConnectionInfo struct
|
2015-04-10 20:34:46 +02:00
|
|
|
func parseConnectionInfo(s *terraform.InstanceState) (*connectionInfo, error) {
|
|
|
|
connInfo := &connectionInfo{}
|
2014-07-15 23:50:53 +02:00
|
|
|
decConf := &mapstructure.DecoderConfig{
|
|
|
|
WeaklyTypedInput: true,
|
2015-04-09 21:58:00 +02:00
|
|
|
Result: connInfo,
|
2014-07-15 23:50:53 +02:00
|
|
|
}
|
|
|
|
dec, err := mapstructure.NewDecoder(decConf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-09-17 01:55:02 +02:00
|
|
|
if err := dec.Decode(s.Ephemeral.ConnInfo); err != nil {
|
2014-07-15 23:50:53 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-04-30 18:02:33 +02:00
|
|
|
|
2015-06-19 23:40:09 +02:00
|
|
|
// To default Agent to true, we need to check the raw string, since the
|
|
|
|
// decoded boolean can't represent "absence of config".
|
|
|
|
//
|
|
|
|
// And if SSH_AUTH_SOCK is not set, there's no agent to connect to, so we
|
|
|
|
// shouldn't try.
|
|
|
|
if s.Ephemeral.ConnInfo["agent"] == "" && os.Getenv("SSH_AUTH_SOCK") != "" {
|
|
|
|
connInfo.Agent = true
|
|
|
|
}
|
|
|
|
|
2015-04-09 21:58:00 +02:00
|
|
|
if connInfo.User == "" {
|
|
|
|
connInfo.User = DefaultUser
|
2014-07-15 23:50:53 +02:00
|
|
|
}
|
2015-04-09 21:58:00 +02:00
|
|
|
if connInfo.Port == 0 {
|
|
|
|
connInfo.Port = DefaultPort
|
2014-07-15 23:50:53 +02:00
|
|
|
}
|
2015-04-09 21:58:00 +02:00
|
|
|
if connInfo.ScriptPath == "" {
|
|
|
|
connInfo.ScriptPath = DefaultScriptPath
|
2014-07-15 23:50:53 +02:00
|
|
|
}
|
2015-04-09 21:58:00 +02:00
|
|
|
if connInfo.Timeout != "" {
|
|
|
|
connInfo.TimeoutVal = safeDuration(connInfo.Timeout, DefaultTimeout)
|
2014-07-15 23:50:53 +02:00
|
|
|
} else {
|
2015-04-09 21:58:00 +02:00
|
|
|
connInfo.TimeoutVal = DefaultTimeout
|
2014-07-15 23:50:53 +02:00
|
|
|
}
|
2015-04-30 18:02:33 +02:00
|
|
|
|
2015-06-22 18:34:02 +02:00
|
|
|
// Default all bastion config attrs to their non-bastion counterparts
|
|
|
|
if connInfo.BastionHost != "" {
|
|
|
|
if connInfo.BastionUser == "" {
|
|
|
|
connInfo.BastionUser = connInfo.User
|
|
|
|
}
|
|
|
|
if connInfo.BastionPassword == "" {
|
|
|
|
connInfo.BastionPassword = connInfo.Password
|
|
|
|
}
|
|
|
|
if connInfo.BastionKeyFile == "" {
|
|
|
|
connInfo.BastionKeyFile = connInfo.KeyFile
|
|
|
|
}
|
|
|
|
if connInfo.BastionPort == 0 {
|
|
|
|
connInfo.BastionPort = connInfo.Port
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-09 21:58:00 +02:00
|
|
|
return connInfo, nil
|
2014-07-15 23:50:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// safeDuration returns either the parsed duration or a default value
|
|
|
|
func safeDuration(dur string, defaultDur time.Duration) time.Duration {
|
|
|
|
d, err := time.ParseDuration(dur)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Invalid duration '%s', using default of %s", dur, defaultDur)
|
|
|
|
return defaultDur
|
|
|
|
}
|
|
|
|
return d
|
|
|
|
}
|
2014-07-16 01:51:20 +02:00
|
|
|
|
2015-04-10 20:34:46 +02:00
|
|
|
// prepareSSHConfig is used to turn the *ConnectionInfo provided into a
|
2015-04-09 21:58:00 +02:00
|
|
|
// usable *SSHConfig for client initialization.
|
2015-04-10 20:34:46 +02:00
|
|
|
func prepareSSHConfig(connInfo *connectionInfo) (*sshConfig, error) {
|
2015-06-22 18:34:02 +02:00
|
|
|
sshAgent, err := connectToAgent(connInfo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-07-16 01:51:20 +02:00
|
|
|
}
|
2015-03-16 00:12:25 +01:00
|
|
|
|
2015-06-22 18:34:02 +02:00
|
|
|
sshConf, err := buildSSHClientConfig(sshClientConfigOpts{
|
|
|
|
user: connInfo.User,
|
|
|
|
keyFile: connInfo.KeyFile,
|
|
|
|
password: connInfo.Password,
|
|
|
|
sshAgent: sshAgent,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-03-16 00:12:25 +01:00
|
|
|
|
2015-06-22 18:34:02 +02:00
|
|
|
var bastionConf *ssh.ClientConfig
|
|
|
|
if connInfo.BastionHost != "" {
|
|
|
|
bastionConf, err = buildSSHClientConfig(sshClientConfigOpts{
|
|
|
|
user: connInfo.BastionUser,
|
|
|
|
keyFile: connInfo.BastionKeyFile,
|
|
|
|
password: connInfo.BastionPassword,
|
|
|
|
sshAgent: sshAgent,
|
|
|
|
})
|
2015-03-16 00:12:25 +01:00
|
|
|
if err != nil {
|
2015-06-22 18:34:02 +02:00
|
|
|
return nil, err
|
2015-03-16 00:12:25 +01:00
|
|
|
}
|
2015-06-22 18:34:02 +02:00
|
|
|
}
|
2015-03-16 00:12:25 +01:00
|
|
|
|
2015-06-22 18:34:02 +02:00
|
|
|
host := fmt.Sprintf("%s:%d", connInfo.Host, connInfo.Port)
|
|
|
|
connectFunc := ConnectFunc("tcp", host)
|
|
|
|
|
|
|
|
if bastionConf != nil {
|
|
|
|
bastionHost := fmt.Sprintf("%s:%d", connInfo.BastionHost, connInfo.BastionPort)
|
|
|
|
connectFunc = BastionConnectFunc("tcp", bastionHost, bastionConf, "tcp", host)
|
2015-03-16 00:12:25 +01:00
|
|
|
}
|
2014-08-05 18:53:02 +02:00
|
|
|
|
2015-06-22 18:34:02 +02:00
|
|
|
config := &sshConfig{
|
|
|
|
config: sshConf,
|
|
|
|
connection: connectFunc,
|
|
|
|
sshAgent: sshAgent,
|
|
|
|
}
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type sshClientConfigOpts struct {
|
|
|
|
keyFile string
|
|
|
|
password string
|
|
|
|
sshAgent *sshAgent
|
|
|
|
user string
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildSSHClientConfig(opts sshClientConfigOpts) (*ssh.ClientConfig, error) {
|
|
|
|
conf := &ssh.ClientConfig{
|
|
|
|
User: opts.user,
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.sshAgent != nil {
|
|
|
|
conf.Auth = append(conf.Auth, opts.sshAgent.Auth())
|
|
|
|
}
|
2014-08-05 18:53:02 +02:00
|
|
|
|
2015-06-22 18:34:02 +02:00
|
|
|
if opts.keyFile != "" {
|
|
|
|
pubKeyAuth, err := readPublicKeyFromPath(opts.keyFile)
|
2014-07-16 01:51:20 +02:00
|
|
|
if err != nil {
|
2015-06-22 18:34:02 +02:00
|
|
|
return nil, err
|
2014-07-16 01:51:20 +02:00
|
|
|
}
|
2015-06-22 18:34:02 +02:00
|
|
|
conf.Auth = append(conf.Auth, pubKeyAuth)
|
|
|
|
}
|
2014-08-05 18:53:02 +02:00
|
|
|
|
2015-06-22 18:34:02 +02:00
|
|
|
if opts.password != "" {
|
|
|
|
conf.Auth = append(conf.Auth, ssh.Password(opts.password))
|
|
|
|
conf.Auth = append(conf.Auth, ssh.KeyboardInteractive(
|
|
|
|
PasswordKeyboardInteractive(opts.password)))
|
2014-07-16 01:51:20 +02:00
|
|
|
}
|
2015-06-22 18:34:02 +02:00
|
|
|
|
|
|
|
return conf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func readPublicKeyFromPath(path string) (ssh.AuthMethod, error) {
|
|
|
|
fullPath, err := homedir.Expand(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to expand home directory: %s", err)
|
2014-07-16 01:51:20 +02:00
|
|
|
}
|
2015-06-22 18:34:02 +02:00
|
|
|
key, err := ioutil.ReadFile(fullPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to read key file %q: %s", path, err)
|
2014-07-16 01:51:20 +02:00
|
|
|
}
|
2015-06-22 18:34:02 +02:00
|
|
|
|
|
|
|
// 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(key)
|
|
|
|
if block == nil {
|
|
|
|
return nil, fmt.Errorf("Failed to read key %q: no key found", path)
|
|
|
|
}
|
|
|
|
if block.Headers["Proc-Type"] == "4,ENCRYPTED" {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Failed to read key %q: password protected keys are\n"+
|
|
|
|
"not supported. Please decrypt the key prior to use.", path)
|
|
|
|
}
|
|
|
|
|
|
|
|
signer, err := ssh.ParsePrivateKey(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to parse key file %q: %s", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ssh.PublicKeys(signer), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func connectToAgent(connInfo *connectionInfo) (*sshAgent, error) {
|
|
|
|
if connInfo.Agent != true {
|
|
|
|
// No agent configured
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
sshAuthSock := os.Getenv("SSH_AUTH_SOCK")
|
|
|
|
|
|
|
|
if sshAuthSock == "" {
|
|
|
|
return nil, fmt.Errorf("SSH Requested but SSH_AUTH_SOCK not-specified")
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := net.Dial("unix", sshAuthSock)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error connecting to SSH_AUTH_SOCK: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// connection close is handled over in Communicator
|
|
|
|
return &sshAgent{
|
|
|
|
agent: agent.NewClient(conn),
|
|
|
|
conn: conn,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// A tiny wrapper around an agent.Agent to expose the ability to close its
|
|
|
|
// associated connection on request.
|
|
|
|
type sshAgent struct {
|
|
|
|
agent agent.Agent
|
|
|
|
conn net.Conn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *sshAgent) Close() error {
|
|
|
|
return a.conn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *sshAgent) Auth() ssh.AuthMethod {
|
|
|
|
return ssh.PublicKeysCallback(a.agent.Signers)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *sshAgent) ForwardToAgent(client *ssh.Client) error {
|
|
|
|
return agent.ForwardToAgent(client, a.agent)
|
2014-07-16 01:51:20 +02:00
|
|
|
}
|