-
Notifications
You must be signed in to change notification settings - Fork 354
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable SSH agent support on Windows #1008
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
//go:build !windows | ||
|
||
package agent | ||
|
||
import ( | ||
"os" | ||
|
||
"golang.org/x/crypto/ssh" | ||
gosshagent "golang.org/x/crypto/ssh/agent" | ||
) | ||
|
||
func GetSSHAuthSocket() string { | ||
sshAuthSocket := os.Getenv("SSH_AUTH_SOCK") | ||
if sshAuthSocket != "" { | ||
return sshAuthSocket | ||
} | ||
|
||
return "" | ||
} | ||
|
||
func ForwardToRemote(client *ssh.Client, addr string) error { | ||
return gosshagent.ForwardToRemote(client, addr) | ||
} | ||
|
||
func RequestAgentForwarding(session *ssh.Session) error { | ||
return gosshagent.RequestAgentForwarding(session) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package agent | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/pkg/errors" | ||
"golang.org/x/crypto/ssh" | ||
gosshagent "golang.org/x/crypto/ssh/agent" | ||
"gopkg.in/natefinch/npipe.v2" | ||
) | ||
|
||
const ( | ||
channelType = "[email protected]" | ||
defaultNamedPipe = "\\\\.\\pipe\\openssh-ssh-agent" | ||
) | ||
|
||
/* | ||
* Cygwin/MSYS2 `SSH_AUTH_SOCK` implementations from ssh-agent(1) are performed using an | ||
* emulated socket rather than a true AF_UNIX socket. As such, those implementations are | ||
* incompatible and a user should either utilize the Win32-OpenSSH implementation found | ||
* in Windows 10/11 or utilize another alternative that support valid AF_UNIX sockets. | ||
*/ | ||
func GetSSHAuthSocket() string { | ||
sshAuthSocket := os.Getenv("SSH_AUTH_SOCK") | ||
if sshAuthSocket != "" { | ||
return sshAuthSocket | ||
} | ||
if _, err := os.Stat(defaultNamedPipe); err == nil { | ||
return defaultNamedPipe | ||
} | ||
|
||
return "" | ||
} | ||
|
||
func ForwardToRemote(client *ssh.Client, addr string) error { | ||
if strings.Contains(addr, "\\\\.\\pipe\\") { | ||
channels := client.HandleChannelOpen(channelType) | ||
if channels == nil { | ||
return errors.New("agent: already have handler for " + channelType) | ||
} | ||
conn, err := npipe.Dial(addr) | ||
if err != nil { | ||
return err | ||
} | ||
conn.Close() | ||
|
||
go func() { | ||
for ch := range channels { | ||
channel, reqs, err := ch.Accept() | ||
if err != nil { | ||
continue | ||
} | ||
go ssh.DiscardRequests(reqs) | ||
go forwardNamedPipe(channel, addr) | ||
} | ||
}() | ||
return nil | ||
} | ||
return gosshagent.ForwardToRemote(client, addr) | ||
} | ||
|
||
func RequestAgentForwarding(session *ssh.Session) error { | ||
return gosshagent.RequestAgentForwarding(session) | ||
} | ||
|
||
func forwardNamedPipe(channel ssh.Channel, addr string) { | ||
conn, err := npipe.Dial(addr) | ||
if err != nil { | ||
return | ||
} | ||
|
||
var wg sync.WaitGroup | ||
wg.Add(2) | ||
go func() { | ||
io.Copy(conn, channel) | ||
wg.Done() | ||
}() | ||
go func() { | ||
io.Copy(channel, conn) | ||
channel.CloseWrite() | ||
wg.Done() | ||
}() | ||
|
||
wg.Wait() | ||
conn.Close() | ||
channel.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm planning to test this just a little bit more tomorrow, but presently I know this works with the OpenSSH agent that ships with Windows 10/11 natively. I'm pretty sure that the Git for Windows variation utilizes a unix socket which would mean a slight adjustment to the agent forwarding to ensure the correct socket type is used for the connection.
More specifically, the OpenSSH build that comes built into Windows 10/11 creates a named pipe at
\\.\pipe\openssh-ssh-agent
which is what I've defined as a constant above here. This pipe is hardcoded in the Microsoft OpenSSH build and is used by default when connecting to the agent.The Git for Windows build on the other hand is creating its files in the TEMP directory.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've confirmed that Git for Windows is in fact using an
AF_UNIX
socket, so I'll update the forward method just a little bit to handle that.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After some more digging after being puzzled why I couldn't connect using the AF_UNIX socket type as directly utilized by the upstream
golang.org/x/crypto/ssh/agent
, I've come to find out that MSYS2 which Git for Windows is based on uses an emulated socket rather than a native AF_UNIX socket as I was led to believe.Now with this said, there are a myriad of authenticators that do create valid AF_UNIX sockets for Windows and I found a PR for Win32-OpenSSH to add native AF_UNIX sockets to the build that comes packaged with Windows. Unfortunately, that PR is not yet merged.
What I think I'm going to do here is to prefer a set SSH_AUTH_SOCK, this way various correct implementations work, then fallback to the named pipe implementation, then finally return an error. When Windows finally natively supports SSH_AUTH_SOCK definitions, then the code will natively migrate to that updated path this way.
I'll submit this small change tomorrow, but then this should be good to go.