From f9dc7b130fcd8fa1ff0f086de89744c8c194c1e5 Mon Sep 17 00:00:00 2001 From: Ethan Young Date: Sun, 28 May 2017 09:46:54 -0600 Subject: Use xanzy/ssh-agent to create the ssh agent correctly based on os. --- plumbing/transport/ssh/auth_method.go | 17 ++++------------- plumbing/transport/ssh/auth_method_test.go | 12 +++++++++++- 2 files changed, 15 insertions(+), 14 deletions(-) (limited to 'plumbing/transport/ssh') diff --git a/plumbing/transport/ssh/auth_method.go b/plumbing/transport/ssh/auth_method.go index 84dfe14..f95235b 100644 --- a/plumbing/transport/ssh/auth_method.go +++ b/plumbing/transport/ssh/auth_method.go @@ -3,25 +3,21 @@ package ssh import ( "crypto/x509" "encoding/pem" - "errors" "fmt" "io/ioutil" - "net" "os" "os/user" "path/filepath" "gopkg.in/src-d/go-git.v4/plumbing/transport" + "github.com/xanzy/ssh-agent" "golang.org/x/crypto/ssh" - "golang.org/x/crypto/ssh/agent" "golang.org/x/crypto/ssh/knownhosts" ) const DefaultUsername = "git" -var ErrEmptySSHAgentAddr = errors.New("SSH_AUTH_SOCK env variable is required") - // AuthMethod is the interface all auth methods for the ssh client // must implement. The clientConfig method returns the ssh client // configuration needed to establish an ssh connection. @@ -189,19 +185,14 @@ func NewSSHAgentAuth(u string) (AuthMethod, error) { u = usr.Username } - sshAgentAddr := os.Getenv("SSH_AUTH_SOCK") - if sshAgentAddr == "" { - return nil, ErrEmptySSHAgentAddr - } - - pipe, err := net.Dial("unix", sshAgentAddr) + a, _, err := sshagent.New() if err != nil { - return nil, fmt.Errorf("error connecting to SSH agent: %q", err) + return nil, fmt.Errorf("error creating SSH agent: %q", err) } return &PublicKeysCallback{ User: u, - Callback: agent.NewClient(pipe).Signers, + Callback: a.Signers, }, nil } diff --git a/plumbing/transport/ssh/auth_method_test.go b/plumbing/transport/ssh/auth_method_test.go index 6f3d82f..aa05f7f 100644 --- a/plumbing/transport/ssh/auth_method_test.go +++ b/plumbing/transport/ssh/auth_method_test.go @@ -94,6 +94,16 @@ func (s *SuiteCommon) TestPublicKeysCallbackString(c *C) { c.Assert(a.String(), Equals, fmt.Sprintf("user: test, name: %s", PublicKeysCallbackName)) } func (s *SuiteCommon) TestNewSSHAgentAuth(c *C) { + if os.Getenv("SSH_AUTH_SOCK") == "" { + c.Skip("SSH_AUTH_SOCK or SSH_TEST_PRIVATE_KEY are required") + } + + auth, err := NewSSHAgentAuth("foo") + c.Assert(err, IsNil) + c.Assert(auth, NotNil) +} + +func (s *SuiteCommon) TestNewSSHAgentAuthNoAgent(c *C) { addr := os.Getenv("SSH_AUTH_SOCK") err := os.Unsetenv("SSH_AUTH_SOCK") c.Assert(err, IsNil) @@ -105,7 +115,7 @@ func (s *SuiteCommon) TestNewSSHAgentAuth(c *C) { k, err := NewSSHAgentAuth("foo") c.Assert(k, IsNil) - c.Assert(err, Equals, ErrEmptySSHAgentAddr) + c.Assert(err, ErrorMatches, ".*SSH_AUTH_SOCK.*") } func (*SuiteCommon) TestNewPublicKeys(c *C) { -- cgit