From ec1d4b96c00173195201fd0d8d2ab381d68cac35 Mon Sep 17 00:00:00 2001 From: Brynnen Owen Date: Tue, 28 May 2024 16:14:27 -0500 Subject: [PATCH] Added parsing of ~/.ssh/config file to look for an alternate ssh-agent pipe. If present, use the pipe specified instead of the default. Allows interoperability with other ssh agents, such as pageant. --- internal/sshutil/agent_windows.go | 36 +++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/internal/sshutil/agent_windows.go b/internal/sshutil/agent_windows.go index 92e508aeb..8cb3ea630 100644 --- a/internal/sshutil/agent_windows.go +++ b/internal/sshutil/agent_windows.go @@ -4,6 +4,8 @@ import ( "context" "net" "os" + "bufio" + "regexp" "github.com/Microsoft/go-winio" "github.com/pkg/errors" @@ -23,13 +25,33 @@ func dialAgent() (*Agent, error) { } } - // Windows OpenSSH agent - conn, err := winio.DialPipeContext(context.Background(), `\\.\\pipe\\openssh-ssh-agent`) - if err != nil { + homepath := os.Getenv("HOMEPATH") + sshagentfile := string(homepath) + "\\.ssh\\config" + + pipename := "\\\\.\\pipe\\ssh-agent" + + file, err := os.Open(sshagentfile); + if err == nil { + sc := bufio.NewScanner(file); + for sc.Scan() { + var line = sc.Text(); + if len(line) > 15 { + compare := line[0:13] + if compare == "IdentityAgent" { + temp := line[14:len(line)] + re := regexp.MustCompile(`/`) + re2 := regexp.MustCompile(`[\s\"]*`) + pipename = re2.ReplaceAllString(re.ReplaceAllString(temp,"\\"),"") + } + } + } + } + if conn, err := winio.DialPipeContext(context.Background(), pipename); err == nil { + return &Agent{ + ExtendedAgent: agent.NewClient(conn), + Conn: conn, + },nil + } else { return nil, errors.Wrap(err, "error connecting with ssh-agent") } - return &Agent{ - ExtendedAgent: agent.NewClient(conn), - Conn: conn, - }, nil }