Skip to content

Commit

Permalink
fix(cli): append workdir to ssh ProxyCommand config if it is reported…
Browse files Browse the repository at this point in the history
… back from devcontainer creation
  • Loading branch information
pascalbreuninger committed Jan 3, 2024
1 parent cc6bb87 commit fa9f690
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
9 changes: 8 additions & 1 deletion cmd/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type SSHCmd struct {

Command string
User string
WorkDir string
}

// NewSSHCmd creates a new ssh command
Expand Down Expand Up @@ -79,6 +80,7 @@ func NewSSHCmd(flags *flags.GlobalFlags) *cobra.Command {
sshCmd.Flags().StringVar(&cmd.ForwardPortsTimeout, "forward-ports-timeout", "", "Specifies the timeout after which the command should terminate when the ports are unused.")
sshCmd.Flags().StringVar(&cmd.Command, "command", "", "The command to execute within the workspace")
sshCmd.Flags().StringVar(&cmd.User, "user", "", "The user of the workspace to use")
sshCmd.Flags().StringVar(&cmd.WorkDir, "workdir", "", "The working directory in the container")
sshCmd.Flags().BoolVar(&cmd.Proxy, "proxy", false, "If true will act as intermediate proxy for a proxy provider")
sshCmd.Flags().BoolVar(&cmd.AgentForwarding, "agent-forwarding", true, "If true forward the local ssh keys to the remote machine")
sshCmd.Flags().BoolVar(&cmd.GPGAgentForwarding, "gpg-agent-forwarding", false, "If true forward the local gpg-agent to the remote machine")
Expand Down Expand Up @@ -363,8 +365,13 @@ func (cmd *SSHCmd) startTunnel(ctx context.Context, devPodConfig *config.Config,
}
}

workdir := filepath.Join("/workspaces", workspaceName)
if cmd.WorkDir != "" {
workdir = cmd.WorkDir
}

log.Debugf("Run outer container tunnel")
command := fmt.Sprintf("'%s' helper ssh-server --track-activity --stdio --workdir '%s'", agent.ContainerDevPodHelperLocation, filepath.Join("/workspaces", workspaceName))
command := fmt.Sprintf("'%s' helper ssh-server --track-activity --stdio --workdir '%s'", agent.ContainerDevPodHelperLocation, workdir)
if cmd.Debug {
command += " --debug"
}
Expand Down
10 changes: 8 additions & 2 deletions cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,14 @@ func (cmd *UpCmd) Run(
// get user from result
user := config2.GetRemoteUser(result)

var workdir string
if result.MergedConfig != nil && result.MergedConfig.WorkspaceFolder != "" {
workdir = result.MergedConfig.WorkspaceFolder
}

// configure container ssh
if cmd.ConfigureSSH {
err = configureSSH(devPodConfig, client, cmd.SSHConfigPath, user,
err = configureSSH(devPodConfig, client, cmd.SSHConfigPath, user, workdir,
cmd.GPGAgentForwarding ||
devPodConfig.ContextOption(config.ContextOptionGPGAgentForwarding) == "true")

Expand Down Expand Up @@ -671,7 +676,7 @@ func startBrowserTunnel(
return nil
}

func configureSSH(c *config.Config, client client2.BaseWorkspaceClient, sshConfigPath, user string, gpgagent bool) error {
func configureSSH(c *config.Config, client client2.BaseWorkspaceClient, sshConfigPath, user, workdir string, gpgagent bool) error {
path, err := devssh.ResolveSSHConfigPath(sshConfigPath)
if err != nil {
return errors.Wrap(err, "Invalid ssh config path")
Expand All @@ -683,6 +688,7 @@ func configureSSH(c *config.Config, client client2.BaseWorkspaceClient, sshConfi
client.Context(),
client.Workspace(),
user,
workdir,
gpgagent,
log.Default,
)
Expand Down
16 changes: 10 additions & 6 deletions pkg/ssh/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ var (
MarkerEndPrefix = "# DevPod End "
)

func ConfigureSSHConfig(sshConfigPath, context, workspace, user string, gpgagent bool, log log.Logger) error {
return configureSSHConfigSameFile(sshConfigPath, context, workspace, user, "", gpgagent, log)
func ConfigureSSHConfig(sshConfigPath, context, workspace, user, workdir string, gpgagent bool, log log.Logger) error {
return configureSSHConfigSameFile(sshConfigPath, context, workspace, user, workdir, "", gpgagent, log)
}

func configureSSHConfigSameFile(sshConfigPath, context, workspace, user, command string, gpgagent bool, log log.Logger) error {
func configureSSHConfigSameFile(sshConfigPath, context, workspace, user, workdir, command string, gpgagent bool, log log.Logger) error {
configLock.Lock()
defer configLock.Unlock()

newFile, err := addHost(sshConfigPath, workspace+"."+"devpod", user, context, workspace, command, gpgagent)
newFile, err := addHost(sshConfigPath, workspace+"."+"devpod", user, context, workspace, workdir, command, gpgagent)
if err != nil {
return errors.Wrap(err, "parse ssh config")
}
Expand All @@ -43,7 +43,7 @@ type DevPodSSHEntry struct {
Workspace string
}

func addHost(path, host, user, context, workspace, command string, gpgagent bool) (string, error) {
func addHost(path, host, user, context, workspace, workdir, command string, gpgagent bool) (string, error) {
newConfig, err := removeFromConfig(path, host)
if err != nil {
return "", err
Expand All @@ -70,7 +70,11 @@ func addHost(path, host, user, context, workspace, command string, gpgagent bool
} else if gpgagent {
newLines = append(newLines, fmt.Sprintf(" ProxyCommand %s ssh --gpg-agent-forwarding --stdio --context %s --user %s %s", execPath, context, user, workspace))
} else {
newLines = append(newLines, fmt.Sprintf(" ProxyCommand %s ssh --stdio --context %s --user %s %s", execPath, context, user, workspace))
proxyCommand := fmt.Sprintf(" ProxyCommand %s ssh --stdio --context %s --user %s %s", execPath, context, user, workspace)
if workdir != "" {
proxyCommand = fmt.Sprintf("%s --workdir %s", proxyCommand, workdir)
}
newLines = append(newLines, proxyCommand)
}
newLines = append(newLines, " User "+user)
newLines = append(newLines, endMarker)
Expand Down

0 comments on commit fa9f690

Please sign in to comment.