From 4a56ccf75eae06789929dac6e6b31efd8069047b Mon Sep 17 00:00:00 2001 From: Kimmo Lehto Date: Mon, 3 May 2021 16:08:32 +0300 Subject: [PATCH] Ignore key decoding error when using agent auth sock (#25) --- ssh.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/ssh.go b/ssh.go index 4b937792..616d9877 100644 --- a/ssh.go +++ b/ssh.go @@ -128,6 +128,16 @@ func (c *SSH) Connect() error { config.HostKeyCallback = trustedHostKeyCallback(c.HostKey) } + sshAgentSock := os.Getenv("SSH_AUTH_SOCK") + + if sshAgentSock != "" { + sshAgent, err := net.Dial("unix", sshAgentSock) + if err != nil { + return fmt.Errorf("cannot connect to SSH agent auth socket %s: %s", sshAgentSock, err) + } + config.Auth = append(config.Auth, ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers)) + } + _, err := os.Stat(c.KeyPath) if err != nil && !c.keypathDefault { return err @@ -139,7 +149,7 @@ func (c *SSH) Connect() error { return err } signer, err := ssh.ParsePrivateKey(key) - if err != nil { + if err != nil && sshAgentSock == "" { return err } config.Auth = append(config.Auth, ssh.PublicKeys(signer)) @@ -147,16 +157,6 @@ func (c *SSH) Connect() error { dst := fmt.Sprintf("%s:%d", c.Address, c.Port) - sshAgentSock := os.Getenv("SSH_AUTH_SOCK") - - if sshAgentSock != "" { - sshAgent, err := net.Dial("unix", sshAgentSock) - if err != nil { - return fmt.Errorf("cannot connect to SSH agent auth socket %s: %s", sshAgentSock, err) - } - config.Auth = append(config.Auth, ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers)) - } - var client *ssh.Client if c.Bastion == nil {