Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: add --ssh-host-prefix flag for "coder ssh"#16088

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
bcpeinhardt merged 1 commit intocoder:mainfromaaronlehmann:ssh-host-prefix-flag
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletioncli/ssh.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -61,6 +61,7 @@ var (
func (r*RootCmd)ssh()*serpent.Command {
var (
stdiobool
hostPrefixstring
forwardAgentbool
forwardGPGbool
identityAgentstring
Expand DownExpand Up@@ -195,7 +196,11 @@ func (r *RootCmd) ssh() *serpent.Command {
parsedEnv=append(parsedEnv, [2]string{k,v})
}

workspace,workspaceAgent,err:=getWorkspaceAndAgent(ctx,inv,client,!disableAutostart,inv.Args[0])
namedWorkspace:=strings.TrimPrefix(inv.Args[0],hostPrefix)
// Support "--" as a delimiter between owner and workspace name
namedWorkspace=strings.Replace(namedWorkspace,"--","/",1)

workspace,workspaceAgent,err:=getWorkspaceAndAgent(ctx,inv,client,!disableAutostart,namedWorkspace)
iferr!=nil {
returnerr
}
Expand DownExpand Up@@ -509,6 +514,12 @@ func (r *RootCmd) ssh() *serpent.Command {
Description:"Specifies whether to emit SSH output over stdin/stdout.",
Value:serpent.BoolOf(&stdio),
},
{
Flag:"ssh-host-prefix",
Env:"CODER_SSH_SSH_HOST_PREFIX",
Description:"Strip this prefix from the provided hostname to determine the workspace name. This is useful when used as part of an OpenSSH proxy command.",
Value:serpent.StringOf(&hostPrefix),
},
{
Flag:"forward-agent",
FlagShorthand:"A",
Expand Down
63 changes: 63 additions & 0 deletionscli/ssh_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1568,6 +1568,69 @@ func TestSSH(t *testing.T) {
})
}
})

t.Run("SSHHostPrefix",func(t*testing.T) {
t.Parallel()
client,workspace,agentToken:=setupWorkspaceForAgent(t)
_,_=tGoContext(t,func(ctx context.Context) {
// Run this async so the SSH command has to wait for
// the build and agent to connect!
_=agenttest.New(t,client.URL,agentToken)
<-ctx.Done()
})

clientOutput,clientInput:=io.Pipe()
serverOutput,serverInput:=io.Pipe()
deferfunc() {
for_,c:=range []io.Closer{clientOutput,clientInput,serverOutput,serverInput} {
_=c.Close()
}
}()

ctx,cancel:=context.WithTimeout(context.Background(),testutil.WaitLong)
defercancel()

user,err:=client.User(ctx,codersdk.Me)
require.NoError(t,err)

inv,root:=clitest.New(t,"ssh","--stdio","--ssh-host-prefix","coder.dummy.com--",fmt.Sprintf("coder.dummy.com--%s--%s",user.Username,workspace.Name))
clitest.SetupConfig(t,client,root)
inv.Stdin=clientOutput
inv.Stdout=serverInput
inv.Stderr=io.Discard

cmdDone:=tGo(t,func() {
err:=inv.WithContext(ctx).Run()
assert.NoError(t,err)
})

conn,channels,requests,err:=ssh.NewClientConn(&stdioConn{
Reader:serverOutput,
Writer:clientInput,
},"",&ssh.ClientConfig{
// #nosec
HostKeyCallback:ssh.InsecureIgnoreHostKey(),
})
require.NoError(t,err)
deferconn.Close()

sshClient:=ssh.NewClient(conn,channels,requests)
session,err:=sshClient.NewSession()
require.NoError(t,err)
defersession.Close()

command:="sh -c exit"
ifruntime.GOOS=="windows" {
command="cmd.exe /c exit"
}
err=session.Run(command)
require.NoError(t,err)
err=sshClient.Close()
require.NoError(t,err)
_=clientOutput.Close()

<-cmdDone
})
}

//nolint:paralleltest // This test uses t.Setenv, parent test MUST NOT be parallel.
Expand Down
5 changes: 5 additions & 0 deletionscli/testdata/coder_ssh_--help.golden
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -45,6 +45,11 @@ OPTIONS:
-R, --remote-forward string-array, $CODER_SSH_REMOTE_FORWARD
Enable remote port forwarding (remote_port:local_address:local_port).

--ssh-host-prefix string, $CODER_SSH_SSH_HOST_PREFIX
Strip this prefix from the provided hostname to determine the
workspace name. This is useful when used as part of an OpenSSH proxy
command.

--stdio bool, $CODER_SSH_STDIO
Specifies whether to emit SSH output over stdin/stdout.

Expand Down
9 changes: 9 additions & 0 deletionsdocs/reference/cli/ssh.md
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

Loading

[8]ページ先頭

©2009-2025 Movatter.jp