Skip to content

Commit 164f303

Browse files
author
Tarrant
committed
Add SSH Agent support
1 parent 55a51b1 commit 164f303

2 files changed

Lines changed: 30 additions & 3 deletions

File tree

builtin/provisioners/remote-exec/resource_provisioner.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,13 @@ func (p *ResourceProvisioner) runScripts(
178178
" Host: %s\n"+
179179
" User: %s\n"+
180180
" Password: %v\n"+
181-
" Private key: %v",
181+
" Private key: %v"+
182+
" SSH Agent: %v",
182183
conf.Host, conf.User,
183184
conf.Password != "",
184-
conf.KeyFile != ""))
185+
conf.KeyFile != "",
186+
conf.Agent,
187+
))
185188

186189
// Wait and retry until we establish the SSH connection
187190
var comm *helper.SSHCommunicator

helper/ssh/provisioner.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ import (
55
"fmt"
66
"io/ioutil"
77
"log"
8+
"net"
9+
"os"
810
"time"
911

10-
"golang.org/x/crypto/ssh"
1112
"github.com/hashicorp/terraform/terraform"
1213
"github.com/mitchellh/go-homedir"
1314
"github.com/mitchellh/mapstructure"
15+
"golang.org/x/crypto/ssh"
16+
"golang.org/x/crypto/ssh/agent"
1417
)
1518

1619
const (
@@ -37,6 +40,7 @@ type SSHConfig struct {
3740
KeyFile string `mapstructure:"key_file"`
3841
Host string
3942
Port int
43+
Agent bool
4044
Timeout string
4145
ScriptPath string `mapstructure:"script_path"`
4246
TimeoutVal time.Duration `mapstructure:"-"`
@@ -102,6 +106,26 @@ func PrepareConfig(conf *SSHConfig) (*Config, error) {
102106
sshConf := &ssh.ClientConfig{
103107
User: conf.User,
104108
}
109+
if conf.Agent {
110+
sshAuthSock := os.Getenv("SSH_AUTH_SOCK")
111+
112+
if sshAuthSock == "" {
113+
return nil, fmt.Errorf("SSH Requested but SSH_AUTH_SOCK not-specified")
114+
}
115+
116+
conn, err := net.Dial("unix", sshAuthSock)
117+
if err != nil {
118+
return nil, fmt.Errorf("Error connecting to SSH_AUTH_SOCK: %v", err)
119+
}
120+
// I need to close this but, later after all connections have been made
121+
// defer conn.Close()
122+
signers, err := agent.NewClient(conn).Signers()
123+
if err != nil {
124+
return nil, fmt.Errorf("Error getting keys from ssh agent: %v", err)
125+
}
126+
127+
sshConf.Auth = append(sshConf.Auth, ssh.PublicKeys(signers...))
128+
}
105129
if conf.KeyFile != "" {
106130
fullPath, err := homedir.Expand(conf.KeyFile)
107131
if err != nil {

0 commit comments

Comments
 (0)