Skip to content

[UNW-177] CLI should create SSH config if it does not exist#9

Merged
caldempsey merged 5 commits intomasterfrom
callum/unw-174
May 22, 2023
Merged

[UNW-177] CLI should create SSH config if it does not exist#9
caldempsey merged 5 commits intomasterfrom
callum/unw-174

Conversation

@caldempsey
Copy link
Copy Markdown
Contributor

@caldempsey caldempsey commented May 22, 2023

This PR addresses a bug where the AddHost function does not ensure the presence of the .ssh/config file before adding the Include directive. Preventing the proper configuration of SSH hosts may prevent VSCode remote from connecting to a new session (or other IDEs which depend on it).

Fixed by adding a check for the existence of the .ssh/config file and creating it if it doesn't exist before adding the Include directive. Adds a test to sanity check, and parameterized the input directory of the ssh/.config file to make the behavior testable.

@caldempsey caldempsey requested a review from noorvir May 22, 2023 10:13
os.Exit(1)
// Add an Include directive to the user's ssh config to unweave_global SSH configs - used for vscode-remote.
// Create one it if it does not exist.
if _, err := os.Stat(sshConfigPath); os.IsNotExist(err) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This should also check for directories/sub-directories and not just the file.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Can you elaborate? In the man page the only place ssh_config reads a user level ssh_config from is /home/.ssh/ssh_config: https://linux.die.net/man/5/ssh_config. Do you mean ensure that the .ssh directory exists, as that might be an issue?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yeah exactly. In this case you're passing the path as a variable which could be arbitrarily deeply nester. So we need a os.MkdirAll

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Great thanks for putting the time in to clarify!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've added a MkDir check using the same permissions as the .ssh folder

cmd/ssh.go Outdated
Comment on lines +163 to +168
homeDir, err := os.UserHomeDir()
if err != nil {
ui.Errorf("Failed to get user home directory: %v", err)
os.Exit(1)
}
if err := ssh.AddHost("uw:"+e.ID, e.Connection.Host, e.Connection.User, e.Connection.Port, filepath.Join(homeDir, ".ssh", "config")); err != nil {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think we can defer all ssh related config and settings to the ssh package. This way all the settings are in one place. i.e. you don't need to dependency inject this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

In this case we would have to use a global variable if we still want to be able to test the code or remove the tests. Which do you prefer?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Using global variable for now is fine since that's just how the CLI is structured atm. Later on we can try and parse all global variables into a config before the command handlers initialize. That would be way cleaner

@caldempsey caldempsey requested a review from noorvir May 22, 2023 14:42
Comment on lines +20 to +25
homeDir, err := os.UserHomeDir()
if err != nil {
ui.Errorf("Failed to get user home directory: %v", err)
os.Exit(1)
}
return filepath.Join(homeDir, ".ssh")
Copy link
Copy Markdown
Contributor Author

@caldempsey caldempsey May 22, 2023

Choose a reason for hiding this comment

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

AFAIK if we can't get the home dir things might go wrong, so don't want to ignore this check for the sake of a one-liner

@caldempsey caldempsey changed the title CLI should create SSH config if it does not exist [UNW-177] CLI should create SSH config if it does not exist May 22, 2023
@caldempsey caldempsey merged commit 7ecea65 into master May 22, 2023
@noorvir noorvir deleted the callum/unw-174 branch May 22, 2023 15:42
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.

2 participants