@@ -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
1619const (
@@ -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