Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix: create ssh directory if it doesn't already exist when running coder config-ssh #17711

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

Merged
merged 7 commits into from
May 8, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions cli/configssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,11 @@ func (r *RootCmd) configSSH() *serpent.Command {
}

if !bytes.Equal(configRaw, configModified) {
sshDir := filepath.Dir(sshConfigFile)
if err := os.MkdirAll(sshDir, 0700); err != nil {
return xerrors.Errorf("failed to create directory %q: %w", sshDir, err)
}

err = atomic.WriteFile(sshConfigFile, bytes.NewReader(configModified))
if err != nil {
return xerrors.Errorf("write ssh config failed: %w", err)
Expand Down
41 changes: 41 additions & 0 deletions cli/configssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,47 @@ func TestConfigSSH(t *testing.T) {
<-copyDone
}

func TestConfigSSH_MissingDirectory(t *testing.T) {
t.Parallel()

if runtime.GOOS == "windows" {
t.Skip("See coder/internal#117")
}

client := coderdtest.New(t, nil)
_ = coderdtest.CreateFirstUser(t, client)

// Create a temporary directory but don't create .ssh subdirectory
tmpdir := t.TempDir()
sshConfigPath := filepath.Join(tmpdir, ".ssh", "config")

// Run config-ssh with a non-existent .ssh directory
args := []string{
"config-ssh",
"--ssh-config-file", sshConfigPath,
"--yes", // Skip confirmation prompts
}
inv, root := clitest.New(t, args...)
clitest.SetupConfig(t, client, root)

err := inv.Run()
require.NoError(t, err, "config-ssh should succeed with non-existent directory")

// Verify that the .ssh directory was created
sshDir := filepath.Dir(sshConfigPath)
_, err = os.Stat(sshDir)
require.NoError(t, err, ".ssh directory should exist")

// Verify that the config file was created
_, err = os.Stat(sshConfigPath)
require.NoError(t, err, "config file should exist")

// Check that the directory has proper permissions (0700)
sshDirInfo, err := os.Stat(sshDir)
require.NoError(t, err)
require.Equal(t, os.FileMode(0700), sshDirInfo.Mode().Perm(), "directory should have 0700 permissions")
}

func TestConfigSSH_FileWriteAndOptionsFlow(t *testing.T) {
t.Parallel()

Expand Down
Loading