2014-07-15 23:50:53 +02:00
|
|
|
package ssh
|
|
|
|
|
|
|
|
import (
|
2017-12-21 22:47:05 +01:00
|
|
|
"bytes"
|
2014-08-05 18:53:02 +02:00
|
|
|
"encoding/pem"
|
2019-02-20 21:05:19 +01:00
|
|
|
"errors"
|
2014-07-15 23:50:53 +02:00
|
|
|
"fmt"
|
2017-12-21 22:47:05 +01:00
|
|
|
"io/ioutil"
|
2014-07-15 23:50:53 +02:00
|
|
|
"log"
|
2015-03-16 00:12:25 +01:00
|
|
|
"net"
|
|
|
|
"os"
|
2017-12-21 22:47:05 +01:00
|
|
|
"path/filepath"
|
2020-11-25 18:40:42 +01:00
|
|
|
"strconv"
|
2017-12-21 22:47:05 +01:00
|
|
|
"strings"
|
2014-07-15 23:50:53 +02:00
|
|
|
"time"
|
|
|
|
|
2016-09-04 00:45:24 +02:00
|
|
|
"github.com/hashicorp/terraform/communicator/shared"
|
2020-02-11 22:18:04 +01:00
|
|
|
sshagent "github.com/xanzy/ssh-agent"
|
2020-11-25 18:40:42 +01:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
2015-03-16 00:12:25 +01:00
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
"golang.org/x/crypto/ssh/agent"
|
2018-02-14 01:27:56 +01:00
|
|
|
"golang.org/x/crypto/ssh/knownhosts"
|
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
|
|
|
|
|
2020-11-12 16:00:48 +01:00
|
|
|
// DefaultUnixScriptPath is used as the path to copy the file to
|
|
|
|
// for remote execution on unix if not provided otherwise.
|
|
|
|
DefaultUnixScriptPath = "/tmp/terraform_%RAND%.sh"
|
|
|
|
// DefaultWindowsScriptPath is used as the path to copy the file to
|
|
|
|
// for remote execution on windows if not provided otherwise.
|
|
|
|
DefaultWindowsScriptPath = "C:/windows/temp/terraform_%RAND%.cmd"
|
2014-07-15 23:50:53 +02:00
|
|
|
|
|
|
|
// DefaultTimeout is used if there is no timeout given
|
|
|
|
DefaultTimeout = 5 * time.Minute
|
2020-11-12 16:00:48 +01:00
|
|
|
|
|
|
|
// TargetPlatformUnix used for cleaner code, and is used if no target platform has been specified
|
|
|
|
TargetPlatformUnix = "unix"
|
|
|
|
//TargetPlatformWindows used for cleaner code
|
|
|
|
TargetPlatformWindows = "windows"
|
2014-07-15 23:50:53 +02:00
|
|
|
)
|
|
|
|
|
2015-04-10 20:34:46 +02:00
|
|
|
// connectionInfo is decoded from the ConnInfo of the resource. These are the
|
2016-10-12 20:30:09 +02:00
|
|
|
// only keys we look at. If a PrivateKey is given, that is used instead
|
2015-04-09 21:58:00 +02:00
|
|
|
// of a password.
|
2015-04-10 20:34:46 +02:00
|
|
|
type connectionInfo struct {
|
2020-11-12 16:00:48 +01:00
|
|
|
User string
|
|
|
|
Password string
|
2020-11-25 18:40:42 +01:00
|
|
|
PrivateKey string
|
|
|
|
Certificate string
|
2020-11-12 16:00:48 +01:00
|
|
|
Host string
|
2020-11-25 18:40:42 +01:00
|
|
|
HostKey string
|
2020-11-12 16:00:48 +01:00
|
|
|
Port int
|
|
|
|
Agent bool
|
2020-11-25 18:40:42 +01:00
|
|
|
ScriptPath string
|
|
|
|
TargetPlatform string
|
2020-11-12 16:00:48 +01:00
|
|
|
Timeout string
|
2020-11-25 18:40:42 +01:00
|
|
|
TimeoutVal time.Duration
|
|
|
|
|
|
|
|
BastionUser string
|
|
|
|
BastionPassword string
|
|
|
|
BastionPrivateKey string
|
|
|
|
BastionCertificate string
|
|
|
|
BastionHost string
|
|
|
|
BastionHostKey string
|
|
|
|
BastionPort int
|
|
|
|
|
|
|
|
AgentIdentity string
|
2014-07-15 23:50:53 +02:00
|
|
|
}
|
|
|
|
|
2020-11-25 18:40:42 +01:00
|
|
|
// decodeConnInfo decodes the given cty.Value using the same behavior as the
|
|
|
|
// lgeacy mapstructure decoder in order to preserve as much of the existing
|
|
|
|
// logic as possible for compatibility.
|
|
|
|
func decodeConnInfo(v cty.Value) (*connectionInfo, error) {
|
2015-04-10 20:34:46 +02:00
|
|
|
connInfo := &connectionInfo{}
|
2020-11-25 18:40:42 +01:00
|
|
|
if v.IsNull() {
|
|
|
|
return connInfo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range v.AsValueMap() {
|
|
|
|
if v.IsNull() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch k {
|
|
|
|
case "user":
|
|
|
|
connInfo.User = v.AsString()
|
|
|
|
case "password":
|
|
|
|
connInfo.Password = v.AsString()
|
|
|
|
case "private_key":
|
|
|
|
connInfo.PrivateKey = v.AsString()
|
|
|
|
case "certificate":
|
|
|
|
connInfo.Certificate = v.AsString()
|
|
|
|
case "host":
|
|
|
|
connInfo.Host = v.AsString()
|
|
|
|
case "host_key":
|
|
|
|
connInfo.HostKey = v.AsString()
|
|
|
|
case "port":
|
|
|
|
p, err := strconv.Atoi(v.AsString())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
connInfo.Port = p
|
|
|
|
case "agent":
|
|
|
|
connInfo.Agent = v.True()
|
|
|
|
case "script_path":
|
|
|
|
connInfo.ScriptPath = v.AsString()
|
|
|
|
case "target_platform":
|
|
|
|
connInfo.TargetPlatform = v.AsString()
|
|
|
|
case "timeout":
|
|
|
|
connInfo.Timeout = v.AsString()
|
|
|
|
case "bastion_user":
|
|
|
|
connInfo.BastionUser = v.AsString()
|
|
|
|
case "bastion_password":
|
|
|
|
connInfo.BastionPassword = v.AsString()
|
|
|
|
case "bastion_private_key":
|
|
|
|
connInfo.BastionPrivateKey = v.AsString()
|
|
|
|
case "bastion_certificate":
|
|
|
|
connInfo.BastionCertificate = v.AsString()
|
|
|
|
case "bastion_host":
|
|
|
|
connInfo.BastionHost = v.AsString()
|
|
|
|
case "bastion_host_key":
|
|
|
|
connInfo.BastionHostKey = v.AsString()
|
|
|
|
case "bastion_port":
|
|
|
|
p, err := strconv.Atoi(v.AsString())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
connInfo.BastionPort = p
|
|
|
|
case "agent_identity":
|
|
|
|
connInfo.AgentIdentity = v.AsString()
|
|
|
|
}
|
2014-07-15 23:50:53 +02:00
|
|
|
}
|
2020-11-25 18:40:42 +01:00
|
|
|
return connInfo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseConnectionInfo is used to convert the raw configuration into the
|
|
|
|
// *connectionInfo struct.
|
|
|
|
func parseConnectionInfo(v cty.Value) (*connectionInfo, error) {
|
|
|
|
v, err := shared.ConnectionBlockSupersetSchema.CoerceValue(v)
|
2014-07-15 23:50:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-11-25 18:40:42 +01:00
|
|
|
|
|
|
|
connInfo, err := decodeConnInfo(v)
|
|
|
|
if 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.
|
2020-11-25 18:40:42 +01:00
|
|
|
agent := v.GetAttr("agent")
|
|
|
|
if agent.IsNull() && os.Getenv("SSH_AUTH_SOCK") != "" {
|
2015-06-19 23:40:09 +02:00
|
|
|
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
|
|
|
}
|
2016-09-04 00:45:24 +02:00
|
|
|
|
2020-02-11 22:18:04 +01:00
|
|
|
// Check if host is empty.
|
|
|
|
// Otherwise return error.
|
|
|
|
if connInfo.Host == "" {
|
|
|
|
return nil, fmt.Errorf("host for provisioner cannot be empty")
|
|
|
|
}
|
|
|
|
|
2016-09-04 00:45:24 +02:00
|
|
|
// Format the host if needed.
|
|
|
|
// Needed for IPv6 support.
|
|
|
|
connInfo.Host = shared.IpFormat(connInfo.Host)
|
|
|
|
|
2015-04-09 21:58:00 +02:00
|
|
|
if connInfo.Port == 0 {
|
|
|
|
connInfo.Port = DefaultPort
|
2014-07-15 23:50:53 +02:00
|
|
|
}
|
2020-11-12 16:00:48 +01:00
|
|
|
// Set default targetPlatform to unix if it's empty
|
|
|
|
if connInfo.TargetPlatform == "" {
|
|
|
|
connInfo.TargetPlatform = TargetPlatformUnix
|
|
|
|
} else if connInfo.TargetPlatform != TargetPlatformUnix && connInfo.TargetPlatform != TargetPlatformWindows {
|
|
|
|
return nil, fmt.Errorf("target_platform for provisioner has to be either %s or %s", TargetPlatformUnix, TargetPlatformWindows)
|
|
|
|
}
|
|
|
|
// Choose an appropriate default script path based on the target platform. There is no single
|
|
|
|
// suitable default script path which works on both UNIX and Windows targets.
|
|
|
|
if connInfo.ScriptPath == "" && connInfo.TargetPlatform == TargetPlatformUnix {
|
|
|
|
connInfo.ScriptPath = DefaultUnixScriptPath
|
|
|
|
}
|
|
|
|
if connInfo.ScriptPath == "" && connInfo.TargetPlatform == TargetPlatformWindows {
|
|
|
|
connInfo.ScriptPath = DefaultWindowsScriptPath
|
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 != "" {
|
2016-09-04 00:45:24 +02:00
|
|
|
// Format the bastion host if needed.
|
|
|
|
// Needed for IPv6 support.
|
|
|
|
connInfo.BastionHost = shared.IpFormat(connInfo.BastionHost)
|
|
|
|
|
2015-06-22 18:34:02 +02:00
|
|
|
if connInfo.BastionUser == "" {
|
|
|
|
connInfo.BastionUser = connInfo.User
|
|
|
|
}
|
|
|
|
if connInfo.BastionPassword == "" {
|
|
|
|
connInfo.BastionPassword = connInfo.Password
|
|
|
|
}
|
2015-11-12 21:39:41 +01:00
|
|
|
if connInfo.BastionPrivateKey == "" {
|
|
|
|
connInfo.BastionPrivateKey = connInfo.PrivateKey
|
2015-06-22 18:34:02 +02:00
|
|
|
}
|
2019-07-21 08:32:48 +02:00
|
|
|
if connInfo.BastionCertificate == "" {
|
|
|
|
connInfo.BastionCertificate = connInfo.Certificate
|
|
|
|
}
|
2015-06-22 18:34:02 +02:00
|
|
|
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
|
|
|
|
2018-02-14 01:27:56 +01:00
|
|
|
host := fmt.Sprintf("%s:%d", connInfo.Host, connInfo.Port)
|
|
|
|
|
2015-06-22 18:34:02 +02:00
|
|
|
sshConf, err := buildSSHClientConfig(sshClientConfigOpts{
|
2018-09-19 02:36:39 +02:00
|
|
|
user: connInfo.User,
|
|
|
|
host: host,
|
|
|
|
privateKey: connInfo.PrivateKey,
|
|
|
|
password: connInfo.Password,
|
|
|
|
hostKey: connInfo.HostKey,
|
|
|
|
certificate: connInfo.Certificate,
|
|
|
|
sshAgent: sshAgent,
|
2015-06-22 18:34:02 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-03-16 00:12:25 +01:00
|
|
|
|
2018-02-14 01:27:56 +01:00
|
|
|
connectFunc := ConnectFunc("tcp", host)
|
|
|
|
|
2015-06-22 18:34:02 +02:00
|
|
|
var bastionConf *ssh.ClientConfig
|
|
|
|
if connInfo.BastionHost != "" {
|
2018-02-14 01:27:56 +01:00
|
|
|
bastionHost := fmt.Sprintf("%s:%d", connInfo.BastionHost, connInfo.BastionPort)
|
|
|
|
|
2015-06-22 18:34:02 +02:00
|
|
|
bastionConf, err = buildSSHClientConfig(sshClientConfigOpts{
|
2019-07-21 08:32:48 +02:00
|
|
|
user: connInfo.BastionUser,
|
|
|
|
host: bastionHost,
|
|
|
|
privateKey: connInfo.BastionPrivateKey,
|
|
|
|
password: connInfo.BastionPassword,
|
|
|
|
hostKey: connInfo.HostKey,
|
|
|
|
certificate: connInfo.BastionCertificate,
|
|
|
|
sshAgent: sshAgent,
|
2015-06-22 18:34:02 +02:00
|
|
|
})
|
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
|
|
|
|
|
|
|
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 {
|
2018-09-19 02:36:39 +02:00
|
|
|
privateKey string
|
|
|
|
password string
|
|
|
|
sshAgent *sshAgent
|
|
|
|
certificate string
|
|
|
|
user string
|
|
|
|
host string
|
|
|
|
hostKey string
|
2015-06-22 18:34:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func buildSSHClientConfig(opts sshClientConfigOpts) (*ssh.ClientConfig, error) {
|
2018-02-14 01:27:56 +01:00
|
|
|
hkCallback := ssh.InsecureIgnoreHostKey()
|
|
|
|
|
|
|
|
if opts.hostKey != "" {
|
|
|
|
// The knownhosts package only takes paths to files, but terraform
|
|
|
|
// generally wants to handle config data in-memory. Rather than making
|
|
|
|
// the known_hosts file an exception, write out the data to a temporary
|
|
|
|
// file to create the HostKeyCallback.
|
|
|
|
tf, err := ioutil.TempFile("", "tf-known_hosts")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create temp known_hosts file: %s", err)
|
|
|
|
}
|
|
|
|
defer tf.Close()
|
|
|
|
defer os.RemoveAll(tf.Name())
|
|
|
|
|
|
|
|
// we mark this as a CA as well, but the host key fallback will still
|
|
|
|
// use it as a direct match if the remote host doesn't return a
|
|
|
|
// certificate.
|
|
|
|
if _, err := tf.WriteString(fmt.Sprintf("@cert-authority %s %s\n", opts.host, opts.hostKey)); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to write temp known_hosts file: %s", err)
|
|
|
|
}
|
|
|
|
tf.Sync()
|
|
|
|
|
|
|
|
hkCallback, err = knownhosts.New(tf.Name())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-22 18:34:02 +02:00
|
|
|
conf := &ssh.ClientConfig{
|
2018-02-14 01:27:56 +01:00
|
|
|
HostKeyCallback: hkCallback,
|
2017-12-21 22:47:05 +01:00
|
|
|
User: opts.user,
|
2015-06-22 18:34:02 +02:00
|
|
|
}
|
|
|
|
|
2015-11-12 23:00:28 +01:00
|
|
|
if opts.privateKey != "" {
|
2018-09-19 02:36:39 +02:00
|
|
|
if opts.certificate != "" {
|
|
|
|
log.Println("using client certificate for authentication")
|
|
|
|
|
|
|
|
certSigner, err := signCertWithPrivateKey(opts.privateKey, opts.certificate)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
conf.Auth = append(conf.Auth, certSigner)
|
|
|
|
} else {
|
|
|
|
log.Println("using private key for authentication")
|
|
|
|
|
|
|
|
pubKeyAuth, err := readPrivateKey(opts.privateKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
conf.Auth = append(conf.Auth, pubKeyAuth)
|
2014-07-16 01:51:20 +02:00
|
|
|
}
|
2015-06-22 18:34:02 +02:00
|
|
|
}
|
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
|
|
|
|
2015-07-02 22:41:23 +02:00
|
|
|
if opts.sshAgent != nil {
|
|
|
|
conf.Auth = append(conf.Auth, opts.sshAgent.Auth())
|
|
|
|
}
|
|
|
|
|
2015-06-22 18:34:02 +02:00
|
|
|
return conf, nil
|
|
|
|
}
|
|
|
|
|
2018-09-19 02:36:39 +02:00
|
|
|
// Create a Cert Signer and return ssh.AuthMethod
|
|
|
|
func signCertWithPrivateKey(pk string, certificate string) (ssh.AuthMethod, error) {
|
|
|
|
rawPk, err := ssh.ParseRawPrivateKey([]byte(pk))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to parse private key %q: %s", pk, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pcert, _, _, _, err := ssh.ParseAuthorizedKey([]byte(certificate))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to parse certificate %q: %s", certificate, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
usigner, err := ssh.NewSignerFromKey(rawPk)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create signer from raw private key %q: %s", rawPk, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ucertSigner, err := ssh.NewCertSigner(pcert.(*ssh.Certificate), usigner)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create cert signer %q: %s", usigner, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ssh.PublicKeys(ucertSigner), nil
|
|
|
|
}
|
|
|
|
|
2015-11-12 21:39:41 +01:00
|
|
|
func readPrivateKey(pk string) (ssh.AuthMethod, error) {
|
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.
|
2016-09-15 14:20:18 +02:00
|
|
|
block, _ := pem.Decode([]byte(pk))
|
2015-06-22 18:34:02 +02:00
|
|
|
if block == nil {
|
2019-02-20 21:05:19 +01:00
|
|
|
return nil, errors.New("Failed to read ssh private key: no key found")
|
2015-06-22 18:34:02 +02:00
|
|
|
}
|
|
|
|
if block.Headers["Proc-Type"] == "4,ENCRYPTED" {
|
2019-02-20 21:05:19 +01:00
|
|
|
return nil, errors.New(
|
|
|
|
"Failed to read ssh private key: password protected keys are\n" +
|
|
|
|
"not supported. Please decrypt the key prior to use.")
|
2015-06-22 18:34:02 +02:00
|
|
|
}
|
|
|
|
|
2016-09-15 14:20:18 +02:00
|
|
|
signer, err := ssh.ParsePrivateKey([]byte(pk))
|
2015-06-22 18:34:02 +02:00
|
|
|
if err != nil {
|
2019-02-20 21:05:19 +01:00
|
|
|
return nil, fmt.Errorf("Failed to parse ssh private key: %s", err)
|
2015-06-22 18:34:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ssh.PublicKeys(signer), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func connectToAgent(connInfo *connectionInfo) (*sshAgent, error) {
|
|
|
|
if connInfo.Agent != true {
|
|
|
|
// No agent configured
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-12-15 16:39:23 +01:00
|
|
|
agent, conn, err := sshagent.New()
|
2015-06-22 18:34:02 +02:00
|
|
|
if err != nil {
|
2015-12-15 16:39:23 +01:00
|
|
|
return nil, err
|
2015-06-22 18:34:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// connection close is handled over in Communicator
|
|
|
|
return &sshAgent{
|
2015-12-15 16:39:23 +01:00
|
|
|
agent: agent,
|
2015-06-22 18:34:02 +02:00
|
|
|
conn: conn,
|
2017-12-21 22:47:05 +01:00
|
|
|
id: connInfo.AgentIdentity,
|
2015-06-22 18:34:02 +02:00
|
|
|
}, nil
|
2015-12-15 16:39:23 +01:00
|
|
|
|
2015-06-22 18:34:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2017-12-21 22:47:05 +01:00
|
|
|
id string
|
2015-06-22 18:34:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *sshAgent) Close() error {
|
2015-12-15 16:39:23 +01:00
|
|
|
if a.conn == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-22 18:34:02 +02:00
|
|
|
return a.conn.Close()
|
|
|
|
}
|
|
|
|
|
2017-12-21 22:47:05 +01:00
|
|
|
// make an attempt to either read the identity file or find a corresponding
|
|
|
|
// public key file using the typical openssh naming convention.
|
|
|
|
// This returns the public key in wire format, or nil when a key is not found.
|
|
|
|
func findIDPublicKey(id string) []byte {
|
|
|
|
for _, d := range idKeyData(id) {
|
|
|
|
signer, err := ssh.ParsePrivateKey(d)
|
|
|
|
if err == nil {
|
|
|
|
log.Println("[DEBUG] parsed id private key")
|
|
|
|
pk := signer.PublicKey()
|
|
|
|
return pk.Marshal()
|
|
|
|
}
|
|
|
|
|
|
|
|
// try it as a publicKey
|
|
|
|
pk, err := ssh.ParsePublicKey(d)
|
|
|
|
if err == nil {
|
|
|
|
log.Println("[DEBUG] parsed id public key")
|
|
|
|
return pk.Marshal()
|
|
|
|
}
|
|
|
|
|
|
|
|
// finally try it as an authorized key
|
|
|
|
pk, _, _, _, err = ssh.ParseAuthorizedKey(d)
|
|
|
|
if err == nil {
|
|
|
|
log.Println("[DEBUG] parsed id authorized key")
|
|
|
|
return pk.Marshal()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to read an id file using the id as the file path. Also read the .pub
|
|
|
|
// file if it exists, as the id file may be encrypted. Return only the file
|
|
|
|
// data read. We don't need to know what data came from which path, as we will
|
|
|
|
// try parsing each as a private key, a public key and an authorized key
|
|
|
|
// regardless.
|
|
|
|
func idKeyData(id string) [][]byte {
|
|
|
|
idPath, err := filepath.Abs(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var fileData [][]byte
|
|
|
|
|
|
|
|
paths := []string{idPath}
|
|
|
|
|
|
|
|
if !strings.HasSuffix(idPath, ".pub") {
|
|
|
|
paths = append(paths, idPath+".pub")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range paths {
|
|
|
|
d, err := ioutil.ReadFile(p)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[DEBUG] error reading %q: %s", p, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
log.Printf("[DEBUG] found identity data at %q", p)
|
|
|
|
fileData = append(fileData, d)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fileData
|
|
|
|
}
|
|
|
|
|
|
|
|
// sortSigners moves a signer with an agent comment field matching the
|
|
|
|
// agent_identity to the head of the list when attempting authentication. This
|
|
|
|
// helps when there are more keys loaded in an agent than the host will allow
|
|
|
|
// attempts.
|
|
|
|
func (s *sshAgent) sortSigners(signers []ssh.Signer) {
|
|
|
|
if s.id == "" || len(signers) < 2 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we can locate the public key, either by extracting it from the id or
|
|
|
|
// locating the .pub file, then we can more easily determine an exact match
|
|
|
|
idPk := findIDPublicKey(s.id)
|
|
|
|
|
|
|
|
// if we have a signer with a connect field that matches the id, send that
|
|
|
|
// first, otherwise put close matches at the front of the list.
|
|
|
|
head := 0
|
|
|
|
for i := range signers {
|
|
|
|
pk := signers[i].PublicKey()
|
|
|
|
k, ok := pk.(*agent.Key)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for an exact match first
|
|
|
|
if bytes.Equal(pk.Marshal(), idPk) || s.id == k.Comment {
|
|
|
|
signers[0], signers[i] = signers[i], signers[0]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// no exact match yet, move it to the front if it's close. The agent
|
|
|
|
// may have loaded as a full filepath, while the config refers to it by
|
|
|
|
// filename only.
|
|
|
|
if strings.HasSuffix(k.Comment, s.id) {
|
|
|
|
signers[head], signers[i] = signers[i], signers[head]
|
|
|
|
head++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ss := []string{}
|
|
|
|
for _, signer := range signers {
|
|
|
|
pk := signer.PublicKey()
|
|
|
|
k := pk.(*agent.Key)
|
|
|
|
ss = append(ss, k.Comment)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sshAgent) Signers() ([]ssh.Signer, error) {
|
|
|
|
signers, err := s.agent.Signers()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.sortSigners(signers)
|
|
|
|
return signers, nil
|
|
|
|
}
|
|
|
|
|
2015-06-22 18:34:02 +02:00
|
|
|
func (a *sshAgent) Auth() ssh.AuthMethod {
|
2017-12-21 22:47:05 +01:00
|
|
|
return ssh.PublicKeysCallback(a.Signers)
|
2015-06-22 18:34:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *sshAgent) ForwardToAgent(client *ssh.Client) error {
|
|
|
|
return agent.ForwardToAgent(client, a.agent)
|
2014-07-16 01:51:20 +02:00
|
|
|
}
|