Skip to content

feat: Refactor CLI config-ssh to Include configurations #1848

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

Closed
wants to merge 1 commit into from

Conversation

mafredri
Copy link
Member

WIP: This PR refactors coder config-ssh to use Include instead of block management, according to the proposal in #1326.

This is a quick Friday hack to prototype the experience, a lot of TODOs and cleanups remaining.

First run:

❯ coder config-ssh
> The following line will be added to /home/maf/.ssh/config:

    Include coder.d/host-*

  And configuration files will be stored in ~/.ssh/coder.d

  Continue? (yes/no) yes

* Added Include directive to /home/maf/.ssh/config
* Created configuration directory /home/maf/.ssh/coder.d
* Created workspace configurations in /home/maf/.ssh/coder.d

You should now be able to ssh into your workspace.
For example, try running:

        $ ssh coder.work

SSH config contents:

❯ cat ~/.ssh/config
Host github.com
        HostName github.com
        User git

Include coder.d/host-*

Workspace config (each as separate files for more flexibility):

❯ cat ~/.ssh/coder.d/host-work
Host coder.work
        HostName coder.work
        ConnectTimeout=0
        StrictHostKeyChecking=no
        UserKnownHostsFile=/dev/null
        LogLevel ERROR
        ProxyCommand "coder" --global-config "/home/maf/.config/coderv2" ssh --stdio work

Re-running the command:

❯ coder config-ssh
* Created workspace configurations in /home/maf/.ssh/coder.d

You should now be able to ssh into your workspace.
For example, try running:

        $ ssh coder.work

// to manage other files in this folder as well, e.g. keys, vscode
// specific config (i.e. for only listing coder files in vscode),
// etc.
sshEnabledLine = "Include coder.d/host-*"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to prefer multiple configurations over a single, larger one?

e.g. ~/.ssh/coder

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did have some thoughts in mind, one was to store a hash in the config file so we can detect user modifications and prompt to overwrite. Another is for the user to be able to include specific hosts into their config (example):

~/.ssh/vscode-ssh-config:

Include coder.d/host-ws1
Include coder.d/host-ws2

In Code you could then set "remote.SSH.configFile": "~/.ssh/vscode-ssh-config".

Comment on lines +80 to +103
err = func() error {
exists := true
configRaw, err := os.Open(sshConfigFile)
if err != nil && !xerrors.Is(err, fs.ErrNotExist) {
return err
} else if xerrors.Is(err, fs.ErrNotExist) {
exists = false
}
defer configRaw.Close()

if exists {
s := bufio.NewScanner(configRaw)
for s.Scan() {
if strings.HasPrefix(s.Text(), sshEnabledLine) {
enabled = true
break
}
}
if s.Err() != nil {
return err
}
}
return nil
}()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 make it its own func, perhaps?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do 👍🏻! (Lots of cleanup left to do here.)

Comment on lines -24 to +33
# 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------------"
const (
// Include path is relative to `~/.ssh` and each workspace will
// have a separate file (e.g. `~/.ssh/coder.d/host-my-workspace`).
// By prefixing hosts as `host-` we give ourselves the flexibility
// to manage other files in this folder as well, e.g. keys, vscode
// specific config (i.e. for only listing coder files in vscode),
// etc.
sshEnabledLine = "Include coder.d/host-*"
// TODO(mafredri): Does this hold on Windows?
sshCoderConfigd = "~/.ssh/coder.d"
// TODO(mafredri): Write a README to the folder?
// sshCoderConfigdReadme = `Information, tricks, removal, etc.`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What kind of migration path will this have for users?
Will they have to remove the existing section of their ssh-configs?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, yes. The idea was originally that we'd get this changed early enough for that not to be a problem. But if it is, we can add a cleanup function (I'd have left it in but it wasn't implemented yet 😄.)

@mafredri
Copy link
Member Author

This PR is replaced by a proper refactor in #1900.

@mafredri mafredri deleted the mafredri/ssh-config-refactor branch May 30, 2022 15:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants