2022-03-22 13:17:50 -06:00
package cli
import (
2022-03-30 17:59:54 -05:00
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"github.com/cli/safeexec"
2022-03-22 13:17:50 -06:00
"github.com/spf13/cobra"
2022-03-30 17:59:54 -05:00
"golang.org/x/sync/errgroup"
"golang.org/x/xerrors"
"github.com/coder/coder/cli/cliflag"
"github.com/coder/coder/cli/cliui"
"github.com/coder/coder/codersdk"
2022-03-22 13:17:50 -06:00
)
2022-03-30 17:59:54 -05:00
const sshStartToken = "# ------------START-CODER-----------"
const sshStartMessage = `# This was generated by "coder config-ssh".
#
# To remove this blob, run:
#
# coder config-ssh --remove
#
# You should not hand-edit this section, unless you are deleting it.`
const sshEndToken = "# ------------END-CODER------------"
2022-03-22 13:17:50 -06:00
func configSSH () * cobra . Command {
2022-03-30 17:59:54 -05:00
var (
2022-04-11 18:54:30 -05:00
sshConfigFile string
sshOptions [] string
skipProxyCommand bool
2022-03-30 17:59:54 -05:00
)
2022-03-22 13:17:50 -06:00
cmd := & cobra . Command {
2022-05-09 17:42:02 -05:00
Annotations : workspaceCommand ,
Use : "config-ssh" ,
Short : "Populate your SSH config with Host entries for all of your workspaces" ,
2022-05-25 21:28:10 +03:00
Example : `
- You can use -o (or --ssh-option) so set SSH options to be used for all your
workspaces.
` + cliui . Styles . Code . Render ( "$ coder config-ssh -o ForwardAgent=yes" ),
2022-03-22 13:17:50 -06:00
RunE : func ( cmd * cobra . Command , args [] string ) error {
2022-03-30 17:59:54 -05:00
client , err := createClient ( cmd )
if err != nil {
return err
}
2022-04-25 16:11:03 -05:00
organization , err := currentOrganization ( cmd , client )
if err != nil {
return err
}
2022-03-30 17:59:54 -05:00
if strings . HasPrefix ( sshConfigFile , "~/" ) {
dirname , _ := os . UserHomeDir ()
sshConfigFile = filepath . Join ( dirname , sshConfigFile [ 2 :])
}
// Doesn't matter if this fails, because we write the file anyways.
sshConfigContentRaw , _ := os . ReadFile ( sshConfigFile )
sshConfigContent := string ( sshConfigContentRaw )
startIndex := strings . Index ( sshConfigContent , sshStartToken )
endIndex := strings . Index ( sshConfigContent , sshEndToken )
if startIndex != - 1 && endIndex != - 1 {
sshConfigContent = sshConfigContent [: startIndex - 1 ] + sshConfigContent [ endIndex + len ( sshEndToken ):]
}
2022-04-25 16:11:03 -05:00
workspaces , err := client . WorkspacesByOwner ( cmd . Context (), organization . ID , codersdk . Me )
2022-03-30 17:59:54 -05:00
if err != nil {
return err
}
if len ( workspaces ) == 0 {
return xerrors . New ( "You don't have any workspaces!" )
}
2022-04-11 18:54:30 -05:00
binaryFile , err := currentBinPath ( cmd )
2022-03-30 17:59:54 -05:00
if err != nil {
return err
}
2022-04-11 18:54:30 -05:00
root := createConfig ( cmd )
2022-03-30 17:59:54 -05:00
sshConfigContent += "\n" + sshStartToken + "\n" + sshStartMessage + "\n\n"
sshConfigContentMutex := sync . Mutex {}
var errGroup errgroup . Group
for _ , workspace := range workspaces {
workspace := workspace
errGroup . Go ( func () error {
2022-04-11 16:06:15 -05:00
resources , err := client . TemplateVersionResources ( cmd . Context (), workspace . LatestBuild . TemplateVersionID )
2022-03-30 17:59:54 -05:00
if err != nil {
return err
}
for _ , resource := range resources {
2022-05-19 13:04:44 -05:00
if resource . Transition != codersdk . WorkspaceTransitionStart {
2022-03-30 17:59:54 -05:00
continue
}
2022-04-11 16:06:15 -05:00
for _ , agent := range resource . Agents {
sshConfigContentMutex . Lock ()
hostname := workspace . Name
if len ( resource . Agents ) > 1 {
hostname += "." + agent . Name
}
2022-04-11 18:54:30 -05:00
configOptions := [] string {
2022-04-11 16:06:15 -05:00
"Host coder." + hostname ,
2022-04-11 18:54:30 -05:00
}
for _ , option := range sshOptions {
configOptions = append ( configOptions , "\t" + option )
}
configOptions = append ( configOptions ,
"\tHostName coder." + hostname ,
2022-04-11 16:06:15 -05:00
"\tConnectTimeout=0" ,
"\tStrictHostKeyChecking=no" ,
2022-04-25 13:59:18 -05:00
// Without this, the "REMOTE HOST IDENTITY CHANGED"
// message will appear.
"\tUserKnownHostsFile=/dev/null" ,
2022-05-01 20:31:22 -05:00
// This disables the "Warning: Permanently added 'hostname' (RSA) to the list of known hosts."
// message from appearing on every SSH. This happens because we ignore the known hosts.
"\tLogLevel ERROR" ,
2022-04-11 18:54:30 -05:00
)
if ! skipProxyCommand {
configOptions = append ( configOptions , fmt . Sprintf ( "\tProxyCommand %q --global-config %q ssh --stdio %s" , binaryFile , root , hostname ))
}
sshConfigContent += strings . Join ( configOptions , "\n" ) + "\n"
2022-04-11 16:06:15 -05:00
sshConfigContentMutex . Unlock ()
}
2022-03-30 17:59:54 -05:00
}
return nil
})
}
err = errGroup . Wait ()
if err != nil {
return err
}
sshConfigContent += "\n" + sshEndToken
err = os . MkdirAll ( filepath . Dir ( sshConfigFile ), os . ModePerm )
if err != nil {
return err
}
err = os . WriteFile ( sshConfigFile , [] byte ( sshConfigContent ), os . ModePerm )
if err != nil {
return err
}
_ , _ = fmt . Fprintf ( cmd . OutOrStdout (), "An auto-generated ssh config was written to %q\n" , sshConfigFile )
_ , _ = fmt . Fprintln ( cmd . OutOrStdout (), "You should now be able to ssh into your workspace" )
_ , _ = fmt . Fprintf ( cmd . OutOrStdout (), "For example, try running\n\n\t$ ssh coder.%s\n\n" , workspaces [ 0 ]. Name )
2022-03-22 13:17:50 -06:00
return nil
},
}
2022-03-30 17:59:54 -05:00
cliflag . StringVarP ( cmd . Flags (), & sshConfigFile , "ssh-config-file" , "" , "CODER_SSH_CONFIG_FILE" , "~/.ssh/config" , "Specifies the path to an SSH config." )
2022-04-11 18:54:30 -05:00
cmd . Flags (). StringArrayVarP ( & sshOptions , "ssh-option" , "o" , [] string {}, "Specifies additional SSH options to embed in each host stanza." )
cmd . Flags (). BoolVarP ( & skipProxyCommand , "skip-proxy-command" , "" , false , "Specifies whether the ProxyCommand option should be skipped. Useful for testing." )
_ = cmd . Flags (). MarkHidden ( "skip-proxy-command" )
2022-03-22 13:17:50 -06:00
return cmd
}
2022-03-30 17:59:54 -05:00
// currentBinPath returns the path to the coder binary suitable for use in ssh
// ProxyCommand.
func currentBinPath ( cmd * cobra . Command ) ( string , error ) {
exePath , err := os . Executable ()
if err != nil {
return "" , xerrors . Errorf ( "get executable path: %w" , err )
}
binName := filepath . Base ( exePath )
// We use safeexec instead of os/exec because os/exec returns paths in
// the current working directory, which we will run into very often when
// looking for our own path.
pathPath , err := safeexec . LookPath ( binName )
// On Windows, the coder-cli executable must be in $PATH for both Msys2/Git
// Bash and OpenSSH for Windows (used by Powershell and VS Code) to function
// correctly. Check if the current executable is in $PATH, and warn the user
// if it isn't.
if err != nil && runtime . GOOS == "windows" {
cliui . Warn ( cmd . OutOrStdout (),
"The current executable is not in $PATH." ,
"This may lead to problems connecting to your workspace via SSH." ,
fmt . Sprintf ( "Please move %q to a location in your $PATH (such as System32) and run `%s config-ssh` again." , binName , binName ),
)
// Return the exePath so SSH at least works outside of Msys2.
return exePath , nil
}
// Warn the user if the current executable is not the same as the one in
// $PATH.
if filepath . Clean ( pathPath ) != filepath . Clean ( exePath ) {
cliui . Warn ( cmd . OutOrStdout (),
"The current executable path does not match the executable path found in $PATH." ,
"This may cause issues connecting to your workspace via SSH." ,
fmt . Sprintf ( "\tCurrent executable path: %q" , exePath ),
fmt . Sprintf ( "\tExecutable path in $PATH: %q" , pathPath ),
)
}
return binName , nil
}