-
Notifications
You must be signed in to change notification settings - Fork 892
feat: add GPG forwarding to coder ssh #5482
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b7bc639
feat: add GPG forwarding to coder ssh
deansheather c270898
fixup! feat: add GPG forwarding to coder ssh
deansheather 6c10fa4
feat: unix forwarding in agent SSH server
deansheather 9ebf840
chore: working agent forwarding tests (tcp and unix)
deansheather d85dc66
feat: working GPG agent forwarding test
deansheather e203bf0
fixup! feat: working GPG agent forwarding test
deansheather a90d3d6
chore: PR comments
deansheather e07d204
fixup! chore: PR comments
deansheather 216c028
fixup! chore: PR comments
deansheather a0f70de
chore: fix tests on darwin for SSH unix forwarding
deansheather 17c7f99
fixup! chore: fix tests on darwin for SSH unix forwarding
deansheather ed5ff26
Merge branch 'main' into dean/gpg-forward
deansheather 130ddfd
fixup! Merge branch 'main' into dean/gpg-forward
deansheather 9c6cfba
chore: PR comments
deansheather 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 hidden or 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 |
---|---|---|
|
@@ -480,12 +480,16 @@ func (a *agent) init(ctx context.Context) { | |
if err != nil { | ||
panic(err) | ||
} | ||
|
||
sshLogger := a.logger.Named("ssh-server") | ||
forwardHandler := &ssh.ForwardedTCPHandler{} | ||
unixForwardHandler := &forwardedUnixHandler{log: a.logger} | ||
|
||
a.sshServer = &ssh.Server{ | ||
ChannelHandlers: map[string]ssh.ChannelHandler{ | ||
"direct-tcpip": ssh.DirectTCPIPHandler, | ||
"session": ssh.DefaultSessionHandler, | ||
"direct-tcpip": ssh.DirectTCPIPHandler, | ||
"[email protected]": directStreamLocalHandler, | ||
"session": ssh.DefaultSessionHandler, | ||
}, | ||
ConnectionFailedCallback: func(conn net.Conn, err error) { | ||
sshLogger.Info(ctx, "ssh connection ended", slog.Error(err)) | ||
|
@@ -525,8 +529,10 @@ func (a *agent) init(ctx context.Context) { | |
return true | ||
}, | ||
RequestHandlers: map[string]ssh.RequestHandler{ | ||
"tcpip-forward": forwardHandler.HandleSSHRequest, | ||
"cancel-tcpip-forward": forwardHandler.HandleSSHRequest, | ||
"tcpip-forward": forwardHandler.HandleSSHRequest, | ||
"cancel-tcpip-forward": forwardHandler.HandleSSHRequest, | ||
"[email protected]": unixForwardHandler.HandleSSHRequest, | ||
"[email protected]": unixForwardHandler.HandleSSHRequest, | ||
}, | ||
ServerConfigCallback: func(ctx ssh.Context) *gossh.ServerConfig { | ||
return &gossh.ServerConfig{ | ||
|
This file contains hidden or 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 |
---|---|---|
|
@@ -273,7 +273,7 @@ func TestAgent_Session_TTY_Hushlogin(t *testing.T) { | |
} | ||
|
||
//nolint:paralleltest // This test reserves a port. | ||
func TestAgent_LocalForwarding(t *testing.T) { | ||
func TestAgent_TCPLocalForwarding(t *testing.T) { | ||
random, err := net.Listen("tcp", "127.0.0.1:0") | ||
require.NoError(t, err) | ||
_ = random.Close() | ||
|
@@ -286,24 +286,239 @@ func TestAgent_LocalForwarding(t *testing.T) { | |
defer local.Close() | ||
tcpAddr, valid = local.Addr().(*net.TCPAddr) | ||
require.True(t, valid) | ||
localPort := tcpAddr.Port | ||
remotePort := tcpAddr.Port | ||
done := make(chan struct{}) | ||
go func() { | ||
defer close(done) | ||
conn, err := local.Accept() | ||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
_ = conn.Close() | ||
defer conn.Close() | ||
b := make([]byte, 4) | ||
_, err = conn.Read(b) | ||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
_, err = conn.Write(b) | ||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
}() | ||
|
||
err = setupSSHCommand(t, []string{"-L", fmt.Sprintf("%d:127.0.0.1:%d", randomPort, localPort)}, []string{"echo", "test"}).Start() | ||
cmd := setupSSHCommand(t, []string{"-L", fmt.Sprintf("%d:127.0.0.1:%d", randomPort, remotePort)}, []string{"sleep", "10"}) | ||
err = cmd.Start() | ||
require.NoError(t, err) | ||
|
||
require.Eventually(t, func() bool { | ||
conn, err := net.Dial("tcp", "127.0.0.1:"+strconv.Itoa(randomPort)) | ||
if err != nil { | ||
return false | ||
} | ||
defer conn.Close() | ||
_, err = conn.Write([]byte("test")) | ||
if !assert.NoError(t, err) { | ||
return false | ||
} | ||
b := make([]byte, 4) | ||
_, err = conn.Read(b) | ||
if !assert.NoError(t, err) { | ||
return false | ||
} | ||
if !assert.Equal(t, "test", string(b)) { | ||
return false | ||
} | ||
|
||
return true | ||
}, testutil.WaitLong, testutil.IntervalSlow) | ||
|
||
<-done | ||
|
||
_ = cmd.Process.Kill() | ||
} | ||
|
||
//nolint:paralleltest // This test reserves a port. | ||
func TestAgent_TCPRemoteForwarding(t *testing.T) { | ||
random, err := net.Listen("tcp", "127.0.0.1:0") | ||
require.NoError(t, err) | ||
_ = random.Close() | ||
tcpAddr, valid := random.Addr().(*net.TCPAddr) | ||
require.True(t, valid) | ||
randomPort := tcpAddr.Port | ||
|
||
conn, err := net.Dial("tcp", "127.0.0.1:"+strconv.Itoa(localPort)) | ||
l, err := net.Listen("tcp", "127.0.0.1:0") | ||
require.NoError(t, err) | ||
conn.Close() | ||
defer l.Close() | ||
tcpAddr, valid = l.Addr().(*net.TCPAddr) | ||
require.True(t, valid) | ||
localPort := tcpAddr.Port | ||
|
||
done := make(chan struct{}) | ||
go func() { | ||
defer close(done) | ||
|
||
conn, err := l.Accept() | ||
if err != nil { | ||
return | ||
} | ||
defer conn.Close() | ||
b := make([]byte, 4) | ||
_, err = conn.Read(b) | ||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
_, err = conn.Write(b) | ||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
}() | ||
|
||
cmd := setupSSHCommand(t, []string{"-R", fmt.Sprintf("127.0.0.1:%d:127.0.0.1:%d", randomPort, localPort)}, []string{"sleep", "10"}) | ||
err = cmd.Start() | ||
require.NoError(t, err) | ||
|
||
require.Eventually(t, func() bool { | ||
conn, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", randomPort)) | ||
if err != nil { | ||
return false | ||
} | ||
defer conn.Close() | ||
_, err = conn.Write([]byte("test")) | ||
if !assert.NoError(t, err) { | ||
return false | ||
} | ||
b := make([]byte, 4) | ||
_, err = conn.Read(b) | ||
if !assert.NoError(t, err) { | ||
return false | ||
} | ||
if !assert.Equal(t, "test", string(b)) { | ||
return false | ||
} | ||
|
||
return true | ||
}, testutil.WaitLong, testutil.IntervalSlow) | ||
|
||
<-done | ||
|
||
_ = cmd.Process.Kill() | ||
} | ||
|
||
func TestAgent_UnixLocalForwarding(t *testing.T) { | ||
t.Parallel() | ||
if runtime.GOOS == "windows" { | ||
t.Skip("unix domain sockets are not fully supported on Windows") | ||
} | ||
|
||
tmpdir := tempDirUnixSocket(t) | ||
remoteSocketPath := filepath.Join(tmpdir, "remote-socket") | ||
localSocketPath := filepath.Join(tmpdir, "local-socket") | ||
|
||
l, err := net.Listen("unix", remoteSocketPath) | ||
require.NoError(t, err) | ||
defer l.Close() | ||
|
||
done := make(chan struct{}) | ||
go func() { | ||
defer close(done) | ||
|
||
conn, err := l.Accept() | ||
if err != nil { | ||
return | ||
} | ||
defer conn.Close() | ||
b := make([]byte, 4) | ||
_, err = conn.Read(b) | ||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
_, err = conn.Write(b) | ||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
}() | ||
|
||
cmd := setupSSHCommand(t, []string{"-L", fmt.Sprintf("%s:%s", localSocketPath, remoteSocketPath)}, []string{"sleep", "10"}) | ||
err = cmd.Start() | ||
require.NoError(t, err) | ||
|
||
require.Eventually(t, func() bool { | ||
_, err := os.Stat(localSocketPath) | ||
return err == nil | ||
}, testutil.WaitLong, testutil.IntervalFast) | ||
|
||
conn, err := net.Dial("unix", localSocketPath) | ||
require.NoError(t, err) | ||
defer conn.Close() | ||
_, err = conn.Write([]byte("test")) | ||
require.NoError(t, err) | ||
b := make([]byte, 4) | ||
_, err = conn.Read(b) | ||
require.NoError(t, err) | ||
require.Equal(t, "test", string(b)) | ||
_ = conn.Close() | ||
<-done | ||
|
||
_ = cmd.Process.Kill() | ||
} | ||
|
||
func TestAgent_UnixRemoteForwarding(t *testing.T) { | ||
t.Parallel() | ||
if runtime.GOOS == "windows" { | ||
t.Skip("unix domain sockets are not fully supported on Windows") | ||
} | ||
|
||
tmpdir := tempDirUnixSocket(t) | ||
remoteSocketPath := filepath.Join(tmpdir, "remote-socket") | ||
localSocketPath := filepath.Join(tmpdir, "local-socket") | ||
|
||
l, err := net.Listen("unix", localSocketPath) | ||
require.NoError(t, err) | ||
defer l.Close() | ||
|
||
done := make(chan struct{}) | ||
go func() { | ||
defer close(done) | ||
|
||
conn, err := l.Accept() | ||
if err != nil { | ||
return | ||
} | ||
defer conn.Close() | ||
b := make([]byte, 4) | ||
_, err = conn.Read(b) | ||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
_, err = conn.Write(b) | ||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
}() | ||
|
||
cmd := setupSSHCommand(t, []string{"-R", fmt.Sprintf("%s:%s", remoteSocketPath, localSocketPath)}, []string{"sleep", "10"}) | ||
err = cmd.Start() | ||
require.NoError(t, err) | ||
|
||
require.Eventually(t, func() bool { | ||
_, err := os.Stat(remoteSocketPath) | ||
return err == nil | ||
}, testutil.WaitLong, testutil.IntervalFast) | ||
|
||
conn, err := net.Dial("unix", remoteSocketPath) | ||
require.NoError(t, err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
defer conn.Close() | ||
_, err = conn.Write([]byte("test")) | ||
require.NoError(t, err) | ||
b := make([]byte, 4) | ||
_, err = conn.Read(b) | ||
require.NoError(t, err) | ||
require.Equal(t, "test", string(b)) | ||
_ = conn.Close() | ||
|
||
<-done | ||
|
||
_ = cmd.Process.Kill() | ||
} | ||
|
||
func TestAgent_SFTP(t *testing.T) { | ||
|
@@ -733,7 +948,10 @@ func setupSSHCommand(t *testing.T, beforeArgs []string, afterArgs []string) *exe | |
args := append(beforeArgs, | ||
"-o", "HostName "+tcpAddr.IP.String(), | ||
"-o", "Port "+strconv.Itoa(tcpAddr.Port), | ||
"-o", "StrictHostKeyChecking=no", "host") | ||
"-o", "StrictHostKeyChecking=no", | ||
"-o", "UserKnownHostsFile=/dev/null", | ||
"host", | ||
) | ||
args = append(args, afterArgs...) | ||
return exec.Command("ssh", args...) | ||
} | ||
|
@@ -919,3 +1137,26 @@ func (*client) PostWorkspaceAgentAppHealth(_ context.Context, _ codersdk.PostWor | |
func (*client) PostWorkspaceAgentVersion(_ context.Context, _ string) error { | ||
return nil | ||
} | ||
|
||
// tempDirUnixSocket returns a temporary directory that can safely hold unix | ||
// sockets (probably). | ||
deansheather marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// | ||
// During tests on darwin we hit the max path length limit for unix sockets | ||
// pretty easily in the default location, so this function uses /tmp instead to | ||
// get shorter paths. | ||
func tempDirUnixSocket(t *testing.T) string { | ||
deansheather marked this conversation as resolved.
Show resolved
Hide resolved
|
||
t.Helper() | ||
if runtime.GOOS == "darwin" { | ||
testName := strings.ReplaceAll(t.Name(), "/", "_") | ||
dir, err := os.MkdirTemp("/tmp", fmt.Sprintf("coder-test-%s-", testName)) | ||
require.NoError(t, err, "create temp dir for gpg test") | ||
|
||
t.Cleanup(func() { | ||
err := os.RemoveAll(dir) | ||
assert.NoError(t, err, "remove temp dir", dir) | ||
}) | ||
return dir | ||
} | ||
|
||
return t.TempDir() | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.